macro_rules! verify_pred {
    ([$($predicate:tt)*]($($arg:tt),* $(,)?)) => { ... };
    ([$($predicate:tt)*] $first:tt $($rest:tt)*) => { ... };
    ($first:tt $($rest:tt)*) => { ... };
}
Expand description

Asserts that the given predicate applied to the given arguments returns true.

Similarly to verify_that, this evaluates to a Result whose Ok variant indicates that the given predicate returned true and whose Err variant indicates that it returned false.

The failure message contains detailed information about the arguments. For example:

fn equals_modulo(a: i32, b: i32, n: i32) -> bool { a % n == b % n }

#[test]
fn test() -> Result<()> {
    let a = 1;
    let b = 7;
    let n = 5;
    verify_pred!(equals_modulo(a, b, n))?;
    Ok(())
}

This results in the following message:

equals_modulo(a, b, n) was false with
  a = 1,
  b = 7,
  n = 5

The function passed to this macro must return bool. Each of the arguments must evaluate to a type implementing std::fmt::Debug. The debug output is used to construct the failure message.

The predicate can also be a method on a struct, e.g.:

struct AStruct {};

impl AStruct {
  fn equals_modulo(...) {...}
}

verify_pred!((AStruct {}).equals_modulo(a, b, n))?;

Warning: This macro assumes that the arguments passed to the predicate are either variables or calls to pure functions. If two subsequent invocations to any of the expresssions passed as arguments result in different values, then the output message of a test failure will deviate from the values actually passed to the predicate. For this reason, *always assign the outputs of non-pure functions to variables before using them in this macro. For example:

let output = generate_random_number();  // Assigned outside of verify_pred.
verify_pred!(is_sufficiently_random(output))?;