macro_rules! assert_panic {
($stmt:stmt$(,)?) => { ... };
($stmt:stmt, $ty:ty$(,)?) => { ... };
($stmt:stmt, $ty:ty, contains $expr:expr$(,)?) => { ... };
($stmt:stmt, $ty:ty, starts with $expr:expr$(,)?) => { ... };
($stmt:stmt, $ty:ty, $expr:expr$(,)?) => { ... };
}Expand description
Asserts that $stmt panics.
- Only this base form with a single expression returns the panic.
Optionally asserts the type of the panic.
Optionally asserts the downcast panic contains, starts with or equals a given expression $expr.
§Panics
- if
$stmtdoesn’t panic. - optionally if the type of the panic doesn’t match.
- optionally if the panic has the wrong value.
§Example
use assert_panic::assert_panic;
let _: Box<dyn Any + Send + 'static> =
assert_panic!(panic!("at the Disco"));
assert_panic!(panic!("at the Disco"), &str);
assert_panic!(
{ assert_panic!({}); },
String,
starts with "assert_panic! argument did not panic:",
);
assert_panic!(
assert_panic!(panic!("at the Disco"), String),
String,
starts with "Expected a `String` panic but found one with TypeId { t: ",
);
assert_panic!(
assert_panic!(panic!("found"), &str, contains "expected"),
String,
"Expected a panic containing \"expected\" but found \"found\"",
);
assert_panic!(
assert_panic!(panic!("found"), &str, starts with "expected"),
String,
"Expected a panic starting with \"expected\" but found \"found\"",
);
assert_panic!(
assert_panic!(panic!(1_usize), usize, 2_usize),
String,
"Expected a panic equal to 2 but found 1",
);§Details
All arguments are evaluated at most once, but $expr must be Copy.
$expr is only evaluated if $stmt panics.
Type assertions use Any::downcast_ref::<$ty>().
The value is examined by reference panic only after downcasting it to $ty:
containsusespanic.contains(expr).starts withusespanic.starts_with(expr).- Equality comparison is done with
*panic == expr.
All of this is duck-typed, so the respective forms only require that matching methods / the matching operator are present.