BurntSushi / quickcheck

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

#[derive(Arbitrary)] #175

Closed remexre closed 4 years ago

remexre commented 7 years ago

Summary:

Implements a stable-friendly #[derive(Arbitrary)], with support for generation, shrinking, and constraints. Example:

#[macro_use]
extern crate quickcheck;
#[macro_use]
extern crate quickcheck_derive;

#[derive(Arbitrary, Clone, Debug)]
#[arbitrary(constraint = "this.alpha == this.bravo.is_positive()")]
struct TestStruct {
    alpha: bool,
    bravo: isize,
}

quickcheck! {
    fn struct_constraint(t: TestStruct) -> bool {
        t.alpha == t.bravo.is_positive()
    }
}

Known Issues:

Marwes commented 7 years ago

Struct shrinking is implementing by transforming a struct into a tuple, shrinking the tuple, and mapping it back to the struct. 9-tuples and larger cannot be shrunk (see #146), so structs with more than 8 members cannot either.

This could be fixed by nesting tuples ie, for a three field struct you could generate an arbitrary for (A, (B, C))

marcelbuesing commented 6 years ago

What's the state of this pull request? Automated deriving of instances would be really great!

remexre commented 6 years ago

@marcelbuesing It's fallen off my plate more or less completely; if someone else wants to take it across the finish line, that'll probably happen before I have time to come back to it.

BurntSushi commented 4 years ago

https://github.com/BurntSushi/quickcheck/pull/222#issuecomment-573267560