rxpect 0.11.0

Extensible fluent expectations for Rust
Documentation
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
#[cfg(feature = "diff")]
use crate::diff::{Color, diff_pretty_debug, format_flagged_list};
use crate::{CheckResult, Expectation, ExpectationBuilder};
#[cfg(feature = "diff")]
use colored::Colorize;
use itertools::EitherOrBoth::Both;
use itertools::Itertools;
use std::fmt::Debug;

/// Extension trait for equality expectations for iterables
pub trait IterableItemEqualityExpectations<'e, B, C>
where
    B: ExpectationBuilder<'e>,
    C: PartialEq + Debug + 'e,
{
    /// Expect an iterable to contain at least one value equal to another value
    /// ```
    /// # use rxpect::expect;
    /// # use rxpect::expectations::iterables::IterableItemEqualityExpectations;
    ///
    /// let haystack = vec!["bar", "foo", "foo"];
    /// let needle = "foo";
    /// expect(haystack).to_contain_equal_to(needle);
    /// ```
    /// asserts that `haystack` contains at least one item equal to `needle`
    fn to_contain_equal_to(self, value: C) -> Self;

    /// Expect an iterable to contain at least one of each of the expected values
    /// ```
    /// # use rxpect::expect;
    /// # use rxpect::expectations::iterables::IterableItemEqualityExpectations;
    ///
    /// let haystack = vec!["apple", "orange", "pear", "apple", "peach"];
    /// let needles = ["orange", "apple"];
    /// expect(haystack).to_contain_equal_to_all_of(needles);
    /// ```
    /// asserts that `haystack` contains at least one item equal to each item in `needles`
    fn to_contain_equal_to_all_of(self, values: impl IntoIterator<Item = C>) -> Self;

    /// Expect an iterable to be equivalent to another iterable
    /// ```
    /// # use rxpect::expect;
    /// # use rxpect::expectations::iterables::IterableItemEqualityExpectations;
    ///
    /// let a = vec!["apple", "orange", "pear", "apple", "peach"];
    /// let b = ["apple", "orange", "pear", "apple", "peach"];
    /// expect(a).to_be_equivalent_to(b);
    /// ```
    /// asserts that `a` contains exactly the same items in the same order as `b`
    fn to_be_equivalent_to(self, values: impl IntoIterator<Item = C>) -> Self;

    /// Expect an iterable to be equivalent to another iterable, ignoring the order of items
    /// ```
    /// # use rxpect::expect;
    /// # use rxpect::expectations::iterables::IterableItemEqualityExpectations;
    ///
    /// let a = vec!["apple", "orange", "pear", "apple", "peach"];
    /// let b = ["orange", "peach", "apple", "apple", "pear"];
    /// let c = ["peach", "apple", "pear", "orange", "apple"];
    /// expect(a.clone()).to_be_equivalent_to_in_any_order(b);
    /// expect(a).to_be_equivalent_to_in_any_order(c);
    /// expect(b).to_be_equivalent_to_in_any_order(c);
    /// ```
    /// asserts that `a` contains exactly the same items as `b`, in any order
    fn to_be_equivalent_to_in_any_order(self, values: impl IntoIterator<Item = C>) -> Self;
}

impl<'e, I, C, B> IterableItemEqualityExpectations<'e, B, C> for B
where
    I: Debug,
    for<'a> &'a I: IntoIterator<Item = &'a C>,
    C: PartialEq + Debug + 'e,
    B: ExpectationBuilder<'e, Value = I>,
{
    fn to_contain_equal_to(self, value: C) -> Self {
        self.to_pass(ContainsEqualToExpectation(vec![value]))
    }

    fn to_contain_equal_to_all_of(self, values: impl IntoIterator<Item = C>) -> Self {
        self.to_pass(ContainsEqualToExpectation(values.into_iter().collect()))
    }

    fn to_be_equivalent_to(self, values: impl IntoIterator<Item = C>) -> Self {
        self.to_pass(IterableIsEquivalentToExpectation(
            values.into_iter().collect(),
        ))
    }

    fn to_be_equivalent_to_in_any_order(self, values: impl IntoIterator<Item = C>) -> Self {
        self.to_pass(IterableIsEquivalentToInAnyOrderExpectation(
            values.into_iter().collect(),
        ))
    }
}

