Skip to content

Commit c5f15a9

Browse files
satylogincopybara-github
authored andcommitted
PUBLIC: False boolean conditions macros
Adding rust flavours for:[] * `verify_false`: check if the given expression is false and returns `Result`. * `assert_false`: **panics** if the expression evaluates to **true**. * `expect_false`: **logs failure** and **continues execution** if expression evaluates to **true**. PiperOrigin-RevId: 632540641
1 parent 6e0a18e commit c5f15a9

8 files changed

+256
-2
lines changed

googletest/src/assertions.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,95 @@ macro_rules! expect_true {
523523
}};
524524
}
525525

526+
/// Verify if the condition evaluates to false and returns `Result`.
527+
///
528+
/// Evaluates to `Result::Ok(())` if the condition is false and
529+
/// `Result::Err(TestAssertionFailure)` if it evaluates to true. The caller
530+
/// must then decide how to handle the `Err` variant. It has a few options:
531+
/// * Abort the current function with the `?` operator. This requires that the
532+
/// function return a suitable `Result`.
533+
/// * Log the failure and continue by calling the method `and_log_failure`.
534+
///
535+
/// Of course, one can also use all other standard methods on `Result`.
536+
///
537+
/// **Invoking this macro by itself does not cause a test failure to be recorded
538+
/// or output.** The resulting `Result` must be handled as described above to
539+
/// cause the test to be recorded as a failure.
540+
///
541+
/// Example:
542+
/// ```ignore
543+
/// use googletest::prelude::*;
544+
///
545+
/// #[test]
546+
/// fn should_fail() -> Result<()> {
547+
/// verify_false!(2 + 2 == 4)
548+
/// }
549+
/// ```
550+
#[macro_export]
551+
macro_rules! verify_false {
552+
($condition:expr) => {{
553+
use $crate::assertions::internal::Subject;
554+
($condition).check(
555+
$crate::matchers::eq(false),
556+
stringify!($condition),
557+
$crate::internal::source_location::SourceLocation::new(file!(), line!(), column!()),
558+
)
559+
}};
560+
}
561+
562+
/// Assert if the condition evaluates to false and panics if true.
563+
///
564+
/// This is the same as calling `and_log_failure` on [`verify_false`] macro
565+
/// Result.
566+
///
567+
/// This can only be invoked inside tests with the
568+
/// [`googletest::test`][crate::test] attribute. The failure must be generated
569+
/// in the same thread as that running the thread itself.
570+
///
571+
/// Example:
572+
/// ```ignore
573+
/// use googletest::prelude::*;
574+
///
575+
/// #[googletest::test]
576+
/// fn should_fail() {
577+
/// assert_false!(2 + 2 == 4);
578+
/// }
579+
/// ```
580+
#[macro_export]
581+
macro_rules! assert_false {
582+
($condition:expr) => {
583+
verify_false!($condition).and_log_failure()
584+
};
585+
}
586+
587+
/// Marks test as failed and continue execution if the expression evaluates to
588+
/// true.
589+
///
590+
/// This is a **not-fatal** failure. The test continues execution even after the
591+
/// macro execution.
592+
///
593+
/// This can only be invoked inside tests with the
594+
/// [`googletest::test`][crate::test] attribute. The failure must be generated
595+
/// in the same thread as that running the thread itself.
596+
///
597+
/// Example:
598+
/// ```ignore
599+
/// use googletest::prelude::*;
600+
///
601+
/// #[googletest::test]
602+
/// fn should_fail() {
603+
/// expect_false!(2 + 2 == 4);
604+
/// println!("This will print");
605+
/// }
606+
/// ```
607+
#[macro_export]
608+
macro_rules! expect_false {
609+
($condition:expr) => {{
610+
use $crate::GoogleTestSupport;
611+
verify_false!($condition).and_log_failure()
612+
}};
613+
}
614+
526615
/// Matches the given value against the given matcher, panicking if it does not
527616
/// match.
528617
///

googletest/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ pub mod prelude {
5050
pub use super::Result;
5151
// Assert macros
5252
pub use super::{
53-
add_failure, add_failure_at, assert_that, assert_true, expect_pred, expect_that,
54-
expect_true, fail, succeed, verify_pred, verify_that, verify_true,
53+
add_failure, add_failure_at, assert_false, assert_that, assert_true, expect_false,
54+
expect_pred, expect_that, expect_true, fail, succeed, verify_false, verify_pred,
55+
verify_that, verify_true,
5556
};
5657
}
5758

integration_tests/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ name = "expect_true_macro_on_false_condition_fails_test_and_continues"
160160
path = "src/expect_true_macro_on_false_condition_fails_test_and_continues.rs"
161161
test = false
162162

