tanu_core/
assertion.rs

1//! tanu assertion macros.
2//!
3//! Those assertions are borrowed from `pretty_assetions` crate and made
4//! with small modification which throws `Result<_, Error>` instead of
5//! panic. The reason for providing own assertion macro is throwing an
6//! error allows tanu to be able to print colorized backtrace powered
7//! by `eyre`.
8
9/// Custom error type used by assertion macros. This `Error` type is
10/// designed to be propagated from test functions using the assertion macros.
11/// tanu wraps the error with `eyre::Report` for enhanced error reporting,
12/// including the ability to generate and display colorized backtraces.
13#[derive(thiserror::Error, Debug)]
14pub enum Error {
15    #[error("{0}")]
16    StrEq(String),
17    #[error("{0}")]
18    Eq(String),
19    #[error("{0}")]
20    Ne(String),
21}
22
23#[macro_export]
24macro_rules! assert {
25    ($cond:expr) => {
26        if !$cond {
27            tanu::eyre::bail!("assertion failed: {}", stringify!($cond));
28        }
29    };
30    ($cond:expr, $($arg:tt)+) => {
31        if !$cond {
32            tanu::eyre::bail!($($arg)+);
33        }
34    };
35}
36
37#[macro_export]
38macro_rules! assert_str_eq {
39    ($left:expr, $right:expr$(,)?) => ({
40		tanu::pretty_assertions::assert_str_eq!(@ $left, $right, "", "");
41    });
42    ($left:expr, $right:expr, $($arg:tt)*) => ({
43        tanu::pretty_assertions::assert_str_eq!(@ $left, $right, ": ", $($arg)+);
44    });
45    (@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)*) => ({
46        match (&($left), &($right)) {
47            (left_val, right_val) => {
48                if !(*left_val == *right_val) {
49                    Err(Error::StrEq(format!("assertion failed: `(left == right)`{}{}\
50                       \n\
51                       \n{}\
52                       \n",
53                       $maybe_colon,
54                       format_args!($($arg)*),
55                       tanu::pretty_assertions::StrComparison::new(left_val, right_val)
56                    )))?;
57                }
58            }
59        }
60    });
61}
62
63#[macro_export]
64macro_rules! assert_eq {
65    ($left:expr, $right:expr$(,)?) => ({
66        $crate::assert_eq!(@ $left, $right, "", "");
67    });
68    ($left:expr, $right:expr, $($arg:tt)*) => ({
69        $crate::assert_eq!(@ $left, $right, ": ", $($arg)+);
70    });
71    (@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)*) => ({
72        match (&($left), &($right)) {
73            (left_val, right_val) => {
74                if !(*left_val == *right_val) {
75                    Err(tanu::assertion::Error::Eq(format!("assertion failed: `(left == right)`{}{}\
76                       \n\
77                       \n{}\
78                       \n",
79                       $maybe_colon,
80                       format_args!($($arg)*),
81                       tanu::pretty_assertions::Comparison::new(left_val, right_val)
82                    )))?;
83                }
84            }
85        }
86    });
87}
88
89#[macro_export]
90macro_rules! assert_ne {
91    ($left:expr, $right:expr$(,)?) => ({
92        tanu::pretty_assertions::assert_ne!(@ $left, $right, "", "");
93    });
94    ($left:expr, $right:expr, $($arg:tt)+) => ({
95        tanu::pretty_assertions::assert_ne!(@ $left, $right, ": ", $($arg)+);
96    });
97    (@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)+) => ({
98        match (&($left), &($right)) {
99            (left_val, right_val) => {
100                if *left_val == *right_val {
101                    Err(Error::Ne(format!("assertion failed: `(left != right)`{}{}\
102                        \n\
103                        \nBoth sides:\
104                        \n{:#?}\
105                        \n\
106                        \n",
107                        $maybe_colon,
108                        format_args!($($arg)+),
109                        left_val
110                    )))?;
111                }
112            }
113        }
114    });
115}