| 1 |
use assert_matches2::assert_matches; |
| 2 |
|
| 3 |
#[test] |
| 4 |
fn no_error() { |
| 5 |
#[derive(Debug)] |
| 6 |
struct Foo { |
| 7 |
bar: Bar, |
| 8 |
} |
| 9 |
|
| 10 |
#[derive(Debug)] |
| 11 |
enum Bar { |
| 12 |
V1(u32), |
| 13 |
V2 { field: String }, |
| 14 |
} |
| 15 |
|
| 16 |
let var = Foo { bar: Bar::V1(10) }; |
| 17 |
assert_matches!(var, Foo { bar: ref r @ Bar::V1(int) }); |
| 18 |
assert_matches!(r, Bar::V1(_)); |
| 19 |
assert_eq!(int, 10); |
| 20 |
|
| 21 |
let var = Foo { bar: Bar::V2 { field: "test".to_owned() } }; |
| 22 |
assert_matches!(var, Foo { bar: Bar::V2 { field: rename } }); |
| 23 |
assert_eq!(rename, "test"); |
| 24 |
} |
| 25 |
|
| 26 |
#[test] |
| 27 |
#[should_panic(expected = "\ |
| 28 |
match assertion failed |
| 29 |
pattern: `None` |
| 30 |
value: `Some(8)`\ |
| 31 |
")] |
| 32 |
fn default_error() { |
| 33 |
assert_matches!(Some(8u8), None); |
| 34 |
} |
| 35 |
|
| 36 |
#[test] |
| 37 |
#[should_panic(expected = "\ |
| 38 |
match assertion failed: custom error message |
| 39 |
pattern: `Some(8u8)` |
| 40 |
value: `None`\ |
| 41 |
")] |
| 42 |
fn custom_error() { |
| 43 |
let msg = "message"; |
| 44 |
assert_matches!(None, Some(8u8), "custom error {msg}"); |
| 45 |
} |