rapid
rapid copied to clipboard
Add constant generators
I'd like to generate one of a slice of constants to be able for instance to draw from a finite slice of hard-coded user IDs strings.
With generators of constants one would be able to use #2 wrapped in a call to OneOf.
Adding a Weighted taking a slice of int-weighted pairs of generators would help in my testing too. (A call to Weighted with generators of equal weights should be equivalent to a call to OneOf). Weights must be > 0.
EDIT: WRT state machine Being able to weight actions based on which ones to run most would be nice.
SampledFrom() does that: https://play.golang.org/p/7YF4zHPkJhS
Regarding weights, I believe that for the majority of tests that they are too low-level of a thing to bother. Also, explicit weights create questions like should a weight be higher for the "common" case or the "rare" case?
For now I want to follow Hypothesis and keep API as high-level and "magical" as possible.
Also, for 1-element SampledFrom() there is a Just() shortcut. So, you can do OneOf(Just(1), Just(2), Just(3)), but this is just a harder to type and slower SampledFrom([]int{1, 2, 3}).
The documentation (and maybe the naming?) should definitely be improved.
I concur on staying on Hypothesis' expressive side however it would be helpful to have a way to prioritize which statem actions to run most. For instance you'd want the login/logout calls to be run less often than all the other calls.
Not sure if it's currently possible but OneOf+Just is the only way to sample from different generators, no? The way SampleFrom uses the given interface's Item() assumes Evey item in the slice is of the same type. Maybe this impl should forbid giving a slice of interface{} as that would probably mean the caller is trying to fiddle a bit too much and should be calling OneOf with a slice of non interface Just calls.
Right now all generators enforce the rule that they generate items of the same type (which can be an interface, though). OneOf(Just(true), Just(0)) is invalid; OneOf() assumes that all of its arguments generate items of the same type. However SampledFrom([]interface{true, 0}) is OK, because the type of SampledFrom() is derived from the type of the slice.
Closing as (hopefuly) fixed in #43.