Skip to main content

multi_assert

Macro multi_assert 

Source
macro_rules! multi_assert {
    [#[dyn] $($this:expr),+ $(,)?] => { ... };
    [$($this:expr),+ $(,)?] => { ... };
}
Expand description

Construct a group of assertions that will all be tested simultaneously. If one assertion fails, the group as a whole will fail.

#[test]
fn multi_assert() -> impl Assertion {
    multi_assert!(
        assert_that!(1 + 2).is_equal_to(3),
        assert_that!(3 + 4).is_equal_to(7),
        assert_that!(23).is_equal_to(10 * 2 + 3),
        assert_that!(7).is_greater_than(5),
        assert_that!(42).is_not_equal_to(6),
    )
}

Note that the assertions passed into multi_assert must (by default) have the same type, but it is possible to use varying types by first supplying the #[dyn] attribute:

#[test]
fn multi_assert_dyn() -> impl Assertion {
    multi_assert!(#[dyn] // <- casts assertions to `dyn Assertion`
        assert_that!(1 + 2).is_equal_to(3), // <- BinaryAssertion<i32, i32>
        assert_that!(true).is_true(), // <- UnaryAssertion<bool>
        assert_that!(true).is_false().should_fail(), // <- ShouldFail<UnaryAssertion<bool>>
    )
}

The #[dyn] attribute is only available if the alloc feature is enabled, as it requires Box to work.