Skip to content

Commit a7de523

Browse files
Merge pull request #255 from google:replace-test-return-type
PiperOrigin-RevId: 546303798
2 parents 056ef2f + d4cdd2c commit a7de523

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

googletest/src/internal/test_outcome.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ impl TestOutcome {
6262
///
6363
/// **For internal use only. API stablility is not guaranteed!**
6464
#[doc(hidden)]
65-
pub fn close_current_test_outcome<E: Display>(inner_result: Result<(), E>) -> Result<(), ()> {
65+
pub fn close_current_test_outcome<E: Display>(
66+
inner_result: Result<(), E>,
67+
) -> Result<(), TestFailure> {
6668
TestOutcome::with_current_test_outcome(|mut outcome| {
6769
let outer_result = match &*outcome {
6870
Some(TestOutcome::Success) => match inner_result {
6971
Ok(()) => Ok(()),
70-
Err(_) => Err(()),
72+
Err(_) => Err(TestFailure),
7173
},
72-
Some(TestOutcome::Failure) => Err(()),
74+
Some(TestOutcome::Failure) => Err(TestFailure),
7375
None => {
7476
panic!("No test context found. This indicates a bug in GoogleTest.")
7577
}
@@ -114,6 +116,29 @@ No test context found.
114116
}
115117
}
116118

119+
/// A marking struct indicating that a test has failed.
120+
///
121+
/// This exists to implement the [Error][std::error::Error] trait. It displays
122+
/// to a message indicating that the actual test assertion failure messages are
123+
/// in the text above.
124+
pub struct TestFailure;
125+
126+
impl std::error::Error for TestFailure {}
127+
128+
impl std::fmt::Debug for TestFailure {
129+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
130+
writeln!(f, "See failure output above")?;
131+
Ok(())
132+
}
133+
}
134+
135+
impl std::fmt::Display for TestFailure {
136+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
137+
writeln!(f, "See failure output above")?;
138+
Ok(())
139+
}
140+
}
141+
117142
/// A report that a single test assertion failed.
118143
///
119144
/// **For internal use only. API stablility is not guaranteed!**

googletest_macro/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn test(
9595
};
9696
let function = quote! {
9797
#(#attrs)*
98-
#sig -> std::result::Result<(), ()> {
98+
#sig -> std::result::Result<(), googletest::internal::test_outcome::TestFailure> {
9999
#maybe_closure
100100
use googletest::internal::test_outcome::TestOutcome;
101101
TestOutcome::init_current_test_outcome();

0 commit comments

Comments
 (0)