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
use crate::float_diff_provider::FloatDiffProvider;
use crate::Shoulda;
use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::{Range, RangeInclusive};

macro_rules! zip_all_test_eq_assertable_impl {
    ($x:ty) => {
        impl<T> Shoulda for $x
        where
            T: Debug,
            T: Shoulda,
        {
            fn test_eq<FloatDiff: FloatDiffProvider>(&self, other: &Self) -> bool {
                self.iter()
                    .zip(other.iter())
                    .all(|(a, b)| a.test_eq::<FloatDiff>(b))
            }
        }
    };
}

zip_all_test_eq_assertable_impl!(Vec<T>);
zip_all_test_eq_assertable_impl!(VecDeque<T>);
zip_all_test_eq_assertable_impl!(&[T]);
zip_all_test_eq_assertable_impl!([T]);

impl<T, K> Shoulda for HashMap<T, K>
where
    T: Debug,
    T: Shoulda,
    T: Eq,
    T: Hash,
    K: Debug,
    K: Shoulda,
{
    fn test_eq<FloatDiff: FloatDiffProvider>(&self, other: &Self) -> bool {
        self.len() == other.len()
            && self.keys().all(|x| {
                other
                    .get(x)
                    .map(|v| v.test_eq::<FloatDiff>(&self[x]))
                    .unwrap_or(false)
            })
    }
}

impl<T> Shoulda for HashSet<T>
where
    T: Debug,
    T: Shoulda,
    T: Eq,
    T: Hash,
{
    fn test_eq<FloatDiff: FloatDiffProvider>(&self, other: &Self) -> bool {
        //TODO: make not dependant on Eq
        self.iter().all(|x| other.contains(x))
    }
}

impl<T> Shoulda for Range<T>
where
    T: Debug,
    T: Shoulda,
{
    fn test_eq<FloatDiff: FloatDiffProvider>(&self, other: &Self) -> bool {
        self.start.test_eq::<FloatDiff>(&other.start) && self.end.test_eq::<FloatDiff>(&other.end)
    }
}

impl<T> Shoulda for RangeInclusive<T>
where
    T: Debug,
    T: Shoulda,
{
    fn test_eq<FloatDiff: FloatDiffProvider>(&self, other: &Self) -> bool {
        self.start().test_eq::<FloatDiff>(other.start())
            && self.end().test_eq::<FloatDiff>(other.end())
    }
}