1use std::borrow::Borrow;
16use std::fmt::Debug;
17
18use crate::assertions::iterator::{
19 check_has_length, check_is_empty, check_is_not_empty, IteratorAssertion,
20};
21use crate::base::{AssertionApi, AssertionResult, AssertionStrategy, Subject};
22
23pub trait VecAssertion<'a, S, T, R>
46where
47 AssertionResult: AssertionStrategy<R>,
48 Self: Sized,
49{
50 #[track_caller]
58 fn contains<B>(&self, element: B) -> R
59 where
60 B: Borrow<T>,
61 T: PartialEq + Debug;
62
63 #[track_caller]
71 fn does_not_contain<B>(&self, element: B) -> R
72 where
73 B: Borrow<T>,
74 T: PartialEq + Debug;
75
76 #[track_caller]
93 fn contains_exactly<B: Borrow<Vec<T>>>(self, expected_vec: B) -> R
94 where
95 T: PartialEq + Debug;
96
97 #[track_caller]
110 fn contains_exactly_in_order<B: Borrow<Vec<T>>>(self, expected_vec: B) -> R
111 where
112 T: PartialEq + Debug;
113
114 #[track_caller]
126 fn does_not_contain_any<B: Borrow<Vec<T>>>(&self, elements: B) -> R
127 where
128 T: PartialEq + Debug;
129
130 #[track_caller]
138 fn is_empty(&self) -> R
139 where
140 T: Debug;
141
142 #[track_caller]
150 fn is_not_empty(&self) -> R
151 where
152 T: Debug;
153
154 #[track_caller]
162 fn has_length(&self, length: usize) -> R;
163}
164
165impl<'a, T, R> VecAssertion<'a, Vec<T>, T, R> for Subject<'a, Vec<T>, (), R>
166where
167 AssertionResult: AssertionStrategy<R>,
168{
169 fn contains<B>(&self, element: B) -> R
170 where
171 B: Borrow<T>,
172 T: PartialEq + Debug,
173 {
174 self.new_subject(&self.actual().iter(), None, ())
175 .contains(element.borrow())
176 }
177
178 fn does_not_contain<B>(&self, element: B) -> R
179 where
180 B: Borrow<T>,
181 T: PartialEq + Debug,
182 {
183 self.new_owned_subject(self.actual().iter(), None, ())
184 .does_not_contain(element.borrow())
185 }
186
187 fn contains_exactly<B: Borrow<Vec<T>>>(self, expected_iter: B) -> R
188 where
189 T: PartialEq + Debug,
190 {
191 self.new_owned_subject(self.actual().iter(), None, ())
192 .contains_exactly(expected_iter.borrow().iter())
193 }
194
195 fn contains_exactly_in_order<B: Borrow<Vec<T>>>(self, expected_iter: B) -> R
196 where
197 T: PartialEq + Debug,
198 {
199 self.new_owned_subject(self.actual().iter(), None, ())
200 .contains_exactly_in_order(expected_iter.borrow().iter())
201 }
202
203 fn does_not_contain_any<B: Borrow<Vec<T>>>(&self, elements: B) -> R
204 where
205 T: PartialEq + Debug,
206 {
207 self.new_owned_subject(self.actual().iter(), None, ())
208 .does_not_contain_any(elements.borrow().iter())
209 }
210
211 fn is_empty(&self) -> R
212 where
213 T: Debug,
214 {
215 check_is_empty(self.new_result(), self.actual().iter())
216 }
217
218 fn is_not_empty(&self) -> R
219 where
220 T: Debug,
221 {
222 check_is_not_empty(self.new_result(), self.actual().iter())
223 }
224
225 fn has_length(&self, length: usize) -> R {
226 check_has_length(self.new_result(), self.actual().iter(), self.expr(), length)
227 }
228}
229
230#[cfg(test)]
231mod tests {
232 use crate::testing::*;
233
234 use super::*;
235
236 #[test]
237 fn contains() {
238 assert_that!(vec![1, 2, 3]).contains(&3);
239
240 assert_that!(check_that!(vec![1, 2, 3]).contains(&10)).facts_are(vec![
242 Fact::new("expected to contain", "10"),
243 Fact::new_simple_fact("but did not"),
244 Fact::new_multi_value_fact("though it did contain", vec!["1", "2", "3"]),
245 ]);
246 }
247
248 #[test]
249 fn contains_exactly() {
250 assert_that!(vec![1, 2, 3]).contains_exactly(vec![1, 2, 3]);
251 assert_that!(vec![2, 1, 3]).contains_exactly(vec![1, 2, 3]);
252 }
253
254 #[test]
255 fn contains_exactly_in_order() {
256 assert_that!(vec![1, 2, 3]).contains_exactly_in_order(vec![1, 2, 3]);
257 assert_that!(check_that!(vec![2, 1, 3]).contains_exactly_in_order(vec![1, 2, 3])).facts_are(
258 vec![
259 Fact::new_simple_fact("contents match, but order was wrong"),
260 Fact::new_splitter(),
261 Fact::new_multi_value_fact("expected", vec!["1", "2", "3"]),
262 Fact::new_multi_value_fact("actual", vec!["2", "1", "3"]),
263 ],
264 )
265 }
266
267 #[test]
268 fn is_empty() {
269 assert_that!(Vec::<usize>::new()).is_empty();
270
271 assert_that!(check_that!(vec![1]).is_empty()).facts_are(vec![
273 Fact::new_simple_fact("expected to be empty"),
274 Fact::new_splitter(),
275 Fact::new_multi_value_fact("actual", vec!["1"]),
276 ])
277 }
278
279 #[test]
280 fn is_not_empty() {
281 assert_that!(Vec::<usize>::from([1])).is_not_empty();
282
283 assert_that!(check_that!(Vec::<usize>::new()).is_not_empty()).facts_are(vec![
285 Fact::new_simple_fact("expected to be non-empty"),
286 Fact::new_splitter(),
287 Fact::new("actual", "[]"),
288 ])
289 }
290
291 #[test]
292 fn has_size() {
293 assert_that!(vec![1, 2, 3]).has_length(3);
294 assert_that!(Vec::<usize>::new()).has_length(0);
295
296 assert_that!(check_that!(Vec::<usize>::new()).has_length(3)).facts_are(vec![
298 Fact::new("value of", "Vec::<usize>::new().size()"),
299 Fact::new("expected", "3"),
300 Fact::new("actual", "0"),
301 ]);
302 }
303}