Macro assert_cmp::assert_fn

source ·
macro_rules! assert_fn {
    ($function:ident($left:expr, $right:expr)) => { ... };
    (not $function:ident($left:expr, $right:expr)) => { ... };
}
Expand description

Assert that a binary function call of 2 expressions returns true.

Syntax:

assert_fn!($function($left, $right))
assert_fn!(not $function($left, $right))
  • $function is an identifier of a binary function.
  • $left and $right are expressions.
  • not’s appearance means expecting the function call to returns false instead of true.

Example: An assertion that passes

fn func<A, B>(_: A, _: B) -> bool {
  true
}
assert_fn!(func(123, 456));

Example: An assertion that fails

fn func<A, B>(_: A, _: B) -> bool {
  false
}
assert_fn!(func(123, 456)); // panic: func(123, 456) ⇒ func(123, 456) ⇒ false

Example: A negative assertion that passes

fn func<A, B>(_: A, _: B) -> bool {
  false
}
assert_fn!(not func(123, 456));

Example: A negative assertion that fails

fn func<A, B>(_: A, _: B) -> bool {
  true
}
assert_fn!(not func(123, 456)); // panic: func(123, 456) ⇒ func(123, 456) ⇒ true