Skip to content

Generated tests for trait impls #616

Open
@steveklabnik

Description

@steveklabnik

Issue by Kimundi
Monday Feb 04, 2013 at 12:53 GMT

For earlier discussion, see rust-lang/rust#4782

This issue was labelled with: A-an-interesting-project, A-testsuite, A-traits, I-wishlist in the Rust repository


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;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-dev-toolsRelevant to the development tools team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions