Module assertables::assert_fn_ok_gt_expr
source · Expand description
Assert a function ok() is greater than 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 = 2;
let b = String::from("1");
assert_fn_ok_gt_expr!(f, a, b);
//-> ()
let a: i8 = 1;
let b = String::from("2");
// Panic with error message
let result = panic::catch_unwind(|| {
assert_fn_ok_gt_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_gt_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: `\"2\"`,\n",
" left: `\"1\"`,\n",
" right: `\"2\"`"
);
assert_eq!(actual, expect);