BurntSushi / quickcheck

Automated property based testing for Rust (with shrinking).
The Unlicense
2.4k stars 149 forks source link

Using the function traits would improve usability. #78

Closed michaelsproul closed 9 years ago

michaelsproul commented 9 years ago

At the moment when passing a function to quickcheck, its type has to specified:

fn prop() -> bool {
    true
}

quickcheck(prop as fn() -> bool);

However, bare functions can be passed as generic items implementing Fn traits without any casting:

fn run<F, T>(f: F) -> T where F: Fn() -> T {
    f()
}

fn prop() -> bool {
    true
}

fn main() {
    let t = run(prop);
    println!("{}", t);
}

If we implemented the Testable trait for F where F: Fn(A, ...) -> T instead of the concrete fn(A, ...) - T types, it would be possible to just do:

quickcheck(function);

I think this is a usability win, especially with #[quickcheck] unusable under 1.0 (people are likely to be writing quickcheck quite a bit).

I had a quick go at implementing this, but ran into trouble with the Fun trait. I couldn't work out a way to call the function (passed as &self) in a move closure passed to safe. It may be possible, or else more extensive restructuring may be required.

michaelsproul commented 9 years ago

Ah, just found #56.

BurntSushi commented 9 years ago

Yup, this is a duplicate of #56.

Even since the introduction of the Fn traits, I've tried to modify quickcheck to use them, but I haven't succeeded yet. As my comment in #56 notes, I think I'm currently blocked on this: https://github.com/rust-lang/rust/issues/25041