Skip to main content

check

Macro check 

Source
macro_rules! check {
    ($($tokens:tt)*) => { ... };
}
Expand description

Check if an expression evaluates to true or matches a pattern.

Use a let expression to test an expression against a pattern: check!(let pattern = expr). For other tests, just give a boolean expression to the macro: check!(1 + 2 == 2).

If the expression evaluates to false or if the pattern doesn’t match, an assertion failure is printed but the macro does not panic immediately. The check macro will cause the running test to fail eventually.

Use assert! if you want the test to panic instantly.

Currently, this macro uses a scope guard to delay the panic. However, this may change in the future if there is a way to signal a test failure without panicking. Do not rely on check!() to panic.

Unlike the assert!(...) macro, placeholders inside a check!(...) call are not made available in the calling scope. However, you can use the placeholders for further testing with a let chain in the same macro call:

check!(
    let Err(e) = std::fs::File::open("/non/existing/file")
    && e.kind() == std::io::ErrorKind::NotFound
    && let Some(_) = e.raw_os_error()
);

§Custom messages

You can pass additional arguments to the macro. These will be used to print a custom message in addition to the normal message.

check!(3 * 4 == 12, "Oh no, math is broken! 1 + 1 == {}", 1 + 1);