#panic #assert #happen #kind #macro

assert-panic

Assert that a panic happens, and optionally what (kind of) panic happens

4 releases (2 stable)

1.0.1 Jun 10, 2020
1.0.0-preview1 May 23, 2020
1.0.0-preview.1 May 24, 2020

#431 in Testing

Download history 1320/week @ 2023-12-07 716/week @ 2023-12-14 330/week @ 2023-12-21 753/week @ 2023-12-28 2198/week @ 2024-01-04 1377/week @ 2024-01-11 1405/week @ 2024-01-18 1499/week @ 2024-01-25 1428/week @ 2024-02-01 1412/week @ 2024-02-08 1040/week @ 2024-02-15 1442/week @ 2024-02-22 2141/week @ 2024-02-29 1514/week @ 2024-03-07 2096/week @ 2024-03-14 1037/week @ 2024-03-21

7,151 downloads per month
Used in 5 crates

MIT/Apache

9KB
86 lines

assert-panic

Latest Version docs.rs

This library provides a macro that asserts that a panic happens, and optionally what (kind of) panic happens.

The simplest version gives you the panic as boxed Any.

Example

# use std::any::Any;
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",
);

Versioning

assert-panic strictly follows Semantic Versioning 2.0.0 with the following exceptions:

  • The minor version will not reset to 0 on major version changes.
    Consider it the global feature level.
  • The patch version will not reset to 0 on major or minor version changes.
    Consider it the global patch level.
  • Panic messages originating from assert-panic are considered an implementation detail.
    Please only evaluate panic values you have direct control over.

No runtime deps