proptest

Macro prop_oneof

source
macro_rules! prop_oneof {
    ($($item:expr),+ $(,)?) => { ... };
    ($_weight0:expr => $item0:expr $(,)?) => { ... };
    ($weight0:expr => $item0:expr,
     $weight1:expr => $item1:expr $(,)?) => { ... };
    ($weight0:expr => $item0:expr,
     $weight1:expr => $item1:expr,
     $weight2:expr => $item2:expr $(,)?) => { ... };
    ($weight0:expr => $item0:expr,
     $weight1:expr => $item1:expr,
     $weight2:expr => $item2:expr,
     $weight3:expr => $item3:expr $(,)?) => { ... };
    ($weight0:expr => $item0:expr,
     $weight1:expr => $item1:expr,
     $weight2:expr => $item2:expr,
     $weight3:expr => $item3:expr,
     $weight4:expr => $item4:expr $(,)?) => { ... };
    ($weight0:expr => $item0:expr,
     $weight1:expr => $item1:expr,
     $weight2:expr => $item2:expr,
     $weight3:expr => $item3:expr,
     $weight4:expr => $item4:expr,
     $weight5:expr => $item5:expr $(,)?) => { ... };
    ($weight0:expr => $item0:expr,
     $weight1:expr => $item1:expr,
     $weight2:expr => $item2:expr,
     $weight3:expr => $item3:expr,
     $weight4:expr => $item4:expr,
     $weight5:expr => $item5:expr,
     $weight6:expr => $item6:expr $(,)?) => { ... };
    ($weight0:expr => $item0:expr,
     $weight1:expr => $item1:expr,
     $weight2:expr => $item2:expr,
     $weight3:expr => $item3:expr,
     $weight4:expr => $item4:expr,
     $weight5:expr => $item5:expr,
     $weight6:expr => $item6:expr,
     $weight7:expr => $item7:expr $(,)?) => { ... };
    ($weight0:expr => $item0:expr,
     $weight1:expr => $item1:expr,
     $weight2:expr => $item2:expr,
     $weight3:expr => $item3:expr,
     $weight4:expr => $item4:expr,
     $weight5:expr => $item5:expr,
     $weight6:expr => $item6:expr,
     $weight7:expr => $item7:expr,
     $weight8:expr => $item8:expr $(,)?) => { ... };
    ($weight0:expr => $item0:expr,
     $weight1:expr => $item1:expr,
     $weight2:expr => $item2:expr,
     $weight3:expr => $item3:expr,
     $weight4:expr => $item4:expr,
     $weight5:expr => $item5:expr,
     $weight6:expr => $item6:expr,
     $weight7:expr => $item7:expr,
     $weight8:expr => $item8:expr,
     $weight9:expr => $item9:expr $(,)?) => { ... };
    ($($weight:expr => $item:expr),+ $(,)?) => { ... };
}
Expand description

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),
];