Skip to content

Commit f79da85

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 feaa4ed commit f79da85

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

src/tester.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,23 @@ where
353353
pub fn of(left: T, right: T) -> Self {
354354
Self(left, right)
355355
}
356+
357+
/// Create a `TestResult` reflecting this equivalence
358+
///
359+
/// If the two values are equal, the returned `TestResult` will indicate
360+
/// success, otherwise it will indicate failure and include a message
361+
/// reflecting both values.
362+
pub fn as_result(&self) -> TestResult {
363+
let Self(l, r) = self;
364+
if l == r {
365+
TestResult::passed()
366+
} else {
367+
TestResult::error(format!(
368+
"Missmatch! Left: '{:?}', Right: '{:?}'",
369+
l, r
370+
))
371+
}
372+
}
356373
}
357374

358375
/// `Testable` describes types (e.g., a function) whose values can be
@@ -393,14 +410,7 @@ where
393410
T: Debug + PartialEq + 'static,
394411
{
395412
fn result(&self, _: &mut Gen) -> TestResult {
396-
if self.0 == self.1 {
397-
TestResult::passed()
398-
} else {
399-
TestResult::error(format!(
400-
"Missmatch! Left: '{:?}', Right: '{:?}'",
401-
self.0, self.1
402-
))
403-
}
413+
self.as_result()
404414
}
405415
}
406416

0 commit comments

Comments
 (0)