Macro proptest::prelude::prop_oneof [−][src]
Produce a strategy which picks one of the listed choices.
This is conceptually equivalent to calling prop_union
on the first two
elements and then chaining .or()
onto the rest after implicitly boxing
all of them. As with Union
, values shrink across elements on the
assumption that earlier ones are "simpler", so they should be listed in
order of ascending complexity when possible.
The macro invocation has two forms. The first is to simply list the
strategies separated by commas; this will cause value generation to pick
from the strategies uniformly. The other form is to provide a weight in the
form of a u32
before each strategy, separated from the strategy with
=>
.
Note that the exact type returned by the macro varies depending on how many inputs there are. In particular, if given exactly one option, it will return it unmodified. It is not recommended to depend on the particular type produced by this macro.
Example
use proptest::prelude::*; #[derive(Clone, Copy, Debug)] enum MyEnum { Big(u64), Medium(u32), Little(i16), } let my_enum_strategy = prop_oneof![ prop::num::i16::ANY.prop_map(MyEnum::Little), prop::num::u32::ANY.prop_map(MyEnum::Medium), prop::num::u64::ANY.prop_map(MyEnum::Big), ]; let my_weighted_strategy = prop_oneof![ 1 => prop::num::i16::ANY.prop_map(MyEnum::Little), // Chose `Medium` twice as frequently as either `Little` or `Big`; i.e., // around 50% of values will be `Medium`, and 25% for each of `Little` // and `Big`. 2 => prop::num::u32::ANY.prop_map(MyEnum::Medium), 1 => prop::num::u64::ANY.prop_map(MyEnum::Big), ];