1 | use itertools::{assert_equal, Itertools}; |
2 | use std::{ops::Range, vec::IntoIter}; |
3 | |
4 | fn mix_data() -> IntoIter<Result<Range<i32>, bool>> { |
5 | vec![Ok(0..2), Err(false), Ok(2..4), Err(true), Ok(4..6)].into_iter() |
6 | } |
7 | |
8 | fn ok_data() -> IntoIter<Result<Range<i32>, bool>> { |
9 | vec![Ok(0..2), Ok(2..4), Ok(4..6)].into_iter() |
10 | } |
11 | |
12 | #[test] |
13 | fn flatten_ok_mixed_expected_forward() { |
14 | assert_equal( |
15 | mix_data().flatten_ok(), |
16 | vec![ |
17 | Ok(0), |
18 | Ok(1), |
19 | Err(false), |
20 | Ok(2), |
21 | Ok(3), |
22 | Err(true), |
23 | Ok(4), |
24 | Ok(5), |
25 | ], |
26 | ); |
27 | } |
28 | |
29 | #[test] |
30 | fn flatten_ok_mixed_expected_reverse() { |
31 | assert_equal( |
32 | mix_data().flatten_ok().rev(), |
33 | vec![ |
34 | Ok(5), |
35 | Ok(4), |
36 | Err(true), |
37 | Ok(3), |
38 | Ok(2), |
39 | Err(false), |
40 | Ok(1), |
41 | Ok(0), |
42 | ], |
43 | ); |
44 | } |
45 | |
46 | #[test] |
47 | fn flatten_ok_collect_mixed_forward() { |
48 | assert_eq!( |
49 | mix_data().flatten_ok().collect::<Result<Vec<_>, _>>(), |
50 | Err(false) |
51 | ); |
52 | } |
53 | |
54 | #[test] |
55 | fn flatten_ok_collect_mixed_reverse() { |
56 | assert_eq!( |
57 | mix_data().flatten_ok().rev().collect::<Result<Vec<_>, _>>(), |
58 | Err(true) |
59 | ); |
60 | } |
61 | |
62 | #[test] |
63 | fn flatten_ok_collect_ok_forward() { |
64 | assert_eq!( |
65 | ok_data().flatten_ok().collect::<Result<Vec<_>, _>>(), |
66 | Ok((0..6).collect()) |
67 | ); |
68 | } |
69 | |
70 | #[test] |
71 | fn flatten_ok_collect_ok_reverse() { |
72 | assert_eq!( |
73 | ok_data().flatten_ok().rev().collect::<Result<Vec<_>, _>>(), |
74 | Ok((0..6).rev().collect()) |
75 | ); |
76 | } |
77 | |