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
use super::*;

//TODO: check if we need to use mutable, with a test with mutable variable

pub trait IteratorAssertions<T>
where
    T: Debug + PartialEq,
{
    /// Checks if the iterator contains the specified element
    fn contains(&self, expected_item: T);
    /// Checks if the iterator contains the specified elements
    fn contains_all(&self, expected_items: &[T]);
    /// Checks if the iterator has the specified count
    fn has_count(&self, expected_count: usize);
    /// Checks if the iterator does not contain any of the specified elements
    fn does_not_contain_any(&self, not_expected_items: &[T]);
    /// Checks if the iterator is empty
    fn is_empty(&self);
    /// Checks if the iterator is not empty
    fn is_not_empty(&self);
}

pub trait IteratorSatisfiesAssertion<T> {
    /// Checks if the elements of the iterators satisfies the specified assertions
    ///
    /// # Examples
    ///
    /// ```rust
    /// #[derive(Clone)]
    /// struct Person {
    ///     name: String,
    ///     age: i32,
    /// }
    ///
    /// #[test]
    /// fn iterator_assertion_for_struct() {
    ///     let people: Vec<Person> = vec![
    ///         Person {
    ///             name: String::from("Daniel"),
    ///             age: 32,
    ///         },
    ///         Person {
    ///             name: String::from("Jimmy"),
    ///             age: 45,
    ///         },
    ///     ];
    ///
    ///     assert_that!(people).satisfies_respectively(with_asserters!(
    ///             |person1: &Person| {
    ///                 assert_that!(&person1.name).is_equal_to(&String::from("Daniel"));
    ///                 assert_that!(&person1.age).is_equal_to(&32);
    ///             },
    ///             |person2: &Person| {
    ///                 assert_that!(&person2.name).is_equal_to(&String::from("Jimmy"));
    ///                 assert_that!(&person2.age).is_equal_to(&45);
    ///             }
    ///         ));
    /// }
    /// ```
    ///
    fn satisfies_respectively(&self, asserter: Vec<Box<dyn Fn(&T)>>);
}

impl<T, K> IteratorAssertions<T> for Asserter<K>
where
    T: Debug + PartialEq,
    K: IntoIterator<Item = T> + Clone,
{
    fn contains(&self, expected_value: T) {
        let contains = &self.value.clone().into_iter().any(|i| i == expected_value);
        if !contains {
            panic!(
                "Expected iterator {:?} to contain {:?}, but it does not.",
                self.name, expected_value
            );
        }
    }

    fn contains_all(&self, expected_values: &[T]) {
        let mut missing_items = std::vec::Vec::<&T>::new();
        for expected_value in expected_values {
            let contains = contains(&self.value, expected_value);
            if !contains {
                missing_items.push(expected_value);
            }
        }

        if !missing_items.is_empty() {
            panic!(
                "Expected iterator {:?} to contain items {:?}, but it does not contain {:?}.",
                self.name, expected_values, missing_items
            );
        }
    }

    fn has_count(&self, expected_count: usize) {
        let count = &self.value.clone().into_iter().count();
        if *count != expected_count {
            panic!(
                "Expected iterator {:?} to have count '{}', but it has '{}'.",
                &self.name, expected_count, count
            );
        }
    }

    fn does_not_contain_any(&self, not_expected_values: &[T]) {
        let mut missing_items = std::vec::Vec::<&T>::new();

        for not_expected_value in not_expected_values {
            let contains = contains(&self.value, not_expected_value);
            if contains {
                missing_items.push(not_expected_value);
            }
        }

        if !missing_items.is_empty() {
            panic!(
                "Expected iterator {:?} to not contain items {:?}, but it contains {:?}.",
                self.name, not_expected_values, missing_items
            );
        }
    }

    fn is_empty(&self) {
        let is_empty = is_empty(&self.value);

        if !is_empty {
            panic!(
                "Expected iterator {:?} to be empty, but it is not.",
                self.name
            );
        }
    }

    fn is_not_empty(&self) {
        let is_empty = is_empty(&self.value);

        if is_empty {
            panic!(
                "Expected iterator {:?} to be not empty, but it is.",
                self.name
            );
        }
    }
}

fn is_empty<T, K>(iterator: &K) -> bool
where
    K: Clone + IntoIterator<Item = T>,
    T: Debug + PartialEq,
{
    iterator.clone().into_iter().next().is_none()
}

fn contains<T, K>(iterator: &K, expected_value: &T) -> bool
where
    K: Clone + IntoIterator<Item = T>,
    T: Debug + PartialEq,
{
    iterator.clone().into_iter().any(|i| i == *expected_value)
}

impl<T, K> IteratorSatisfiesAssertion<T> for Asserter<K>
where
    K: IntoIterator<Item = T> + Clone,
{
    //TODO: remove Clone somehow
    fn satisfies_respectively(&self, asserter: Vec<Box<dyn Fn(&T)>>) {
        //TODO: S - rename to asserters
        let iter = &self.value.clone().into_iter().collect::<Vec<T>>();

        if iter.len() != asserter.len() {
            panic!(
                "Expected number of items to be {}, but was {}.",
                asserter.len(),
                iter.len()
            )
        }

        for i in 0..asserter.len() {
            asserter[i](&iter[i])
        }
    }
}
///Helper macro to be used to specify assertions [`Receiver<T>`]
#[macro_export]
macro_rules! with_asserters {
    ($($closure:expr),*)  => {
        vec![
            $( Box::new($closure) as Box<dyn for<'a> Fn(&'a _) -> _> ),*
        ]
    };
}