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