#recursion #proptest #strategies #helper #strategy #clone #mutually

proptest-recurse

Helper for defining mutually recursive strategies with proptest

5 releases (breaking)

0.5.0 Aug 9, 2021
0.4.0 Sep 4, 2020
0.3.0 Oct 19, 2019
0.2.0 Nov 3, 2018
0.1.0 Nov 1, 2018

#1256 in Data structures

42 downloads per month

MIT/Apache

11KB
152 lines

Crates.io Docs.rs

proptest-recurse

Helper for defining mutually recursive strategies with proptest.


lib.rs:

This crate provides a helper struct for defining mutually recursive strategies with proptest. The prop_recursive combinator is useful for defining simple recursive strategies but for two or more mutually recursive strategies it becomes cumbersome to use. StrategySet aims to solve this problem.

Examples

Suppose we have the following mutually recursive types First and Second

#[derive(Clone, Debug)]
enum First {
    Zero,
    Second(Vec<Second>),
}

#[derive(Clone, Debug)]
enum Second {
    Zero,
    First(First),
}

We can define strategies for each using a StrategySet

#
#
#
use proptest_recurse::{StrategySet, StrategyExt};

fn arb_first(set: &mut StrategySet) -> SBoxedStrategy<First> {
    Just(First::Zero).prop_mutually_recursive(5, 32, 8, set, |set| {
        vec(set.get::<Second, _>(arb_second), 0..8)
            .prop_map(First::Second)
            .sboxed()
    })
}

fn arb_second(set: &mut StrategySet) -> SBoxedStrategy<Second> {
    Just(Second::Zero)
        .prop_mutually_recursive(3, 32, 1, set, |set| {
            set.get::<First, _>(arb_first)
                .prop_map(Second::First)
                .sboxed()
        }).sboxed()
}
#

To use these strategies, simply pass in an empty StrategySet

#
#
#
#
#
#
#
proptest! {
    #[test]
    fn create(_ in arb_first(&mut Default::default())) {}
}

Dependencies

~4.5MB
~90K SLoC