assertables/assert_fn_ok/assert_fn_ok_eq.rs
1//! Assert a function Ok(…) is equal to 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 = 1;
19//! assert_fn_ok_eq!(f, a, f, b);
20//! ```
21//!
22//! # Module macros
23//!
24//! * [`assert_fn_ok_eq`](macro@crate::assert_fn_ok_eq)
25//! * [`assert_fn_ok_eq_as_result`](macro@crate::assert_fn_ok_eq_as_result)
26//! * [`debug_assert_fn_ok_eq`](macro@crate::debug_assert_fn_ok_eq)
27
28/// Assert a function Ok(…) is equal to 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_eq`](macro@crate::assert_fn_ok_eq)
43/// * [`assert_fn_ok_eq_as_result`](macro@crate::assert_fn_ok_eq_as_result)
44/// * [`debug_assert_fn_ok_eq`](macro@crate::debug_assert_fn_ok_eq)
45///
46#[macro_export]
47macro_rules! assert_fn_ok_eq_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_eq!(a_function, a_param, b_function, b_param)`\n",
66 "https://docs.rs/assertables/9.6.0/assertables/macro.assert_fn_ok_eq.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_ok_eq!(a_function, a_param, b_function, b_param)`\n",
93 "https://docs.rs/assertables/9.6.0/assertables/macro.assert_fn_ok_eq.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_eq!(a_function, b_function)`\n",
134 "https://docs.rs/assertables/9.6.0/assertables/macro.assert_fn_ok_eq.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_ok_eq!(a_function, b_function)`\n",
153 "https://docs.rs/assertables/9.6.0/assertables/macro.assert_fn_ok_eq.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_eq_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 eq() {
187 let a: i8 = 1;
188 let b: i8 = 1;
189 let actual = assert_fn_ok_eq_as_result!(f, a, g, b);
190 assert_eq!(actual.unwrap(), (1, 1));
191 }
192
193 #[test]
194 fn ne() {
195 let a: i8 = 1;
196 let b: i8 = 2;
197 let actual = assert_fn_ok_eq_as_result!(f, a, g, b);
198 let message = concat!(
199 "assertion failed: `assert_fn_ok_eq!(a_function, a_param, b_function, b_param)`\n",
200 "https://docs.rs/assertables/9.6.0/assertables/macro.assert_fn_ok_eq.html\n",
201 " a_function label: `f`,\n",
202 " a_param label: `a`,\n",
203 " a_param debug: `1`,\n",
204 " b_function label: `g`,\n",
205 " b_param label: `b`,\n",
206 " b_param debug: `2`,\n",
207 " a: `1`,\n",
208 " b: `2`"
209 );
210 assert_eq!(actual.unwrap_err(), message);
211 }
212 }
213
214 mod arity_0 {
215
216 fn f() -> Result<i8, i8> {
217 return Ok(1);
218 }
219
220 fn g() -> Result<i8, i8> {
221 return Ok(2);
222 }
223
224 #[test]
225 fn eq() {
226 let actual = assert_fn_ok_eq_as_result!(f, f);
227 assert_eq!(actual.unwrap(), (1, 1));
228 }
229
230 #[test]
231 fn ne() {
232 let actual = assert_fn_ok_eq_as_result!(f, g);
233 let message = concat!(
234 "assertion failed: `assert_fn_ok_eq!(a_function, b_function)`\n",
235 "https://docs.rs/assertables/9.6.0/assertables/macro.assert_fn_ok_eq.html\n",
236 " a_function label: `f`,\n",
237 " b_function label: `g`,\n",
238 " a: `1`,\n",
239 " b: `2`"
240 );
241 assert_eq!(actual.unwrap_err(), message);
242 }
243 }
244}
245
246/// Assert a function Ok(…) is equal to another.
247///
248/// Pseudocode:<br>
249/// (a_function(a_param) ⇒ Ok(a) ⇒ a) = (b_function(b_param) ⇒ Ok(b) ⇒ b)
250///
251/// * If true, return `(a, b)`.
252///
253/// * Otherwise, call [`panic!`] with a message and the values of the
254/// expressions with their debug representations.
255///
256/// # Examples
257///
258/// ```rust
259/// use assertables::*;
260/// # use std::panic;
261/// fn f(i: i8) -> Result<String, String> {
262/// match i {
263/// 0..=9 => Ok(format!("{}", i)),
264/// _ => Err(format!("{:?} is out of range", i)),
265/// }
266/// }
267///
268/// # fn main() {
269/// let a: i8 = 1;
270/// let b: i8 = 1;
271/// assert_fn_ok_eq!(f, a, f, b);
272///
273/// # let result = panic::catch_unwind(|| {
274/// // This will panic
275/// let a: i8 = 1;
276/// let b: i8 = 2;
277/// assert_fn_ok_eq!(f, a, f, b);
278/// # });
279/// // assertion failed: `assert_fn_ok_eq!(a_function, a_param, b_function, b_param)`
280/// // https://docs.rs/assertables/9.6.0/assertables/macro.assert_fn_ok_eq.html
281/// // a_function label: `f`,
282/// // a_param label: `a`,
283/// // a_param debug: `1`,
284/// // b_function label: `f`,
285/// // b_param label: `b`,
286/// // b_param debug: `2`,
287/// // a: `\"1\"`,
288/// // b: `\"2\"`
289/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
290/// # let message = concat!(
291/// # "assertion failed: `assert_fn_ok_eq!(a_function, a_param, b_function, b_param)`\n",
292/// # "https://docs.rs/assertables/9.6.0/assertables/macro.assert_fn_ok_eq.html\n",
293/// # " a_function label: `f`,\n",
294/// # " a_param label: `a`,\n",
295/// # " a_param debug: `1`,\n",
296/// # " b_function label: `f`,\n",
297/// # " b_param label: `b`,\n",
298/// # " b_param debug: `2`,\n",
299/// # " a: `\"1\"`,\n",
300/// # " b: `\"2\"`"
301/// # );
302/// # assert_eq!(actual, message);
303/// # }
304/// ```
305///
306/// # Module macros
307///
308/// * [`assert_fn_ok_eq`](macro@crate::assert_fn_ok_eq)
309/// * [`assert_fn_ok_eq_as_result`](macro@crate::assert_fn_ok_eq_as_result)
310/// * [`debug_assert_fn_ok_eq`](macro@crate::debug_assert_fn_ok_eq)
311///
312#[macro_export]
313macro_rules! assert_fn_ok_eq {
314
315 //// Arity 1
316
317 ($a_function:path, $a_param:expr, $b_function:path, $b_param:expr $(,)?) => {
318 match $crate::assert_fn_ok_eq_as_result!($a_function, $a_param, $b_function, $b_param) {
319 Ok(x) => x,
320 Err(err) => panic!("{}", err),
321 }
322 };
323
324 ($a_function:path, $a_param:expr, $b_function:path, $b_param:expr, $($message:tt)+) => {
325 match $crate::assert_fn_ok_eq_as_result!($a_function, $a_param, $b_function, $b_param) {
326 Ok(x) => x,
327 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
328 }
329 };
330
331 //// Arity 0
332
333 ($a_function:path, $b_function:path) => {
334 match $crate::assert_fn_ok_eq_as_result!($a_function, $b_function) {
335 Ok(x) => x,
336 Err(err) => panic!("{}", err),
337 }
338 };
339
340 ($a_function:path, $b_function:path, $($message:tt)+) => {
341 match $crate::assert_fn_ok_eq_as_result!($a_function, $b_function) {
342 Ok(x) => x,
343 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
344 }
345 };
346}
347
348#[cfg(test)]
349mod test_assert_fn_ok_eq {
350 use std::panic;
351
352 mod arity_1 {
353 use super::*;
354
355 fn f(i: i8) -> Result<i8, i8> {
356 return Ok(i);
357 }
358
359 fn g(i: i8) -> Result<i8, i8> {
360 return Ok(i);
361 }
362
363 #[test]
364 fn eq() {
365 let a: i8 = 1;
366 let b: i8 = 1;
367 let actual = assert_fn_ok_eq!(f, a, g, b);
368 assert_eq!(actual, (1, 1));
369 }
370
371 #[test]
372 fn ne() {
373 let result = panic::catch_unwind(|| {
374 let a: i8 = 1;
375 let b: i8 = 2;
376 let _actual = assert_fn_ok_eq!(f, a, g, b);
377 });
378 let message = concat!(
379 "assertion failed: `assert_fn_ok_eq!(a_function, a_param, b_function, b_param)`\n",
380 "https://docs.rs/assertables/9.6.0/assertables/macro.assert_fn_ok_eq.html\n",
381 " a_function label: `f`,\n",
382 " a_param label: `a`,\n",
383 " a_param debug: `1`,\n",
384 " b_function label: `g`,\n",
385 " b_param label: `b`,\n",
386 " b_param debug: `2`,\n",
387 " a: `1`,\n",
388 " b: `2`"
389 );
390 assert_eq!(
391 result
392 .unwrap_err()
393 .downcast::<String>()
394 .unwrap()
395 .to_string(),
396 message
397 );
398 }
399 }
400
401 mod arity_0 {
402 use super::*;
403
404 fn f() -> Result<i8, i8> {
405 return Ok(1);
406 }
407
408 fn g() -> Result<i8, i8> {
409 return Ok(2);
410 }
411
412 #[test]
413 fn eq() {
414 let actual = assert_fn_ok_eq!(f, f);
415 assert_eq!(actual, (1, 1));
416 }
417
418 #[test]
419 fn ne() {
420 let result = panic::catch_unwind(|| {
421 let _actual = assert_fn_ok_eq!(f, g);
422 });
423 let message = concat!(
424 "assertion failed: `assert_fn_ok_eq!(a_function, b_function)`\n",
425 "https://docs.rs/assertables/9.6.0/assertables/macro.assert_fn_ok_eq.html\n",
426 " a_function label: `f`,\n",
427 " b_function label: `g`,\n",
428 " a: `1`,\n",
429 " b: `2`"
430 );
431 assert_eq!(
432 result
433 .unwrap_err()
434 .downcast::<String>()
435 .unwrap()
436 .to_string(),
437 message
438 );
439 }
440 }
441}
442
443/// Assert a function Ok(…) is equal to another.
444///
445/// Pseudocode:<br>
446/// (a_function(a_param) ⇒ Ok(a) ⇒ a) = (b_function(b_param) ⇒ Ok(b) ⇒ b)
447///
448/// This macro provides the same statements as [`assert_fn_ok_eq`](macro.assert_fn_ok_eq.html),
449/// except this macro's statements are only enabled in non-optimized
450/// builds by default. An optimized build will not execute this macro's
451/// statements unless `-C debug-assertions` is passed to the compiler.
452///
453/// This macro is useful for checks that are too expensive to be present
454/// in a release build but may be helpful during development.
455///
456/// The result of expanding this macro is always type checked.
457///
458/// An unchecked assertion allows a program in an inconsistent state to
459/// keep running, which might have unexpected consequences but does not
460/// introduce unsafety as long as this only happens in safe code. The
461/// performance cost of assertions, however, is not measurable in general.
462/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
463/// after thorough profiling, and more importantly, only in safe code!
464///
465/// This macro is intended to work in a similar way to
466/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
467///
468/// # Module macros
469///
470/// * [`assert_fn_ok_eq`](macro@crate::assert_fn_ok_eq)
471/// * [`assert_fn_ok_eq`](macro@crate::assert_fn_ok_eq)
472/// * [`debug_assert_fn_ok_eq`](macro@crate::debug_assert_fn_ok_eq)
473///
474#[macro_export]
475macro_rules! debug_assert_fn_ok_eq {
476 ($($arg:tt)*) => {
477 if $crate::cfg!(debug_assertions) {
478 $crate::assert_fn_ok_eq!($($arg)*);
479 }
480 };
481}