|0.3.5||Feb 12, 2021|
|0.3.4||Jan 25, 2021|
|0.3.3||Sep 29, 2020|
|0.3.1||Jul 25, 2020|
|0.0.9||Jan 12, 2020|
#30 in Debugging
415 downloads per month
Used in 23 crates (22 directly)
check!(...) macros, inspired by Catch2.
There is also a
debug_assert!(...) macro that is disabled on optimized builds by default.
As cherry on top there is a
let_assert!(...) macro that lets you test a pattern while capturing parts of it.
These macros offer some benefits over the assertions from the standard library:
- The macros parse your expression to detect comparisons and adjust the error message accordingly.
assert_ne, just write
assert!(1 + 1 == 2), or even
assert!(1 + 1 > 1)!
- You can test for pattern matches:
assert!(let Err(_) = File::open("/non/existing/file")).
- You can capture parts of the pattern for further testing by using the
checkmacro can be used to perform multiple checks before panicking.
- The macros provide more information when the assertion fails.
- Colored failure messages!
The macros also accept additional arguments for a custom message, so it is fully compatible with
That means you don't have to worry about overwriting the standard
check!(6 + 1 <= 2 * 3);
check!(true && false);
check!(let Ok(_) = File::open("/non/existing/file"));
let_assert!(Err(e) = File::open("/non/existing/file")); check!(e.kind() == ErrorKind::PermissionDenied);
The crate provides two macros:
The main difference is that
check is really intended for test cases and doesn't immediately panic.
Instead, it will print the assertion error and fail the test.
This allows you to run multiple checks and can help to determine the reason of a test failure more easily.
assert macro on the other hand simply prints the error and panics,
and can be used outside of tests just as well.
check uses a scope guard to delay the panic until the current scope ends.
check doesn't panic at all, but only signals that a test case has failed.
If this becomes possible in the future, the
check macro will change, so you should not rely on
check to panic.
If available, the crate uses the
proc_macro_span feature to get the original source code.
On stable and beta, it falls back to stringifying the expression.
This makes the output a bit more readable on nightly.
You can also use the
It is very similar to
but all placeholders will be made available as variables in the calling scope.
This allows you to run additional checks on the captured variables.
let_assert!(Ok(foo) = Foo::try_new("bar")); check!(foo.name() == "bar"); let_assert!(Err(Error::InvalidName(e)) = Foo::try_new("bogus name")); check!(e.name() == "bogus name"); check!(e.to_string() == "invalid name: bogus name");
Colored output can be controlled using environment variables, as per the clicolors spec:
CLICOLOR != 0: ANSI colors are supported and should be used when the program isn't piped.
CLICOLOR == 0: Don't output ANSI color escape codes.
CLICOLOR_FORCE != 0: ANSI colors should be enabled no matter what.