assert_eq 0.2.0

assert_eq: location-aware equality assertions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use std::panic::{catch_unwind, resume_unwind, UnwindSafe};

pub fn check_panic_message(func: impl FnOnce() + UnwindSafe, msg: &'static str) {
    let chk = |panic_msg: &'_ str| panic_msg.contains(msg);
    if let Err(err) = catch_unwind(func) {
        let resume_panicking = if let Some(s) = err.downcast_ref::<String>() {
            chk(&*s)
        } else if let Some(s) = err.downcast_ref::<&'static str>() {
            chk(*s)
        } else {
            false
        };

        if resume_panicking {
            resume_unwind(err)
        }
    }
}