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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use super::{AssertionFailure, Spec};

use std::borrow::Borrow;
use std::collections::HashMap;
use std::fmt::Debug;
use std::hash::Hash;

pub trait HashMapAssertions<'s> {
    fn has_length(&mut self, expected: usize);
    fn is_empty(&mut self);
    fn is_not_empty(&mut self);
}

pub trait KeyHashMapAssertions<'s, K: Hash + Eq, V> {
    fn contains_key<E: Borrow<K>>(&mut self, expected_key: E) -> Spec<'s, V>;
    fn does_not_contain_key<E: Borrow<K>>(&mut self, expected_key: E);
}

pub trait EntryHashMapAssertions<'s, K: Hash + Eq, V: PartialEq> {
    fn contains_entry<E: Borrow<K>, F: Borrow<V>>(&mut self, expected_key: E, expected_value: F);
    fn does_not_contain_entry<E: Borrow<K>, F: Borrow<V>>(
        &mut self,
        expected_key: E,
        expected_value: F,
    );
}

impl<'s, K, V> HashMapAssertions<'s> for Spec<'s, HashMap<K, V>>
where
    K: Hash + Eq + Debug,
    V: Debug,
{
    /// Asserts that the length of the subject hashmap is equal to the provided length. The subject
    /// type must be of `HashMap`.
    ///
    /// ```rust
    /// # use speculoos::prelude::*;
    /// # use std::collections::HashMap;
    /// let mut test_map = HashMap::new();
    /// test_map.insert(1, 1);
    /// test_map.insert(2, 2);
    ///
    /// assert_that(&test_map).has_length(2);
    /// ```
    fn has_length(&mut self, expected: usize) {
        let subject = self.subject;

        if subject.len() != expected {
            AssertionFailure::from_spec(self)
                .with_expected(format!("hashmap to have length <{}>", expected))
                .with_actual(format!("<{}>", subject.len()))
                .fail();
        }
    }

    /// Asserts that the subject hashmap is empty. The subject type must be of `HashMap`.
    ///
    /// ```rust
    /// # use speculoos::prelude::*;
    /// # use std::collections::HashMap;
    /// let test_map: HashMap<u8, u8> = HashMap::new();
    /// assert_that(&test_map).is_empty();
    /// ```
    fn is_empty(&mut self) {
        let subject = self.subject;

        if !subject.is_empty() {
            AssertionFailure::from_spec(self)
                .with_expected("an empty hashmap".to_string())
                .with_actual(format!("a hashmap with length <{:?}>", subject.len()))
                .fail();
        }
    }

    /// Asserts that the subject hashmap is not empty. The subject type must be of `HashMap`.
    ///
    /// ```rust
    /// # use speculoos::prelude::*;
    /// # use std::collections::HashMap;
    /// let mut test_map: HashMap<u8, u8> = HashMap::new();
    /// test_map.insert(1, 2);
    /// assert_that(&test_map).is_not_empty();
    /// ```
    fn is_not_empty(&mut self) {
        let subject = self.subject;

        if subject.is_empty() {
            AssertionFailure::from_spec(self)
                .with_expected("an non empty hashmap".to_string())
                .with_actual(format!("a hashmap with length <{:?}>", subject.len()))
                .fail();
        }
    }
}

impl<'s, K, V> KeyHashMapAssertions<'s, K, V> for Spec<'s, HashMap<K, V>>
where
    K: Hash + Eq + Debug,
    V: Debug,
{
    /// Asserts that the subject hashmap contains the expected key. The subject type must be
    /// of `HashMap`.
    ///
    /// This will return a new `Spec` containing the associated value if the key is present.
    ///
    /// ```rust
    /// # use speculoos::prelude::*;
    /// # use std::collections::HashMap;
    /// let mut test_map = HashMap::new();
    /// test_map.insert("hello", "hi");
    ///
    /// assert_that(&test_map).contains_key(&"hello");
    /// ```
    fn contains_key<E: Borrow<K>>(&mut self, expected_key: E) -> Spec<'s, V> {
        let subject = self.subject;
        let borrowed_expected_key = expected_key.borrow();

        if let Some(value) = subject.get(borrowed_expected_key) {
            return Spec {
                subject: value,
                subject_name: self.subject_name,
                location: self.location.clone(),
                description: self.description,
            };
        }

        let subject_keys: Vec<&K> = subject.keys().collect();

        AssertionFailure::from_spec(self)
            .with_expected(format!(
                "hashmap to contain key <{:?}>",
                borrowed_expected_key
            ))
            .with_actual(format!("<{:?}>", subject_keys))
            .fail();

        unreachable!();
    }

    /// Asserts that the subject hashmap does not contain the provided key. The subject type must be
    /// of `HashMap`.
    ///
    /// ```rust
    /// # use speculoos::prelude::*;
    /// # use std::collections::HashMap;
    /// let mut test_map = HashMap::new();
    /// test_map.insert("hello", "hi");
    ///
    /// assert_that(&test_map).does_not_contain_key(&"hey");
    /// ```
    fn does_not_contain_key<E: Borrow<K>>(&mut self, expected_key: E) {
        let subject = self.subject;
        let borrowed_expected_key = expected_key.borrow();

        if subject.get(borrowed_expected_key).is_some() {
            AssertionFailure::from_spec(self)
                .with_expected(format!(
                    "hashmap to not contain key <{:?}>",
                    borrowed_expected_key
                ))
                .with_actual("present in hashmap".to_string())
                .fail();
        }
    }
}

