Skip to content

Commit da6b9e9

Browse files
committed
Add a function to Equivalence for convenient conversion to a TestResult
`quickcheck` allows to skip tests (for given inputs) by returning a `TestResult` indicating that it should be ignored. Naturally, an `Equivalence` cannot express such an intent and neither can any other `Testable`s other than functions and `TestResult` itself. Hence, if we need the ability to skip tests, we need to return a `TestResult` or some dependent type (e.g. `Result<TestResult>`) from our property function. If we still want to make use of `Equivalence` in such tests, we need to convert it to a `TestResult`. Previously, we had to resort to using `Testable::result` with some dummy `&mut Gen`, even though it isn't even used in the conversion. As a remedy, this change moves the conversion into a dedicates function (with no additional parameters) which may be called inside property functions.
1 parent 25d155f commit da6b9e9

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/tester.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,23 @@ where
347347
pub fn of(left: T, right: T) -> Self {
348348
Self(left, right)
349349
}
350+
351+
/// Create a `TestResult` reflecting this equivalence
352+
///
353+
/// If the two values are equal, the returned `TestResult` will indicate
354+
/// success, otherwise it will indicate failure and include a message
355+
/// reflecting both values.
356+
pub fn as_result(&self) -> TestResult {
357+
let Self(l, r) = self;
358+
if l == r {
359+
TestResult::passed()
360+
} else {
361+
TestResult::error(format!(
362+
"Missmatch! Left: '{:?}', Right: '{:?}'",
363+
l, r
364+
))
365+
}
366+
}
350367
}
351368

352369
/// `Testable` describes types (e.g., a function) whose values can be
@@ -387,14 +404,7 @@ where
387404
T: Debug + PartialEq + 'static,
388405
{
389406
fn result(&self, _: &mut Gen) -> TestResult {
390-
if self.0 == self.1 {
391-
TestResult::passed()
392-
} else {
393-
TestResult::error(format!(
394-
"Missmatch! Left: '{:?}', Right: '{:?}'",
395-
self.0, self.1
396-
))
397-
}
407+
self.as_result()
398408
}
399409
}
400410

0 commit comments

Comments
 (0)