stdont/
macros.rs

1/// Like [`assert_eq!()`], but the left argument has semantics of "expected value".
2///
3/// Panic message looks like this:
4///
5/// ```text
6/// assertion failed: `(expected == actual)`
7/// expected: 2
8///   actual: 3
9/// ```
10#[macro_export]
11macro_rules! expect_eq {
12    ($expected:expr, $actual:expr $(,)?) => {
13        assert!(
14            $expected == $actual,
15            "assertion failed: `(expected == actual)`\nexpected: {:?}\n  actual: {:?}",
16            $expected,
17            $actual
18        )
19    };
20}
21
22#[cfg(test)]
23#[allow(clippy::unwrap_used)] // it's test code, duh
24mod tests {
25    use crate::expect_eq;
26
27    #[test]
28    fn expect_eq() {
29        let panic = std::panic::catch_unwind(|| expect_eq!(2, 3)).unwrap_err();
30        let message = *panic.downcast::<String>().unwrap();
31        assert_eq!(
32            message,
33            "assertion failed: `(expected == actual)`\nexpected: 2\n  actual: 3",
34        );
35    }
36}