Crate more_asserts

Source
Expand description

Small library providing some macros helpful for asserting. The API is very similar to the API provided by the stdlib’s own assert_eq!, assert_ne!, debug_assert_eq!, and debug_assert_ne!.

NameEnabledEquivalent to
assert_le!Alwaysassert!(a <= b)
assert_lt!Alwaysassert!(a < b)
assert_ge!Alwaysassert!(a >= b)
assert_gt!Alwaysassert!(a > b)
debug_assert_le!if cfg!(debug_assertions)debug_assert!(a <= b)
debug_assert_lt!if cfg!(debug_assertions)debug_assert!(a < b)
debug_assert_ge!if cfg!(debug_assertions)debug_assert!(a >= b)
debug_assert_gt!if cfg!(debug_assertions)debug_assert!(a > b)
debug_unreachable!if cfg!(debug_assertions)unreachable! when debug_assertions are on.

When one of the assertions fails, it prints out a message like the following:

thread 'main' panicked at 'assertion failed: `left < right`
  left: `4`,
 right: `3`', src/main.rs:47:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.

§Example

use more_asserts as ma;

#[derive(Debug, PartialEq, PartialOrd)]
enum Example { Foo, Bar }

ma::assert_le!(3, 4);
ma::assert_ge!(
    10, 10,
    "You can pass a message too (just like `assert_eq!`)",
);
ma::debug_assert_lt!(
    1.3, 4.5,
    "Format syntax is supported ({}).",
    "also like `assert_eq!`"
);

ma::assert_gt!(
    Example::Bar, Example::Foo,
    "It works on anything that implements PartialOrd and Debug!",
);

Macros§

  • Panics if the first expression is not greater than or equal to the second.
  • Panics if the first expression is not strictly greater than the second.
  • Panics if the first expression is not less than or equal to the second.
  • Panics if the first expression is not strictly less than the second.
  • Same as assert_ge! in builds with debug assertions enabled, and a no-op otherwise.
  • Same as assert_gt! in builds with debug assertions enabled, and a no-op otherwise.
  • Same as assert_le! in builds with debug assertions enabled, and a no-op otherwise.
  • Same as assert_lt! in builds with debug assertions enabled, and a no-op otherwise.
  • Panics if reached when debug assertions are enabled.