From faec46f84c52a3333fdc941679780e7e5890bd04 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Wed, 25 Jun 2025 02:16:36 -0700 Subject: [PATCH] Use UFCS for `with_message` assertions Before this CL, the assertion macro variants which accept messages would add the message via a call to the `with_message` method from the `GoogleTestSupport` trait. However, that trait may not be imported if users are not bringing in the full googletest prelude. This CL switches those method calls to UFCS and adds a utility method `and_log_failure_with_message` in order to make such calls more readable. PiperOrigin-RevId: 775593149 --- googletest/src/assertions.rs | 117 +++++++++++---------- googletest/src/lib.rs | 25 +++++ integration_tests/src/integration_tests.rs | 5 +- integration_tests/src/macro_hygiene.rs | 5 + 4 files changed, 95 insertions(+), 57 deletions(-) diff --git a/googletest/src/assertions.rs b/googletest/src/assertions.rs index e53037e1..ff3b1a8e 100644 --- a/googletest/src/assertions.rs +++ b/googletest/src/assertions.rs @@ -513,9 +513,9 @@ macro_rules! expect_true { $crate::GoogleTestSupport::and_log_failure($crate::verify_true!($condition)) }}; ($condition:expr, $($format_args:expr),* $(,)?) => {{ - $crate::GoogleTestSupport::and_log_failure($crate::verify_true!($condition) - .with_failure_message(|| format!($($format_args),*)) - ); + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_true!($condition), + || format!($($format_args),*)); }}; } pub use expect_true; @@ -602,9 +602,9 @@ macro_rules! expect_false { $crate::GoogleTestSupport::and_log_failure(($crate::verify_false!($condition))) }}; ($condition:expr, $($format_args:expr),* $(,)?) => {{ - $crate::GoogleTestSupport::and_log_failure($crate::verify_false!($condition) - .with_failure_message(|| format!($($format_args),*)) - ); + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_false!($condition), + || format!($($format_args),*)) }}; } pub use expect_false; @@ -731,25 +731,25 @@ macro_rules! expect_eq { $crate::GoogleTestSupport::and_log_failure($crate::verify_eq!($actual, [$($expected),*])); }}; ($actual:expr, [$($expected:expr),+ $(,)?], $($format_args:expr),* $(,)?) => {{ - $crate::GoogleTestSupport::and_log_failure($crate::verify_eq!($actual, [$($expected),*]) - .with_failure_message(|| format!($($format_args),*)) - ); + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_eq!($actual, [$($expected),*]), + || format!($($format_args),*)); }}; ($actual:expr, {$($expected:expr),+ $(,)?} $(,)?) => {{ $crate::GoogleTestSupport::and_log_failure($crate::verify_eq!($actual, {$($expected),*})); }}; ($actual:expr, {$($expected:expr),+ $(,)?}, $($format_args:expr),* $(,)?) => {{ - $crate::GoogleTestSupport::and_log_failure($crate::verify_eq!($actual, {$($expected),*}) - .with_failure_message(|| format!($($format_args),*)) - ); + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_eq!($actual, {$($expected),*}), + || format!($($format_args),*)); }}; ($actual:expr, $expected:expr $(,)?) => {{ $crate::GoogleTestSupport::and_log_failure($crate::verify_eq!($actual, $expected)); }}; ($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => {{ - $crate::GoogleTestSupport::and_log_failure($crate::verify_eq!($actual, $expected) - .with_failure_message(|| format!($($format_args),*)) - ); + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_eq!($actual, $expected), + || format!($($format_args),*)); }}; } pub use expect_eq; @@ -822,9 +822,9 @@ pub use verify_ne; #[macro_export] macro_rules! expect_ne { ($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {{ - $crate::GoogleTestSupport::and_log_failure($crate::verify_ne!($actual, $expected) - .with_failure_message(|| format!($($format_args),*)) - ); + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_ne!($actual, $expected), + || format!($($format_args),*)); }}; ($actual:expr, $expected:expr $(,)?) => {{ $crate::GoogleTestSupport::and_log_failure($crate::verify_ne!($actual, $expected)); @@ -900,9 +900,9 @@ pub use verify_lt; #[macro_export] macro_rules! expect_lt { ($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {{ - $crate::GoogleTestSupport::and_log_failure($crate::verify_lt!($actual, $expected) - .with_failure_message(|| format!($($format_args),*)) - ); + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_lt!($actual, $expected), + || format!($($format_args),*)); }}; ($actual:expr, $expected:expr $(,)?) => {{ $crate::GoogleTestSupport::and_log_failure($crate::verify_lt!($actual, $expected)); @@ -979,9 +979,9 @@ pub use verify_le; #[macro_export] macro_rules! expect_le { ($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {{ - $crate::GoogleTestSupport::and_log_failure($crate::verify_le!($actual, $expected) - .with_failure_message(|| format!($($format_args),*)) - ); + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_le!($actual, $expected), + || format!($($format_args),*)); }}; ($actual:expr, $expected:expr $(,)?) => {{ $crate::GoogleTestSupport::and_log_failure($crate::verify_le!($actual, $expected)); @@ -1057,9 +1057,9 @@ pub use verify_gt; #[macro_export] macro_rules! expect_gt { ($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {{ - $crate::GoogleTestSupport::and_log_failure($crate::verify_gt!($actual, $expected) - .with_failure_message(|| format!($($format_args),*)) - ); + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_gt!($actual, $expected), + || format!($($format_args),*)); }}; ($actual:expr, $expected:expr $(,)?) => {{ $crate::GoogleTestSupport::and_log_failure($crate::verify_gt!($actual, $expected)); @@ -1137,9 +1137,9 @@ pub use verify_ge; #[macro_export] macro_rules! expect_ge { ($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {{ - $crate::GoogleTestSupport::and_log_failure($crate::verify_ge!($actual, $expected) - .with_failure_message(|| format!($($format_args),*)) - ); + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_ge!($actual, $expected), + || format!($($format_args),*)); }}; ($actual:expr, $expected:expr $(,)?) => {{ $crate::GoogleTestSupport::and_log_failure($crate::verify_ge!($actual, $expected)); @@ -1232,9 +1232,9 @@ pub use verify_float_eq; #[macro_export] macro_rules! expect_float_eq { ($actual:expr, $expected:expr, $($format_args:expr),+ $(,)?) => {{ - $crate::GoogleTestSupport::and_log_failure($crate::verify_float_eq!($actual, $expected) - .with_failure_message(|| format!($($format_args),*)) - ); + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_float_eq!($actual, $expected), + || format!($($format_args),*)); }}; ($actual:expr, $expected:expr $(,)?) => {{ $crate::GoogleTestSupport::and_log_failure($crate::verify_float_eq!($actual, $expected)); @@ -1313,9 +1313,9 @@ pub use verify_near; #[macro_export] macro_rules! expect_near { ($actual:expr, $expected:expr, $max_abs_error:expr, $($format_args:expr),+ $(,)?) => {{ - $crate::GoogleTestSupport::and_log_failure($crate::verify_near!($actual, $expected, $max_abs_error) - .with_failure_message(|| format!($($format_args),*)) - ); + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_near!($actual, $expected, $max_abs_error), + || format!($($format_args),*)); }}; ($actual:expr, $expected:expr, $max_abs_error:expr $(,)?) => {{ $crate::GoogleTestSupport::and_log_failure($crate::verify_near!($actual, $expected, $max_abs_error)); @@ -1394,8 +1394,9 @@ macro_rules! assert_that { // w/ format args, specialized to sequence: ($actual:expr, [ $($expected:expr),* ], $($format_args:expr),* $(,)?) => { - match $crate::verify_that!($actual, [ $($expected),* ]) - .with_failure_message(|| format!($($format_args),*)) + match $crate::GoogleTestSupport::with_failure_message( + $crate::verify_that!($actual, [ $($expected),* ]), + || format!($($format_args),*)) { Ok(_) => {} Err(e) => { @@ -1408,8 +1409,9 @@ macro_rules! assert_that { // w/ format args, specialized to unordered sequence: ($actual:expr, { $($expected:expr),* }, $($format_args:expr),* $(,)?) => { - match $crate::verify_that!($actual, { $($expected),* }) - .with_failure_message(|| format!($($format_args),*)) + match $crate::GoogleTestSupport::with_failure_message( + $crate::verify_that!($actual, { $($expected),* }), + || format!($($format_args),*)) { Ok(_) => {} Err(e) => { @@ -1434,8 +1436,9 @@ macro_rules! assert_that { // w/ format args, general case: ($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => { - match $crate::verify_that!($actual, $expected) - .with_failure_message(|| format!($($format_args),*)) + match $crate::GoogleTestSupport::with_failure_message( + $crate::verify_that!($actual, $expected), + || format!($($format_args),*)) { Ok(_) => {} Err(e) => { @@ -1490,8 +1493,10 @@ macro_rules! assert_pred { // w/ format args ($content:expr $(,)?, $($format_args:expr),* $(,)?) => { - match $crate::verify_pred!($content) - .with_failure_message(|| format!($($format_args),*)) { + match $crate::GoogleTestSupport::with_failure_message( + $crate::verify_pred!($content), + || format!($($format_args),*) + ) { Ok(_) => {} Err(e) => { // The extra newline before the assertion failure message makes the failure a @@ -1559,16 +1564,16 @@ macro_rules! expect_that { // w/ format args, specialized to sequence: ($actual:expr, [$($expected:expr),*], $($format_args:expr),* $(,)?) => { - $crate::GoogleTestSupport::and_log_failure($crate::verify_that!($actual, [$($expected),*]) - .with_failure_message(|| format!($($format_args),*)) - ) + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_that!($actual, [$($expected),*]), + || format!($($format_args),*)) }; // w/ format args, specialized to unordered sequence: ($actual:expr, {$($expected:expr),*}, $($format_args:expr),* $(,)?) => { - $crate::GoogleTestSupport::and_log_failure($crate::verify_that!($actual, {$($expected),*}) - .with_failure_message(|| format!($($format_args),*)) - ) + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_that!($actual, {$($expected),*}), + || format!($($format_args),*)) }; // general case: @@ -1578,9 +1583,9 @@ macro_rules! expect_that { // w/ format args, general case: ($actual:expr, $expected:expr, $($format_args:expr),* $(,)?) => { - $crate::GoogleTestSupport::and_log_failure($crate::verify_that!($actual, $expected) - .with_failure_message(|| format!($($format_args),*)) - ) + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_that!($actual, $expected), + || format!($($format_args),*)) }; } pub use expect_that; @@ -1628,9 +1633,9 @@ macro_rules! expect_pred { }}; // w/ format args ($content:expr $(,)?, $($format_args:expr),* $(,)?) => { - $crate::GoogleTestSupport::and_log_failure($crate::verify_pred!($content) - .with_failure_message(|| format!($($format_args),*)) - ) + $crate::GoogleTestSupport::and_log_failure_with_message( + $crate::verify_pred!($content), + || format!($($format_args),*)) }; } pub use expect_pred; diff --git a/googletest/src/lib.rs b/googletest/src/lib.rs index 7f36f1ff..81e9af31 100644 --- a/googletest/src/lib.rs +++ b/googletest/src/lib.rs @@ -148,6 +148,31 @@ pub trait GoogleTestSupport { /// ``` fn and_log_failure(self); + /// If `self` is a `Result::Err`, writes to `stdout` with a failure report + /// and the message returned by `provider`. + /// + /// This is equivalent to combining [`GoogleTestSupport::and_log_failure`] + /// with a call to [`GoogleTestSupport::with_failure_message`]. + /// + /// Example: + /// + /// ``` + /// # use googletest::GoogleTestSupport; + /// # use googletest::assertions::verify_eq; + /// # use googletest::internal::test_outcome::TestOutcome; + /// # TestOutcome::init_current_test_outcome(); + /// let actual = 0; + /// verify_eq!(actual, 42) + /// .and_log_failure_with_message(|| format!("Actual {} was wrong!", actual)); + /// # TestOutcome::close_current_test_outcome::<&str>(Ok(())).unwrap_err(); + /// ``` + fn and_log_failure_with_message(self, provider: impl FnOnce() -> String) + where + Self: Sized, + { + self.with_failure_message(provider).and_log_failure(); + } + /// Adds `message` to the logged failure message if `self` is a /// `Result::Err`. Otherwise, does nothing. /// diff --git a/integration_tests/src/integration_tests.rs b/integration_tests/src/integration_tests.rs index 47f22c86..6e53166f 100644 --- a/integration_tests/src/integration_tests.rs +++ b/integration_tests/src/integration_tests.rs @@ -2002,6 +2002,9 @@ mod tests { contains_substring("test tests::expect_false_works ... ok"), contains_substring("test tests::verify_eq_works ... ok"), contains_substring("test tests::expect_eq_works ... ok"), + contains_substring( + "test tests::expect_eq_with_message_compiles_without_trait_imports ... ok" + ), contains_substring("test tests::verify_ne_works ... ok"), contains_substring("test tests::expect_ne_works ... ok"), contains_substring("test tests::verify_lt_works ... ok"), @@ -2020,7 +2023,7 @@ mod tests { contains_substring("test tests::assert_pred_works ... ok"), contains_substring("test tests::expect_that_works ... ok"), contains_substring("test tests::expect_pred_works ... ok"), - contains_substring("test result: FAILED. 27 passed; 3 failed;") + contains_substring("test result: FAILED. 28 passed; 3 failed;") ) ) } diff --git a/integration_tests/src/macro_hygiene.rs b/integration_tests/src/macro_hygiene.rs index 0649f54e..77f51b18 100644 --- a/integration_tests/src/macro_hygiene.rs +++ b/integration_tests/src/macro_hygiene.rs @@ -88,6 +88,11 @@ mod tests { googletest::expect_eq!(2 + 2, 4); } + #[gtest] + fn expect_eq_with_message_compiles_without_trait_imports() { + googletest::expect_eq!(1, 1, "Some message."); + } + #[gtest] fn verify_ne_works() -> googletest::Result<()> { googletest::verify_ne!(2 + 2, 5)