macro_rules! expect_that { ( $actual: expr, panics ) => { ... }; ( $actual: expr, does not panic ) => { ... }; ( $actual: expr) => { ... }; ( $actual: expr , otherwise $reason: expr ) => { ... }; ( $actual: expr, $matcher: expr ) => { ... }; }
Expand description
States that the asserted values satisfies the required properties of the supplied Matcher
but waits until the end of the block to inspect the results.
The postulated assertion is verfied immediately, but a potential panic is deferred until the end of the block wherein the expectation is stated. It is safe for multiple expectations to fail. The assertion code will prevent nested panics.
The macro comes in three different forms:
-
Expect that some expression is true, supplied with an optional error message.
ⓘexpect_that!(EXPRESSION); expect_that!(EXPRESSION, otherwise "some error message");
-
Expect that some expression satifies the properties of some
Matcher
. Expressions used withMatcher
s must return a reference to a value. TheMatcher
is either predefined, a user defined type with aMatcher
implementation, or a closure returning a `MatchResultⓘexpect_that!(&1, eq(1)); expect_that!(&1, |x| { let builder = MatchResultBuilder::for_("my_matcher"); if x == 1 { builder.matched } else { builder.failed_because("some reason") } })
-
Expect that some expression is expected to panic/not panic.
ⓘexpect_that!(panic!("panic"), panics); expect_that!(1+1, does not panic);
An expectation is verified at the end of the block it is stated in:
{
expect_that!(&1+1, equal_to(0));
expect_that!(&1+1, less_than(4)); // is executed
}
expect_that!(1+1, panics); // is never executed as e1 panics