expect_rs/
map.rs

1use crate::Assert;
2use std::collections::HashMap;
3use std::fmt::Debug;
4use std::hash::Hash;
5
6impl<'a, K, V> Assert<'a, HashMap<K, V>>
7where
8    K: Eq + Hash + Debug + PartialEq,
9    V: PartialEq + Debug,
10{
11    pub fn contains_key(&self, key: &K) -> &Self {
12        match self.actual.get(key) {
13            Some(_) => {
14                // ok
15            }
16            None => {
17                panic!("[contains_key]should be found: {:?}", key);
18            }
19        }
20        self
21    }
22
23    pub fn contains_all(&self, expected: &HashMap<K, V>) -> &Self {
24        let mut not_found = HashMap::new();
25
26        for (key, value) in expected {
27            match self.actual.get(key) {
28                Some(v) => {
29                    if value != v {
30                        not_found.insert(key, value);
31                    }
32                }
33                None => {
34                    not_found.insert(key, value);
35                }
36            }
37        }
38
39        assert!(
40            not_found.len() == 0,
41            "[contains_all]should not be found: {:?}",
42            not_found
43        );
44
45        self
46    }
47}