#assert #debug-assert #debug #assertions #testing

more-asserts

Small library providing additional assert_* and debug_assert_* macros

5 unstable releases

0.3.0 Jun 3, 2022
0.2.2 Nov 17, 2021
0.2.1 May 23, 2018
0.2.0 May 23, 2018
0.1.0 Jun 18, 2016

#34 in Rust patterns

Download history 67059/week @ 2022-04-18 59271/week @ 2022-04-25 55837/week @ 2022-05-02 64646/week @ 2022-05-09 60552/week @ 2022-05-16 62736/week @ 2022-05-23 52290/week @ 2022-05-30 62742/week @ 2022-06-06 87009/week @ 2022-06-13 99836/week @ 2022-06-20 104355/week @ 2022-06-27 108900/week @ 2022-07-04 121707/week @ 2022-07-11 85581/week @ 2022-07-18 88574/week @ 2022-07-25 85346/week @ 2022-08-01

387,638 downloads per month
Used in 299 crates (59 directly)

CC0 license

16KB
229 lines

More Asserts (for Rust).

Docs Latest Version Minimum Rust Version

Small library providing assertion macros similar to the {debug_,}assert_{eq,ne} macros in the stdlib.

Usage

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!",
);

License

CC0 (public domain).


lib.rs:

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!.

Name Enabled Equivalent to
assert_le! Always assert!(a <= b)
assert_lt! Always assert!(a < b)
assert_ge! Always assert!(a >= b)
assert_gt! Always assert!(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!",
);

No runtime deps