Skip to main content

assertr/assertions/core/
array.rs

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