163+
[[bin]]
164+
name = "verify_false_macro_on_true_condition"
165+
path = "src/verify_false_macro_on_true_condition.rs"
166+
test = false
167+
168+
[[bin]]
169+
name = "assert_false_macro_on_true_condition_panics"
170+
path = "src/assert_false_macro_on_true_condition_panics.rs"
171+
test = false
172+
173+
[[bin]]
174+
name = "expect_false_macro_on_true_condition_fails_test_and_continues"
175+
path = "src/expect_false_macro_on_true_condition_fails_test_and_continues.rs"
176+
test = false
177+
163178
[[bin]]
164179
name = "failure_due_to_returned_error"
165180
path = "src/failure_due_to_returned_error.rs"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
fn main() {}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use googletest::prelude::*;
20+
21+
#[googletest::test]
22+
fn should_fail() {
23+
assert_false!(2 + 2 == 4);
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
fn main() {}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use googletest::prelude::*;
20+
21+
#[googletest::test]
22+
fn should_fail() {
23+
expect_false!(2 + 2 == 4);
24+
println!("This will print");
25+
}
26+
}

integration_tests/src/integration_tests.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,76 @@ mod tests {
777777
verify_that!(output, contains_regex("This will print"))
778778
}
779779

780+
#[test]
781+
fn verify_false_when_false_returns_ok() {
782+
assert!(verify_false!("test" == "not test").is_ok())
783+
}
784+
785+
#[test]
786+
fn verify_false_when_true_returns_err() {
787+
assert!(verify_false!(2 + 2 == 4).is_err())
788+
}
789+
790+
#[googletest::test]
791+
fn verify_false_macro_on_true_condition_logs_error_when_handled() -> Result<()> {
792+
let output =
793+
run_external_process_in_tests_directory("verify_false_macro_on_true_condition")?;
794+
795+
verify_that!(
796+
output,
797+
contains_regex(indoc! {"
798+
Expected: is equal to false
799+
Actual: true,
800+
which isn't equal to false
801+
at .*verify_false_macro_on_true_condition.rs:23:9
802+
"})
803+
)
804+
}
805+
806+
#[googletest::test]
807+
fn assert_false_macro_on_false_condition_does_nothing() {
808+
assert_false!(2 + 2 == 5);
809+
}
810+
811+
#[googletest::test]
812+
fn assert_false_macro_on_true_condition_panics() -> Result<()> {
813+
let output =
814+
run_external_process_in_tests_directory("assert_false_macro_on_true_condition_panics")?;
815+
816+
verify_that!(
817+
output,
818+
contains_regex(indoc! {"
819+
Expected: is equal to false
820+
Actual: true,
821+
which isn't equal to false
822+
at .*assert_false_macro_on_true_condition_panics.rs:23:9
823+
"})
824+
)
825+
}
826+
827+
#[googletest::test]
828+
fn expect_false_macro_on_false_condition_does_nothing() {
829+
expect_false!(2 + 2 == 5)
830+
}
831+
832+
#[googletest::test]
833+
fn expect_false_macro_on_true_condition_fails_test_and_continues() -> Result<()> {
834+
let output = run_external_process_in_tests_directory(
835+
"expect_false_macro_on_true_condition_fails_test_and_continues",
836+
)?;
837+
838+
expect_that!(
839+
output,
840+
contains_regex(indoc! {"
841+
Expected: is equal to false
842+
Actual: true,
843+
which isn't equal to false
844+
at .*expect_false_macro_on_true_condition_fails_test_and_continues.rs:23:9
845+
"})
846+
);
847+
verify_that!(output, contains_regex("This will print"))
848+
}
849+
780850
#[test]
781851
fn test_using_normal_test_attribute_macro_formats_failure_message_correctly() -> Result<()> {
782852
let result = should_display_error_correctly_without_google_test_macro();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
fn main() {}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use googletest::prelude::*;
20+
21+
#[test]
22+
fn should_fail() -> Result<()> {
23+
verify_false!(2 + 2 == 4)
24+
}
25+
}

run_integration_tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ INTEGRATION_TEST_BINARIES=(
4949
"verify_true_macro_on_false_condition"
5050
"assert_true_macro_on_false_condition_panics"
5151
"expect_true_macro_on_false_condition_fails_test_and_continues"
52+
"verify_false_macro_on_true_condition"
53+
"assert_false_macro_on_true_condition_panics"
54+
"expect_false_macro_on_true_condition_fails_test_and_continues"
5255
"fatal_and_non_fatal_failure"
5356
"first_failure_aborts"
5457
"google_test_with_rstest"

0 commit comments

Comments
 (0)