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.7.0/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.7.0/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.7.0/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.7.0/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 // use std::sync::Once;
174
175 mod arity_1 {
176
177 fn f(i: i8) -> Result<i8, i8> {
178 return Ok(i);
179 }
180
181 fn g(i: i8) -> Result<i8, i8> {
182 return Ok(i);
183 }
184
185 #[test]
186 fn lt() {
187 let a: i8 = 1;
188 let b: i8 = 2;
189 for _ in 0..1 {
190 let actual = assert_fn_ok_lt_as_result!(f, a, g, b);
191 assert_eq!(actual.unwrap(), (1, 2));
192 }
193 }
194
195 #[test]
196 fn eq() {
197 let a: i8 = 1;
198 let b: i8 = 1;
199 let actual = assert_fn_ok_lt_as_result!(f, a, g, b);
200 let message = concat!(
201 "assertion failed: `assert_fn_ok_lt!(a_function, a_param, b_function, b_param)`\n",
202 "https://docs.rs/assertables/9.7.0/assertables/macro.assert_fn_ok_lt.html\n",
203 " a_function label: `f`,\n",
204 " a_param label: `a`,\n",
205 " a_param debug: `1`,\n",
206 " b_function label: `g`,\n",
207 " b_param label: `b`,\n",
208 " b_param debug: `1`,\n",
209 " a: `1`,\n",
210 " b: `1`"
211 );
212 assert_eq!(actual.unwrap_err(), message);
213 }
214
215 #[test]
216 fn gt() {
217 let a: i8 = 2;
218 let b: i8 = 1;
219 let actual = assert_fn_ok_lt_as_result!(f, a, g, b);
220 let message = concat!(
221 "assertion failed: `assert_fn_ok_lt!(a_function, a_param, b_function, b_param)`\n",
222 "https://docs.rs/assertables/9.7.0/assertables/macro.assert_fn_ok_lt.html\n",
223 " a_function label: `f`,\n",
224 " a_param label: `a`,\n",
225 " a_param debug: `2`,\n",
226 " b_function label: `g`,\n",
227 " b_param label: `b`,\n",
228 " b_param debug: `1`,\n",
229 " a: `2`,\n",
230 " b: `1`"
231 );
232 assert_eq!(actual.unwrap_err(), message);
233 }
234 }
235
236 mod arity_0 {
237
238 fn f() -> Result<i8, i8> {
239 return Ok(1);
240 }
241
242 fn g() -> Result<i8, i8> {
243 return Ok(2);
244 }
245
246 #[test]
247 fn lt() {
248 for _ in 0..1 {
249 let actual = assert_fn_ok_lt_as_result!(f, g);
250 assert_eq!(actual.unwrap(), (1, 2));
251 }
252 }
253
254 #[test]
255 fn eq() {
256 let actual = assert_fn_ok_lt_as_result!(f, f);
257 let message = concat!(
258 "assertion failed: `assert_fn_ok_lt!(a_function, b_function)`\n",
259 "https://docs.rs/assertables/9.7.0/assertables/macro.assert_fn_ok_lt.html\n",
260 " a_function label: `f`,\n",
261 " b_function label: `f`,\n",
262 " a: `1`,\n",
263 " b: `1`"
264 );
265 assert_eq!(actual.unwrap_err(), message);
266 }
267
268 #[test]
269 fn gt() {
270 let actual = assert_fn_ok_lt_as_result!(g, f);
271 let message = concat!(
272 "assertion failed: `assert_fn_ok_lt!(a_function, b_function)`\n",
273 "https://docs.rs/assertables/9.7.0/assertables/macro.assert_fn_ok_lt.html\n",
274 " a_function label: `g`,\n",
275 " b_function label: `f`,\n",
276 " a: `2`,\n",
277 " b: `1`"
278 );
279 assert_eq!(actual.unwrap_err(), message);
280 }
281 }
282}
283
284/// Assert a function Ok(…) is less than another.
285///
286/// Pseudocode:<br>
287/// (a_function(a_param) ⇒ Ok(a) ⇒ a) < (b_function(b_param) ⇒ Ok(b) ⇒ b)
288///
289/// * If true, return `(a, b)`.
290///
291/// * Otherwise, call [`panic!`] with a message and the values of the
292/// expressions with their debug representations.
293///
294/// # Examples
295///
296/// ```rust
297/// use assertables::*;
298/// # use std::panic;
299/// fn f(i: i8) -> Result<String, String> {
300/// match i {
301/// 0..=9 => Ok(format!("{}", i)),
302/// _ => Err(format!("{:?} is out of range", i)),
303/// }
304/// }
305///
306/// # fn main() {
307/// let a: i8 = 1;
308/// let b: i8 = 2;
309/// assert_fn_ok_lt!(f, a, f, b);
310///
311/// # let result = panic::catch_unwind(|| {
312/// // This will panic
313/// let a: i8 = 2;
314/// let b: i8 = 1;
315/// assert_fn_ok_lt!(f, a, f, b);
316/// # });
317/// // a_function label: `f`,
318/// // a_param label: `a`,
319/// // a_param debug: `2`,
320/// // b_function label: `f`,
321/// // b_param label: `b`,
322/// // b_param debug: `1`,
323/// // a: `\"2\"`,
324/// // b: `\"1\"`
325/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
326/// # let message = concat!(
327/// # "assertion failed: `assert_fn_ok_lt!(a_function, a_param, b_function, b_param)`\n",
328/// # "https://docs.rs/assertables/9.7.0/assertables/macro.assert_fn_ok_lt.html\n",
329/// # " a_function label: `f`,\n",
330/// # " a_param label: `a`,\n",
331/// # " a_param debug: `2`,\n",
332/// # " b_function label: `f`,\n",
333/// # " b_param label: `b`,\n",
334/// # " b_param debug: `1`,\n",
335/// # " a: `\"2\"`,\n",
336/// # " b: `\"1\"`"
337/// # );
338/// # assert_eq!(actual, message);
339/// # }
340/// ```
341///
342/// # Module macros
343///
344/// * [`assert_fn_ok_lt`](macro@crate::assert_fn_ok_lt)
345/// * [`assert_fn_ok_lt_as_result`](macro@crate::assert_fn_ok_lt_as_result)
346/// * [`debug_assert_fn_ok_lt`](macro@crate::debug_assert_fn_ok_lt)
347///
348#[macro_export]
349macro_rules! assert_fn_ok_lt {
350
351 //// Arity 1
352
353 ($a_function:path, $a_param:expr, $b_function:path, $b_param:expr $(,)?) => {
354 match $crate::assert_fn_ok_lt_as_result!($a_function, $a_param, $b_function, $b_param) {
355 Ok(x) => x,
356 Err(err) => panic!("{}", err),
357 }
358 };
359
360 ($a_function:path, $a_param:expr, $b_function:path, $b_param:expr, $($message:tt)+) => {
361 match $crate::assert_fn_ok_lt_as_result!($a_function, $a_param, $b_function, $b_param) {
362 Ok(x) => x,
363 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
364 }
365 };
366
367 //// Arity 0
368
369 ($a_function:path, $b_function:path) => {
370 match $crate::assert_fn_ok_lt_as_result!($a_function, $b_function) {
371 Ok(x) => x,
372 Err(err) => panic!("{}", err),
373 }
374 };
375
376 ($a_function:path, $b_function:path, $($message:tt)+) => {
377 match $crate::assert_fn_ok_lt_as_result!($a_function, $b_function) {
378 Ok(x) => x,
379 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
380 }
381 };
382}
383
384#[cfg(test)]
385mod test_assert_fn_ok_lt {
386 use std::panic;
387
388 mod arity_1 {
389 use super::*;
390
391 fn f(i: i8) -> Result<i8, i8> {
392 return Ok(i);
393 }
394
395 fn g(i: i8) -> Result<i8, i8> {
396 return Ok(i);
397 }
398
399 #[test]
400 fn lt() {
401 let a: i8 = 1;
402 let b: i8 = 2;
403 for _ in 0..1 {
404 let actual = assert_fn_ok_lt!(f, a, g, b);
405 assert_eq!(actual, (1, 2));
406 }
407 }
408
409 #[test]
410 fn eq() {
411 let result = panic::catch_unwind(|| {
412 let a: i8 = 1;
413 let b: i8 = 1;
414 let _actual = assert_fn_ok_lt!(f, a, g, b);
415 });
416 let message = concat!(
417 "assertion failed: `assert_fn_ok_lt!(a_function, a_param, b_function, b_param)`\n",
418 "https://docs.rs/assertables/9.7.0/assertables/macro.assert_fn_ok_lt.html\n",
419 " a_function label: `f`,\n",
420 " a_param label: `a`,\n",
421 " a_param debug: `1`,\n",
422 " b_function label: `g`,\n",
423 " b_param label: `b`,\n",
424 " b_param debug: `1`,\n",
425 " a: `1`,\n",
426 " b: `1`"
427 );
428 assert_eq!(
429 result
430 .unwrap_err()
431 .downcast::<String>()
432 .unwrap()
433 .to_string(),
434 message
435 );
436 }
437
438 #[test]
439 fn gt() {
440 let result = panic::catch_unwind(|| {
441 let a: i8 = 2;
442 let b: i8 = 1;
443 let _actual = assert_fn_ok_lt!(f, a, g, b);
444 });
445 let message = concat!(
446 "assertion failed: `assert_fn_ok_lt!(a_function, a_param, b_function, b_param)`\n",
447 "https://docs.rs/assertables/9.7.0/assertables/macro.assert_fn_ok_lt.html\n",
448 " a_function label: `f`,\n",
449 " a_param label: `a`,\n",
450 " a_param debug: `2`,\n",
451 " b_function label: `g`,\n",
452 " b_param label: `b`,\n",
453 " b_param debug: `1`,\n",
454 " a: `2`,\n",
455 " b: `1`"
456 );
457 assert_eq!(
458 result
459 .unwrap_err()
460 .downcast::<String>()
461 .unwrap()
462 .to_string(),
463 message
464 );
465 }
466 }
467
468 mod arity_0 {
469 use super::*;
470
471 fn f() -> Result<i8, i8> {
472 return Ok(1);
473 }
474
475 fn g() -> Result<i8, i8> {
476 return Ok(2);
477 }
478
479 #[test]
480 fn lt() {
481 for _ in 0..1 {
482 let actual = assert_fn_ok_lt!(f, g);
483 assert_eq!(actual, (1, 2));
484 }
485 }
486
487 #[test]
488 fn eq() {
489 let result = panic::catch_unwind(|| {
490 let _actual = assert_fn_ok_lt!(f, f);
491 });
492 let message = concat!(
493 "assertion failed: `assert_fn_ok_lt!(a_function, b_function)`\n",
494 "https://docs.rs/assertables/9.7.0/assertables/macro.assert_fn_ok_lt.html\n",
495 " a_function label: `f`,\n",
496 " b_function label: `f`,\n",
497 " a: `1`,\n",
498 " b: `1`"
499 );
500 assert_eq!(
501 result
502 .unwrap_err()
503 .downcast::<String>()
504 .unwrap()
505 .to_string(),
506 message
507 );
508 }
509
510 #[test]
511 fn gt() {
512 let result = panic::catch_unwind(|| {
513 let _actual = assert_fn_ok_lt!(g, f);
514 });
515 let message = concat!(
516 "assertion failed: `assert_fn_ok_lt!(a_function, b_function)`\n",
517 "https://docs.rs/assertables/9.7.0/assertables/macro.assert_fn_ok_lt.html\n",
518 " a_function label: `g`,\n",
519 " b_function label: `f`,\n",
520 " a: `2`,\n",
521 " b: `1`"
522 );
523 assert_eq!(
524 result
525 .unwrap_err()
526 .downcast::<String>()
527 .unwrap()
528 .to_string(),
529 message
530 );
531 }
532 }
533}
534
535/// Assert a function Ok(…) is less than another.
536///
537/// Pseudocode:<br>
538/// (a_function(a_param) ⇒ Ok(a) ⇒ a) < (b_function(b_param) ⇒ Ok(b) ⇒ b)
539///
540/// This macro provides the same statements as [`assert_fn_ok_lt`](macro.assert_fn_ok_lt.html),
541/// except this macro's statements are only enabled in non-optimized
542/// builds by default. An optimized build will not execute this macro's
543/// statements unless `-C debug-assertions` is passed to the compiler.
544///
545/// This macro is useful for checks that are too expensive to be present
546/// in a release build but may be helpful during development.
547///
548/// The result of expanding this macro is always type checked.
549///
550/// An unchecked assertion allows a program in an inconsistent state to
551/// keep running, which might have unexpected consequences but does not
552/// introduce unsafety as long as this only happens in safe code. The
553/// performance cost of assertions, however, is not measurable in general.
554/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
555/// after thorough profiling, and more importantly, only in safe code!
556///
557/// This macro is intended to work in a similar way to
558/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
559///
560/// # Module macros
561///
562/// * [`assert_fn_ok_lt`](macro@crate::assert_fn_ok_lt)
563/// * [`assert_fn_ok_lt`](macro@crate::assert_fn_ok_lt)
564/// * [`debug_assert_fn_ok_lt`](macro@crate::debug_assert_fn_ok_lt)
565///
566#[macro_export]
567macro_rules! debug_assert_fn_ok_lt {
568 ($($arg:tt)*) => {
569 if $crate::cfg!(debug_assertions) {
570 $crate::assert_fn_ok_lt!($($arg)*);
571 }
572 };
573}