abi_stable_shared/
test_utils.rs

1use std::{
2    any::Any,
3    panic::{catch_unwind, AssertUnwindSafe, Location},
4};
5
6pub type ThreadError = Box<dyn Any + Send + 'static>;
7
8#[derive(Debug, Clone)]
9pub struct ShouldHavePanickedAt {
10    pub span: &'static std::panic::Location<'static>,
11}
12
13#[track_caller]
14pub fn must_panic<F, R>(f: F) -> Result<ThreadError, ShouldHavePanickedAt>
15where
16    F: FnOnce() -> R,
17{
18    match catch_unwind(AssertUnwindSafe(f)) {
19        Ok(_) => Err(ShouldHavePanickedAt {
20            span: Location::caller(),
21        }),
22        Err(e) => Ok(e),
23    }
24}
25
26#[test]
27fn test_must_panic() {
28    assert!(must_panic(|| panic!()).is_ok());
29    assert!(must_panic(|| ()).is_err());
30}