Hello, my question has already been asked + answered in this post:
But I think this is not a sufficient answer. It hides all panic stack traces, but I want to do that only for the specific panic I want to test for.
[should_panic = "foo"]
I tried to create a custom panic hook like this:
fn should_panic(contains: &'static str){ //call at the first line of a `should_panic` unit test
let default_hook = take_hook();
set_hook(Box::new(move |panic_info|{
if !matches!(panic_info.payload().downcast_ref::<&str>(), Some(msg) if msg.contains(contains)){
default_hook(panic_info);
}
}));
}
But this does not seem to work, it causes my unit test to fail even with the correct panic.
What could I do? Shouldn't this be the default behaviour of should_panic
unit tests?