testutils/
assert_err_eq.rs

1// Taken and modified from https://github.com/svartalf/rust-claim/commit/815fb7da4965117917406941bc4f3a6b076d1404
2/// Asserts that expression returns [`Err(E)`] variant
3/// and its value of `E` type equals to the right expression.
4///
5/// ## Uses
6///
7/// Assertions are always checked in both debug and release builds, and cannot be disabled.
8/// See [`debug_assert_err_eq!`] for assertions that are not enabled in release builds by default.
9///
10/// ## Custom messages
11///
12/// This macro has a second form, where a custom panic message can be provided
13/// with or without arguments for formatting. See [`std::fmt`] for syntax for this form.
14///
15/// ## Examples
16///
17/// ```rust
18/// use testutils::assert_err_eq;
19/// # fn main() {
20/// let res: Result<(), i32> = Err(1);
21///
22/// assert_err_eq!(res, 1);
23///
24/// // With custom messages
25/// assert_err_eq!(res, 1, "Everything is good with {:?}", res);
26/// # }
27/// ```
28///
29/// Value of `E` type from `Err(E)` will be returned from the macro call:
30///
31/// ```rust
32/// use testutils::assert_err_eq;
33/// # fn main() {
34/// let res: Result<(), i32> = Err(1);
35///
36/// let value = assert_err_eq!(res, 1);
37/// assert_eq!(value, 1);
38/// # }
39/// ```
40///
41/// `Ok(..)` variant will cause panic:
42///
43/// ```rust,should_panic
44/// use testutils::assert_err_eq;
45/// # fn main() {
46/// let res: Result<(), i32> = Ok(());
47///
48/// assert_err_eq!(res, 1); // Will panic
49/// //
50/// # }
51/// ```
52///
53/// [`Err(E)`]: https://doc.rust-lang.org/core/result/enum.Result.html#variant.Err
54/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
55/// [`debug_assert_err_eq!`]: ./macro.debug_assert_err_eq.html
56#[macro_export]
57macro_rules! assert_err_eq {
58    ($cond:expr, $expected:expr,) => {
59        $crate::assert_err_eq!($cond, $expected);
60    };
61    ($cond:expr, $expected:expr) => {
62        match $cond {
63            Err(t) => {
64                assert_eq!(t, $expected);
65                t
66            },
67            ok @ Ok(..) => {
68                panic!("assertion failed, expected Err(..), got {ok:?}");
69            }
70        }
71    };
72    ($cond:expr, $expected:expr, $($arg:tt)+) => {
73        match $cond {
74            Err(t) => {
75                assert_eq!(t, $expected);
76                t
77            },
78            ok @ Ok(..) => {
79                panic!("assertion failed, expected Err(..), got {ok:?}: {}", format_args!($($arg)+));
80            }
81        }
82    };
83}
84
85/// Asserts that expression returns [`Err(E)`] variant in runtime.
86///
87/// Like [`assert_err_eq!`], this macro also has a second version,
88/// where a custom panic message can be provided.
89///
90/// ## Uses
91///
92/// See [`debug_assert!`] documentation for possible use cases.
93/// The same applies to this macro.
94///
95/// [`Err(E)`]: https://doc.rust-lang.org/core/result/enum.Result.html#variant.Err
96/// [`debug_assert!`]: https://doc.rust-lang.org/std/macro.debug_assert.html
97/// [`assert_err_eq!`]: ./macro.assert_err_eq.html
98#[macro_export]
99macro_rules! debug_assert_err_eq {
100    ($($arg:tt)*) => (if cfg!(debug_assertions) { $crate::assert_err_eq!($($arg)*); })
101}