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