Skip to content

Commit 2720c76

Browse files
cramertjcopybara-github
authored andcommitted
Rename into_test_result to or_fail
This name is shorter and aligns with the existing `or_` methods on `Option` and `Result`. It reads more clearly when used in situations like `let error = my_result.err().or_fail()?;` This CL also removes IntoTestResult's generic parameter. All types that implement IntoTestResult do so for only one particular output type, so the generic parameter is unnecessary. NOTE: this is a backwards-incompatible change. PiperOrigin-RevId: 712700561
1 parent 887b137 commit 2720c76

7 files changed

+35
-30
lines changed

googletest/crate_docs.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,8 @@ fn test_png_image_dimensions() -> googletest::Result<()> {
475475
```
476476

477477
If your setup function returns `Option<T>` or `std::result::Result<T, E>` where
478-
`E: !std::error::Error`, then you can convert these types with `into_test_result()`
479-
from the `IntoTestResult` extension trait.
478+
`E: !std::error::Error`, then you can convert these types with `or_fail()`
479+
from the `OrFail` extension trait.
480480

481481
```
482482
# use googletest::prelude::*;
@@ -494,7 +494,7 @@ impl PngImage {
494494
# */
495495
fn test_png_image_binary() -> googletest::Result<()> {
496496
// Arrange
497-
let png_image = PngImage::new_from_binary(&PNG_BINARY).into_test_result()?;
497+
let png_image = PngImage::new_from_binary(&PNG_BINARY).or_fail()?;
498498
/* ... */
499499
# Ok(())
500500
}
@@ -511,7 +511,7 @@ impl PngImage {
511511
# */
512512
fn test_png_from_cache() -> googletest::Result<()> {
513513
// Arrange
514-
let png_image = PngImage::new_from_cache(123).into_test_result()?;
514+
let png_image = PngImage::new_from_cache(123).or_fail()?;
515515
/* ... */
516516
# Ok(())
517517
}
@@ -528,6 +528,6 @@ GoogleTest assertion failures into Proptest
528528
through the `?` operator.
529529

530530
[`and_log_failure()`]: GoogleTestSupport::and_log_failure
531-
[`into_test_result()`]: IntoTestResult::into_test_result
531+
[`or_fail()`]: OrFail::or_fail
532532
[`Matcher`]: matcher::Matcher
533533
[`MatcherBase`]: matcher::MatcherBase

googletest/src/lib.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub mod prelude {
5353
pub use super::matchers::*;
5454
pub use super::verify_current_test_outcome;
5555
pub use super::GoogleTestSupport;
56-
pub use super::IntoTestResult;
56+
pub use super::OrFail;
5757
pub use super::Result;
5858
// Assert macros
5959
pub use super::{
@@ -238,8 +238,8 @@ impl<T> GoogleTestSupport for std::result::Result<T, TestAssertionFailure> {
238238
}
239239
}
240240

241-
/// Provides an extension method for converting an arbitrary type into a
242-
/// [`Result`].
241+
/// Provides an extension method for converting an arbitrary type into
242+
/// `googletest`'s [`Result`] type.
243243
///
244244
/// A type can implement this trait to provide an easy way to return immediately
245245
/// from a test in conjunction with the `?` operator. This is useful for
@@ -252,41 +252,48 @@ impl<T> GoogleTestSupport for std::result::Result<T, TestAssertionFailure> {
252252
/// ```ignore
253253
/// #[test]
254254
/// fn should_work() -> googletest::Result<()> {
255-
/// let value = something_which_can_fail().into_test_result()?;
256-
/// let value = something_which_can_fail_with_option().into_test_result()?;
255+
/// let value = something_which_can_fail().or_fail()?;
256+
/// let value = something_which_can_fail_with_option().or_fail()?;
257257
/// ...
258258
/// }
259259
///
260260
/// fn something_which_can_fail() -> std::result::Result<T, String> { ... }
261261
/// fn something_which_can_fail_with_option() -> Option<T> { ... }
262262
/// ```
263-
pub trait IntoTestResult<T> {
264-
/// Converts this instance into a [`Result`].
263+
pub trait OrFail {
264+
/// The success type of the test result.
265+
type Output;
266+
267+
/// Converts a value into a [`Result`] containing
268+
/// either the [`Self::Output`] type or a [`TestAssertionFailure`].
265269
///
266-
/// Typically, the `Self` type is itself an implementation of the
267-
/// [`std::ops::Try`] trait. This method should then map the `Residual`
268-
/// variant to a [`TestAssertionFailure`] and leave the `Output` variant
269-
/// unchanged.
270-
fn into_test_result(self) -> Result<T>;
270+
/// The most frequently used implementations convert
271+
/// `Result<T, E>` into `Result<T, TestAssertionFailure>` and
272+
/// `Option<T>` into `Result<T, TestAssertionFailure>`.
273+
fn or_fail(self) -> Result<Self::Output>;
271274
}
272275

273-
impl<T, E: std::fmt::Debug> IntoTestResult<T> for std::result::Result<T, E> {
276+
impl<T, E: std::fmt::Debug> OrFail for std::result::Result<T, E> {
277+
type Output = T;
278+
274279
#[track_caller]
275-
fn into_test_result(self) -> std::result::Result<T, TestAssertionFailure> {
280+
fn or_fail(self) -> std::result::Result<T, TestAssertionFailure> {
276281
match self {
277282
Ok(t) => Ok(t),
278283
Err(e) => Err(TestAssertionFailure::create(format!("{e:?}"))),
279284
}
280285
}
281286
}
282287

283-
impl<T> IntoTestResult<T> for Option<T> {
288+
impl<T> OrFail for Option<T> {
289+
type Output = T;
290+
284291
#[track_caller]
285-
fn into_test_result(self) -> std::result::Result<T, TestAssertionFailure> {
292+
fn or_fail(self) -> std::result::Result<T, TestAssertionFailure> {
286293
match self {
287294
Some(t) => Ok(t),
288295
None => Err(TestAssertionFailure::create(format!(
289-
"called `Option::into_test_result()` on a `Option::<{}>::None` value",
296+
"called `Option::or_fail()` on a `Option::<{}>::None` value",
290297
std::any::type_name::<T>()
291298
))),
292299
}

googletest/tests/proptest_integration_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ use proptest::test_runner::{Config, TestRunner};
2020
#[test]
2121
fn numbers_are_greater_than_zero() -> Result<()> {
2222
let mut runner = TestRunner::new(Config::default());
23-
runner.run(&(1..100i32), |v| Ok(verify_that!(v, gt(0))?)).into_test_result()
23+
runner.run(&(1..100i32), |v| Ok(verify_that!(v, gt(0))?)).or_fail()
2424
}
2525

2626
#[test]
2727
fn strings_are_nonempty() -> Result<()> {
2828
let mut runner = TestRunner::new(Config::default());
29-
runner.run(&"[a-zA-Z0-9]+", |v| Ok(verify_that!(v, not(eq("")))?)).into_test_result()
29+
runner.run(&"[a-zA-Z0-9]+", |v| Ok(verify_that!(v, not(eq("")))?)).or_fail()
3030
}

integration_tests/src/integration_tests.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,9 +1790,7 @@ mod tests {
17901790
verify_that!(
17911791
output,
17921792
all![
1793-
contains_substring(
1794-
"called `Option::into_test_result()` on a `Option::<()>::None` value"
1795-
),
1793+
contains_substring("called `Option::or_fail()` on a `Option::<()>::None` value"),
17961794
contains_substring("test_returning_option.rs:23")
17971795
]
17981796
)

integration_tests/src/test_returning_anyhow_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod tests {
2020

2121
#[test]
2222
fn should_fail_due_to_error_in_subroutine() -> Result<()> {
23-
returns_anyhow_error().into_test_result()?;
23+
returns_anyhow_error().or_fail()?;
2424
Ok(())
2525
}
2626

integration_tests/src/test_returning_option.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod tests {
2020

2121
#[test]
2222
fn should_fail_due_to_none_in_subroutine() -> Result<()> {
23-
returns_option().into_test_result()?;
23+
returns_option().or_fail()?;
2424
Ok(())
2525
}
2626

integration_tests/src/test_returning_string_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod tests {
2020

2121
#[test]
2222
fn should_fail_due_to_error_in_subroutine() -> Result<()> {
23-
returns_string_error().into_test_result()?;
23+
returns_string_error().or_fail()?;
2424
Ok(())
2525
}
2626

0 commit comments

Comments
 (0)