Macro assertables::assert_option_some

source ·
macro_rules! assert_option_some {
    ($a:expr $(,)?) => { ... };
    ($a:expr, $($message:tt)+) => { ... };
}
Expand description

Assert expression.is_some() is true.

  • If true, return ().

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

§Examples

let a: Option<i8> = Option::Some(1);
assert_option_some!(a);
//-> ()

let a: Option<i8> = Option::None;
// Panic with error message
let result = panic::catch_unwind(|| {
assert_option_some!(a);
//-> panic!
});
assert!(result.is_err());
let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
let expect = concat!(
    "assertion failed: `assert_option_some!(expr)`\n",
    "     expr label: `a`,\n",
    "     expr debug: `None`,\n",
    " expr.is_some(): `false`",
);
assert_eq!(actual, expect);

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

§Module macros