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