@@ -43,6 +43,7 @@ pub mod matchers;
4343pub mod prelude {
4444 pub use super :: matcher:: Matcher ;
4545 pub use super :: matchers:: * ;
46+ pub use super :: verify_current_test_outcome;
4647 pub use super :: GoogleTestSupport ;
4748 pub use super :: IntoTestResult ;
4849 pub use super :: Result ;
@@ -83,6 +84,39 @@ use internal::test_outcome::{TestAssertionFailure, TestOutcome};
8384/// failed but allow it to continue running, are not encoded in this type.
8485pub type Result < T > = std:: result:: Result < T , TestAssertionFailure > ;
8586
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+
86120/// Adds to `Result` support for GoogleTest Rust functionality.
87121pub trait GoogleTestSupport {
88122 /// If `self` is a `Result::Err`, writes to `stdout` a failure report
0 commit comments