Skip to content

Commit b2fad5d

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 55efb17 commit b2fad5d

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
@@ -322,6 +322,23 @@ where
322322
pub fn of(left: T, right: T) -> Self {
323323
Self(left, right)
324324
}
325+
326+
/// Create a `TestResult` reflecting this equivalence
327+
///
328+
/// If the two values are equal, the returned `TestResult` will indicate
329+
/// success, otherwise it will indicate failure and include a message
330+
/// reflecting both values.
331+
pub fn as_result(&self) -> TestResult {
332+
let Self(l, r) = self;
333+
if l == r {
334+
TestResult::passed()
335+
} else {
336+
TestResult::error(format!(
337+
"Missmatch! Left: '{:?}', Right: '{:?}'",
338+
l, r
339+
))
340+
}
341+
}
325342
}
326343

327344
/// `Testable` describes types (e.g., a function) whose values can be
@@ -362,14 +379,7 @@ where
362379
T: Debug + PartialEq + 'static,
363380
{
364381
fn result(&self, _: &mut Gen) -> TestResult {
365-
if self.0 == self.1 {
366-
TestResult::passed()
367-
} else {
368-
TestResult::error(format!(
369-
"Missmatch! Left: '{:?}', Right: '{:?}'",
370-
self.0, self.1
371-
))
372-
}
382+
self.as_result()
373383
}
374384
}
375385

0 commit comments

Comments
 (0)