assertables/assert_fn_err/assert_fn_err_eq_x.rs
1//! Assert a function Err(…) is equal to an expression.
2//!
3//! Pseudocode:<br>
4//! (function(param) ⇒ Err(a) ⇒ a) = expr
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//!
18//! # Module macros
19//!
20//! * [`assert_fn_err_eq_x`](macro@crate::assert_fn_err_eq_x)
21//! * [`assert_fn_err_eq_x_as_result`](macro@crate::assert_fn_err_eq_x_as_result)
22//! * [`debug_assert_fn_err_eq_x`](macro@crate::debug_assert_fn_err_eq_x)
23
24/// Assert a function error is equal to an expression.
25///
26/// Pseudocode:<br>
27/// (function(param) ⇒ Err(a) ⇒ a) = expr
28///
29/// * If true, return Result `Ok(a)`.
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/// # Module macros
37///
38/// * [`assert_fn_err_eq_x`](macro@crate::assert_fn_err_eq_x)
39/// * [`assert_fn_err_eq_x_as_result`](macro@crate::assert_fn_err_eq_x_as_result)
40/// * [`debug_assert_fn_err_eq_x`](macro@crate::debug_assert_fn_err_eq_x)
41///
42#[macro_export]
43macro_rules! assert_fn_err_eq_x_as_result {
44
45 //// Arity 1
46
47 ($a_function:path, $a_param:expr, $b_expr:expr $(,)?) => {
48 match (&$a_function, &$a_param, &$b_expr) {
49 (_a_function, a_param, b_expr) => {
50 match ($a_function($a_param)) {
51 Err(a) => {
52 if a == $b_expr {
53 Ok(a)
54 } else {
55 Err(
56 format!(
57 concat!(
58 "assertion failed: `assert_fn_err_eq_x!(a_function, a_param, b_expr)`\n",
59 "https://docs.rs/assertables/9.8.2/assertables/macro.assert_fn_err_eq_x.html\n",
60 " a_function label: `{}`,\n",
61 " a_param label: `{}`,\n",
62 " a_param debug: `{:?}`,\n",
63 " b_expr label: `{}`,\n",
64 " b_expr debug: `{:?}`,\n",
65 " a: `{:?}`,\n",
66 " b: `{:?}`",
67 ),
68 stringify!($a_function),
69 stringify!($a_param),
70 a_param,
71 stringify!($b_expr),
72 b_expr,
73 a,
74 $b_expr
75 )
76 )
77 }
78 },
79 a => {
80 Err(
81 format!(
82 concat!(
83 "assertion failed: `assert_fn_err_eq_x!(a_function, a_param, b_expr)`\n",
84 "https://docs.rs/assertables/9.8.2/assertables/macro.assert_fn_err_eq_x.html\n",
85 " a_function label: `{}`,\n",
86 " a_param label: `{}`,\n",
87 " a_param debug: `{:?}`,\n",
88 " b_expr label: `{}`,\n",
89 " b_expr debug: `{:?}`,\n",
90 " a: `{:?}`",
91 ),
92 stringify!($a_function),
93 stringify!($a_param),
94 a_param,
95 stringify!($b_expr),
96 b_expr,
97 a
98 )
99 )
100 }
101 }
102 }
103 }
104 };
105
106 //// Arity 0
107
108 ($a_function:path, $b_expr:expr $(,)?) => {
109 match (&$a_function, &$b_expr) {
110 (_a_function, b_expr) => {
111 match ($a_function()) {
112 Err(a) => {
113 if a == $b_expr {
114 Ok(a)
115 } else {
116 Err(
117 format!(
118 concat!(
119 "assertion failed: `assert_fn_err_eq_x!(a_function, b_expr)`\n",
120 "https://docs.rs/assertables/9.8.2/assertables/macro.assert_fn_err_eq_x.html\n",
121 " a_function label: `{}`,\n",
122 " b_expr label: `{}`,\n",
123 " b_expr debug: `{:?}`,\n",
124 " a: `{:?}`,\n",
125 " b: `{:?}`",
126 ),
127 stringify!($a_function),
128 stringify!($b_expr),
129 b_expr,
130 a,
131 $b_expr
132 )
133 )
134 }
135 },
136 a => {
137 Err(
138 format!(
139 concat!(
140 "assertion failed: `assert_fn_err_eq_x!(a_function, b_expr)`\n",
141 "https://docs.rs/assertables/9.8.2/assertables/macro.assert_fn_err_eq_x.html\n",
142 " a_function label: `{}`,\n",
143 " b_expr label: `{}`,\n",
144 " b_expr debug: `{:?}`,\n",
145 " a: `{:?}`",
146 ),
147 stringify!($a_function),
148 stringify!($b_expr),
149 b_expr,
150 a
151 )
152 )
153 }
154 }
155 }
156 }
157 };
158}
159
160#[cfg(test)]
161mod test_assert_fn_err_eq_x_as_result {
162 // use std::sync::Once;
163
164 mod arity_1 {
165
166 fn f(i: i8) -> Result<i8, i8> {
167 Err(i)
168 }
169
170 #[test]
171 fn eq() {
172 let a: i8 = 1;
173 let b: i8 = 1;
174 for _ in 0..1 {
175 let actual = assert_fn_err_eq_x_as_result!(f, a, b);
176 assert_eq!(actual.unwrap(), 1);
177 }
178 }
179
180 #[test]
181 fn ne() {
182 let a: i8 = 1;
183 let b: i8 = 2;
184 let actual = assert_fn_err_eq_x_as_result!(f, a, b);
185 let message = concat!(
186 "assertion failed: `assert_fn_err_eq_x!(a_function, a_param, b_expr)`\n",
187 "https://docs.rs/assertables/9.8.2/assertables/macro.assert_fn_err_eq_x.html\n",
188 " a_function label: `f`,\n",
189 " a_param label: `a`,\n",
190 " a_param debug: `1`,\n",
191 " b_expr label: `b`,\n",
192 " b_expr debug: `2`,\n",
193 " a: `1`,\n",
194 " b: `2`"
195 );
196 assert_eq!(actual.unwrap_err(), message);
197 }
198 }
199
200 mod arity_0 {
201
202 fn f() -> Result<i8, i8> {
203 Err(1)
204 }
205
206 #[test]
207 fn eq() {
208 for _ in 0..1 {
209 let actual = assert_fn_err_eq_x_as_result!(f, 1);
210 assert_eq!(actual.unwrap(), 1);
211 }
212 }
213
214 #[test]
215 fn ne() {
216 let b: i8 = 2;
217 let actual = assert_fn_err_eq_x_as_result!(f, b);
218 let message = concat!(
219 "assertion failed: `assert_fn_err_eq_x!(a_function, b_expr)`\n",
220 "https://docs.rs/assertables/9.8.2/assertables/macro.assert_fn_err_eq_x.html\n",
221 " a_function label: `f`,\n",
222 " b_expr label: `b`,\n",
223 " b_expr debug: `2`,\n",
224 " a: `1`,\n",
225 " b: `2`"
226 );
227 assert_eq!(actual.unwrap_err(), message);
228 }
229 }
230}
231
232/// Assert a function error is equal to an expression.
233///
234/// Pseudocode:<br>
235/// (function(param) ⇒ Err(a) ⇒ a) = expr
236///
237/// * If true, return `a`.
238///
239/// * Otherwise, call [`panic!`] with a message and the values of the
240/// expressions with their debug representations.
241///
242/// # Examples
243///
244/// ```rust
245/// use assertables::*;
246/// # use std::panic;
247/// fn f(i: i8) -> Result<String, String> {
248/// match i {
249/// 0..=9 => Ok(format!("{}", i)),
250/// _ => Err(format!("{:?} is out of range", i)),
251/// }
252/// }
253///
254/// # fn main() {
255/// let a: i8 = 10;
256/// let b = String::from("10 is out of range");
257/// assert_fn_err_eq_x!(f, a, b);
258///
259/// # let result = panic::catch_unwind(|| {
260/// // This will panic
261/// let a: i8 = 10;
262/// let b = String::from("20 is out of range");
263/// assert_fn_err_eq_x!(f, a, b);
264/// # });
265/// // assertion failed: `assert_fn_err_eq_x!(a_function, a_param, b_expr)`
266/// // https://docs.rs/assertables/…/assertables/macro.assert_fn_err_eq_x.html
267/// // a_function label: `f`,
268/// // a_param label: `a`,
269/// // a_param debug: `10`,
270/// // b_expr label: `b`,
271/// // b_expr debug: `\"20 is out of range\"`,
272/// // a: `\"10 is out of range\"`,
273/// // b: `\"20 is out of range\"`
274/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
275/// # let message = concat!(
276/// # "assertion failed: `assert_fn_err_eq_x!(a_function, a_param, b_expr)`\n",
277/// # "https://docs.rs/assertables/9.8.2/assertables/macro.assert_fn_err_eq_x.html\n",
278/// # " a_function label: `f`,\n",
279/// # " a_param label: `a`,\n",
280/// # " a_param debug: `10`,\n",
281/// # " b_expr label: `b`,\n",
282/// # " b_expr debug: `\"20 is out of range\"`,\n",
283/// # " a: `\"10 is out of range\"`,\n",
284/// # " b: `\"20 is out of range\"`"
285/// # );
286/// # assert_eq!(actual, message);
287/// # }
288/// ```
289///
290/// # Module macros
291///
292/// * [`assert_fn_err_eq_x`](macro@crate::assert_fn_err_eq_x)
293/// * [`assert_fn_err_eq_x_as_result`](macro@crate::assert_fn_err_eq_x_as_result)
294/// * [`debug_assert_fn_err_eq_x`](macro@crate::debug_assert_fn_err_eq_x)
295///
296#[macro_export]
297macro_rules! assert_fn_err_eq_x {
298
299 //// Arity 1
300
301 ($a_function:path, $a_param:expr, $b_expr:expr $(,)?) => {
302 match $crate::assert_fn_err_eq_x_as_result!($a_function, $a_param, $b_expr) {
303 Ok(x) => x,
304 Err(err) => panic!("{}", err),
305 }
306 };
307
308 ($a_function:path, $a_param:expr, $b_expr:expr, $($message:tt)+) => {
309 match $crate::assert_fn_err_eq_x_as_result!($a_function, $a_param, $b_expr) {
310 Ok(x) => x,
311 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
312 }
313 };
314
315 //// Arity 0
316
317 ($a_function:path, $b_expr:expr $(,)?) => {
318 match $crate::assert_fn_err_eq_x_as_result!($a_function, $b_expr) {
319 Ok(x) => x,
320 Err(err) => panic!("{}", err),
321 }
322 };
323
324 ($a_function:path, $b_expr:expr, $($message:tt)+) => {
325 match $crate::assert_fn_err_eq_x_as_result!($a_function, $b_expr) {
326 Ok(x) => x,
327 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
328 }
329 };
330
331}
332
333#[cfg(test)]
334mod test_assert_fn_err_eq_x {
335 use std::panic;
336
337 mod arity_1 {
338 use super::*;
339
340 fn f(i: i8) -> Result<i8, i8> {
341 Err(i)
342 }
343
344 #[test]
345 fn eq() {
346 let a: i8 = 1;
347 let b: i8 = 1;
348 for _ in 0..1 {
349 let actual = assert_fn_err_eq_x!(f, a, b);
350 assert_eq!(actual, 1);
351 }
352 }
353
354 #[test]
355 fn ne() {
356 let result = panic::catch_unwind(|| {
357 let a: i8 = 1;
358 let b: i8 = 2;
359 let _actual = assert_fn_err_eq_x!(f, a, b);
360 });
361 let message = concat!(
362 "assertion failed: `assert_fn_err_eq_x!(a_function, a_param, b_expr)`\n",
363 "https://docs.rs/assertables/9.8.2/assertables/macro.assert_fn_err_eq_x.html\n",
364 " a_function label: `f`,\n",
365 " a_param label: `a`,\n",
366 " a_param debug: `1`,\n",
367 " b_expr label: `b`,\n",
368 " b_expr debug: `2`,\n",
369 " a: `1`,\n",
370 " b: `2`"
371 );
372 assert_eq!(
373 result
374 .unwrap_err()
375 .downcast::<String>()
376 .unwrap()
377 .to_string(),
378 message
379 );
380 }
381 }
382
383 mod arity_0 {
384 use super::*;
385
386 fn f() -> Result<i8, i8> {
387 Err(1)
388 }
389
390 #[test]
391 fn eq() {
392 for _ in 0..1 {
393 let actual = assert_fn_err_eq_x!(f, 1);
394 assert_eq!(actual, 1);
395 }
396 }
397
398 #[test]
399 fn ne() {
400 let result = panic::catch_unwind(|| {
401 let b: i8 = 2;
402 let _actual = assert_fn_err_eq_x!(f, b);
403 });
404 let message = concat!(
405 "assertion failed: `assert_fn_err_eq_x!(a_function, b_expr)`\n",
406 "https://docs.rs/assertables/9.8.2/assertables/macro.assert_fn_err_eq_x.html\n",
407 " a_function label: `f`,\n",
408 " b_expr label: `b`,\n",
409 " b_expr debug: `2`,\n",
410 " a: `1`,\n",
411 " b: `2`"
412 );
413 assert_eq!(
414 result
415 .unwrap_err()
416 .downcast::<String>()
417 .unwrap()
418 .to_string(),
419 message
420 );
421 }
422 }
423}
424
425/// Assert a function error is equal to an expression.
426///
427/// Pseudocode:<br>
428/// (function(param) ⇒ Err(a) ⇒ a) = expr
429///
430/// This macro provides the same statements as [`assert_fn_err_eq_x`](macro.assert_fn_err_eq_x.html),
431/// except this macro's statements are only enabled in non-optimized
432/// builds by default. An optimized build will not execute this macro's
433/// statements unless `-C debug-assertions` is passed to the compiler.
434///
435/// This macro is useful for checks that are too expensive to be present
436/// in a release build but may be helpful during development.
437///
438/// The result of expanding this macro is always type checked.
439///
440/// An unchecked assertion allows a program in an inconsistent state to
441/// keep running, which might have unexpected consequences but does not
442/// introduce unsafety as long as this only happens in safe code. The
443/// performance cost of assertions, however, is not measurable in general.
444/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
445/// after thorough profiling, and more importantly, only in safe code!
446///
447/// This macro is intended to work in a similar way to
448/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
449///
450/// # Module macros
451///
452/// * [`assert_fn_err_eq_x`](macro@crate::assert_fn_err_eq_x)
453/// * [`assert_fn_err_eq_x`](macro@crate::assert_fn_err_eq_x)
454/// * [`debug_assert_fn_err_eq_x`](macro@crate::debug_assert_fn_err_eq_x)
455///
456#[macro_export]
457macro_rules! debug_assert_fn_err_eq_x {
458 ($($arg:tt)*) => {
459 if $crate::cfg!(debug_assertions) {
460 $crate::assert_fn_err_eq_x!($($arg)*);
461 }
462 };
463}