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