@@ -53,7 +53,7 @@ pub mod prelude {
53
53
pub use super :: matchers:: * ;
54
54
pub use super :: verify_current_test_outcome;
55
55
pub use super :: GoogleTestSupport ;
56
- pub use super :: IntoTestResult ;
56
+ pub use super :: OrFail ;
57
57
pub use super :: Result ;
58
58
// Assert macros
59
59
pub use super :: {
@@ -238,8 +238,8 @@ impl<T> GoogleTestSupport for std::result::Result<T, TestAssertionFailure> {
238
238
}
239
239
}
240
240
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 .
243
243
///
244
244
/// A type can implement this trait to provide an easy way to return immediately
245
245
/// 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> {
252
252
/// ```ignore
253
253
/// #[test]
254
254
/// 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 ()?;
257
257
/// ...
258
258
/// }
259
259
///
260
260
/// fn something_which_can_fail() -> std::result::Result<T, String> { ... }
261
261
/// fn something_which_can_fail_with_option() -> Option<T> { ... }
262
262
/// ```
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`].
265
269
///
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 > ;
271
274
}
272
275
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
+
274
279
#[ track_caller]
275
- fn into_test_result ( self ) -> std:: result:: Result < T , TestAssertionFailure > {
280
+ fn or_fail ( self ) -> std:: result:: Result < T , TestAssertionFailure > {
276
281
match self {
277
282
Ok ( t) => Ok ( t) ,
278
283
Err ( e) => Err ( TestAssertionFailure :: create ( format ! ( "{e:?}" ) ) ) ,
279
284
}
280
285
}
281
286
}
282
287
283
- impl < T > IntoTestResult < T > for Option < T > {
288
+ impl < T > OrFail for Option < T > {
289
+ type Output = T ;
290
+
284
291
#[ track_caller]
285
- fn into_test_result ( self ) -> std:: result:: Result < T , TestAssertionFailure > {
292
+ fn or_fail ( self ) -> std:: result:: Result < T , TestAssertionFailure > {
286
293
match self {
287
294
Some ( t) => Ok ( t) ,
288
295
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" ,
290
297
std:: any:: type_name:: <T >( )
291
298
) ) ) ,
292
299
}
0 commit comments