assert_hex/
lib.rs

1//! # assert_hex
2//! display expression using `{:#x?}` format when false assertion causes `panic!()`.
3//!
4//! # why
5//! Writing and testing protocol level libraries requires many tests to be written
6//! with respect to byte level protocol sections in hex. This library simplifies the process
7//! of viewing the differences between these types when tests fail by displaying by using the
8//! `{:#x?}` representation.
9//!
10//! # usage
11//! Replace `assert_eq` or `assert_ne` with `assert_eq_hex` or `assert_ne_hex`
12//! respectively.
13
14/// Asserts that two expressions are equal to each other
15///
16/// On panic, this macro will print values of the expressions in their
17/// `{:#x?}` (hexadecimal) representation
18#[macro_export]
19macro_rules! assert_eq_hex {
20    ($left:expr, $right:expr $(,)?) => ({
21        match (&$left, &$right) {
22            (left_val, right_val) => {
23                if !(*left_val == *right_val) {
24                    // The reborrows below are intentional. Without them, the stack slot for the
25                    // borrow is initialized even before the values are compared, leading to a
26                    // noticeable slow down.
27                    panic!(r#"assertion `left == right` failed
28  left: {:#x?}
29 right: {:#x?}"#, &*left_val, &*right_val)
30                }
31            }
32        }
33    });
34    ($left:expr, $right:expr, $($arg:tt)+) => ({
35        match (&($left), &($right)) {
36            (left_val, right_val) => {
37                if !(*left_val == *right_val) {
38                    // The reborrows below are intentional. Without them, the stack slot for the
39                    // borrow is initialized even before the values are compared, leading to a
40                    // noticeable slow down.
41                    panic!(r#"assertion `left == right` failed: {}
42  left: {:#x?}
43 right: {:#x?}"#, format_args!($($arg)+), &*left_val, &*right_val)
44                }
45            }
46        }
47    });
48}
49
50/// Asserts that two expressions are not equal to each other
51///
52/// On panic, this macro will print values of the expressions in their
53/// `{:#x?}` (hexadecimal) representation
54#[macro_export]
55macro_rules! assert_ne_hex {
56    ($left:expr, $right:expr $(,)?) => ({
57        match (&$left, &$right) {
58            (left_val, right_val) => {
59                if *left_val == *right_val {
60                    // The reborrows below are intentional. Without them, the stack slot for the
61                    // borrow is initialized even before the values are compared, leading to a
62                    // noticeable slow down.
63                    panic!(r#"assertion `left != right` failed
64  left: {:#x?}
65 right: {:#x?}"#, &*left_val, &*right_val)
66                }
67            }
68        }
69    });
70    ($left:expr, $right:expr, $($arg:tt)+) => ({
71        match (&($left), &($right)) {
72            (left_val, right_val) => {
73                if *left_val == *right_val {
74                    // The reborrows below are intentional. Without them, the stack slot for the
75                    // borrow is initialized even before the values are compared, leading to a
76                    // noticeable slow down.
77                    panic!(r#"assertion `left != right` failed: {}
78  left: {:#x?}
79 right: {:#x?}"#, format_args!($($arg)+), &*left_val, &*right_val)
80                }
81            }
82        }
83    });
84}
85
86#[cfg(test)]
87mod tests {
88    use crate::{assert_eq_hex, assert_ne_hex};
89
90    #[test]
91    #[should_panic(expected = r#"assertion `left == right` failed
92  left: 0x50
93 right: 0x46"#)]
94    fn test_eq_0() {
95        assert_eq_hex!(0x50, 0x46);
96    }
97
98    #[test]
99    #[should_panic(expected = r#"assertion `left == right` failed
100  left: 0xff
101 right: 0x46"#)]
102    fn test_eq_1() {
103        assert_eq_hex!(0xff, 0x46);
104    }
105
106    #[test]
107    #[should_panic(expected = r#"assertion `left == right` failed
108  left: 0xff
109 right: 0x46"#)]
110    fn test_eq_2() {
111        assert_eq_hex!(0xff, 0x46);
112    }
113
114    #[test]
115    #[should_panic(expected = r#"assertion `left == right` failed
116  left: [
117    0x0,
118    0x1,
119    0x2,
120]
121 right: [
122    0x46,
123    0x50,
124    0x40,
125]"#)]
126    fn test_eq_3() {
127        assert_eq_hex!(vec![0x00, 0x01, 0x02], vec![0x46, 0x50, 0x40]);
128    }
129
130    #[test]
131    #[should_panic(expected = r#"assertion `left == right` failed: yikes
132  left: 0xff
133 right: 0x0"#)]
134    fn test_eq_more() {
135        assert_eq_hex!(0xff, 0x00, "yikes");
136    }
137
138    #[test]
139    #[should_panic(expected = r#"assertion `left != right` failed
140  left: 0x50
141 right: 0x50"#)]
142    fn test_ne_0() {
143        assert_ne_hex!(0x50, 0x50);
144    }
145
146    #[test]
147    #[should_panic(expected = r#"assertion `left != right` failed
148  left: 0xff
149 right: 0xff"#)]
150    fn test_ne_1() {
151        assert_ne_hex!(0xff, 0xff);
152    }
153
154    #[test]
155    #[should_panic(expected = r#"assertion `left != right` failed: yikes
156  left: 0xff
157 right: 0xff"#)]
158    fn test_ne_more() {
159        assert_ne_hex!(0xff, 0xff, "yikes");
160    }
161}