verify_that_code

Macro verify_that_code 

Source
macro_rules! verify_that_code {
    ($subject:expr) => { ... };
}
Available on crate feature panic only.
Expand description

Starts an assertion for some piece of code in the CollectFailures mode.

It takes a closure and wraps it into a Spec. On the Spec any assertion method that is implemented for closures can be called.

Assertions started with verify_that_code! will collect AssertFailures for all failing assertions. The collected failures can be queried by calling one of the methods failures or display_failures on the Spec.

ยงExamples

use asserting::prelude::*;

fn divide(a: i32, b: i32) -> i32 {
    a / b
}

let failures = verify_that_code!(|| { divide(7, 3); })
    .does_not_panic()
    .failures();

assert_that!(failures).is_empty();

let failures = verify_that_code!(|| { divide(7, 0); })
    .does_not_panic()
    .display_failures();

assert_that!(failures).contains_exactly([
    r#"expected || { divide(7, 0); } to not panic, but did panic
  with message: "attempt to divide by zero"
"#
]);

let failures = verify_that_code!(|| { divide(7, 0); })
    .panics_with_message("division by zero")
    .display_failures();

assert_that!(failures).contains_exactly([
    r#"expected || { divide(7, 0); } to panic with message "division by zero"
   but was: "attempt to divide by zero"
  expected: "division by zero"
"#
]);