assertables/assert_fn/
assert_fn_ne_x.rs

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