proptest_arbitrary/
extras.rs

1//! Extra strategies.
2
3use super::*;
4use std::fmt::{Debug, Formatter, Result as FResult};
5use proptest::test_runner::TestRunner;
6
7/// Shorthand for `Generator<V, fn() -> V>`.
8pub type FnGenerator<V> = Generator<V, fn() -> V>;
9
10/// Strategy for generating `V`s from an `Fn() -> V`.
11/// It's not a very interesting Strategy, but required sometimes
12/// when the only means of creating an object of some type is
13/// via a function while type type is also not Clone.
14#[derive(Clone, Copy)]
15pub struct Generator<V, F: Fn() -> V> {
16    generator: F,
17}
18
19impl<V, F: Fn() -> V> Generator<V, F> {
20    /// Constructs a `Generator` strategy.
21    pub fn new(generator: F) -> Self {
22        Self { generator }
23    }
24}
25
26impl<V: Debug, F: Clone + Fn() -> V> Strategy for Generator<V, F> {
27    type Value = Self;
28    fn new_value(&self, _: &mut TestRunner) -> Result<Self::Value, String> {
29        Ok(Generator { generator: self.generator.clone() })
30    }
31}
32
33impl<V: Debug, F: Fn() -> V> ValueTree for Generator<V, F> {
34    type Value = V;
35    fn current(&self) -> Self::Value { (self.generator)() }
36    fn simplify(&mut self) -> bool { false }
37    fn complicate(&mut self) -> bool { false }
38}
39
40impl<V, F: Fn() -> V> Debug for Generator<V, F> {
41    fn fmt(&self, fmt: &mut Formatter) -> FResult {
42        fmt.debug_struct("Generator")
43           .field("generator", &"<function>")
44           .finish()
45    }
46}