impl<'s, K, V> EntryHashMapAssertions<'s, K, V> for Spec<'s, HashMap<K, V>>
where
    K: Hash + Eq + Debug,
    V: PartialEq + Debug,
{
    /// Asserts that the subject hashmap contains the expected key with the expected value.
    /// The subject type must be of `HashMap`.
    ///
    /// ```rust
    /// # use speculoos::prelude::*;
    /// # use std::collections::HashMap;
    /// let mut test_map = HashMap::new();
    /// test_map.insert("hello", "hi");
    ///
    /// assert_that(&test_map).contains_entry(&"hello", &"hi");
    /// ```
    fn contains_entry<E: Borrow<K>, F: Borrow<V>>(&mut self, expected_key: E, expected_value: F) {
        let subject = self.subject;
        let borrowed_expected_key = expected_key.borrow();
        let borrowed_expected_value = expected_value.borrow();

        let expected_message = format!(
            "hashmap containing key <{:?}> with value <{:?}>",
            borrowed_expected_key, borrowed_expected_value
        );

        if let Some(value) = subject.get(borrowed_expected_key) {
            if value.eq(borrowed_expected_value) {
                return;
            }

            AssertionFailure::from_spec(self)
                .with_expected(expected_message)
                .with_actual(format!(
                    "key <{:?}> with value <{:?}> instead",
                    borrowed_expected_key, value
                ))
                .fail();

            unreachable!();
        }

        let subject_keys: Vec<&K> = subject.keys().collect();

        AssertionFailure::from_spec(self)
            .with_expected(expected_message)
            .with_actual(format!("no matching key, keys are <{:?}>", subject_keys))
            .fail();
    }

    /// Asserts that the subject hashmap does not contains the provided key and value.
    /// The subject type must be of `HashMap`.
    ///
    /// ```rust
    /// # use speculoos::prelude::*;
    /// # use std::collections::HashMap;
    /// let mut test_map = HashMap::new();
    /// test_map.insert("hello", "hi");
    ///
    /// assert_that(&test_map).does_not_contain_entry(&"hello", &"hey");
    /// ```
    fn does_not_contain_entry<E: Borrow<K>, F: Borrow<V>>(
        &mut self,
        expected_key: E,
        expected_value: F,
    ) {
        let subject = self.subject;
        let borrowed_expected_key = expected_key.borrow();
        let borrowed_expected_value = expected_value.borrow();

        if let Some(value) = subject.get(borrowed_expected_key) {
            if !value.eq(borrowed_expected_value) {
                return;
            }

            AssertionFailure::from_spec(self)
                .with_expected(format!(
                    "hashmap to not contain key <{:?}> with value <{:?}>",
                    borrowed_expected_key, borrowed_expected_value
                ))
                .with_actual("present in hashmap".to_string())
                .fail();
        }
    }
}

#[cfg(test)]
mod tests {

    use super::super::prelude::*;

    use std::collections::HashMap;

    #[test]
    fn should_not_panic_if_hashmap_length_matches_expected() {
        let mut test_map = HashMap::new();
        test_map.insert(1, 1);
        test_map.insert(2, 2);

        assert_that(&test_map).has_length(2);
    }

    #[test]
    fn should_not_panic_if_hashmap_length_matches_expected_and_value_is_not_partialeq() {
        #[derive(Debug)]
        struct NoPartialEq;
        let mut test_map = HashMap::new();
        test_map.insert(1, NoPartialEq);
        test_map.insert(2, NoPartialEq);

        assert_that(&test_map).has_length(2);
    }

    #[test]
    #[should_panic(expected = "\n\texpected: hashmap to have length <1>\n\t but was: <2>")]
    fn should_panic_if_hashmap_length_does_not_match_expected() {
        let mut test_map = HashMap::new();
        test_map.insert(1, 1);
        test_map.insert(2, 2);

        assert_that(&test_map).has_length(1);
    }

    #[test]
    fn should_not_panic_if_hashmap_was_expected_to_be_empty_and_is() {
        let test_map: HashMap<u8, u8> = HashMap::new();
        assert_that(&test_map).is_empty();
    }

    #[test]
    fn should_not_panic_if_hashmap_was_expected_to_not_be_empty_and_is_not() {
        let mut test_map: HashMap<u8, u8> = HashMap::new();
        test_map.insert(1, 2);
        assert_that(&test_map).is_not_empty();
    }

    #[test]
    #[should_panic(expected = "\n\texpected: an empty hashmap\
                   \n\t but was: a hashmap with length <1>")]
    fn should_panic_if_hashmap_was_expected_to_be_empty_and_is_not() {
        let mut test_map = HashMap::new();
        test_map.insert(1, 1);

        assert_that(&test_map).is_empty();
    }

