Macro assertables::assert_fn_err_gt_other
source · macro_rules! assert_fn_err_gt_other {
($function:path, $a_input:expr, $b_expr:expr $(,)?) => { ... };
($function:path, $a_input:expr, $b_expr:expr, $($message:tt)+) => { ... };
}Expand description
Assert a function err() is greater than another.
-
If true, return
(). -
Otherwise, call
panic!with a message and the values of the expressions with their debug representations.
Examples
fn example_digit_to_string(i: i32) -> Result<String, String> {
match i {
0..=9 => Ok(format!("{}", i)),
_ => Err(format!("{:?} is out of range", i)),
}
}
// Return Ok
let a: i32 = 20;
let b: i32 = 10;
assert_fn_err_gt_other!(example_digit_to_string, a, b);
//-> ()
let a: i32 = 10;
let b: i32 = 20;
// Panic with error message
let result = panic::catch_unwind(|| {
assert_fn_err_gt_other!(example_digit_to_string, a, b);
//-> panic!
});
assert!(result.is_err());
let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
let expect = concat!(
"assertion failed: `assert_fn_err_gt_other!(pair_function, left_input, right_input)`\n",
" pair_function label: `example_digit_to_string`,\n",
" left_input label: `a`,\n",
" left_input debug: `10`,\n",
" right_input label: `b`,\n",
" right_input debug: `20`,\n",
" left: `\"10 is out of range\"`,\n",
" right: `\"20 is out of range\"`"
);
assert_eq!(actual, expect);
// Panic with error message
let result = panic::catch_unwind(|| {
assert_fn_err_gt_other!(example_digit_to_string, 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);