You can use the trait_tests
crate, though I do believe the crate is merely an experiment, so there may be rough edges.
Specifically, I couldn't figure out how to actually test all implementations of Metric<T>
, rather only for a concrete type, Metric<i32>
.
For your example:
use trait_tests::*;
pub trait Metric<T> {
fn distance(o1: &T, o2: &T) -> f64;
}
#[trait_tests]
pub trait MetricTests: Metric<i32> {
fn test_distance() {
// These could possibly be extended using quickcheck or proptest
assert!(Self::distance(&42, &42) == 0.0);
}
}
struct CartesianPlane {}
#[test_impl]
impl Metric<i32> for CartesianPlane {
fn distance(o1: &i32, o2: &i32) -> f64 {
(*o2 - *o1) as f64
}
}
Then cargo test
should include the auto-generated test for the implementors of the trait that are annotated with #[test_impl]
.
assert_eq!
, or return aOption
/Result
if the distance is not valid. – hellow