Skip to content

Commit 6e0a18e

Browse files
satylogincopybara-github
authored andcommitted
PUBLIC: True boolean conditions macros
Adding rust flavours for:[] * `verify_true`: check if the given expression is true and returns `Result`. * `assert_true`: **panics** if the expression evaluates to false. * `expect_true`: **logs** failure and **continues** execution if expression evaluates to false. PiperOrigin-RevId: 632437788
1 parent 904c7cb commit 6e0a18e

8 files changed

+255
-2
lines changed

googletest/src/assertions.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,95 @@ macro_rules! add_failure_at {
434434
};
435435
}
436436

437+
/// Verify if the condition evaluates to true and returns `Result`.
438+
///
439+
/// Evaluates to `Result::Ok(())` if the condition is true and
440+
/// `Result::Err(TestAssertionFailure)` if it evaluates to false. The caller
441+
/// must then decide how to handle the `Err` variant. It has a few options:
442+
/// * Abort the current function with the `?` operator. This requires that the
443+
/// function return a suitable `Result`.
444+
/// * Log the failure and continue by calling the method `and_log_failure`.
445+
///
446+
/// Of course, one can also use all other standard methods on `Result`.
447+
///
448+
/// **Invoking this macro by itself does not cause a test failure to be recorded
449+
/// or output.** The resulting `Result` must be handled as described above to
450+
/// cause the test to be recorded as a failure.
451+
///
452+
/// Example:
453+
/// ```ignore
454+
/// use googletest::prelude::*;
455+
///
456+
/// #[test]
457+
/// fn should_fail() -> Result<()> {
458+
/// verify_true!(2 + 2 == 5)
459+
/// }
460+
/// ```
461+
#[macro_export]
462+
macro_rules! verify_true {
463+
($condition:expr) => {{
464+
use $crate::assertions::internal::Subject;
465+
($condition).check(
466+
$crate::matchers::eq(true),
467+
stringify!($condition),
468+
$crate::internal::source_location::SourceLocation::new(file!(), line!(), column!()),
469+
)
470+
}};
471+
}
472+
473+
/// Assert if the condition evaluates to true and panics if false.
474+
///
475+
/// This is the same as calling `and_log_failure` on [`verify_true`] macro
476+
/// Result.
477+
///
478+
/// This can only be invoked inside tests with the
479+
/// [`googletest::test`][crate::test] attribute. The failure must be generated
480+
/// in the same thread as that running the thread itself.
481+
///
482+
/// Example:
483+
/// ```ignore
484+
/// use googletest::prelude::*;
485+
///
486+
/// #[googletest::test]
487+
/// fn should_fail() {
488+
/// assert_true!(2 + 2 == 5);
489+
/// }
490+
/// ```
491+
#[macro_export]
492+
macro_rules! assert_true {
493+
($condition:expr) => {
494+
verify_true!($condition).and_log_failure()
495+
};
496+
}
497+
498+
/// Marks test as failed and continue execution if the expression evaluates to
499+
/// false.
500+
///
501+
/// This is a **not-fatal** failure. The test continues execution even after the
502+
/// macro execution.
503+
///
504+
/// This can only be invoked inside tests with the
505+
/// [`googletest::test`][crate::test] attribute. The failure must be generated
506+
/// in the same thread as that running the thread itself.
507+
///
508+
/// Example:
509+
/// ```ignore
510+
/// use googletest::prelude::*;
511+
///
512+
/// #[googletest::test]
513+
/// fn should_fail() {
514+
/// expect_true!(2 + 2 == 5);
515+
/// println!("This will print");
516+
/// }
517+
/// ```
518+
#[macro_export]
519+
macro_rules! expect_true {
520+
($condition:expr) => {{
521+
use $crate::GoogleTestSupport;
522+
verify_true!($condition).and_log_failure()
523+
}};
524+
}
525+
437526
/// Matches the given value against the given matcher, panicking if it does not
438527
/// match.
439528
///

googletest/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ pub mod prelude {
5050
pub use super::Result;
5151
// Assert macros
5252
pub use super::{
53-
add_failure, add_failure_at, assert_that, expect_pred, expect_that, fail, succeed,
54-
verify_pred, verify_that,
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,
5555
};
5656
}
5757

