1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use super::*;
use std::collections::HashMap;
use std::fmt::Display;
use std::hash::Hash;

//TODO: add trait

pub struct ValueAssertions<'a, K, V> {
    key: &'a K,
    value: &'a V,
    hash_map_name: String,
}

impl<'a, K, V> ValueAssertions<'a, K, V>
where
    V: PartialEq + Display,
    K: Display,
{
    pub fn with_value(&'a self, expected_value: V) {
        if *self.value != expected_value {
            panic!(
                "Expected {} to contain {} with value '{}', but it has the value '{}'.",
                &self.hash_map_name, &self.key, expected_value, &self.value
            )
        }
    }
}

impl<'a, K, V> Asserter<&HashMap<K, V>>
where
    K: Eq + Hash + Display,
{
    /// Checks the length of the HashMap
    pub fn has_length(&self, expected_length: usize) {
        if self.value.len() != expected_length {
            panic!(
                "Expected {} to have length {}, but it has {}",
                &self.name,
                expected_length,
                self.value.len()
            );
        }
    }

    /// Checks if the HashMap is empty
    pub fn is_empty(&self) {
        if self.value.len() > 0 {
            panic!(
                "Expected {} to be empty, but it has length {}.",
                &self.name,
                self.value.len()
            )
        }
    }

    /// Checks if the HashMap is not empty
    pub fn is_not_empty(&self) {
        if self.value.is_empty() {
            panic!("Expected {} to not to be empty, but it is.", &self.name)
        }
    }

    /// Checks if the HashMap contains the specified key
    pub fn contains_key(&'a self, expected_key: &'a K) -> ValueAssertions<'a, K, V> {
        if !&self.value.contains_key(expected_key) {
            panic!(
                "Expected {} to contain {}, but it does not.",
                &self.name, &expected_key
            );
        }

        let value = &self.value.get(expected_key);
        let value_for_key = value.unwrap();

        ValueAssertions {
            key: expected_key,
            value: value_for_key,
            hash_map_name: String::from(&self.name),
        }
    }

    /// Checks if the HashMap does not contain the specified key
    pub fn does_not_contain_key(&self, not_expected_key: K) {
        if self.value.contains_key(&not_expected_key) {
            panic!(
                "Expected {} to not to contain {}, but it does.",
                &self.name, &not_expected_key
            );
        }
    }
}