Macro assertables::assert_infix

source ·
macro_rules! assert_infix {
    ($x:tt $infix:tt $y:tt) => { ... };
    ($x:tt $infix:tt $y:tt, $($message:tt)+) => { ... };
}
Expand description

Assert a infix operator, such as assert_infix!(a == b).

  • If true, return ().

  • Otherwise, call panic! with a message and the values of the expressions with their debug representations.

§Examples

// Return Ok
let a = 1;
let b = 1;
assert_infix!(a == b);
//-> ()

let a = 1;
let b = 2;
// Panic with error message
let result = panic::catch_unwind(|| {
assert_infix!(a == b);
//-> panic!
});
assert!(result.is_err());
let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
let expect = concat!(
    "assertion failed: `assert_infix!(x == y)`\n",
    " x label: `a`,\n",
    " x debug: `1`,\n",
    " y label: `b`,\n",
    " y debug: `2`\n",
);
assert_eq!(actual, expect);

// Panic with error message
let result = panic::catch_unwind(|| {
assert_infix!(a == b, "message");
//-> panic!
});
assert!(result.is_err());
let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
let expect = "message";
assert_eq!(actual, expect);

§Infix operators

For values:

  • == equal
  • != not equal
  • < less than
  • <= less than or equal to
  • > greater than
  • >= greater than or equal to

For booleans:

  • ^ logical XOR
  • ! logical NOT
  • & logical AND
  • | logical OR
  • && logical lazy AND
  • || logical lazy OR

§Module macros