Macro cool_asserts::assertion_failure
source · [−]macro_rules! assertion_failure {
($message:literal $($(, $key:ident $($spec:ident)? : $value:expr)+)? $(; $( $fmt_pattern:literal $($fmt:tt)* )?)? ) => { ... };
($message:literal $($(, $key:ident $($spec:ident)? : $value:expr)+)? ,) => { ... };
}
Expand description
Panic with an assertion failure message
This macro is used in failing assertions to issue a panic with a message in a standardized format, which includes:
- A static description of what went wrong.
- The file and line number of the error.
- Optionally, key-value components of the failed assertion (for example,
the
lhs
andrhs
of a failedassert_eq!
). These components are visually aligned for an easier debugging experience. - Optionally, a trailing formatted message.
The format of this macro is designed to be similar to the message printed
by assert_eq!
or assert_ne!
. The exact output is not currently
guaranteed to be identical with matching semver versions, though this may
change in the future.
Example:
use std::panic::catch_unwind;
use cool_asserts::{assertion_failure, get_panic_message};
let result = catch_unwind(|| assertion_failure!(
"Assertion Message",
// Add key-value data to the debug message
key: 10,
// Use the "debug" keyword to debug-print something.
// Additionally, notice in the output that the key and long-key
// are visually aligned.
long_key debug: "Hello\tWorld!";
// After a semicolon, add a trailing format message
"Trailing message: {}, {}!", "Hello", "World"
));
let panic = result.unwrap_err();
let message = get_panic_message(&panic).unwrap();
assert_eq!(
message,
r#"assertion failed at src/assertion_failure.rs:7: `(Assertion Message)`
key: 10
long_key: "Hello\tWorld!"
Trailing message: Hello, World!"#
);