macro_rules! assert_match {
( $pat:pat = $expr:expr) => {{
let value = $expr;
if let $pat = &value {
assert!(true)
} else {
eprintln!("failed to match {} = $expr", stringify!($pat));
eprintln!(" with $expr = {}", stringify!($expr));
eprintln!(" which evaluates to: {:?}", value);
panic!("assertion failed");
}
}}
}
macro_rules! assert_ok {
( $expr:expr ) => {{
let value = $expr;
match value {
Ok(x) => x,
Err(e) => {
eprintln!("failed to assert that $expr is Ok(...)");
eprintln!(" with $expr = {}", stringify!($expr));
eprintln!(" which is an Err(...): {}", e);
panic!("assertion failed");
}
}
}}
}
#[derive(Debug)]
enum Foo {
Foo(i32),
Bar(i32),
}
#[test]
fn test_assert_match() {
assert_match!(Foo::Foo(10) = Foo::Foo(10));
assert_match!(Foo::Bar(11) = Foo::Bar(11));
}
#[test]
#[should_panic]
fn test_assert_match_different_variant() {
assert_match!(Foo::Foo(10) = Foo::Bar(10));
}
#[test]
#[should_panic]
fn test_assert_match_different_value() {
assert_match!(Foo::Foo(10) = Foo::Foo(11));
}
#[test]
fn test_assert_ok() {
let result : Result<(), String> = Ok(());
assert_ok!(result);
}
#[test]
#[should_panic]
fn test_assert_ok_err() {
let result : Result<(), String> = Err(String::from("this is an error"));
assert_ok!(result);
}