macro_rules! assert_contains {
($container:expr, $containee:expr $(,)?) => { ... };
($container:expr, $containee:expr, $($message:tt)+) => { ... };
}Expand description
Assert a container is a match for an expression.
Pseudocode:
a.contains(b)
-
If true, return
(). -
Otherwise, call
panic!with a message and the values of the expressions with their debug representations.
§Examples
use assertables::*;
use std::collections::HashSet;
// String contains a substring
let a = "alfa";
let b = "lf";
assert_contains!(a, b);
// Range contains a value
let a = 1..3;
let b = 2;
assert_contains!(a, &b);
// Vector contains element
let a = vec![1, 2, 3];
let b = 2;
assert_contains!(a, &b);
// HashSet contains element.
// Notice the &b because the macro calls HashSet.contains(&self, &value).
let a: HashSet<String> = [String::from("1")].into();
let b: String = String::from("1");
assert_contains!(a, &b);
// HashSet contains element with automatic borrow optimization.
// Notice the b because the value type &str is already a reference,
// which HashSet.contains knows how to borrow to compare to String.
let a: HashSet<String> = [String::from("1")].into();
let b = "1";
assert_contains!(a, b);
// This will panic
let a = "alfa";
let b = "xx";
assert_contains!(a, b);
// assertion failed: `assert_contains!(container, containee)`
// https://docs.rs/assertables/…/assertables/macro.assert_contains.html
// container label: `a`,
// container debug: `\"alfa\"`,
// containee label: `b`,
// containee debug: `\"xx\"`