[][src]Macro assert_panic::assert_panic

macro_rules! assert_panic {
    ($stmt:stmt$(,)?) => { ... };
    ($stmt:stmt, $ty:ty$(,)?) => { ... };
    ($stmt:stmt, $ty:ty, starts with $expr:expr$(,)?) => { ... };
    ($stmt:stmt, $ty:ty, $expr:expr$(,)?) => { ... };
}

Asserts that $stmt panics.

  • Only this base form with a single expression returns the panic.

Optionally asserts the type of the panic.
Optionally asserts a panic text start, or a given panic value.

Panics

  • if $stmt doesn't panic.
  • optionally if the type of the panic doesn't match.
  • optionally if the panic text starts in the wrong way, or 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!("found"), &str, starts with "expected"),
    String,
    "Expected a panic starting with \"expected\" but found \"found\"",
);

assert_panic!(
    assert_panic!(panic!(1usize), usize, 2usize),
    String,
    "Expected 2 but found 1",
);