struct ContainsEqualToExpectation<T>(Vec<T>);

struct IterableIsEquivalentToExpectation<T>(Vec<T>);

struct IterableIsEquivalentToInAnyOrderExpectation<T>(Vec<T>);

impl<I, C> Expectation<I> for ContainsEqualToExpectation<C>
where
    I: Debug,
    for<'a> &'a I: IntoIterator<Item = &'a C>,
    C: PartialEq + Debug,
{
    fn check(&self, value: &I) -> CheckResult {
        if self
            .0
            .iter()
            .all(|needle| value.into_iter().any(|candidate| candidate.eq(needle)))
        {
            CheckResult::Pass
        } else {
            CheckResult::Fail(format!(
                "Expectation failed (a ⊇ b)\na: `{:?}`\nb: `{:?}`",
                value, self.0
            ))
        }
    }
}

impl<I, C> Expectation<I> for IterableIsEquivalentToExpectation<C>
where
    I: Debug,
    for<'a> &'a I: IntoIterator<Item = &'a C>,
    C: PartialEq + Debug,
{
    fn check(&self, value: &I) -> CheckResult {
        if self.0.iter().zip_longest(value).all(|pair| match pair {
            Both(a, b) => a.eq(b),
            _ => false,
        }) {
            CheckResult::Pass
        } else {
            #[cfg(feature = "diff")]
            {
                let diff = diff_pretty_debug(&self.0, value);
                CheckResult::Fail(format!(
                    "Expectation failed ({} == {})\n{diff}",
                    "expected".on_ansi_color(Color::RemovedRow),
                    "actual".on_ansi_color(Color::AddedRow)
                ))
            }
            #[cfg(not(feature = "diff"))]
            {
                CheckResult::Fail(format!(
                    "Expectation failed (a == b)\na: `{:?}`\nb: `{:?}`",
                    value, self.0
                ))
            }
        }
    }
}