integration_tests/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,21 @@ name = "add_failure_at_macro_needs_googletest_attribute"
145145
path = "src/add_failure_at_macro_needs_googletest_attribute.rs"
146146
test = false
147147

148+
[[bin]]
149+
name = "verify_true_macro_on_false_condition"
150+
path = "src/verify_true_macro_on_false_condition.rs"
151+
test = false
152+
153+
[[bin]]
154+
name = "assert_true_macro_on_false_condition_panics"
155+
path = "src/assert_true_macro_on_false_condition_panics.rs"
156+
test = false
157+
158+
[[bin]]
159+
name = "expect_true_macro_on_false_condition_fails_test_and_continues"
160+
path = "src/expect_true_macro_on_false_condition_fails_test_and_continues.rs"
161+
test = false
162+
148163
[[bin]]
149164
name = "failure_due_to_returned_error"
150165
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_true!(2 + 2 == 5);
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_true!(2 + 2 == 5);
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
@@ -707,6 +707,76 @@ mod tests {
707707
verify_that!(output, contains_regex("Did you annotate the test with googletest::test?"))
708708
}
709709

710+
#[test]
711+
fn verify_true_when_true_returns_ok() {
712+
assert!(verify_true!("test" == "test").is_ok())
713+
}
714+
715+
#[test]
716+
fn verify_true_when_false_returns_err() {
717+
assert!(verify_true!(2 + 2 == 5).is_err())
718+
}
719+
720+
#[googletest::test]
721+
fn verify_true_macro_on_false_condition_logs_error_when_handled() -> Result<()> {
722+
let output =
723+
run_external_process_in_tests_directory("verify_true_macro_on_false_condition")?;
724+
725+
verify_that!(
726+
output,
727+
contains_regex(indoc! {"
728+
Expected: is equal to true
729+
Actual: false,
730+
which isn't equal to true
731+
at .*verify_true_macro_on_false_condition.rs:23:9
732+
"})
733+
)
734+
}
735+
736+
#[googletest::test]
737+
fn assert_true_macro_on_true_condition_does_nothing() {
738+
assert_true!(2 + 2 == 4);
739+
}
740+
741+
#[googletest::test]
742+
fn assert_true_macro_on_false_condition_panics() -> Result<()> {
743+
let output =
744+
run_external_process_in_tests_directory("assert_true_macro_on_false_condition_panics")?;
745+
746+
verify_that!(
747+
output,
748+
contains_regex(indoc! {"
749+
Expected: is equal to true
750+
Actual: false,
751+
which isn't equal to true
752+
at .*assert_true_macro_on_false_condition_panics.rs:23:9
753+
"})
754+
)
755+
}
756+
757+
#[googletest::test]
758+
fn expect_true_macro_on_true_condition_does_nothing() {
759+
expect_true!(2 + 2 == 4)
760+
}
761+
762+
#[googletest::test]
763+
fn expect_true_macro_on_false_condition_fails_test_and_continues() -> Result<()> {
764+
let output = run_external_process_in_tests_directory(
765+
"expect_true_macro_on_false_condition_fails_test_and_continues",
766+
)?;
767+
768+
expect_that!(
769+
output,
770+
contains_regex(indoc! {"
771+
Expected: is equal to true
772+
Actual: false,
773+
which isn't equal to true
774+
at .*expect_true_macro_on_false_condition_fails_test_and_continues.rs:23:9
775+
"})
776+
);
777+
verify_that!(output, contains_regex("This will print"))
778+
}
779+
710780
#[test]
711781
fn test_using_normal_test_attribute_macro_formats_failure_message_correctly() -> Result<()> {
712782
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_true!(2 + 2 == 5)
24+
}
25+
}

run_integration_tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ INTEGRATION_TEST_BINARIES=(
4646
"add_failure_at_macro_allows_empty_message"
4747
"add_failure_at_macro_allows_formatted_arguments"
4848
"add_failure_at_macro_needs_googletest_attribute"
49+
"verify_true_macro_on_false_condition"
50+
"assert_true_macro_on_false_condition_panics"
51+
"expect_true_macro_on_false_condition_fails_test_and_continues"
4952
"fatal_and_non_fatal_failure"
5053
"first_failure_aborts"
5154
"google_test_with_rstest"

0 commit comments

Comments
 (0)