From 0925bc967e8d317129039e5bacecc3abed3230b0 Mon Sep 17 00:00:00 2001 From: Googler Date: Sun, 10 May 2026 23:54:01 -0700 Subject: [PATCH] Add assert_true! and assert_false! macros to gtest_rust These macros provide fatal assertions, panicking if the condition is not met. This aligns with the assertion a developer would expect from other test frameworks. The macros are added to `assertions.rs` and exported in `lib.rs`. Integration tests are added to: - `passing_tests.rs`: Ensure the macros don't panic when the condition is met. - `failing_tests.rs`: Ensure the macros *do* panic when the condition is not met. - `integration_tests.sh`: Updated to expect the new failures in `failing_tests_bin`. PUBLIC: Add assert_true! and assert_false! macros for fatal assertions. PiperOrigin-RevId: 913525450 --- googletest/src/assertions.rs | 119 +++++++++++++++++++++++++++++++++++ googletest/src/lib.rs | 10 +-- 2 files changed, 124 insertions(+), 5 deletions(-) diff --git a/googletest/src/assertions.rs b/googletest/src/assertions.rs index d1e35bbd..14874220 100644 --- a/googletest/src/assertions.rs +++ b/googletest/src/assertions.rs @@ -524,6 +524,66 @@ macro_rules! expect_true { } pub use expect_true; +/// Marks the test as failed and panics if the given value is `false`. +/// +/// This is a *fatal* assertion: the test panics +/// in the event of assertion failure. +/// +/// This can only be invoked inside tests with the +/// [`gtest`][crate::gtest] attribute. The assertion must +/// occur in the same thread as that running the test itself. +/// +/// Example: +/// +/// ```ignore +/// use googletest::prelude::*; +/// +/// #[gtest] +/// fn should_fail() { +/// assert_true!(false); // panic! +/// } +/// ``` +/// +/// One may optionally add arguments which will be formatted and appended to a +/// failure message. For example: +/// +/// ```ignore +/// use googletest::prelude::*; +/// +/// #[gtest] +/// fn should_fail() { +/// let extra_information = "Some additional information"; +/// assert_true!(false, "Test failed. Extra information: {extra_information}."); +/// } +/// ``` +/// +/// The output is as follows: +/// +/// ```text +/// Value of: false +/// Expected: is equal to true +/// Actual: false, +/// which isn't equal to true +/// Test failed. Extra information: Some additional information. +/// ``` +#[macro_export] +macro_rules! assert_true { + ($condition:expr) => { + if let Err(e) = $crate::verify_true!($condition) { + panic!("\n{}", e); + } + }; + ($condition:expr, $($format_args:expr),* $(,)?) => { + if let Err(e) = $crate::GoogleTestSupport::with_failure_message( + $crate::verify_true!($condition), + || format!($($format_args),*)) + { + panic!("\n{}", e); + } + }; +} +pub use assert_true; + /// Verify if the condition evaluates to false and returns `Result`. /// /// Evaluates to `Result::Ok(())` if the condition is false and @@ -613,6 +673,65 @@ macro_rules! expect_false { } pub use expect_false; +/// Marks the test as failed and panics if the given value is `true`. +/// +/// This is a *fatal* assertion: the test panics +/// in the event of assertion failure. +/// +/// This can only be invoked inside tests with the +/// [`gtest`][crate::gtest] attribute. The assertion must +/// occur in the same thread as that running the test itself. +/// +/// Example: +/// ```ignore +/// use googletest::prelude::*; +/// +/// #[gtest] +/// fn should_fail() { +/// assert_false!(true); // panic! +/// } +/// ``` +/// +/// One may optionally add arguments which will be formatted and appended to a +/// failure message. For example: +/// +/// ``` ignore +/// use googletest::prelude::*; +/// +/// #[gtest] +/// fn should_fail() { +/// let extra_information = "Some additional information"; +/// assert_false!(true, "Test failed. Extra information: {extra_information}."); +/// } +/// ``` +/// +/// The output is as follows: +/// +/// ```text +/// Value of: true +/// Expected: is equal to false +/// Actual: true, +/// which isn't equal to false +/// Test failed. Extra information: Some additional information. +/// ``` +#[macro_export] +macro_rules! assert_false { + ($condition:expr) => { + if let Err(e) = $crate::verify_false!($condition) { + panic!("\n{}", e); + } + }; + ($condition:expr, $($format_args:expr),* $(,)?) => { + if let Err(e) = $crate::GoogleTestSupport::with_failure_message( + $crate::verify_false!($condition), + || format!($($format_args),*)) + { + panic!("\n{}", e); + } + }; +} +pub use assert_false; + /// Checks whether the second argument is equal to the first argument. /// /// Evaluates to `Result::Ok(())` if they are equal and diff --git a/googletest/src/lib.rs b/googletest/src/lib.rs index 15baec41..cf16a936 100644 --- a/googletest/src/lib.rs +++ b/googletest/src/lib.rs @@ -57,11 +57,11 @@ pub mod prelude { pub use super::Result; // Assert macros pub use super::{ - add_failure, add_failure_at, assert_pred, assert_that, expect_eq, expect_false, - expect_float_eq, expect_ge, expect_gt, expect_le, expect_lt, expect_ne, expect_near, - expect_pred, expect_that, expect_true, fail, succeed, verify_eq, verify_false, - verify_float_eq, verify_ge, verify_gt, verify_le, verify_lt, verify_ne, verify_near, - verify_pred, verify_that, verify_true, + add_failure, add_failure_at, assert_false, assert_pred, assert_that, assert_true, + expect_eq, expect_false, expect_float_eq, expect_ge, expect_gt, expect_le, expect_lt, + expect_ne, expect_near, expect_pred, expect_that, expect_true, fail, succeed, verify_eq, + verify_false, verify_float_eq, verify_ge, verify_gt, verify_le, verify_lt, verify_ne, + verify_near, verify_pred, verify_that, verify_true, }; }