Releases: google/googletest-rust
0.6.0
API changes
-
Changed the way the
Matcher
trait is generic with respect to the type of the actual value from a type parameter to an associated type.This should not break any code using matchers, but does affect how matchers are written. First, the
Matcher
implementation block must now use the associated type.OLD:
impl Matcher<ActualType> for MyMatcher { fn matches(&self, actual: &ActualType) -> MatcherResult {...} }
NEW:
impl Matcher for MyMatcher { type ActualT = ActualType; fn matches(&self, actual: &ActualType) -> MatcherResult {...} }
When the matcher is generic over the actual type (as is common), then the concrete matcher type must also reference the actual value type. This will often require the use of
PhantomData
in the struct body.OLD:
struct MyMatcher {...} impl<ActualT> Matcher<ActualT> for MyMatcher { fn matches(&self, actual: &ActualT) -> MatcherResult {...} }
NEW:
struct MyMatcher<ActualT> { ... phantom: PhantomData<ActualT>, } impl<ActualT> Matcher for MyMatcher<ActualT> { type ActualT = ActualT; fn matches(&self, actual: &ActualT) -> MatcherResult {...} }
Why did we make this change? This forces the concrete matcher type always to carry the type against which it matches. Doing so solves problems which we discovered where the compiler is unable to perform type inference on certain combinations of matchers due to missing information about that type (see below).
Bug fixes
-
The
and()
andor()
methods now work with strings. Previously, using them with strings would cause compilation to fail due to problems with type inference.verify_that!("A string", starts_with("A").and(ends_with("string")))
New features
-
The new
prelude
module makes it easier to include all core functionality and all matchers in one import statement:use googletest::prelude::*; #[test] fn should_work() -> Result<()> { verify_that!(123, eq(123)) }
-
The
eq
matcher now outputs a diff of debug output of the actual and expected values when the assertion fails due to their not being equal, if each value is more than one line. -
The new matcher
eq_deref_of
works likeeq
but takes a reference to the expected value. This is useful when one only has a reference to the expected value and it cannot or should not be cloned.
Other changes
- Many fixes to documentation examples. Examples corresponding to real code should now be correct.
- Doctests are now enabled on all examples where it makes sense to do so.
- The module import setup has been simplified, making things easier for external contributors.
0.5.0
New additions
-
The pattern used in
matches_pattern!
can now accept method invocations:impl MyStruct { fn get_foo(&self) -> u32 {...} } verify_that!(value, matches_pattern!(MyStruct { get_foo(): eq(5) }))
-
The
unordered_elements_are!
,contains_each!
, andis_contained_in!
macros now support map data structures such asHashMap
andBTreeMap
:let value = HashMap::from_iter([(1, "One"), (2, "Two")]); verify_that!(value, unordered_elements_are![(eq(2), eq("Two")), (eq(1), eq("One"))])
-
The
pointwise!
matcher now explicitly supports closures, and allows supplying up to three containers:verify_that!(value, pointwise!(|v| near(v, 0.1), [1.0, 2.0, 3.0]) verify_that!(value, pointwise!(near, [1.0, 2.0, 3.0], [0.01, 0.001, 0.1])
-
There is now support for easily mapping errors from the anyhow crate to test results:
#[test] fn invokes_fallible_function() -> Result<()> { a_fallible_function().into_test_result()?; ... }
Other improvements
- Async tests should now work correctly with the
googletest::test
attribute (see below). googletest::test
should now be compatible with other test attributes such astokio::test
.- The syn dependency has been upgraded to major version 2.
API changes
-
The attribute macro
google_test
has been renamed totest
:OLD:
#[googletest::google_test] fn should_work() -> Result<()> {...}
NEW:
#[googletest::test] fn should_work() -> Result<()> {...}
Downstream tests do not currently have to update their code. The old
google_test
name has been retained as an alias, but is marked deprecated.
0.4.2
API additions
- A new method
times
on the matchercontains_substring
allows asserting on the number of times the actual string contains the given substring. - The enum
MatcherResult
can now be converted to and frombool
(representingMatcherResult::Matches
) via theInto
andFrom
traits.
Bugfixes
- Using both
#[google_test]
and#[rstest]
on a test should no longer result in problems with compilation. This is a partial fix for #77, as much as is possible within this library alone. The rest of clarified in the documentation.
0.4.1
This is a bugfix release from 0.4.0.
Version 0.4.0 contained a regression which broke compilation of downstream tests using the matches_pattern!
macro. This version fixes that problem and modifies the testing approach to avoid similar regressions in the future.
It contains no further changes.
0.4.0
New additions
- The new matcher
StrMatcher
combines string equality (usingeq
withString
or&str
),starts_with
,ends_with
, andcontains_substring
and provides common features for all of these. This includes ASCII case-insensitive matching and ignoring leading and trailing whitespace. - The new matcher
predicate
allows matching using an arbitrary predicate on the input.
Other improvements
- There is now crate-level documentation, including a table of available matchers.
- The documentation for the declarative macros which generate matchers (
all!
,matchers_pattern!
,unordered_elements_are!
and so on) has been improved to more clearly indicate what the arguments should be as well as the types of values against which they match. elements_are!
,unordered_elements_are!
,pointwise!
, andsize
no longer require the actual value to implementHasSize
.- Match failure and expectation descriptions are now properly indented.
API changes
-
The minimum supported Rust version is now 1.67.0.
-
The trait
HasSize
has been removed. The matchers which depended on it now just require the actual value to supportIntoIterator
.If you implemented
HasSize
for one of your collections, just remove theimpl HasSize
since it is not needed. -
The matcher
eq_ignoring_ascii_case
has been removed. Useeq("...").ignoring_ascii_case()
instead:OLD:
verify_that!("ABC", eq_ignoring_ascii_case("abc"))
NEW:
verify_that!("ABC", eq("abc").ignoring_ascii_case())
-
One should no longer take a reference to a slice when using the
size
macher:OLD:
let value = &[1, 2, 3]; verify_that(&value[0..1], size(eq(1)))?;
NEW:
let value = &[1, 2, 3]; verify_that(value[0..1], size(eq(1)))?;
0.3.0
New additions
- The new macros
expect_that!
andexpect_pred!
can be used for non-fatal assertions. They are equivalent toverify_*!(...).and_log_failure()
. - The new macros
contains_each!
andis_contained_in!
allow for matching on containers containing some set of elements, or contained in some superset of elements, specified by matchers. These are analogous tosuperset_of
andsubset_of
but the arguments are matchers rather than values. - There is now an extension method
or
(in the traitOrMatcherExt
) which can be used to express disjunction in matchers:eq(123).or(eq(234))
. This is analogous to the existingand
method.
Other improvements
- All assertion macros which produce
Result
are now annotated with#[must_use]
so that the compiler will produce a warning if the result is ignored. - Improvements to the readability and usefulness of test assertion failure messages. In particular, actual values are now pretty-printed, redundant display has been removed, and mismatches from nested matchers are more consistently explained.
- The struct
TestAssertionFailure
now implementsFrom<T>
for allT: std::error::Error
, so the?
operator works transparently in tests. - Test assertion failure messages for the
tuple!
macro now produce more detailed information about which tuple elements failed to match. - The macros
elements_are!
,unordered_elements_are!
now support trailing commas. - Documentation pages for empty and internal-only modules should no longer be published on docs.rs.
API changes
-
The trait
Describe
has been folded intoMatcher
and removed. Existing matchers must be updated with the new version. To do this, move thedescribe
method implementation into theimpl Matcher
block, like so:OLD:
impl Matcher<T> for MyMatcher { fn matches(&self, actual: &T) -> MatcherResult {...} } impl Describe for MyMatcher { fn describe(&self, matcher_result: MatcherResult) -> String {...} }
NEW:
impl Matcher<T> for MyMatcher { fn matches(&self, actual: &T) -> MatcherResult {...} fn describe(&self, matcher_result: MatcherResult) -> String {...} }
-
The extension method
err_to_test_failure
and its associated traitMapErrorToTestFailure
are no longer needed and have been removed. The?
operator should now work transparently as long as theErr
type of theResult
implementsstd::error::Error
(which is standard practice). Calls toerr_to_test_failure
can be removed:
OLD:#[test] fn test_that_something_works() -> Result<()> { do_something().err_to_test_failure()?; ... }
NEW:
#[test] fn test_that_something_works() -> Result<()> { do_something()?; ... }
If the output of such a call is used directly in a
return
statement or expression, then it should be replaced byOk(...?)
:
OLD:fn run_do_something() -> Result<Something> { do_something().err_to_test_failure() }
NEW:
fn run_do_something() -> Result<Something> { Ok(do_something()?) }
0.2.0
- Introduced
assert_that!
andassert_pred!
macros which panic on failure, acting more like other Rust test libraries. - Introduced a
tuple!
matcher macro to match plain (non-struct) tuples by supplying matchers for each entry. - Modified
contains(...).times(...)
to take a matcher rather than a plain count as input. Now one can match on containers containing, say, more than a given number of elements meeting the criteria. - Folded the attribute macro #[google_test_wrapper] into #[google_test].