Trait proptest::strategy::Strategy
[−]
[src]
pub trait Strategy { type Value: ValueTree; fn new_value(&self, runner: &mut TestRunner) -> Result<Self::Value, String>; fn prop_map<O: Debug, F: Fn(<Self::Value as ValueTree>::Value) -> O>(
self,
fun: F
) -> Map<Self, F>
where
Self: Sized, { ... } fn prop_filter<F: Fn(&<Self::Value as ValueTree>::Value) -> bool>(
self,
whence: String,
fun: F
) -> Filter<Self, F>
where
Self: Sized, { ... } fn prop_union(self, other: Self) -> Union<Self>
where
Self: Sized, { ... } fn boxed(self) -> BoxedStrategy<<Self::Value as ValueTree>::Value>
where
Self: Sized + 'static, { ... } fn no_shrink(self) -> NoShrink<Self>
where
Self: Sized, { ... } }
A strategy for producing arbitrary values of a given type.
Associated Types
type Value: ValueTree
The value tree generated by this Strategy
.
This also implicitly describes the ultimate value type governed by the
Strategy
.
Required Methods
fn new_value(&self, runner: &mut TestRunner) -> Result<Self::Value, String>
Generate a new value tree from the given runner.
This may fail if there are constraints on the generated value and the
generator is unable to produce anything that satisfies them. Any
failure is wrapped in TestError::Abort
.
Provided Methods
fn prop_map<O: Debug, F: Fn(<Self::Value as ValueTree>::Value) -> O>(
self,
fun: F
) -> Map<Self, F> where
Self: Sized,
self,
fun: F
) -> Map<Self, F> where
Self: Sized,
Returns a strategy which produces values transformed by the function
fun
.
There is no need (or possibility, for that matter) to define how the output is to be shrunken. Shrinking continues to take place in terms of the source value.
fn prop_filter<F: Fn(&<Self::Value as ValueTree>::Value) -> bool>(
self,
whence: String,
fun: F
) -> Filter<Self, F> where
Self: Sized,
self,
whence: String,
fun: F
) -> Filter<Self, F> where
Self: Sized,
Returns a strategy which only produces values accepted by fun
.
This results in a very naïve form of rejection sampling and should only
be used if (a) relatively few values will actually be rejected; (b) it
isn't easy to express what you want by using another strategy and/or
map()
.
There are a lot of downsides to this form of filtering. It slows
testing down, since values must be generated but then discarded.
Proptest only allows a limited number of rejects this way (across the
entire TestRunner
). Rejection can interfere with shrinking;
particularly, complex filters may largely or entirely prevent shrinking
from substantially altering the original value.
Local rejection sampling is still preferable to rejecting the entire
input to a test (via TestCaseError::Reject
), however, and the default
number of local rejections allowed is much higher than the number of
whole-input rejections.
whence
is used to record where and why the rejection occurred.
fn prop_union(self, other: Self) -> Union<Self> where
Self: Sized,
Self: Sized,
Returns a strategy which picks uniformly from self
and other
.
When shrinking, if a value from other
was originally chosen but that
value can be shrunken no further, it switches to a value from self
and starts shrinking that.
Be aware that chaining prop_union
calls will result in a very
right-skewed distribution. If this is not what you want, you can call
the .or()
method on the Union
to add more values to the same union,
or directly call Union::new()
.
Both self
and other
must be of the same type. To combine
heterogeneous strategies, call the boxed()
method on both self
and
other
to erase the type differences before calling prop_union()
.
fn boxed(self) -> BoxedStrategy<<Self::Value as ValueTree>::Value> where
Self: Sized + 'static,
Self: Sized + 'static,
Erases the type of this Strategy
so it can be passed around as a
simple trait object.
fn no_shrink(self) -> NoShrink<Self> where
Self: Sized,
Self: Sized,
Wraps this strategy to prevent values from being subject to shrinking.
Suppressing shrinking is useful when testing things like linear
approximation functions. Ordinarily, proptest will tend to shrink the
input to the function until the result is just barely outside the
acceptable range whereas the original input may have produced a result
far outside of it. Since this makes it harder to see what the actual
problem is, making the input NoShrink
allows learning about inputs
that produce more incorrect results.
Implementors
impl<S: Strategy + ?Sized> Strategy for Box<S>
impl<'a, S: Strategy + ?Sized> Strategy for &'a S
impl<'a, S: Strategy + ?Sized> Strategy for &'a mut S
impl<S: Strategy + ?Sized> Strategy for Rc<S>
impl<S: Strategy + ?Sized> Strategy for Arc<S>
impl<S: Strategy, O: Debug, F: Fn(<S::Value as ValueTree>::Value) -> O> Strategy for Map<S, F>
impl<S: Strategy, F: Fn(&<S::Value as ValueTree>::Value) -> bool> Strategy for Filter<S, F>
impl<T: Strategy> Strategy for Union<T>
impl<T: Clone + Debug> Strategy for Singleton<T>
impl<T: Strategy> Strategy for NoShrink<T>
impl Strategy for proptest::bool::Any
impl Strategy for proptest::num::i8::Any
impl Strategy for Range<i8>
impl Strategy for RangeFrom<i8>
impl Strategy for RangeTo<i8>
impl Strategy for proptest::num::i16::Any
impl Strategy for Range<i16>
impl Strategy for RangeFrom<i16>
impl Strategy for RangeTo<i16>
impl Strategy for proptest::num::i32::Any
impl Strategy for Range<i32>
impl Strategy for RangeFrom<i32>
impl Strategy for RangeTo<i32>
impl Strategy for proptest::num::i64::Any
impl Strategy for Range<i64>
impl Strategy for RangeFrom<i64>
impl Strategy for RangeTo<i64>
impl Strategy for proptest::num::isize::Any
impl Strategy for Range<isize>
impl Strategy for RangeFrom<isize>
impl Strategy for RangeTo<isize>
impl Strategy for proptest::num::u8::Any
impl Strategy for Range<u8>
impl Strategy for RangeFrom<u8>
impl Strategy for RangeTo<u8>
impl Strategy for proptest::num::u16::Any
impl Strategy for Range<u16>
impl Strategy for RangeFrom<u16>
impl Strategy for RangeTo<u16>
impl Strategy for proptest::num::u32::Any
impl Strategy for Range<u32>
impl Strategy for RangeFrom<u32>
impl Strategy for RangeTo<u32>
impl Strategy for proptest::num::u64::Any
impl Strategy for Range<u64>
impl Strategy for RangeFrom<u64>
impl Strategy for RangeTo<u64>
impl Strategy for proptest::num::usize::Any
impl Strategy for Range<usize>
impl Strategy for RangeFrom<usize>
impl Strategy for RangeTo<usize>
impl Strategy for proptest::num::f32::Any
impl Strategy for Range<f32>
impl Strategy for RangeFrom<f32>
impl Strategy for RangeTo<f32>
impl Strategy for proptest::num::f64::Any
impl Strategy for Range<f64>
impl Strategy for RangeFrom<f64>
impl Strategy for RangeTo<f64>
impl<T: BitSetLike> Strategy for BitSetStrategy<T>
impl<A: Strategy> Strategy for (A,)
impl<A: Strategy, B: Strategy> Strategy for (A, B)
impl<A: Strategy, B: Strategy, C: Strategy> Strategy for (A, B, C)
impl<A: Strategy, B: Strategy, C: Strategy, D: Strategy> Strategy for (A, B, C, D)
impl<A: Strategy, B: Strategy, C: Strategy, D: Strategy, E: Strategy> Strategy for (A, B, C, D, E)
impl<A: Strategy, B: Strategy, C: Strategy, D: Strategy, E: Strategy, F: Strategy> Strategy for (A, B, C, D, E, F)
impl<A: Strategy, B: Strategy, C: Strategy, D: Strategy, E: Strategy, F: Strategy, G: Strategy> Strategy for (A, B, C, D, E, F, G)
impl<A: Strategy, B: Strategy, C: Strategy, D: Strategy, E: Strategy, F: Strategy, G: Strategy, H: Strategy> Strategy for (A, B, C, D, E, F, G, H)
impl<A: Strategy, B: Strategy, C: Strategy, D: Strategy, E: Strategy, F: Strategy, G: Strategy, H: Strategy, I: Strategy> Strategy for (A, B, C, D, E, F, G, H, I)
impl<A: Strategy, B: Strategy, C: Strategy, D: Strategy, E: Strategy, F: Strategy, G: Strategy, H: Strategy, I: Strategy, J: Strategy> Strategy for (A, B, C, D, E, F, G, H, I, J)
impl<S: Strategy> Strategy for [S; 1]
impl<S: Strategy> Strategy for [S; 2]
impl<S: Strategy> Strategy for [S; 3]
impl<S: Strategy> Strategy for [S; 4]
impl<S: Strategy> Strategy for [S; 5]
impl<S: Strategy> Strategy for [S; 6]
impl<S: Strategy> Strategy for [S; 7]
impl<S: Strategy> Strategy for [S; 8]
impl<S: Strategy> Strategy for [S; 9]
impl<S: Strategy> Strategy for [S; 10]
impl<S: Strategy> Strategy for [S; 11]
impl<S: Strategy> Strategy for [S; 12]
impl<S: Strategy> Strategy for [S; 13]
impl<S: Strategy> Strategy for [S; 14]
impl<S: Strategy> Strategy for [S; 15]
impl<S: Strategy> Strategy for [S; 16]
impl<S: Strategy> Strategy for [S; 17]
impl<S: Strategy> Strategy for [S; 18]
impl<S: Strategy> Strategy for [S; 19]
impl<S: Strategy> Strategy for [S; 20]
impl<S: Strategy> Strategy for [S; 21]
impl<S: Strategy> Strategy for [S; 22]
impl<S: Strategy> Strategy for [S; 23]
impl<S: Strategy> Strategy for [S; 24]
impl<S: Strategy> Strategy for [S; 25]
impl<S: Strategy> Strategy for [S; 26]
impl<S: Strategy> Strategy for [S; 27]
impl<S: Strategy> Strategy for [S; 28]
impl<S: Strategy> Strategy for [S; 29]
impl<S: Strategy> Strategy for [S; 30]
impl<S: Strategy> Strategy for [S; 31]
impl<S: Strategy> Strategy for [S; 32]
impl<T: Strategy> Strategy for VecStrategy<T>
impl<'a> Strategy for CharStrategy<'a>
impl Strategy for str