assertr/assertions/core/
array.rs

1use core::fmt::Debug;
2
3use crate::{AssertThat, Mode, prelude::SliceAssertions};
4
5pub trait ArrayAssertions<T: Debug> {
6    fn contains_exactly<E: AsRef<[T]>>(self, expected: E) -> Self
7    where
8        T: PartialEq;
9
10    fn contains_exactly_in_any_order<E: AsRef<[T]>>(self, expected: E) -> Self
11    where
12        T: PartialEq;
13}
14
15/// Assertions for generic arrays.
16impl<T: Debug, const N: usize, M: Mode> ArrayAssertions<T> for AssertThat<'_, [T; N], M> {
17    #[track_caller]
18    fn contains_exactly<E: AsRef<[T]>>(self, expected: E) -> Self
19    where
20        T: PartialEq,
21    {
22        self.derive(|actual| actual.as_slice())
23            .contains_exactly(expected);
24        self
25    }
26
27    #[track_caller]
28    fn contains_exactly_in_any_order<E: AsRef<[T]>>(self, expected: E) -> Self
29    where
30        T: PartialEq,
31    {
32        self.derive(|actual| actual.as_slice())
33            .contains_exactly_in_any_order(expected);
34        self
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use indoc::formatdoc;
41
42    use crate::prelude::*;
43
44    #[test]
45    fn contains_exactly_succeeds_when_exact_match() {
46        assert_that([1, 2, 3]).contains_exactly([1, 2, 3]);
47    }
48
49    #[test]
50    fn contains_exactly_succeeds_when_exact_match_provided_as_slice() {
51        assert_that([1, 2, 3]).contains_exactly(&[1, 2, 3]);
52    }
53
54    #[test]
55    fn contains_exactly_panics_when_not_exact_match() {
56        assert_that_panic_by(|| {
57            assert_that([1, 2, 3])
58                .with_location(false)
59                .contains_exactly([3, 4, 1])
60        })
61        .has_type::<String>()
62        .is_equal_to(formatdoc! {r#"
63                -------- assertr --------
64                Actual: [
65                    1,
66                    2,
67                    3,
68                ],
69
70                did not exactly match
71
72                Expected: [
73                    3,
74                    4,
75                    1,
76                ]
77
78                Details: [
79                    Elements not expected: [
80                        2,
81                    ],
82                    Elements not found: [
83                        4,
84                    ],
85                ]
86                -------- assertr --------
87            "#});
88    }
89
90    #[test]
91    fn contains_exactly_panics_with_detail_message_when_only_differing_in_order() {
92        assert_that_panic_by(|| {
93            assert_that([1, 2, 3])
94                .with_location(false)
95                .contains_exactly([3, 2, 1])
96        })
97        .has_type::<String>()
98        .is_equal_to(formatdoc! {r#"
99                -------- assertr --------
100                Actual: [
101                    1,
102                    2,
103                    3,
104                ],
105
106                did not exactly match
107
108                Expected: [
109                    3,
110                    2,
111                    1,
112                ]
113
114                Details: [
115                    The order of elements does not match!,
116                ]
117                -------- assertr --------
118            "#});
119    }
120
121    #[test]
122    fn contains_exactly_in_any_order_succeeds_when_slices_match() {
123        assert_that([1, 2, 3]).contains_exactly_in_any_order([2, 3, 1]);
124    }
125
126    #[test]
127    fn contains_exactly_in_any_order_panics_when_slice_contains_unknown_data() {
128        assert_that_panic_by(|| {
129            assert_that([1, 2, 3])
130                .with_location(false)
131                .contains_exactly_in_any_order([2, 3, 4])
132        })
133        .has_type::<String>()
134        .is_equal_to(formatdoc! {"
135                -------- assertr --------
136                Actual: [
137                    1,
138                    2,
139                    3,
140                ],
141
142                Elements expected: [
143                    2,
144                    3,
145                    4,
146                ]
147
148                Elements not found: [
149                    4,
150                ]
151
152                Elements not expected: [
153                    1,
154                ]
155                -------- assertr --------
156            "});
157    }
158}