macro_rules! assert_fn_ok_ne_expr {
    ($a_function:path, $a_param:expr, $b_expr:expr) => { ... };
    ($a_function:path, $a_param:expr, $b_expr:expr, $($message:tt)+) => { ... };
    ($a_function:path, $b_expr:expr) => { ... };
    ($a_function:path, $b_expr:expr, $($message:tt)+) => { ... };
}
Expand description

Assert a function ok() is not equal to an expression.

  • If true, return ().

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

Examples

fn f(i: i8) -> Result<String, String> {
    match i {
        0..=9 => Ok(format!("{}", i)),
        _ => Err(format!("{:?} is out of range", i)),
    }
}

// Return Ok
let a: i8 = 1;
let b = String::from("2");
assert_fn_ok_ne_expr!(f, a, b);
//-> ()

let a: i8 = 1;
let b = String::from("1");
// Panic with error message
let result = panic::catch_unwind(|| {
assert_fn_ok_ne_expr!(f, a, b);
//-> panic!
});
assert!(result.is_err());
let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
let expect = concat!(
    "assertion failed: `assert_fn_ok_ne_expr!(left_function, left_param, right_expr)`\n",
    " left_function label: `f`,\n",
    "    left_param label: `a`,\n",
    "    left_param debug: `1`,\n",
    "    right_expr label: `b`,\n",
    "    right_expr debug: `\"1\"`,\n",
    "                left: `\"1\"`,\n",
    "               right: `\"1\"`"
);
assert_eq!(actual, expect);