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