assertables/assert_iter/assert_iter_le.rs
1//! Assert an iter is less than or 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_le!(&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_le`](macro@crate::assert_iter_le)
21//! * [`assert_iter_le_as_result`](macro@crate::assert_iter_le_as_result)
22//! * [`debug_assert_iter_le`](macro@crate::debug_assert_iter_le)
23
24/// Assert an iterable is less than or 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_le`](macro@crate::assert_iter_le)
41/// * [`assert_iter_le_as_result`](macro@crate::assert_iter_le_as_result)
42/// * [`debug_assert_iter_le`](macro@crate::debug_assert_iter_le)
43///
44#[macro_export]
45macro_rules! assert_iter_le_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.le(b) {
52 Ok(())
53 } else {
54 Err(
55 format!(
56 concat!(
57 "assertion failed: `assert_iter_le!(a_collection, b_collection)`\n",
58 "https://docs.rs/assertables/9.5.3/assertables/macro.assert_iter_le.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_le_as_result {
78
79 #[test]
80 fn lt() {
81 let a = [1, 2];
82 let b = [3, 4];
83 let actual = assert_iter_le_as_result!(&a, &b);
84 assert_eq!(actual.unwrap(), ());
85 }
86
87 #[test]
88 fn eq() {
89 let a = [1, 2];
90 let b = [1, 2];
91 let actual = assert_iter_le_as_result!(&a, &b);
92 assert_eq!(actual.unwrap(), ());
93 }
94
95 #[test]
96 fn gt() {
97 let a = [3, 4];
98 let b = [1, 2];
99 let actual = assert_iter_le_as_result!(&a, &b);
100 let message = concat!(
101 "assertion failed: `assert_iter_le!(a_collection, b_collection)`\n",
102 "https://docs.rs/assertables/9.5.3/assertables/macro.assert_iter_le.html\n",
103 " a label: `&a`,\n",
104 " a debug: `[3, 4]`,\n",
105 " b label: `&b`,\n",
106 " b debug: `[1, 2]`"
107 );
108 assert_eq!(actual.unwrap_err(), message);
109 }
110}
111
112/// Assert an iterable is less than or equal to another.
113///
114/// Pseudocode:<br>
115/// (collection1 into iter) ≤ (collection2 into iter)
116///
117/// * If true, return `()`.
118///
119/// * Otherwise, call [`panic!`] with a message and the values of the
120/// expressions with their debug representations.
121///
122/// # Examples
123///
124/// ```rust
125/// use assertables::*;
126/// # use std::panic;
127///
128/// # fn main() {
129/// let a = [1, 2];
130/// let b = [3, 4];
131/// assert_iter_le!(&a, &b);
132///
133/// # let result = panic::catch_unwind(|| {
134/// // This will panic
135/// let a = [3, 4];
136/// let b = [1, 2];
137/// assert_iter_le!(&a, &b);
138/// # });
139/// // assertion failed: `assert_iter_le!(a_collection, b_collection)`
140/// // https://docs.rs/assertables/9.5.3/assertables/macro.assert_iter_le.html
141/// // a label: `&a`,
142/// // a debug: `[3, 4]`,
143/// // b label: `&b`,
144/// // b debug: `[1, 2]`
145/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
146/// # let message = concat!(
147/// # "assertion failed: `assert_iter_le!(a_collection, b_collection)`\n",
148/// # "https://docs.rs/assertables/9.5.3/assertables/macro.assert_iter_le.html\n",
149/// # " a label: `&a`,\n",
150/// # " a debug: `[3, 4]`,\n",
151/// # " b label: `&b`,\n",
152/// # " b debug: `[1, 2]`",
153/// # );
154/// # assert_eq!(actual, message);
155/// # }
156/// ```
157///
158/// This implementation uses [`::std::iter::Iterator`](https://doc.rust-lang.org/std/iter/trait.Iterator.html).
159///
160/// # Module macros
161///
162/// * [`assert_iter_le`](macro@crate::assert_iter_le)
163/// * [`assert_iter_le_as_result`](macro@crate::assert_iter_le_as_result)
164/// * [`debug_assert_iter_le`](macro@crate::debug_assert_iter_le)
165///
166#[macro_export]
167macro_rules! assert_iter_le {
168 ($a_collection:expr, $b_collection:expr $(,)?) => {{
169 match $crate::assert_iter_le_as_result!($a_collection, $b_collection) {
170 Ok(()) => (),
171 Err(err) => panic!("{}", err),
172 }
173 }};
174 ($a_collection:expr, $b_collection:expr, $($message:tt)+) => {{
175 match $crate::assert_iter_le_as_result!($a_collection, $b_collection) {
176 Ok(()) => (),
177 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
178 }
179 }};
180}
181
182#[cfg(test)]
183mod test_assert_iter_le {
184 use std::panic;
185
186 #[test]
187 fn lt() {
188 let a = [1, 2];
189 let b = [3, 4];
190 let actual = assert_iter_le!(&a, &b);
191 assert_eq!(actual, ());
192 }
193
194 #[test]
195 fn eq() {
196 let a = [1, 2];
197 let b = [1, 2];
198 let actual = assert_iter_le!(&a, &b);
199 assert_eq!(actual, ());
200 }
201
202 #[test]
203 fn gt() {
204 let a = [3, 4];
205 let b = [1, 2];
206 let result = panic::catch_unwind(|| {
207 let _actual = assert_iter_le!(&a, &b);
208 });
209 let message = concat!(
210 "assertion failed: `assert_iter_le!(a_collection, b_collection)`\n",
211 "https://docs.rs/assertables/9.5.3/assertables/macro.assert_iter_le.html\n",
212 " a label: `&a`,\n",
213 " a debug: `[3, 4]`,\n",
214 " b label: `&b`,\n",
215 " b debug: `[1, 2]`"
216 );
217 assert_eq!(
218 result
219 .unwrap_err()
220 .downcast::<String>()
221 .unwrap()
222 .to_string(),
223 message
224 );
225 }
226}
227
228/// Assert an iterable is less than or equal to another.
229///
230/// Pseudocode:<br>
231/// (collection1 into iter) ≤ (collection2 into iter)
232///
233/// This macro provides the same statements as [`assert_iter_le`](macro.assert_iter_le.html),
234/// except this macro's statements are only enabled in non-optimized
235/// builds by default. An optimized build will not execute this macro's
236/// statements unless `-C debug-assertions` is passed to the compiler.
237///
238/// This macro is useful for checks that are too expensive to be present
239/// in a release build but may be helpful during development.
240///
241/// The result of expanding this macro is always type checked.
242///
243/// An unchecked assertion allows a program in an inconsistent state to
244/// keep running, which might have unexpected consequences but does not
245/// introduce unsafety as long as this only happens in safe code. The
246/// performance cost of assertions, however, is not measurable in general.
247/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
248/// after thorough profiling, and more importantly, only in safe code!
249///
250/// This macro is intended to work in a similar way to
251/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
252///
253/// # Module macros
254///
255/// * [`assert_iter_le`](macro@crate::assert_iter_le)
256/// * [`assert_iter_le`](macro@crate::assert_iter_le)
257/// * [`debug_assert_iter_le`](macro@crate::debug_assert_iter_le)
258///
259#[macro_export]
260macro_rules! debug_assert_iter_le {
261 ($($arg:tt)*) => {
262 if $crate::cfg!(debug_assertions) {
263 $crate::assert_iter_le!($($arg)*);
264 }
265 };
266}