Skip to content

Commit 43a95ea

Browse files
Googlercopybara-github
authored andcommitted
Add support for sequences (ordered and unordered) for assert_that.
This adds the other patterns necessary for `assert_that!` to work with ordered and unordered sequences. PiperOrigin-RevId: 695781813
1 parent 0d71c7a commit 43a95ea

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

googletest/src/assertions.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,59 @@ macro_rules! expect_near {
13081308
/// equivalent to `ASSERT_THAT`, use [`verify_that!`] with the `?` operator.
13091309
#[macro_export]
13101310
macro_rules! assert_that {
1311+
// specialized to sequence:
1312+
($actual:expr, [ $($expected:expr),* ] $(,)?) => {
1313+
match $crate::verify_that!($actual, [ $($expected),* ]) {
1314+
Ok(_) => {}
1315+
Err(e) => {
1316+
// The extra newline before the assertion failure message makes the failure a
1317+
// bit easier to read when there's some generic boilerplate from the panic.
1318+
panic!("\n{}", e);
1319+
}
1320+
}
1321+
};
1322+
1323+
// specialized to unordered sequence
1324+
($actual:expr, { $($expected:expr),* } $(,)?) => {
1325+
match $crate::verify_that!($actual, { $($expected),* }) {
1326+
Ok(_) => {}
1327+
Err(e) => {
1328+
// The extra newline before the assertion failure message makes the failure a
1329+
// bit easier to read when there's some generic boilerplate from the panic.
1330+
panic!("\n{}", e);
1331+
}
1332+
}
1333+
};
1334+
1335+
// w/ format args, specialized to sequence:
1336+
($actual:expr, [ $($expected:expr),* ], $($format_args:expr),* $(,)?) => {
1337+
match $crate::verify_that!($actual, [ $($expected),* ])
1338+
.with_failure_message(|| format!($($format_args),*))
1339+
{
1340+
Ok(_) => {}
1341+
Err(e) => {
1342+
// The extra newline before the assertion failure message makes the failure a
1343+
// bit easier to read when there's some generic boilerplate from the panic.
1344+
panic!("\n{}", e);
1345+
}
1346+
}
1347+
};
1348+
1349+
// w/ format args, specialized to unordered sequence:
1350+
($actual:expr, { $($expected:expr),* }, $($format_args:expr),* $(,)?) => {
1351+
match $crate::verify_that!($actual, { $($expected),* })
1352+
.with_failure_message(|| format!($($format_args),*))
1353+
{
1354+
Ok(_) => {}
1355+
Err(e) => {
1356+
// The extra newline before the assertion failure message makes the failure a
1357+
// bit easier to read when there's some generic boilerplate from the panic.
1358+
panic!("\n{}", e);
1359+
}
1360+
}
1361+
};
1362+
1363+
// general case:
13111364
($actual:expr, $expected:expr $(,)?) => {
13121365
match $crate::verify_that!($actual, $expected) {
13131366
Ok(_) => {}
@@ -1319,6 +1372,7 @@ macro_rules! assert_that {
13191372
}
13201373
};
13211374

1375+
// w/ format args, general case:
13221376
($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => {
13231377
match $crate::verify_that!($actual, $expected)
13241378
.with_failure_message(|| format!($($format_args),*))

integration_tests/src/integration_tests.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@ mod tests {
7171
assert_that!(value, eq(2), "A custom error message",);
7272
}
7373

74+
#[gtest]
75+
fn assert_that_supports_element_sequences() {
76+
assert_that!(vec![1, 2], [eq(&1), eq(&2)]);
77+
}
78+
79+
#[gtest]
80+
fn assert_that_supports_unordered_element_sequences() {
81+
assert_that!(vec![1, 2], {eq(&2), eq(&1)});
82+
}
83+
84+
#[gtest]
85+
fn assert_that_supports_ordered_element_sequences_with_format_string() {
86+
assert_that!(vec![1, 2], [eq(&1), eq(&2)], "A custom error message");
87+
}
88+
89+
#[gtest]
90+
fn assert_that_supports_unordered_element_sequences_with_format_string() {
91+
assert_that!(vec![1, 2], {eq(&2), eq(&1)}, "A custom error message");
92+
}
93+
7494
#[gtest]
7595
fn should_pass_with_expect_that() -> Result<()> {
7696
let value = 2;

0 commit comments

Comments
 (0)