Function googletest::matchers::has_entry

source ·
pub fn has_entry<KeyT: Debug + Eq + Hash, ValueT: Debug, MatcherT: Matcher<ActualT = ValueT>>(
    key: KeyT,
    inner: MatcherT
) -> impl Matcher<ActualT = HashMap<KeyT, ValueT>>
Expand description

Matches a HashMap containing the given key whose value is matched by the matcher inner.

let value = HashMap::from([(0, 1), (1, -1)]);
verify_that!(value, has_entry(0, eq(1)))?;  // Passes
verify_that!(value, has_entry(1, gt(0)))?;  // Fails: value not matched
verify_that!(value, has_entry(2, eq(0)))?;  // Fails: key not present

Note: One could obtain the same effect by collecting entries into a Vec and using contains:

let value = HashMap::from([(0, 1), (1, -1)]);
verify_that!(value.into_iter().collect::<Vec<_>>(), contains(eq((0, 1))))?;

However, has_entry will offer somewhat better diagnostic messages in the case of assertion failure. And it avoid the extra allocation hidden in the code above.