assertables/assert_iter/assert_iter_eq.rs
1//! Assert an iter is equal to another.
2//!
3//! Pseudocode:<br>
4//! (collection1 into iter) = (collection2 into iter)
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a = [1, 2];
12//! let b = [1, 2];
13//! assert_iter_eq!(&a, &b);
14//! ```
15//!
16//! This implementation uses [`::std::iter::Iterator`](https://doc.rust-lang.org/std/iter/trait.Iterator.html).
17//!
18//! # Module macros
19//!
20//! * [`assert_iter_eq`](macro@crate::assert_iter_eq)
21//! * [`assert_iter_eq_as_result`](macro@crate::assert_iter_eq_as_result)
22//! * [`debug_assert_iter_eq`](macro@crate::debug_assert_iter_eq)
23
24/// Assert an iterable is equal to another.
25///
26/// Pseudocode:<br>
27/// (collection1 into iter) = (collection2 into iter)
28///
29/// * If true, return Result `Ok(())`.
30///
31/// * Otherwise, return Result `Err(message)`.
32///
33/// This macro is useful for runtime checks, such as checking parameters,
34/// or sanitizing inputs, or handling different results in different ways.
35///
36/// This implementation uses [`::std::iter::Iterator`](https://doc.rust-lang.org/std/iter/trait.Iterator.html).
37///
38/// # Module macros
39///
40/// * [`assert_iter_eq`](macro@crate::assert_iter_eq)
41/// * [`assert_iter_eq_as_result`](macro@crate::assert_iter_eq_as_result)
42/// * [`debug_assert_iter_eq`](macro@crate::debug_assert_iter_eq)
43///
44#[macro_export]
45macro_rules! assert_iter_eq_as_result {
46 ($a_collection:expr, $b_collection:expr $(,)?) => {{
47 match (&$a_collection, &$b_collection) {
48 (a_collection, b_collection) => {
49 let a = a_collection.into_iter();
50 let b = b_collection.into_iter();
51 if a.eq(b) {
52 Ok(())
53 } else {
54 Err(
55 format!(
56 concat!(
57 "assertion failed: `assert_iter_eq!(a_collection, b_collection)`\n",
58 "https://docs.rs/assertables/9.5.4/assertables/macro.assert_iter_eq.html\n",
59 " a label: `{}`,\n",
60 " a debug: `{:?}`,\n",
61 " b label: `{}`,\n",
62 " b debug: `{:?}`"
63 ),
64 stringify!($a_collection),
65 a_collection,
66 stringify!($b_collection),
67 b_collection
68 )
69 )
70 }
71 }
72 }
73 }};
74}
75
76#[cfg(test)]
77mod test_assert_iter_eq_as_result {
78
79 #[test]
80 fn success() {
81 let a = [1, 2];
82 let b = [1, 2];
83 let actual = assert_iter_eq_as_result!(&a, &b);
84 assert_eq!(actual.unwrap(), ());
85 }
86
87 #[test]
88 fn failure() {
89 let a = [1, 2];
90 let b = [2, 1];
91 let actual = assert_iter_eq_as_result!(&a, &b);
92 let message = concat!(
93 "assertion failed: `assert_iter_eq!(a_collection, b_collection)`\n",
94 "https://docs.rs/assertables/9.5.4/assertables/macro.assert_iter_eq.html\n",
95 " a label: `&a`,\n",
96 " a debug: `[1, 2]`,\n",
97 " b label: `&b`,\n",
98 " b debug: `[2, 1]`"
99 );
100 assert_eq!(actual.unwrap_err(), message);
101 }
102}
103
104/// Assert an iterable is equal to another.
105///
106/// Pseudocode:<br>
107/// (collection1 into iter) = (collection2 into iter)
108///
109/// * If true, return `()`.
110///
111/// * Otherwise, call [`panic!`] with a message and the values of the
112/// expressions with their debug representations.
113///
114/// # Examples
115///
116/// ```rust
117/// use assertables::*;
118/// # use std::panic;
119///
120/// # fn main() {
121/// let a = [1, 2];
122/// let b = [1, 2];
123/// assert_iter_eq!(&a, &b);
124///
125/// # let result = panic::catch_unwind(|| {
126/// // This will panic
127/// let a = [1, 2];
128/// let b = [2, 1];
129/// assert_iter_eq!(&a, &b);
130/// # });
131/// // assertion failed: `assert_iter_eq!(a_collection, b_collection)`
132/// // https://docs.rs/assertables/9.5.4/assertables/macro.assert_iter_eq.html
133/// // a label: `&a`,
134/// // a debug: `[1, 2]`,
135/// // b label: `&b`,
136/// // b debug: `[2, 1]`
137/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
138/// # let message = concat!(
139/// # "assertion failed: `assert_iter_eq!(a_collection, b_collection)`\n",
140/// # "https://docs.rs/assertables/9.5.4/assertables/macro.assert_iter_eq.html\n",
141/// # " a label: `&a`,\n",
142/// # " a debug: `[1, 2]`,\n",
143/// # " b label: `&b`,\n",
144/// # " b debug: `[2, 1]`",
145/// # );
146/// # assert_eq!(actual, message);
147/// # }
148/// ```
149///
150/// This implementation uses [`::std::iter::Iterator`](https://doc.rust-lang.org/std/iter/trait.Iterator.html).
151///
152/// # Module macros
153///
154/// * [`assert_iter_eq`](macro@crate::assert_iter_eq)
155/// * [`assert_iter_eq_as_result`](macro@crate::assert_iter_eq_as_result)
156/// * [`debug_assert_iter_eq`](macro@crate::debug_assert_iter_eq)
157///
158#[macro_export]
159macro_rules! assert_iter_eq {
160 ($a_collection:expr, $b_collection:expr $(,)?) => {{
161 match $crate::assert_iter_eq_as_result!($a_collection, $b_collection) {
162 Ok(()) => (),
163 Err(err) => panic!("{}", err),
164 }
165 }};
166 ($a_collection:expr, $b_collection:expr, $($message:tt)+) => {{
167 match $crate::assert_iter_eq_as_result!($a_collection, $b_collection) {
168 Ok(()) => (),
169 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
170 }
171 }};
172}
173
174#[cfg(test)]
175mod test_assert_iter_eq {
176 use std::panic;
177
178 #[test]
179 fn success() {
180 let a = [1, 2];
181 let b = [1, 2];
182 let actual = assert_iter_eq!(&a, &b);
183 assert_eq!(actual, ());
184 }
185
186 #[test]
187 fn failure() {
188 let a = [1, 2];
189 let b = [2, 1];
190 let result = panic::catch_unwind(|| {
191 let _actual = assert_iter_eq!(&a, &b);
192 });
193 let message = concat!(
194 "assertion failed: `assert_iter_eq!(a_collection, b_collection)`\n",
195 "https://docs.rs/assertables/9.5.4/assertables/macro.assert_iter_eq.html\n",
196 " a label: `&a`,\n",
197 " a debug: `[1, 2]`,\n",
198 " b label: `&b`,\n",
199 " b debug: `[2, 1]`"
200 );
201 assert_eq!(
202 result
203 .unwrap_err()
204 .downcast::<String>()
205 .unwrap()
206 .to_string(),
207 message
208 );
209 }
210}
211
212/// Assert an iterable is equal to another.
213///
214/// Pseudocode:<br>
215/// (collection1 into iter) = (collection2 into iter)
216///
217/// This macro provides the same statements as [`assert_iter_eq`](macro.assert_iter_eq.html),
218/// except this macro's statements are only enabled in non-optimized
219/// builds by default. An optimized build will not execute this macro's
220/// statements unless `-C debug-assertions` is passed to the compiler.
221///
222/// This macro is useful for checks that are too expensive to be present
223/// in a release build but may be helpful during development.
224///
225/// The result of expanding this macro is always type checked.
226///
227/// An unchecked assertion allows a program in an inconsistent state to
228/// keep running, which might have unexpected consequences but does not
229/// introduce unsafety as long as this only happens in safe code. The
230/// performance cost of assertions, however, is not measurable in general.
231/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
232/// after thorough profiling, and more importantly, only in safe code!
233///
234/// This macro is intended to work in a similar way to
235/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
236///
237/// # Module macros
238///
239/// * [`assert_iter_eq`](macro@crate::assert_iter_eq)
240/// * [`assert_iter_eq`](macro@crate::assert_iter_eq)
241/// * [`debug_assert_iter_eq`](macro@crate::debug_assert_iter_eq)
242///
243#[macro_export]
244macro_rules! debug_assert_iter_eq {
245 ($($arg:tt)*) => {
246 if $crate::cfg!(debug_assertions) {
247 $crate::assert_iter_eq!($($arg)*);
248 }
249 };
250}