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
use crate::{CheckResult, Expectation, ExpectationBuilder};
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 value equal to another value
/// ```
/// # 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 in the same order as `b`
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 {
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 mut remaining: Vec<&C> = self.0.iter().collect();
let mut extras: Vec<&C> = Vec::new();
for actual in value.into_iter() {
if let Some(pos) = remaining.iter().position(|e| (*e).eq(actual)) {
// Remove matched item; swap_remove is O(1)
remaining.swap_remove(pos);
} else {
// No match found for this actual item; record as extra and continue
extras.push(actual);
}
}
if remaining.is_empty() && extras.is_empty() {
CheckResult::Pass
} else {
CheckResult::Fail(format!(
"Expectation failed (a ≅ b, any order)\na: `{:?}`\nb: `{:?}`\nextra: `{:?}`\nunmatched: `{:?}`",
value, self.0, extras, remaining
))
}
}
}
#[cfg(test)]
mod tests {
use super::IterableItemEqualityExpectations;
use crate::expect;
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);
}
}