[][src]Macro cool_asserts::assertion_failure

macro_rules! assertion_failure {
    ($message:literal $($(, $key:ident $($spec:ident)?: $value:expr)+)? $(; $($fmt:tt)+)? ) => { ... };
    ($message:literal $($(, $key:ident $($spec:ident)?: $value:expr)+)? ,) => { ... };
    ($message:literal $($(, $key:ident $($spec:ident)?: $value:expr)+)? ;) => { ... };
}

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 and rhs of a failed assert_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/lib.rs:7: `(Assertion Message)`
      key: 10
 long_key: "Hello\tWorld!"
  Trailing message: Hello, World!"#
   );