#let #pattern #assert #match #macro

no-std assert_let_bind

A convenience macro for writing pattern-matching tests

2 releases

0.1.1 Aug 15, 2022
0.1.0 Aug 15, 2022

#706 in Debugging

Download history 898/week @ 2024-08-26 809/week @ 2024-09-02 635/week @ 2024-09-09 849/week @ 2024-09-16 1221/week @ 2024-09-23 1653/week @ 2024-09-30 4705/week @ 2024-10-07 7128/week @ 2024-10-14 8591/week @ 2024-10-21 6658/week @ 2024-10-28 6130/week @ 2024-11-04 5990/week @ 2024-11-11 2151/week @ 2024-11-18 2107/week @ 2024-11-25 1963/week @ 2024-12-02 3723/week @ 2024-12-09

10,031 downloads per month
Used in 3 crates

MIT license

5KB

assert_let_bind

A convenience macro for writing pattern-matching tests in the Rust programming language.

The assert_let macro tests whether a value matches a given pattern, binding the pattern in the current scope if the match succeeds and causing a panic if the match fails.

(Strongly inspired by assert_matches)

use assert_let_bind::assert_let;

#[derive(Debug)]
enum Foo {
    A(i32, i32),
    B(i32),
}

let foo = Foo::A(3000, 2000);

assert_let!(Foo::A(x, y), foo);
assert_eq!(x + y, 5000);

Generally speaking, assert_let(pattern, expr) is roughly equivalent to:

let pattern = expr else { panic!("some panic message with {} {}", pattern, expr)};

Usage

This macro relies on let else, and thus only compiles in nightly Rust for now.

To add it to your project:

cargo add assert_let_bind

To add it only to your tests:

cargo add --dev assert_let_bind

License

This code is distributed under the terms of the MIT license. See LICENSE file for details.

Contributing

Contributions are unnecessary because this crate is already perfect.

In the unlikely eventuality that it isn't, issues and pull requests welcome.

No runtime deps