galvanic_assert

Macro expect_that

Source
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:

  1. Expect that some expression is true, supplied with an optional error message.

    expect_that!(EXPRESSION);
    expect_that!(EXPRESSION, otherwise "some error message");
  2. Expect that some expression satifies the properties of some Matcher. Expressions used with Matchers must return a reference to a value. The Matcher is either predefined, a user defined type with a Matcher 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") }
    })
  3. 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