Skip to content

Commit 0925bc9

Browse files
Googlercopybara-github
authored andcommitted
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
1 parent 7d6b657 commit 0925bc9

2 files changed

Lines changed: 124 additions & 5 deletions

File tree

googletest/src/assertions.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,66 @@ macro_rules! expect_true {
524524
}
525525
pub use expect_true;
526526

527+
/// Marks the test as failed and panics if the given value is `false`.
528+
///
529+
/// This is a *fatal* assertion: the test panics
530+
/// in the event of assertion failure.
531+
///
532+
/// This can only be invoked inside tests with the
533+
/// [`gtest`][crate::gtest] attribute. The assertion must
534+
/// occur in the same thread as that running the test itself.
535+
///
536+
/// Example:
537+
///
538+
/// ```ignore
539+
/// use googletest::prelude::*;
540+
///
541+
/// #[gtest]
542+
/// fn should_fail() {
543+
/// assert_true!(false); // panic!
544+
/// }
545+
/// ```
546+
///
547+
/// One may optionally add arguments which will be formatted and appended to a
548+
/// failure message. For example:
549+
///
550+
/// ```ignore
551+
/// use googletest::prelude::*;
552+
///
553+
/// #[gtest]
554+
/// fn should_fail() {
555+
/// let extra_information = "Some additional information";
556+
/// assert_true!(false, "Test failed. Extra information: {extra_information}.");
557+
/// }
558+
/// ```
559+
///
560+
/// The output is as follows:
561+
///
562+
/// ```text
563+
/// Value of: false
564+
/// Expected: is equal to true
565+
/// Actual: false,
566+
/// which isn't equal to true
567+
/// Test failed. Extra information: Some additional information.
568+
/// ```
569+
#[macro_export]
570+
macro_rules! assert_true {
571+
($condition:expr) => {
572+
if let Err(e) = $crate::verify_true!($condition) {
573+
panic!("\n{}", e);
574+
}
575+
};
576+
($condition:expr, $($format_args:expr),* $(,)?) => {
577+
if let Err(e) = $crate::GoogleTestSupport::with_failure_message(
578+
$crate::verify_true!($condition),
579+
|| format!($($format_args),*))
580+
{
581+
panic!("\n{}", e);
582+
}
583+
};
584+
}
585+
pub use assert_true;
586+
527587
/// Verify if the condition evaluates to false and returns `Result`.
528588
///
529589
/// Evaluates to `Result::Ok(())` if the condition is false and
@@ -613,6 +673,65 @@ macro_rules! expect_false {
613673
}
614674
pub use expect_false;
615675

676+
/// Marks the test as failed and panics if the given value is `true`.
677+
///
678+
/// This is a *fatal* assertion: the test panics
679+
/// in the event of assertion failure.
680+
///
681+
/// This can only be invoked inside tests with the
682+
/// [`gtest`][crate::gtest] attribute. The assertion must
683+
/// occur in the same thread as that running the test itself.
684+
///
685+
/// Example:
686+
/// ```ignore
687+
/// use googletest::prelude::*;
688+
///
689+
/// #[gtest]
690+
/// fn should_fail() {
691+
/// assert_false!(true); // panic!
692+
/// }
693+
/// ```
694+
///
695+
/// One may optionally add arguments which will be formatted and appended to a
696+
/// failure message. For example:
697+
///
698+
/// ``` ignore
699+
/// use googletest::prelude::*;
700+
///
701+
/// #[gtest]
702+
/// fn should_fail() {
703+
/// let extra_information = "Some additional information";
704+
/// assert_false!(true, "Test failed. Extra information: {extra_information}.");
705+
/// }
706+
/// ```
707+
///
708+
/// The output is as follows:
709+
///
710+
/// ```text
711+
/// Value of: true
712+
/// Expected: is equal to false
713+
/// Actual: true,
714+
/// which isn't equal to false
715+
/// Test failed. Extra information: Some additional information.
716+
/// ```
717+
#[macro_export]
718+
macro_rules! assert_false {
719+
($condition:expr) => {
720+
if let Err(e) = $crate::verify_false!($condition) {
721+
panic!("\n{}", e);
722+
}
723+
};
724+
($condition:expr, $($format_args:expr),* $(,)?) => {
725+
if let Err(e) = $crate::GoogleTestSupport::with_failure_message(
726+
$crate::verify_false!($condition),
727+
|| format!($($format_args),*))
728+
{
729+
panic!("\n{}", e);
730+
}
731+
};
732+
}
733+
pub use assert_false;
734+
616735
/// Checks whether the second argument is equal to the first argument.
617736
///
618737
/// Evaluates to `Result::Ok(())` if they are equal and

googletest/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ pub mod prelude {
5757
pub use super::Result;
5858
// Assert macros
5959
pub use super::{
60-
add_failure, add_failure_at, assert_pred, assert_that, expect_eq, expect_false,
61-
expect_float_eq, expect_ge, expect_gt, expect_le, expect_lt, expect_ne, expect_near,
62-
expect_pred, expect_that, expect_true, fail, succeed, verify_eq, verify_false,
63-
verify_float_eq, verify_ge, verify_gt, verify_le, verify_lt, verify_ne, verify_near,
64-
verify_pred, verify_that, verify_true,
60+
add_failure, add_failure_at, assert_false, assert_pred, assert_that, assert_true,
61+
expect_eq, expect_false, expect_float_eq, expect_ge, expect_gt, expect_le, expect_lt,
62+
expect_ne, expect_near, expect_pred, expect_that, expect_true, fail, succeed, verify_eq,
63+
verify_false, verify_float_eq, verify_ge, verify_gt, verify_le, verify_lt, verify_ne,
64+
verify_near, verify_pred, verify_that, verify_true,
6565
};
6666
}
6767

0 commit comments

Comments
 (0)