1 // Copyright 2022 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // TODO(danakj): Reuse code for comparison macros with an expect_op!() macro? 6 7 /// Internal helper to log an expectation failure. Other expect_* macros invoke 8 /// it with their standard expectation message, plus optionally a user-provided 9 /// custom string. 10 /// 11 /// Both the the expectation message and the user-provided message are format 12 /// strings with arguments. To disambiguate between them, the expectation 13 /// message is wrapped in extra parentheses. 14 #[macro_export] 15 macro_rules! internal_add_expectation_failure { 16 // Rule that both the below are forwarded to. 17 (@imp $fmt:literal, $($arg:tt)+) => { 18 $crate::__private::add_failure_at( 19 file!(), 20 line!(), 21 &format!($fmt, $($arg)+), 22 ) 23 }; 24 25 // Add a failure with the standard message. 26 (($expectation:literal, $($e:tt)+)) => { 27 $crate::internal_add_expectation_failure!(@imp $expectation, $($e)+) 28 }; 29 30 // Add a failure with the standard message plus an additional message. 31 (($expectation:literal, $($e:tt)+), $($arg:tt)+) => { 32 $crate::internal_add_expectation_failure!(@imp 33 "{}\n\n{}", 34 format_args!($expectation, $($e)+), 35 format_args!($($arg)+), 36 ) 37 }; 38 } 39 40 /// Evaluates the given expression. If false, a failure is reported to Gtest. 41 /// 42 /// # Examples 43 /// ``` 44 /// expect_true(check_the_status_is_true()); 45 /// ``` 46 #[macro_export] 47 macro_rules! expect_true { 48 ($e:expr $(, $($arg:tt)*)?) => { 49 match &$e { 50 val => { 51 if !(*val) { 52 $crate::internal_add_expectation_failure!( 53 ( 54 "Expected: {} is true\nActual: {} is {:?}", 55 stringify!($e), 56 stringify!($e), 57 *val 58 ) 59 $(, $($arg)*)? 60 ) 61 } 62 } 63 } 64 }; 65 } 66 67 /// Evaluates the given expression. If true, a failure is reported to Gtest. 68 /// 69 /// # Examples 70 /// ``` 71 /// expect_false(check_the_status_is_false()); 72 /// ``` 73 #[macro_export] 74 macro_rules! expect_false { 75 ($e:expr $(, $($arg:tt)*)?) => { 76 match &$e { 77 val => { 78 if *val { 79 $crate::internal_add_expectation_failure!( 80 ( 81 "Expected: {} is false\nActual: {} is {:?}", 82 stringify!($e), 83 stringify!($e), 84 *val 85 ) 86 $(, $($arg)*)? 87 ) 88 } 89 } 90 } 91 }; 92 } 93 94 /// Evaluates and compares the two given expressions. If not equal, a failure is 95 /// reported to Gtest. The expressions must evaluate to the same type, which 96 /// must be PartialEq. 97 /// 98 /// # Examples 99 /// ``` 100 /// expect_eq(1 + 1, 2); 101 /// ``` 102 #[macro_export] 103 macro_rules! expect_eq { 104 ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => { 105 match (&$e1, &$e2) { 106 (val1, val2) => { 107 if !(*val1 == *val2) { 108 $crate::internal_add_expectation_failure!( 109 ( 110 "Expected: {} == {}\nActual: {:?} vs {:?}", 111 stringify!($e1), 112 stringify!($e2), 113 *val1, 114 *val2 115 ) 116 $(, $($arg)*)? 117 ) 118 } 119 } 120 } 121 }; 122 } 123 124 /// Evaluates and compares the two given expressions. If equal, a failure is 125 /// reported to Gtest. The expressions must evaluate to the same type, which 126 /// must be PartialEq. 127 /// 128 /// # Examples 129 /// ``` 130 /// expect_ne(1 + 1, 3); 131 /// ``` 132 #[macro_export] 133 macro_rules! expect_ne { 134 ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => { 135 match (&$e1, &$e2) { 136 (val1, val2) => { 137 if !(*val1 != *val2) { 138 $crate::internal_add_expectation_failure!( 139 ( 140 "Expected: {} != {}\nActual: {:?} vs {:?}", 141 stringify!($e1), 142 stringify!($e2), 143 *val1, 144 *val2 145 ) 146 $(, $($arg)*)? 147 ) 148 } 149 } 150 } 151 }; 152 } 153 154 /// Evaluates and compares the two given expressions. If the first is not 155 /// greater than the second, a failure is reported to Gtest. The expressions 156 /// must evaluate to the same type, which must be PartialOrd. If a PartialOrd 157 /// comparison fails, the result is false. 158 /// 159 /// # Examples 160 /// ``` 161 /// expect_gt(1 + 1, 1); 162 /// ``` 163 #[macro_export] 164 macro_rules! expect_gt { 165 ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => { 166 match (&$e1, &$e2) { 167 (val1, val2) => { 168 if !(*val1 > *val2) { 169 $crate::internal_add_expectation_failure!( 170 ( 171 "Expected: {} > {}\nActual: {:?} vs {:?}", 172 stringify!($e1), 173 stringify!($e2), 174 *val1, 175 *val2 176 ) 177 $(, $($arg)*)? 178 ) 179 } 180 } 181 } 182 }; 183 } 184 185 /// Evaluates and compares the two given expressions. If the first is not less 186 /// than the second, a failure is reported to Gtest. The expressions must 187 /// evaluate to the same type, which must be PartialOrd. If a PartialOrd 188 /// comparison fails, the result is false. 189 /// 190 /// # Examples 191 /// ``` 192 /// expect_lt(1 + 1, 1 + 2); 193 /// ``` 194 #[macro_export] 195 macro_rules! expect_lt { 196 ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => { 197 match (&$e1, &$e2) { 198 (val1, val2) => { 199 if !(*val1 < *val2) { 200 $crate::internal_add_expectation_failure!( 201 ( 202 "Expected: {} < {}\nActual: {:?} vs {:?}", 203 stringify!($e1), 204 stringify!($e2), 205 *val1, 206 *val2 207 ) 208 $(, $($arg)*)? 209 ) 210 } 211 } 212 } 213 }; 214 } 215 216 /// Evaluates and compares the two given expressions. If the first is not 217 /// greater than or equal to the second, a failure is reported to Gtest. The 218 /// expressions must evaluate to the same type, which must be PartialOrd. If a 219 /// PartialOrd comparison fails, the result is false. 220 /// 221 /// # Examples 222 /// ``` 223 /// expect_ge(1 + 1, 2); 224 /// ``` 225 #[macro_export] 226 macro_rules! expect_ge { 227 ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => { 228 match (&$e1, &$e2) { 229 (val1, val2) => { 230 if !(*val1 >= *val2) { 231 $crate::internal_add_expectation_failure!( 232 ( 233 "Expected: {} >= {}\nActual: {:?} vs {:?}", 234 stringify!($e1), 235 stringify!($e2), 236 *val1, 237 *val2 238 ) $(, $($arg)*)? 239 ) 240 } 241 } 242 } 243 }; 244 } 245 246 /// Evaluates and compares the two given expressions. If the first is not less 247 /// than or equal to the second, a failure is reported to Gtest. The expressions 248 /// must evaluate to the same type, /which must be PartialOrd. If a PartialOrd 249 /// comparison fails, the result is false. 250 /// 251 /// # Examples 252 /// ``` 253 /// expect_le(2, 1 + 1); 254 /// ``` 255 #[macro_export] 256 macro_rules! expect_le { 257 ($e1:expr, $e2:expr $(, $($arg:tt)*)?) => { 258 match (&$e1, &$e2) { 259 (val1, val2) => { 260 if !(*val1 <= *val2) { 261 $crate::internal_add_expectation_failure!( 262 ( 263 "Expected: {} <= {}\nActual: {:?} vs {:?}", 264 stringify!($e1), 265 stringify!($e2), 266 *val1, 267 *val2 268 ) $(, $($arg)*)? 269 ) 270 } 271 } 272 } 273 }; 274 } 275 276 // TODO(danakj): There's a bunch more useful macros to write, even ignoring 277 // gmock: 278 // - EXPECT_NONFATAL_FAILURE 279 // - SCOPED_TRACE 280 // - EXPECT_DEATH 281 // - FAIL (fatal failure, would this work?) 282 // - ADD_FAILURE 283 // - SUCCEED 284 // - EXPECT_PANIC (catch_unwind() with an error if no panic, but this depends on 285 // us using a stdlib with -Cpanic=unwind in tests, currently we use 286 // -Cpanic=abort.) 287 // - EXPECT_NO_PANIC (Like above, depende on -Cpanic=unwind, or else all panics 288 // just abort the process.) 289 // - EXPECT_FLOAT_EQ (Comparison for equality within a small range.) 290 // - EXPECT_NEAR (Comparison for equality within a user-specified range.) 291