1#[macro_export]
19macro_rules! assure_io_ne {
20 ($left:expr, $right:expr $(,)?) => ({
21 match (&$left, &$right) {
22 (left_val, right_val) => {
23 if (left_val != right_val) {
24 Ok($left)
25 } else {
26 Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("assure_io_ne left:{:?} right:{:?}", left_val, right_val)))
27 }
28 }
29 }
30 });
31 ($left:expr, $right:expr, $($arg:tt)+) => ({
32 match (&($left), &($right)) {
33 (left_val, right_val) => {
34 if (left_val != right_val) {
35 Ok($left)
36 } else {
37 Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, $($arg)+))
38 }
39 }
40 }
41 });
42}
43
44#[cfg(test)]
45mod tests {
46
47 #[test]
48 fn test_assure_io_ne_x_i32_arity_2_return_ok() {
49 let a = 1;
50 let b = 2;
51 let x = assure_io_ne!(a, b);
52 assert!(x.is_ok());
53 assert_eq!(
54 x.unwrap(),
55 a
56 );
57 }
58
59 #[test]
60 fn test_assure_io_ne_x_i32_arity_2_return_err() {
61 let a = 1;
62 let b = 1;
63 let x = assure_io_ne!(a, b);
64 assert!(x.is_err());
65 assert_eq!(
66 x.unwrap_err().get_ref().unwrap().to_string(),
67 "assure_io_ne left:1 right:1"
68 );
69 }
70
71 #[test]
72 fn test_assure_io_ne_x_i32_arity_3_return_ok() {
73 let a = 1;
74 let b = 2;
75 let x = assure_io_ne!(a, b, "message");
76 assert!(x.is_ok());
77 assert_eq!(
78 x.unwrap(),
79 a
80 );
81 }
82
83 #[test]
84 fn test_assure_io_ne_x_i32_arity_3_return_err() {
85 let a = 1;
86 let b = 1;
87 let x = assure_io_ne!(a, b, "message");
88 assert!(x.is_err());
89 assert_eq!(
90 x.unwrap_err().get_ref().unwrap().to_string(),
91 "message"
92 );
93 }
94
95 #[test]
96 fn test_assure_io_ne_x_str_arity_2_return_ok() {
97 let a = "aa";
98 let b = "bb";
99 let x = assure_io_ne!(a, b);
100 assert!(x.is_ok());
101 assert_eq!(
102 x.unwrap(),
103 a
104 );
105 }
106
107 #[test]
108 fn test_assure_io_ne_x_str_arity_2_return_err() {
109 let a = "aa";
110 let b = "aa";
111 let x = assure_io_ne!(a, b);
112 assert!(x.is_err());
113 assert_eq!(
114 x.unwrap_err().get_ref().unwrap().to_string(),
115 "assure_io_ne left:\"aa\" right:\"aa\""
116 );
117 }
118
119 #[test]
120 fn test_assure_io_ne_x_str_arity_3_return_ok() {
121 let a = "aa";
122 let b = "bb";
123 let x = assure_io_ne!(a, b, "message");
124 assert!(x.is_ok());
125 assert_eq!(
126 x.unwrap(),
127 a
128 );
129 }
130
131 #[test]
132 fn test_assure_io_ne_x_str_arity_3_return_err() {
133 let a = "aa";
134 let b = "aa";
135 let x = assure_io_ne!(a, b, "message");
136 assert!(x.is_err());
137 assert_eq!(
138 x.unwrap_err().get_ref().unwrap().to_string(),
139 "message"
140 );
141 }
142
143}