Skip to content

Commit e1abbb8

Browse files
Googlercopybara-github
authored andcommitted
Support sequences and sets in expect_that!.
This adds additional patterns to match the sequences and sets in `expect_that`, so that the macro can delegate those cases to `verify_that!`, which knows how to work with those arguments. PiperOrigin-RevId: 694604266
1 parent c1b2cb4 commit e1abbb8

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

googletest/src/assertions.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,12 +1361,43 @@ macro_rules! assert_pred {
13611361
/// ```
13621362
#[macro_export]
13631363
macro_rules! expect_that {
1364+
// specialized to sequence:
1365+
($actual:expr, [$($expected:expr),*] $(,)?) => {{
1366+
use $crate::GoogleTestSupport as _;
1367+
$crate::verify_that!($actual, [$($expected),*]).and_log_failure();
1368+
}};
1369+
1370+
// specialized to unordered sequence:
1371+
($actual:expr, {$($expected:expr),*} $(,)?) => {{
1372+
use $crate::GoogleTestSupport as _;
1373+
$crate::verify_that!($actual, {$($expected),*}).and_log_failure();
1374+
}};
1375+
1376+
// general case:
13641377
($actual:expr, $expected:expr $(,)?) => {{
13651378
use $crate::GoogleTestSupport as _;
13661379
$crate::verify_that!($actual, $expected).and_log_failure();
13671380
}};
13681381

1382+
// w/ format args, specialized to sequence:
1383+
($actual:expr, [$($expected:expr),*], $($format_args:expr),* $(,)?) => {
1384+
use $crate::GoogleTestSupport as _;
1385+
$crate::verify_that!($actual, [$($expected),*])
1386+
.with_failure_message(|| format!($($format_args),*))
1387+
.and_log_failure()
1388+
};
1389+
1390+
// w/ format args, specialized to unordered sequence:
1391+
($actual:expr, {$($expected:expr),*}, $($format_args:expr),* $(,)?) => {
1392+
use $crate::GoogleTestSupport as _;
1393+
$crate::verify_that!($actual, {$($expected),*})
1394+
.with_failure_message(|| format!($($format_args),*))
1395+
.and_log_failure()
1396+
};
1397+
1398+
// w/ format args, general case:
13691399
($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => {
1400+
use $crate::GoogleTestSupport as _;
13701401
$crate::verify_that!($actual, $expected)
13711402
.with_failure_message(|| format!($($format_args),*))
13721403
.and_log_failure()

integration_tests/src/integration_tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ mod tests {
9696
expect_that!(value, eq(2), "A custom error message",);
9797
}
9898

99+
#[gtest]
100+
fn expect_that_with_omitted_elements_are() {
101+
expect_that!(vec![1, 2], [eq(&1), eq(&2)]);
102+
}
103+
104+
#[gtest]
105+
fn expect_that_with_omitted_elements_supports_custom_error_msg() {
106+
expect_that!(vec![1, 2], [eq(&1), eq(&2)], "A custom error message");
107+
}
108+
99109
#[gtest]
100110
fn should_fail_on_assertion_failure() -> Result<()> {
101111
let status = run_external_process("simple_assertion_failure").status()?;

0 commit comments

Comments
 (0)