Skip to content

Commit 5992211

Browse files
hovinenbcopybara-github
authored andcommitted
Make arbitrary types implementing std::error::Error work transparently with the ? operator in tests.
Previously, this worked only by manually calling err_to_test_failure() on the test result. This isn't necessary, since the ? operator automatically invokes Into::into() as long as that's available. This change introduces the required blanket trait implementation and eliminates err_to_test_failure() altogether. This is a breaking API change. To port existing code, just remove the call to err_to_test_failure(). If the output of that is returned directly, then replace it with a ? operator usage: OLD: return do_something().err_to_test_failure(); NEW: return Ok(do_something()?); PiperOrigin-RevId: 505056332
1 parent 0dd36a7 commit 5992211

File tree

3 files changed

+19
-55
lines changed

3 files changed

+19
-55
lines changed

googletest/integration_tests/integration_tests.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod tests {
2020
use googletest::{all, matchers};
2121
use googletest::{
2222
assert_that, expect_pred, expect_that, google_test, verify_pred, verify_that,
23-
GoogleTestSupport, MapErrorToTestFailure, Result,
23+
GoogleTestSupport, Result,
2424
};
2525
#[cfg(google3)]
2626
use matchers::all;
@@ -49,24 +49,21 @@ mod tests {
4949

5050
#[google_test]
5151
fn should_fail_on_assertion_failure() -> Result<()> {
52-
let status =
53-
run_external_process("simple_assertion_failure").status().err_to_test_failure()?;
52+
let status = run_external_process("simple_assertion_failure").status()?;
5453

5554
verify_that!(status.success(), eq(false))
5655
}
5756

5857
#[google_test]
5958
fn should_fail_on_assertion_failure_with_assert_that() -> Result<()> {
60-
let status = run_external_process("simple_assertion_failure_with_assert_that")
61-
.status()
62-
.err_to_test_failure()?;
59+
let status = run_external_process("simple_assertion_failure_with_assert_that").status()?;
6360

6461
verify_that!(status.success(), eq(false))
6562
}
6663

6764
#[google_test]
6865
fn should_fail_on_assertion_failure_with_expect_that() -> Result<()> {
69-
let status = run_external_process("expect_that_failure").status().err_to_test_failure()?;
66+
let status = run_external_process("expect_that_failure").status()?;
7067

7168
verify_that!(status.success(), eq(false))
7269
}
@@ -143,25 +140,21 @@ Actual: 2, which isn't equal to 4
143140

144141
#[google_test]
145142
fn should_fail_due_to_assertion_failure_in_subroutine() -> Result<()> {
146-
let status =
147-
run_external_process("simple_assertion_failure").status().err_to_test_failure()?;
143+
let status = run_external_process("simple_assertion_failure").status()?;
148144

149145
verify_that!(status.success(), eq(false))
150146
}
151147

152148
#[google_test]
153149
fn should_fail_due_to_returned_error_in_subroutine() -> Result<()> {
154-
let status =
155-
run_external_process("failure_due_to_returned_error").status().err_to_test_failure()?;
150+
let status = run_external_process("failure_due_to_returned_error").status()?;
156151

157152
verify_that!(status.success(), eq(false))
158153
}
159154

160155
#[google_test]
161156
fn should_fail_test_on_and_log_failure() -> Result<()> {
162-
let status = run_external_process("non_fatal_failure_in_subroutine")
163-
.status()
164-
.err_to_test_failure()?;
157+
let status = run_external_process("non_fatal_failure_in_subroutine").status()?;
165158

166159
verify_that!(status.success(), eq(false))
167160
}
@@ -257,8 +250,7 @@ Actual: 2, which isn't equal to 3
257250

258251
#[google_test]
259252
fn verify_pred_should_fail_test_on_failure() -> Result<()> {
260-
let status =
261-
run_external_process("verify_predicate_with_failure").status().err_to_test_failure()?;
253+
let status = run_external_process("verify_predicate_with_failure").status()?;
262254

263255
verify_that!(status.success(), eq(false))
264256
}
@@ -281,15 +273,14 @@ eq_predicate(a, b) was false with
281273

282274
#[google_test]
283275
fn assert_pred_should_fail_test_on_failure() -> Result<()> {
284-
let status =
285-
run_external_process("assert_predicate_with_failure").status().err_to_test_failure()?;
276+
let status = run_external_process("assert_predicate_with_failure").status()?;
286277

287278
verify_that!(status.success(), eq(false))
288279
}
289280

290281
#[google_test]
291282
fn expect_pred_should_fail_test_on_failure() -> Result<()> {
292-
let status = run_external_process("expect_pred_failure").status().err_to_test_failure()?;
283+
let status = run_external_process("expect_pred_failure").status()?;
293284

294285
verify_that!(status.success(), eq(false))
295286
}
@@ -414,8 +405,7 @@ a_submodule :: A_STRUCT_IN_SUBMODULE.eq_predicate_as_method(a, b) was false with
414405

415406
#[google_test]
416407
fn fail_macro_causes_test_failure() -> Result<()> {
417-
let status =
418-
run_external_process("failure_due_to_fail_macro").status().err_to_test_failure()?;
408+
let status = run_external_process("failure_due_to_fail_macro").status()?;
419409

420410
verify_that!(status.success(), eq(false))
421411
}
@@ -478,8 +468,8 @@ Actual: 1, which isn't equal to 2
478468

479469
fn run_external_process_in_tests_directory(name: &'static str) -> Result<String> {
480470
let mut command = run_external_process(name);
481-
let std::process::Output { stdout, .. } = command.output().err_to_test_failure()?;
482-
String::from_utf8(stdout).err_to_test_failure()
471+
let std::process::Output { stdout, .. } = command.output()?;
472+
Ok(String::from_utf8(stdout)?)
483473
}
484474

485475
fn run_external_process(name: &'static str) -> Command {

googletest/src/internal/test_outcome.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,6 @@ impl TestAssertionFailure {
114114
TestOutcome::fail_current_test();
115115
print!("{}", self);
116116
}
117-
118-
pub(crate) fn from_error<T: std::fmt::Display>(err: T) -> TestAssertionFailure {
119-
TestAssertionFailure::create(format!("{}", err))
120-
}
121117
}
122118

123119
impl Display for TestAssertionFailure {
@@ -138,3 +134,9 @@ impl Debug for TestAssertionFailure {
138134
Display::fmt(self, f)
139135
}
140136
}
137+
138+
impl<T: std::error::Error> From<T> for TestAssertionFailure {
139+
fn from(value: T) -> Self {
140+
TestAssertionFailure::create(format!("{value}"))
141+
}
142+
}

googletest/src/lib.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -133,31 +133,3 @@ impl<T> GoogleTestSupport for std::result::Result<T, TestAssertionFailure> {
133133
self
134134
}
135135
}
136-
137-
/// Adds to `Result` support for ergonomically mapping the `Err` variant to a
138-
/// [`TestAssertionFailure`].
139-
pub trait MapErrorToTestFailure<T> {
140-
/// Converts the `Err` variant of a `Result` to a [`TestAssertionFailure`].
141-
///
142-
/// This allows the use of the `?` operator with any supported `Result`
143-
/// inside a test or other function returning [`Result`]. For
144-
/// example:
145-
///
146-
/// ```rust
147-
/// fn test_something() -> Result<()> {
148-
/// let output =
149-
/// std::process::Command("do_something").output()
150-
/// .err_to_test_failure()?;
151-
/// }
152-
/// ```
153-
///
154-
/// The current implementation requires that the `Err` variant of the
155-
/// `Result` implement [`std::fmt::Display`].
156-
fn err_to_test_failure(self) -> Result<T>;
157-
}
158-
159-
impl<T, E: std::fmt::Display> MapErrorToTestFailure<T> for std::result::Result<T, E> {
160-
fn err_to_test_failure(self) -> Result<T> {
161-
self.map_err(TestAssertionFailure::from_error)
162-
}
163-
}

0 commit comments

Comments
 (0)