abi_stable_shared/
test_utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::{
    any::Any,
    panic::{catch_unwind, AssertUnwindSafe, Location},
};

pub type ThreadError = Box<dyn Any + Send + 'static>;

#[derive(Debug, Clone)]
pub struct ShouldHavePanickedAt {
    pub span: &'static std::panic::Location<'static>,
}

#[track_caller]
pub fn must_panic<F, R>(f: F) -> Result<ThreadError, ShouldHavePanickedAt>
where
    F: FnOnce() -> R,
{
    match catch_unwind(AssertUnwindSafe(f)) {
        Ok(_) => Err(ShouldHavePanickedAt {
            span: Location::caller(),
        }),
        Err(e) => Ok(e),
    }
}

#[test]
fn test_must_panic() {
    assert!(must_panic(|| panic!()).is_ok());
    assert!(must_panic(|| ()).is_err());
}