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        $crate::assert!(@ $cond, "", "");
27    };
28    ($cond:expr, $($arg:tt)+) => {
29        $crate::assert!(@ $cond, ":", $($arg)+);
30    };
31    (@ $cond:expr, $maybe_colon:expr, $($arg:tt)*) => {
32        if !$cond {
33            let __message = format!("assertion failed: {}{}{}", stringify!($cond), $maybe_colon, format_args!($($arg)*));
34            let __check = tanu::runner::Check::error(&__message);
35            tanu::runner::publish(tanu::runner::EventBody::Check(Box::new(__check)))?;
36            tanu::eyre::bail!(__message);
37        } else {
38            let __message = format!("assertion succeeded: {}{}{}", stringify!($cond), $maybe_colon, format_args!($($arg)*));
39            let __check = tanu::runner::Check::success(&__message);
40            tanu::runner::publish(tanu::runner::EventBody::Check(Box::new(__check)))?;
41        }
42    };
43}
44
45#[macro_export]
46macro_rules! assert_str_eq {
47    ($left:expr, $right:expr$(,)?) => ({
48        $crate::assert_str_eq!(@ $left, $right, "", "");
49    });
50    ($left:expr, $right:expr, $($arg:tt)*) => ({
51        $crate::assert_str_eq!(@ $left, $right, ": ", $($arg)+);
52    });
53    (@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)*) => ({
54        match (&($left), &($right)) {
55            (left_val, right_val) => {
56                if !(*left_val == *right_val) {
57                    let __message = format!("assertion failed: `(left == right)`{}{}\
58                       \n\
59                       \n{}\
60                       \n",
61                       $maybe_colon,
62                       format_args!($($arg)*),
63                       tanu::pretty_assertions::StrComparison::new(left_val, right_val)
64                    );
65                    let __check = tanu::runner::Check::error(&__message);
66                    tanu::runner::publish(tanu::runner::EventBody::Check(Box::new(__check)))?;
67                    Err(Error::StrEq(__message))?;
68                } else {
69                    let __message = format!("assertion succeeded: `(left == right)`{}{}\
70                       \n\
71                       \n{}\
72                       \n",
73                       $maybe_colon,
74                       format_args!($($arg)*),
75                       tanu::pretty_assertions::StrComparison::new(left_val, right_val)
76                    );
77                    let __check = tanu::runner::Check::success(&__message);
78                    tanu::runner::publish(tanu::runner::EventBody::Check(Box::new(__check)))?;
79                }
80            }
81        }
82    });
83}
84
85#[macro_export]
86macro_rules! assert_eq {
87    ($left:expr, $right:expr$(,)?) => ({
88        $crate::assert_eq!(@ $left, $right, "", "");
89    });
90    ($left:expr, $right:expr, $($arg:tt)*) => ({
91        $crate::assert_eq!(@ $left, $right, ": ", $($arg)+);
92    });
93    (@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)*) => ({
94        match (&($left), &($right)) {
95            (left_val, right_val) => {
96                if !(*left_val == *right_val) {
97                    let __message = format!("assertion failed: `(left == right)`{}{}\
98                       \n\
99                       \n{}\
100                       \n",
101                       $maybe_colon,
102                       format_args!($($arg)*),
103                       tanu::pretty_assertions::Comparison::new(left_val, right_val)
104                    );
105                    let __check = tanu::runner::Check::error(&__message);
106                    tanu::runner::publish(tanu::runner::EventBody::Check(Box::new(__check)))?;
107                    Err(tanu::assertion::Error::Eq(__message))?;
108                } else {
109                    let __message = format!("assertion succeeded: `(left == right)`{}{}\
110                       \n\
111                       \n{}\
112                       \n",
113                       $maybe_colon,
114                       format_args!($($arg)*),
115                       tanu::pretty_assertions::Comparison::new(left_val, right_val)
116                    );
117                    let __check = tanu::runner::Check::success(&__message);
118                    tanu::runner::publish(tanu::runner::EventBody::Check(Box::new(__check)))?;
119                }
120            }
121        }
122    });
123}
124
125#[macro_export]
126macro_rules! assert_ne {
127    ($left:expr, $right:expr$(,)?) => ({
128        $crate::assert_ne!(@ $left, $right, "", "");
129    });
130    ($left:expr, $right:expr, $($arg:tt)+) => ({
131        $crate::assert_ne!(@ $left, $right, ": ", $($arg)+);
132    });
133    (@ $left:expr, $right:expr, $maybe_colon:expr, $($arg:tt)+) => ({
134        match (&($left), &($right)) {
135            (left_val, right_val) => {
136                if *left_val == *right_val {
137                    let __message = format!("assertion failed: `(left != right)`{}{}\
138                        \n\
139                        \nBoth sides:\
140                        \n{:#?}\
141                        \n\
142                        \n",
143                        $maybe_colon,
144                        format_args!($($arg)+),
145                        left_val
146                    );
147                    let __check = tanu::runner::Check::error(&__message);
148                    tanu::runner::publish(tanu::runner::EventBody::Check(Box::new(__check)))?;
149                    Err(Error::Ne(__message))?;
150                } else {
151                    let __message = format!("assertion succeeded: `(left != right)`{}{}\
152                        \n\
153                        \nBoth sides:\
154                        \n{:#?}\
155                        \n\
156                        \n",
157                        $maybe_colon,
158                        format_args!($($arg)+),
159                        left_val
160                    );
161                    let __check = tanu::runner::Check::success(&__message);
162                    tanu::runner::publish(tanu::runner::EventBody::Check(Box::new(__check)))?;
163                }
164            }
165        }
166    });
167}