assertables/assert_fn_ok/
assert_fn_ok_lt.rs

1//! Assert a function Ok(…) is less than another.
2//!
3//! Pseudocode:<br>
4//! (a_function(a_param) ⇒ Ok(a) ⇒ a) < (b_function(b_param) ⇒ Ok(b) ⇒ b)
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//! fn f(i: i8) -> Result<String, String> {
11//!     match i {
12//!         0..=9 => Ok(format!("{}", i)),
13//!         _ => Err(format!("{:?} is out of range", i)),
14//!     }
15//! }
16//!
17//! let a: i8 = 1;
18//! let b: i8 = 2;
19//! assert_fn_ok_lt!(f, a, f, b);
20//! ```
21//!
22//! # Module macros
23//!
24//! * [`assert_fn_ok_lt`](macro@crate::assert_fn_ok_lt)
25//! * [`assert_fn_ok_lt_as_result`](macro@crate::assert_fn_ok_lt_as_result)
26//! * [`debug_assert_fn_ok_lt`](macro@crate::debug_assert_fn_ok_lt)
27
28/// Assert a function Ok(…) is less than another.
29///
30/// Pseudocode:<br>
31/// (a_function(a_param) ⇒ Ok(a) ⇒ a) < (b_function(b_param) ⇒ Ok(b) ⇒ b)
32///
33/// * If true, return Result `Ok(a, b)`.
34///
35/// * Otherwise, return Result `Err(message)`.
36///
37/// This macro is useful for runtime checks, such as checking parameters,
38/// or sanitizing inputs, or handling different results in different ways.
39///
40/// # Module macros
41///
42/// * [`assert_fn_ok_lt`](macro@crate::assert_fn_ok_lt)
43/// * [`assert_fn_ok_lt_as_result`](macro@crate::assert_fn_ok_lt_as_result)
44/// * [`debug_assert_fn_ok_lt`](macro@crate::debug_assert_fn_ok_lt)
45///
46#[macro_export]
47macro_rules! assert_fn_ok_lt_as_result {
48
49    //// Arity 1
50
51    ($a_function:path, $a_param:expr, $b_function:path, $b_param:expr $(,)?) => {{
52        match (&$a_function, &$a_param, &$b_function, &$b_param) {
53            (_a_function, a_param, _b_function, b_param) => {
54                match (
55                    $a_function($a_param),
56                    $b_function($b_param)
57                ) {
58                    (Ok(a), Ok(b)) => {
59                        if a < b {
60                            Ok((a, b))
61                        } else {
62                            Err(
63                                format!(
64                                    concat!(
65                                        "assertion failed: `assert_fn_ok_lt!(a_function, a_param, b_function, b_param)`\n",
66                                        "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_ok_lt.html\n",
67                                        " a_function label: `{}`,\n",
68                                        "    a_param label: `{}`,\n",
69                                        "    a_param debug: `{:?}`,\n",
70                                        " b_function label: `{}`,\n",
71                                        "    b_param label: `{}`,\n",
72                                        "    b_param debug: `{:?}`,\n",
73                                        "                a: `{:?}`,\n",
74                                        "                b: `{:?}`"
75                                    ),
76                                    stringify!($a_function),
77                                    stringify!($a_param),
78                                    a_param,
79                                    stringify!($b_function),
80                                    stringify!($b_param),
81                                    b_param,
82                                    a,
83                                    b
84                                )
85                            )
86                        }
87                    },
88                    (a, b) => {
89                        Err(
90                            format!(
91                                concat!(
92                                    "assertion failed: `assert_fn_err_lt!(a_function, a_param, b_function, b_param)`\n",
93                                    "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_err_lt.html\n",
94                                    " a_function label: `{}`,\n",
95                                    "    a_param label: `{}`,\n",
96                                    "    a_param debug: `{:?}`,\n",
97                                    " b_function label: `{}`,\n",
98                                    "    b_param label: `{}`,\n",
99                                    "    b_param debug: `{:?}`,\n",
100                                    "                a: `{:?}`,\n",
101                                    "                b: `{:?}`"
102                                ),
103                                stringify!($a_function),
104                                stringify!($a_param),
105                                a_param,
106                                stringify!($b_function),
107                                stringify!($b_param),
108                                b_param,
109                                a,
110                                b
111                            )
112                        )
113                    }
114                }
115            }
116        }
117    }};
118
119    //// Arity 0
120
121    ($a_function:path, $b_function:path) => {{
122        match (
123            $a_function(),
124            $b_function()
125        ) {
126            (Ok(a), Ok(b)) => {
127                if a < b {
128                    Ok((a, b))
129                } else {
130                    Err(
131                        format!(
132                            concat!(
133                                "assertion failed: `assert_fn_ok_lt!(a_function, b_function)`\n",
134                                "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_ok_lt.html\n",
135                                " a_function label: `{}`,\n",
136                                " b_function label: `{}`,\n",
137                                "                a: `{:?}`,\n",
138                                "                b: `{:?}`"
139                            ),
140                            stringify!($a_function),
141                            stringify!($b_function),
142                            a,
143                            b
144                        )
145                    )
146                }
147            },
148            (a, b) => {
149                Err(
150                    format!(
151                        concat!(
152                            "assertion failed: `assert_fn_err_lt!(a_function, b_function)`\n",
153                            "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_err_lt.html\n",
154                            " a_function label: `{}`,\n",
155                            " b_function label: `{}`,\n",
156                            "                a: `{:?}`,\n",
157                            "                b: `{:?}`"
158                        ),
159                        stringify!($a_function),
160                        stringify!($b_function),
161                        a,
162                        b
163                    )
164                )
165            }
166        }
167    }};
168
169}
170
171#[cfg(test)]
172mod test_assert_fn_ok_lt_as_result {
173
174    mod arity_1 {
175
176        fn f(i: i8) -> Result<i8, i8> {
177            return Ok(i);
178        }
179
180        fn g(i: i8) -> Result<i8, i8> {
181            return Ok(i);
182        }
183
184        #[test]
185        fn lt() {
186            let a: i8 = 1;
187            let b: i8 = 2;
188            let actual = assert_fn_ok_lt_as_result!(f, a, g, b);
189            assert_eq!(actual.unwrap(), (1, 2));
190        }
191
192        #[test]
193        fn eq() {
194            let a: i8 = 1;
195            let b: i8 = 1;
196            let actual = assert_fn_ok_lt_as_result!(f, a, g, b);
197            let message = concat!(
198                "assertion failed: `assert_fn_ok_lt!(a_function, a_param, b_function, b_param)`\n",
199                "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_ok_lt.html\n",
200                " a_function label: `f`,\n",
201                "    a_param label: `a`,\n",
202                "    a_param debug: `1`,\n",
203                " b_function label: `g`,\n",
204                "    b_param label: `b`,\n",
205                "    b_param debug: `1`,\n",
206                "                a: `1`,\n",
207                "                b: `1`"
208            );
209            assert_eq!(actual.unwrap_err(), message);
210        }
211
212        #[test]
213        fn gt() {
214            let a: i8 = 2;
215            let b: i8 = 1;
216            let actual = assert_fn_ok_lt_as_result!(f, a, g, b);
217            let message = concat!(
218                "assertion failed: `assert_fn_ok_lt!(a_function, a_param, b_function, b_param)`\n",
219                "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_ok_lt.html\n",
220                " a_function label: `f`,\n",
221                "    a_param label: `a`,\n",
222                "    a_param debug: `2`,\n",
223                " b_function label: `g`,\n",
224                "    b_param label: `b`,\n",
225                "    b_param debug: `1`,\n",
226                "                a: `2`,\n",
227                "                b: `1`"
228            );
229            assert_eq!(actual.unwrap_err(), message);
230        }
231    }
232
233    mod arity_0 {
234
235        fn f() -> Result<i8, i8> {
236            return Ok(1);
237        }
238
239        fn g() -> Result<i8, i8> {
240            return Ok(2);
241        }
242
243        #[test]
244        fn lt() {
245            let actual = assert_fn_ok_lt_as_result!(f, g);
246            assert_eq!(actual.unwrap(), (1, 2));
247        }
248
249        #[test]
250        fn eq() {
251            let actual = assert_fn_ok_lt_as_result!(f, f);
252            let message = concat!(
253                "assertion failed: `assert_fn_ok_lt!(a_function, b_function)`\n",
254                "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_ok_lt.html\n",
255                " a_function label: `f`,\n",
256                " b_function label: `f`,\n",
257                "                a: `1`,\n",
258                "                b: `1`"
259            );
260            assert_eq!(actual.unwrap_err(), message);
261        }
262
263        #[test]
264        fn gt() {
265            let actual = assert_fn_ok_lt_as_result!(g, f);
266            let message = concat!(
267                "assertion failed: `assert_fn_ok_lt!(a_function, b_function)`\n",
268                "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_ok_lt.html\n",
269                " a_function label: `g`,\n",
270                " b_function label: `f`,\n",
271                "                a: `2`,\n",
272                "                b: `1`"
273            );
274            assert_eq!(actual.unwrap_err(), message);
275        }
276    }
277}
278
279/// Assert a function Ok(…) is less than another.
280///
281/// Pseudocode:<br>
282/// (a_function(a_param) ⇒ Ok(a) ⇒ a) < (b_function(b_param) ⇒ Ok(b) ⇒ b)
283///
284/// * If true, return `(a, b)`.
285///
286/// * Otherwise, call [`panic!`] with a message and the values of the
287///   expressions with their debug representations.
288///
289/// # Examples
290///
291/// ```rust
292/// use assertables::*;
293/// # use std::panic;
294/// fn f(i: i8) -> Result<String, String> {
295///     match i {
296///         0..=9 => Ok(format!("{}", i)),
297///         _ => Err(format!("{:?} is out of range", i)),
298///     }
299/// }
300///
301/// # fn main() {
302/// let a: i8 = 1;
303/// let b: i8 = 2;
304/// assert_fn_ok_lt!(f, a, f, b);
305///
306/// # let result = panic::catch_unwind(|| {
307/// // This will panic
308/// let a: i8 = 2;
309/// let b: i8 = 1;
310/// assert_fn_ok_lt!(f, a, f, b);
311/// # });
312/// //  a_function label: `f`,
313/// //     a_param label: `a`,
314/// //     a_param debug: `2`,
315/// //  b_function label: `f`,
316/// //     b_param label: `b`,
317/// //     b_param debug: `1`,
318/// //                 a: `\"2\"`,
319/// //                 b: `\"1\"`
320/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
321/// # let message = concat!(
322/// #     "assertion failed: `assert_fn_ok_lt!(a_function, a_param, b_function, b_param)`\n",
323/// #     "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_ok_lt.html\n",
324/// #     " a_function label: `f`,\n",
325/// #     "    a_param label: `a`,\n",
326/// #     "    a_param debug: `2`,\n",
327/// #     " b_function label: `f`,\n",
328/// #     "    b_param label: `b`,\n",
329/// #     "    b_param debug: `1`,\n",
330/// #     "                a: `\"2\"`,\n",
331/// #     "                b: `\"1\"`"
332/// # );
333/// # assert_eq!(actual, message);
334/// # }
335/// ```
336///
337/// # Module macros
338///
339/// * [`assert_fn_ok_lt`](macro@crate::assert_fn_ok_lt)
340/// * [`assert_fn_ok_lt_as_result`](macro@crate::assert_fn_ok_lt_as_result)
341/// * [`debug_assert_fn_ok_lt`](macro@crate::debug_assert_fn_ok_lt)
342///
343#[macro_export]
344macro_rules! assert_fn_ok_lt {
345
346    //// Arity 1
347
348    ($a_function:path, $a_param:expr, $b_function:path, $b_param:expr $(,)?) => {{
349        match $crate::assert_fn_ok_lt_as_result!($a_function, $a_param, $b_function, $b_param) {
350            Ok(x) => x,
351            Err(err) => panic!("{}", err),
352        }
353    }};
354
355    ($a_function:path, $a_param:expr, $b_function:path, $b_param:expr, $($message:tt)+) => {{
356        match $crate::assert_fn_ok_lt_as_result!($a_function, $a_param, $b_function, $b_param) {
357            Ok(x) => x,
358            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
359        }
360    }};
361
362    //// Arity 0
363
364    ($a_function:path, $b_function:path) => {{
365        match $crate::assert_fn_ok_lt_as_result!($a_function, $b_function) {
366            Ok(x) => x,
367            Err(err) => panic!("{}", err),
368        }
369    }};
370
371    ($a_function:path, $b_function:path, $($message:tt)+) => {{
372        match $crate::assert_fn_ok_lt_as_result!($a_function, $b_function) {
373            Ok(x) => x,
374            Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
375        }
376    }};
377}
378
379#[cfg(test)]
380mod test_assert_fn_ok_lt {
381    use std::panic;
382
383    mod arity_1 {
384        use super::*;
385
386        fn f(i: i8) -> Result<i8, i8> {
387            return Ok(i);
388        }
389
390        fn g(i: i8) -> Result<i8, i8> {
391            return Ok(i);
392        }
393
394        #[test]
395        fn lt() {
396            let a: i8 = 1;
397            let b: i8 = 2;
398            let actual = assert_fn_ok_lt!(f, a, g, b);
399            assert_eq!(actual, (1, 2));
400        }
401
402        #[test]
403        fn eq() {
404            let result = panic::catch_unwind(|| {
405                let a: i8 = 1;
406                let b: i8 = 1;
407                let _actual = assert_fn_ok_lt!(f, a, g, b);
408            });
409            let message = concat!(
410                "assertion failed: `assert_fn_ok_lt!(a_function, a_param, b_function, b_param)`\n",
411                "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_ok_lt.html\n",
412                " a_function label: `f`,\n",
413                "    a_param label: `a`,\n",
414                "    a_param debug: `1`,\n",
415                " b_function label: `g`,\n",
416                "    b_param label: `b`,\n",
417                "    b_param debug: `1`,\n",
418                "                a: `1`,\n",
419                "                b: `1`"
420            );
421            assert_eq!(
422                result
423                    .unwrap_err()
424                    .downcast::<String>()
425                    .unwrap()
426                    .to_string(),
427                message
428            );
429        }
430
431        #[test]
432        fn gt() {
433            let result = panic::catch_unwind(|| {
434                let a: i8 = 2;
435                let b: i8 = 1;
436                let _actual = assert_fn_ok_lt!(f, a, g, b);
437            });
438            let message = concat!(
439                "assertion failed: `assert_fn_ok_lt!(a_function, a_param, b_function, b_param)`\n",
440                "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_ok_lt.html\n",
441                " a_function label: `f`,\n",
442                "    a_param label: `a`,\n",
443                "    a_param debug: `2`,\n",
444                " b_function label: `g`,\n",
445                "    b_param label: `b`,\n",
446                "    b_param debug: `1`,\n",
447                "                a: `2`,\n",
448                "                b: `1`"
449            );
450            assert_eq!(
451                result
452                    .unwrap_err()
453                    .downcast::<String>()
454                    .unwrap()
455                    .to_string(),
456                message
457            );
458        }
459    }
460
461    mod arity_0 {
462        use super::*;
463
464        fn f() -> Result<i8, i8> {
465            return Ok(1);
466        }
467
468        fn g() -> Result<i8, i8> {
469            return Ok(2);
470        }
471
472        #[test]
473        fn lt() {
474            let actual = assert_fn_ok_lt!(f, g);
475            assert_eq!(actual, (1, 2));
476        }
477
478        #[test]
479        fn eq() {
480            let result = panic::catch_unwind(|| {
481                let _actual = assert_fn_ok_lt!(f, f);
482            });
483            let message = concat!(
484                "assertion failed: `assert_fn_ok_lt!(a_function, b_function)`\n",
485                "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_ok_lt.html\n",
486                " a_function label: `f`,\n",
487                " b_function label: `f`,\n",
488                "                a: `1`,\n",
489                "                b: `1`"
490            );
491            assert_eq!(
492                result
493                    .unwrap_err()
494                    .downcast::<String>()
495                    .unwrap()
496                    .to_string(),
497                message
498            );
499        }
500
501        #[test]
502        fn gt() {
503            let result = panic::catch_unwind(|| {
504                let _actual = assert_fn_ok_lt!(g, f);
505            });
506            let message = concat!(
507                "assertion failed: `assert_fn_ok_lt!(a_function, b_function)`\n",
508                "https://docs.rs/assertables/9.5.1/assertables/macro.assert_fn_ok_lt.html\n",
509                " a_function label: `g`,\n",
510                " b_function label: `f`,\n",
511                "                a: `2`,\n",
512                "                b: `1`"
513            );
514            assert_eq!(
515                result
516                    .unwrap_err()
517                    .downcast::<String>()
518                    .unwrap()
519                    .to_string(),
520                message
521            );
522        }
523    }
524}
525
526/// Assert a function Ok(…) is less than another.
527///
528/// Pseudocode:<br>
529/// (a_function(a_param) ⇒ Ok(a) ⇒ a) < (b_function(b_param) ⇒ Ok(b) ⇒ b)
530///
531/// This macro provides the same statements as [`assert_fn_ok_lt`](macro.assert_fn_ok_lt.html),
532/// except this macro's statements are only enabled in non-optimized
533/// builds by default. An optimized build will not execute this macro's
534/// statements unless `-C debug-assertions` is passed to the compiler.
535///
536/// This macro is useful for checks that are too expensive to be present
537/// in a release build but may be helpful during development.
538///
539/// The result of expanding this macro is always type checked.
540///
541/// An unchecked assertion allows a program in an inconsistent state to
542/// keep running, which might have unexpected consequences but does not
543/// introduce unsafety as long as this only happens in safe code. The
544/// performance cost of assertions, however, is not measurable in general.
545/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
546/// after thorough profiling, and more importantly, only in safe code!
547///
548/// This macro is intended to work in a similar way to
549/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
550///
551/// # Module macros
552///
553/// * [`assert_fn_ok_lt`](macro@crate::assert_fn_ok_lt)
554/// * [`assert_fn_ok_lt`](macro@crate::assert_fn_ok_lt)
555/// * [`debug_assert_fn_ok_lt`](macro@crate::debug_assert_fn_ok_lt)
556///
557#[macro_export]
558macro_rules! debug_assert_fn_ok_lt {
559    ($($arg:tt)*) => {
560        if $crate::cfg!(debug_assertions) {
561            $crate::assert_fn_ok_lt!($($arg)*);
562        }
563    };
564}