camshaft / bolero

property testing and verification front-end for Rust
https://camshaft.github.io/bolero
MIT License
181 stars 14 forks source link

Global settings for bolero? #19

Open ckaran opened 4 years ago

ckaran commented 4 years ago

Is there a way of setting compile-time settings for all of bolero? I have very complex data structures, which lead to something similar to the following (the real code uses many different collection types, but this was the easiest way to illustrate):

use bolero::generator::*;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, TypeGenerator)]
pub struct A {
    field: Vec<u128>
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, TypeGenerator)]
pub struct B {
    field: Vec<A>
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, TypeGenerator)]
pub struct C {
    field: Vec<B>
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, TypeGenerator)]
pub struct D {
    field: Vec<C>
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, TypeGenerator)]
pub struct E {
    field: Vec<D>
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, TypeGenerator)]
pub struct F {
    field: Vec<E>
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use bolero::fuzz;

    #[test]
    fn new() {
        fuzz!().with_type::<F>().for_each(|_| {
            // Deliberately empty
        });
    }
}

I know I can manually go into each type and annotate the vectors with something like #[generator(Vec::gen().with().len(20usize..50))], but there are a lot of places where I'd need to do this. Is there a simple way of telling bolero that all collections should be limited to 5 items or fewer?

camshaft commented 4 years ago

Rather than global settings, I think a good solution would be to use the depth of the generator when determining length. Basically the deeper the value, the more likely the generated length will be on the lower end.

I plan on exploring something like this in the coming weeks.

ckaran commented 4 years ago

Basically the deeper the value, the more likely the generated length will be on the lower end.

I like that; I think that is something like what Hypothesis does, and it seems to work fairly well.