    #[test]
    #[should_panic(expected = "\n\texpected: an non empty hashmap\
                   \n\t but was: a hashmap with length <0>")]
    fn should_panic_if_hashmap_was_expected_to_not_be_empty_and_is() {
        let test_map: HashMap<u8, u8> = HashMap::new();
        assert_that(&test_map).is_not_empty();
    }

    #[test]
    fn contains_key_should_allow_multiple_borrow_forms() {
        let mut test_map = HashMap::new();
        test_map.insert("hello", "hi");

        assert_that(&test_map).contains_key("hello");
        assert_that(&test_map).contains_key(&mut "hello");
        assert_that(&test_map).contains_key(&"hello");
    }

    #[test]
    fn should_not_panic_if_hashmap_contains_key() {
        let mut test_map = HashMap::new();
        test_map.insert("hello", "hi");

        assert_that(&test_map).contains_key(&"hello");
    }

    #[test]
    // Unfortunately the order of the keys can change. Doesn't seem to make sense to sort them
    // just for the sake of checking the panic message.
    #[should_panic]
    fn should_not_panic_if_hashmap_does_not_contain_key() {
        let mut test_map = HashMap::new();
        test_map.insert("hi", "hi");
        test_map.insert("hey", "hey");

        assert_that(&test_map).contains_key(&"hello");
    }

    #[test]
    fn should_be_able_to_chain_value_from_contains_key() {
        let mut test_map = HashMap::new();
        test_map.insert("hello", "hi");

        assert_that(&test_map)
            .contains_key(&"hello")
            .is_equal_to(&"hi");
    }

    #[test]
    fn does_not_contain_key_should_allow_multiple_borrow_forms() {
        let mut test_map = HashMap::new();
        test_map.insert("hello", "hi");

        assert_that(&test_map).does_not_contain_key("hey");
        assert_that(&test_map).does_not_contain_key(&mut "hey");
        assert_that(&test_map).does_not_contain_key(&"hey");
    }

    #[test]
    fn should_not_panic_if_hashmap_does_not_contain_key_when_expected() {
        let mut test_map = HashMap::new();
        test_map.insert("hello", "hi");

        assert_that(&test_map).does_not_contain_key(&"hey");
    }

    #[test]
    #[should_panic(expected = "\n\texpected: hashmap to not contain key <\"hello\">\
                   \n\t but was: present in hashmap")]
    fn should_panic_if_hashmap_does_contain_key_when_not_expected() {
        let mut test_map = HashMap::new();
        test_map.insert("hello", "hi");

        assert_that(&test_map).does_not_contain_key(&"hello");
    }

    #[test]
    fn contains_entry_should_allow_multiple_borrow_forms() {
        let mut test_map = HashMap::new();
        test_map.insert("hello", "hi");

        assert_that(&test_map).contains_entry("hello", "hi");
        assert_that(&test_map).contains_entry(&mut "hello", &mut "hi");
        assert_that(&test_map).contains_entry("hello", &mut "hi");
        assert_that(&test_map).contains_entry(&"hello", &"hi");
    }

    #[test]
    fn should_not_panic_if_hashmap_contains_entry() {
        let mut test_map = HashMap::new();
        test_map.insert("hello", "hi");

        assert_that(&test_map).contains_entry(&"hello", &"hi");
    }

    #[test]
    #[should_panic(
        expected = "\n\texpected: hashmap containing key <\"hey\"> with value <\"hi\">\
                   \n\t but was: no matching key, keys are <[\"hello\"]>"
    )]
    fn should_panic_if_hashmap_contains_entry_without_key() {
        let mut test_map = HashMap::new();
        test_map.insert("hello", "hi");

        assert_that(&test_map).contains_entry(&"hey", &"hi");
    }

    #[test]
    #[should_panic(
        expected = "\n\texpected: hashmap containing key <\"hi\"> with value <\"hey\">\
                   \n\t but was: key <\"hi\"> with value <\"hello\"> instead"
    )]
    fn should_panic_if_hashmap_contains_entry_with_different_value() {
        let mut test_map = HashMap::new();
        test_map.insert("hi", "hello");

        assert_that(&test_map).contains_entry(&"hi", &"hey");
    }

    #[test]
    fn should_not_panic_if_hashmap_does_not_contain_entry_if_expected() {
        let mut test_map = HashMap::new();
        test_map.insert("hello", "hi");

        assert_that(&test_map).does_not_contain_entry(&"hey", &"hi");
    }

    #[test]
    fn should_not_panic_if_hashmap_contains_entry_with_different_value_if_expected() {
        let mut test_map = HashMap::new();
        test_map.insert("hi", "hello");

        assert_that(&test_map).does_not_contain_entry(&"hi", &"hey");
    }

    #[test]
    #[should_panic(expected = "\n\texpected: hashmap to not contain key <\"hello\"> \
    with value <\"hi\">\
                   \n\t but was: present in hashmap")]
    fn should_panic_if_hashmap_contains_entry_if_not_expected() {
        let mut test_map = HashMap::new();
        test_map.insert("hello", "hi");

        assert_that(&test_map).does_not_contain_entry(&"hello", &"hi");
    }
}