use crate::*;
use std::any::Any;
use std::panic;
use std::panic::{AssertUnwindSafe, PanicHookInfo};
pub fn test_panic<F, R>(f: F) -> TestPanicResult<R>
where
F: FnOnce() -> R,
{
let default_panic_hook = panic::take_hook();
let empty_panic_hook = Box::new(|_: &PanicHookInfo| {});
panic::set_hook(empty_panic_hook);
let result = panic::catch_unwind(AssertUnwindSafe(f));
panic::set_hook(default_panic_hook);
if result.is_err() {
return TestPanicResult::Panic(result.err().unwrap());
}
TestPanicResult::Cool(result.unwrap())
}
pub fn ok<R>(value: R) -> TestPanicResult<R> {
TestPanicResult::Cool(value)
}
pub fn ng<R>() -> TestPanicResult<R> {
TestPanicResult::Panic(Box::new(()))
}
pub fn msg<R, P>(payload: P) -> TestPanicResult<R>
where
P: Any + Send,
{
TestPanicResult::Panic(Box::new(payload))
}