impl<I, C> Expectation<I> for IterableIsEquivalentToInAnyOrderExpectation<C>
where
    I: Debug,
    for<'a> &'a I: IntoIterator<Item = &'a C>,
    C: PartialEq + Debug,
{
    fn check(&self, value: &I) -> CheckResult {
        let value = value.into_iter().collect_vec();
        let mut remaining: Vec<&C> = self.0.iter().collect();
        let mut extras: Vec<&C> = Vec::new();
        for actual in value.iter() {
            if let Some(pos) = remaining.iter().position(|e| (*e).eq(actual)) {
                // Remove matched item to keep track of remaining ones
                remaining.remove(pos);
            } else {
                // No match found for this item; record as extra and continue
                extras.push(actual);
            }
        }
        if remaining.is_empty() && extras.is_empty() {
            CheckResult::Pass
        } else {
            #[cfg(feature = "diff")]
            {
                if remaining.len() == 1 && extras.len() == 1 {
                    // Special case when there is only one element differing
                    let remaining = remaining[0];
                    let extra = extras[0];
                    let diff = diff_pretty_debug(remaining, extra);
                    CheckResult::Fail(format!(
                        "Expectation failed ({} == {}, any order)\nSingle differing element\n{diff}",
                        "expected".on_ansi_color(Color::RemovedRow),
                        "actual".on_ansi_color(Color::AddedRow)
                    ))
                } else {
                    CheckResult::Fail(format!(
                        "Expectation failed ({} == {}, any order)\nexpected: {}\nactual: {}",
                        "expected".on_ansi_color(Color::RemovedRow),
                        "actual".on_ansi_color(Color::AddedRow),
                        format_flagged_list(
                            &self.0.iter().collect_vec(),
                            &remaining,
                            '-',
                            Color::RemovedRow
                        ),
                        format_flagged_list(&value, &extras, '+', Color::AddedRow)
                    ))
                }
            }
            #[cfg(not(feature = "diff"))]
            {
                CheckResult::Fail(format!(
                    "Expectation failed (expected == actual, any order)\nactual: {}\nexpected: {}\nextra: {}\nunmatched: {}",
                    format!("{:#?}", value),
                    format!("{:#?}", self.0),
                    format!("{:#?}", extras),
                    format!("{:#?}", remaining)
                ))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::IterableItemEqualityExpectations;
    #[cfg(feature = "diff")]
    use super::{IterableIsEquivalentToExpectation, IterableIsEquivalentToInAnyOrderExpectation};
    #[cfg(feature = "diff")]
    use crate::diff::{Color, diff_pretty_debug, format_flagged_list};
    use crate::expect;
    #[cfg(feature = "diff")]
    use crate::expectations::EqualityExpectations;
    #[cfg(feature = "diff")]
    use crate::{CheckResult, Expectation};
    #[cfg(feature = "diff")]
    use colored::Colorize;
    #[cfg(feature = "diff")]
    use dedent::dedent;
    #[cfg(feature = "diff")]
    use itertools::Itertools;
    use rstest::rstest;

    #[test]
    pub fn that_singleton_vec_contains_the_one_item() {
        // Given a vector with a single value that implements PartialEq
        let value = vec![1];

        // Expect the to_contain_equal_to expectation to pass with an identical value
        expect(value).to_contain_equal_to(1);
    }

    #[test]
    #[should_panic]
    pub fn that_empty_vec_does_not_contain_an_item() {
        // Given an empty vec
        let value: Vec<u32> = vec![];

        // Expect the to_contain_equal_to expectation to fail
        expect(value).to_contain_equal_to(1);
    }

    #[test]
    #[should_panic]
    pub fn that_unequal_values_are_not_considered_contained() {
        // Given a vec with a value that implements PartialEq
        let value = vec![1];

        // Expect the to_contain_equal_to expectation to fail with a different value
        expect(value).to_contain_equal_to(2);
    }

    #[test]
    pub fn that_empty_list_is_contained() {
        // Given a vec with a value that implements PartialEq
        let value = vec![1];

        // Expect the to_contain_equal_to_all_of expectation to pass with an empty list
        expect(value).to_contain_equal_to_all_of(Vec::<i32>::new());
    }

    #[test]
    pub fn that_order_of_items_is_insignificant_for_contains_all_of() {
        // Given a vector with multiple values
        let value = vec![1, 3, 5, 7, 8, 9];

        // Expect the to_contain_equal_to_all_of expectation to pass with values in different order
        expect(value).to_contain_equal_to_all_of([5, 1]);
    }

    #[rstest]
    #[case(vec![5, 1])]
    #[case(vec![1, 3, 5, 7, 8])]
    #[case(vec![3, 5, 7, 8, 9])]
    #[case(vec![1, 5, 3, 7, 8, 9])]
    #[case(vec![9, 8, 7, 5, 3, 1])]
    #[should_panic]
    pub fn that_nonequivalent_collections_are_not_considered_equal(
        #[case] non_equivalent: Vec<u32>,
    ) {
        // Given a vector with multiple values
        let value = vec![1, 3, 5, 7, 8, 9];

        // Expect the to_be_equivalent_to expectation to fail with an unequal collection
        expect(value).to_be_equivalent_to(non_equivalent);
    }

    #[rstest]
    #[case(vec![1, 1, 3, 5, 7, 8, 3, 9])]
    #[case(vec![1, 3, 1, 5, 7, 8, 3, 9])]
    #[case(vec![9, 3, 8, 7, 5, 3, 1, 1])]
    #[case(vec![7, 3, 1, 9, 1, 3, 8, 5])]
    pub fn that_equivalent_collections_are_considered_equivalent_regardless_of_order(
        #[case] non_equivalent: Vec<u32>,
    ) {
        // Given a vector with multiple values
        let value = vec![1, 1, 3, 5, 7, 8, 3, 9];

        // Expect the to_be_equivalent_to expectation to pass with an unequal collection
        expect(value).to_be_equivalent_to_in_any_order(non_equivalent);
    }

    #[rstest]
    #[case(vec![5, 1])]
    #[case(vec![1, 3, 5, 7, 8])]
    #[case(vec![3, 5, 7, 8, 9])]
    #[case(vec![1, 5, 3, 7, 8, 9])]
    #[case(vec![1, 3, 3, 5, 7, 8, 3, 9])]
    #[case(vec![1, 1, 1, 5, 7, 8, 3, 9])]
    #[case(vec![1, 1, 3, 7, 7, 8, 3, 9])]
    #[case(vec![1, 1, 3, 6, 7, 8, 3, 9])]
    #[should_panic]
    pub fn that_nonequivalent_collections_are_not_considered_equal_regardless_of_order(
        #[case] non_equivalent: Vec<u32>,
    ) {
        // Given a vector with multiple values
        let value = vec![1, 1, 3, 5, 7, 8, 3, 9];

        // Expect the to_be_equivalent_to expectation to fail with an unequal collection
        expect(value).to_be_equivalent_to_in_any_order(non_equivalent);
    }

    #[cfg(feature = "diff")]
    #[test]
    pub fn that_equivalent_to_with_diffing_returns_colored_diff() {
        // Given an actual collection and an expected collection that differ
        let actual = vec![1, 2, 3];
        let expected = vec![1, 4, 3];

        // When the equivalence expectation is checked
        let result = IterableIsEquivalentToExpectation(expected.clone()).check(&actual);

        // Then the failure message contains the colored diff of expected against actual
        let message = match result {
            CheckResult::Fail(message) => message,
            _ => "Passed".to_string(),
        };
        expect(message).to_equal(format!(
            "Expectation failed ({} == {})\n{}",
            "expected".on_ansi_color(Color::RemovedRow),
            "actual".on_ansi_color(Color::AddedRow),
            diff_pretty_debug(&expected, &actual)
        ));
    }

    #[cfg(feature = "diff")]
    #[test]
    pub fn that_equivalent_to_in_any_order_with_single_difference_returns_colored_diff() {
        // Given an actual collection and an expected collection that differ in a single element
        let actual = vec![1, 2, 3];
        let expected = vec![1, 4, 3];

        // When the any-order equivalence expectation is checked
        let result = IterableIsEquivalentToInAnyOrderExpectation(expected).check(&actual);

        // Then the failure message contains the colored diff of the single differing element
        let message = match result {
            CheckResult::Fail(message) => message,
            _ => "Passed".to_string(),
        };
        expect(message).to_equal(format!(
            "Expectation failed ({} == {}, any order)\nSingle differing element\n{}",
            "expected".on_ansi_color(Color::RemovedRow),
            "actual".on_ansi_color(Color::AddedRow),
            diff_pretty_debug(&4, &2)
        ));
    }

    #[cfg(feature = "diff")]
    #[test]
    pub fn that_equivalent_to_in_any_order_with_multiple_differences_returns_colored_set_difference()
     {
        // Given an actual collection and an expected collection that differ in multiple elements
        let actual = vec![1, 2, 9, 8];
        let expected = vec![1, 2, 3, 4];

        // When the any-order equivalence expectation is checked
        let result = IterableIsEquivalentToInAnyOrderExpectation(expected.clone()).check(&actual);

        // Then the failure message contains the colored set difference of missing and extra items
        let message = match result {
            CheckResult::Fail(message) => message,
            _ => "Passed".to_string(),
        };
        expect(message).to_equal(format!(
            dedent!(
                r#"
            Expectation failed ({} == {}, any order)
            expected: {}
            actual: {}
            "#
            ),
            "expected".on_ansi_color(Color::RemovedRow),
            "actual".on_ansi_color(Color::AddedRow),
            format_flagged_list(
                &expected.iter().collect_vec(),
                &[&expected[2], &expected[3]],
                '-',
                Color::RemovedRow
            ),
            format_flagged_list(
                &actual.iter().collect_vec(),
                &[&actual[2], &actual[3]],
                '+',
                Color::AddedRow
            )
        ));
    }
}