Macro mz_ore::assert_contains

source ·
macro_rules! assert_contains {
    ($left:expr, $right:expr $(,)?) => { ... };
}
Available on crate feature test only.
Expand description

Asserts that the left expression contains the right expression.

Containment is determined by the contains method on the left type. If the left expression does not contain the right expression, the macro will panic with a descriptive message that includes both the left and right expressions.

§Motivation

The standard pattern for asserting containment uses the assert! macro

assert!(left.contains(&right))

but this pattern panics with a message that only displays false as the cause. This hampers determination of the true cause of the assertion failure.

§Examples

Check whether a string contains a substring:

use mz_ore::assert_contains;
assert_contains!("hello", "ello");

Check whether a slice contains an element:

use mz_ore::assert_contains;
assert_contains!(&[1, 2, 3], 2);

Failed assertions panic:

use mz_ore::assert_contains;
assert_contains!("hello", "yellow");