xmtp_common/test/
macros.rs

1/// wrapper over assert!(matches!()) for Errors
2/// assert_err!(fun(), StorageError::Explosion)
3///
4/// or the message variant,
5/// assert_err!(fun(), StorageError::Explosion, "the storage did not explode");
6#[macro_export]
7macro_rules! assert_err {
8        ( $x:expr , $y:pat $(,)? ) => {
9            assert!(matches!($x, Err($y)))
10        };
11
12        ( $x:expr, $y:pat $(,)?, $($msg:tt)+) => {{
13            assert!(matches!($x, Err($y)), $($msg)+)
14        }}
15    }
16
17/// wrapper over assert! macros for Ok's
18///
19/// Make sure something is Ok(_) without caring about return value.
20/// assert_ok!(fun());
21///
22/// Against an expected value, e.g Ok(true)
23/// assert_ok!(fun(), true);
24///
25/// or the message variant,
26/// assert_ok!(fun(), Ok(_), "the storage is not ok");
27#[macro_export]
28macro_rules! assert_ok {
29
30        ( $e:expr ) => {
31            assert_ok!($e,)
32        };
33
34        ( $e:expr, ) => {{
35            use std::result::Result::*;
36            match $e {
37                Ok(v) => v,
38                Err(e) => panic!("assertion failed: Err({:?})", e),
39            }
40        }};
41
42        ( $x:expr , $y:expr $(,)? ) => {
43            assert_eq!($x, Ok($y.into()));
44        };
45
46        ( $x:expr, $y:expr $(,)?, $($msg:tt)+) => {{
47            assert_eq!($x, Ok($y.into()), $($msg)+);
48        }}
49    }