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::epsilon_provider::EpsilonProvider;
use crate::ShouldaEqual;
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> ShouldaEqual for $x
        where
            T: Debug,
            T: ShouldaEqual,
        {
            fn should_eq<Epsilon: EpsilonProvider>(&self, other: &Self) -> bool {
                self.iter()
                    .zip(other.iter())
                    .all(|(a, b)| a.should_eq::<Epsilon>(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> ShouldaEqual for HashMap<T, K>
where
    T: Debug,
    T: ShouldaEqual,
    T: Eq,
    T: Hash,
    K: Debug,
    K: ShouldaEqual,
{
    fn should_eq<Epsilon: EpsilonProvider>(&self, other: &Self) -> bool {
        self.len() == other.len()
            && self.keys().all(|x| {
                other
                    .get(x)
                    .map(|v| v.should_eq::<Epsilon>(&self[x]))
                    .unwrap_or(false)
            })
    }
}

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

impl<T> ShouldaEqual for Range<T>
where
    T: Debug,
    T: ShouldaEqual,
{
    fn should_eq<Epsilon: EpsilonProvider>(&self, other: &Self) -> bool {
        self.start.should_eq::<Epsilon>(&other.start) && self.end.should_eq::<Epsilon>(&other.end)
    }
}

impl<T> ShouldaEqual for RangeInclusive<T>
where
    T: Debug,
    T: ShouldaEqual,
{
    fn should_eq<Epsilon: EpsilonProvider>(&self, other: &Self) -> bool {
        self.start().should_eq::<Epsilon>(other.start())
            && self.end().should_eq::<Epsilon>(other.end())
    }
}