@@ -43,6 +43,7 @@ pub mod matchers;
43
43
pub mod prelude {
44
44
pub use super :: matcher:: Matcher ;
45
45
pub use super :: matchers:: * ;
46
+ pub use super :: verify_current_test_outcome;
46
47
pub use super :: GoogleTestSupport ;
47
48
pub use super :: IntoTestResult ;
48
49
pub use super :: Result ;
@@ -83,6 +84,39 @@ use internal::test_outcome::{TestAssertionFailure, TestOutcome};
83
84
/// failed but allow it to continue running, are not encoded in this type.
84
85
pub type Result < T > = std:: result:: Result < T , TestAssertionFailure > ;
85
86
87
+ /// Returns a [`Result`] corresponding to the outcome of the currently running
88
+ /// test.
89
+ ///
90
+ /// This returns `Result::Err` precisely if the current test has recorded at
91
+ /// least one test assertion failure via [`expect_that!`][crate::expect_that],
92
+ /// [`expect_pred!`][crate::expect_pred], or
93
+ /// [`GoogleTestSupport::and_log_failure`]. It can be used in concert with the
94
+ /// `?` operator to continue execution of the test conditionally on there not
95
+ /// having been any failure yet.
96
+ ///
97
+ /// This requires the use of the [`#[googletest::test]`][crate::test] attribute
98
+ /// macro.
99
+ ///
100
+ /// ```
101
+ /// # use googletest::prelude::*;
102
+ /// # /* Make sure this also compiles as a doctest.
103
+ /// #[googletest::test]
104
+ /// # */
105
+ /// # fn foo() -> u32 { 1 }
106
+ /// # fn bar() -> u32 { 2 }
107
+ /// fn should_fail_and_not_execute_last_assertion() -> Result<()> {
108
+ /// # googletest::internal::test_outcome::TestOutcome::init_current_test_outcome();
109
+ /// expect_that!(foo(), eq(2)); // May fail, but will not abort the test.
110
+ /// expect_that!(bar(), gt(1)); // May fail, but will not abort the test.
111
+ /// verify_current_test_outcome()?; // Aborts the test if one of the previous assertions failed.
112
+ /// verify_that!(foo(), gt(0)) // Does not execute if the line above aborts.
113
+ /// }
114
+ /// # verify_that!(should_fail_and_not_execute_last_assertion(), err(displays_as(contains_substring("Test failed")))).unwrap();
115
+ /// ```
116
+ pub fn verify_current_test_outcome ( ) -> Result < ( ) > {
117
+ TestOutcome :: get_current_test_outcome ( )
118
+ }
119
+
86
120
/// Adds to `Result` support for GoogleTest Rust functionality.
87
121
pub trait GoogleTestSupport {
88
122
/// If `self` is a `Result::Err`, writes to `stdout` a failure report
0 commit comments