Skip to content

Commit

Permalink
Support sequences and sets in expect_that!.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Googler authored and copybara-github committed Nov 8, 2024
1 parent c1b2cb4 commit e1abbb8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions googletest/src/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,12 +1361,43 @@ macro_rules! assert_pred {
/// ```
#[macro_export]
macro_rules! expect_that {
// specialized to sequence:
($actual:expr, [$($expected:expr),*] $(,)?) => {{
use $crate::GoogleTestSupport as _;
$crate::verify_that!($actual, [$($expected),*]).and_log_failure();
}};

// specialized to unordered sequence:
($actual:expr, {$($expected:expr),*} $(,)?) => {{
use $crate::GoogleTestSupport as _;
$crate::verify_that!($actual, {$($expected),*}).and_log_failure();
}};

// general case:
($actual:expr, $expected:expr $(,)?) => {{
use $crate::GoogleTestSupport as _;
$crate::verify_that!($actual, $expected).and_log_failure();
}};

// w/ format args, specialized to sequence:
($actual:expr, [$($expected:expr),*], $($format_args:expr),* $(,)?) => {
use $crate::GoogleTestSupport as _;
$crate::verify_that!($actual, [$($expected),*])
.with_failure_message(|| format!($($format_args),*))
.and_log_failure()
};

// w/ format args, specialized to unordered sequence:
($actual:expr, {$($expected:expr),*}, $($format_args:expr),* $(,)?) => {
use $crate::GoogleTestSupport as _;
$crate::verify_that!($actual, {$($expected),*})
.with_failure_message(|| format!($($format_args),*))
.and_log_failure()
};

// w/ format args, general case:
($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => {
use $crate::GoogleTestSupport as _;
$crate::verify_that!($actual, $expected)
.with_failure_message(|| format!($($format_args),*))
.and_log_failure()
Expand Down
10 changes: 10 additions & 0 deletions integration_tests/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ mod tests {
expect_that!(value, eq(2), "A custom error message",);
}

#[gtest]
fn expect_that_with_omitted_elements_are() {
expect_that!(vec![1, 2], [eq(&1), eq(&2)]);
}

#[gtest]
fn expect_that_with_omitted_elements_supports_custom_error_msg() {
expect_that!(vec![1, 2], [eq(&1), eq(&2)], "A custom error message");
}

#[gtest]
fn should_fail_on_assertion_failure() -> Result<()> {
let status = run_external_process("simple_assertion_failure").status()?;
Expand Down

0 comments on commit e1abbb8

Please sign in to comment.