Closed
Description
After an discussion about how for example 0 == -0
, and how it interacts with traits, I had the thought it might be nice to have unit tests on traits that get generated for for each impl
of it.
Example:
trait Bar {
static fn zero() -> Self;
fn neg(&self) -> Self;
}
#[test(trait)]
fn test_bar<T: Bar>() {
let x: T = Bar::zero();
let y = x.neg();
assert x == y;
}
in some other crate:
impl Bar for float {
static fn zero() -> float { 0.0 }
fn neg(&self) -> float { - *self }
}
Then a rustc --test for that crate would generate this function:
#[test]
fn test_bar_float() { test_bar::<float>() }
This would require trait test to somehow be made publicly callable from other crates for test compilation.
Alternative example, which might be easier to implement:
trait Bar {
static fn zero() -> Self;
fn neg(&self) -> Self;
#[test]
fn test_bar() {
let x: Self = zero();
let y = x.neg();
assert x == y;
}
}