surprise-me
Generating random type instances.
Surprise
is a simple trait whose sole purpose is to generate random things.
If you need a more sophisticated approach to prop testing and the like, check out proptest and quicktest.
Deriving the trait
use HashMap;
use ;
let mut rng = thread_rng;
let value: MyEnum = generate;
Using the trait
use Surprise;
use ;
use rand; // Re-export of the rand crate
let mut rng = thread_rng;
// Generate a vec filled with random bytes
let vec: = generate;
// Generating as above uses the type's default surprise factor.
// In the case of a vec that means a maximum length of 100.
// To fine-tune these factors, create one and pass it to the generation like so:
let factor = VecSurprise ;
// Vec containing at most 10 random capital ASCII letter bytes
let vec: = generate_with_factor;