refactor(tvix/value): use proptest strategies from imbl crate

Instead of going through Vec/BTreeMap for generating our internal
types, use the proptest strategies from imbl.

The one thing I couldn't figure out in the previous implementation is
where the ranges/sizes of generated collections came from. The
strategies in proptest use different types (Range, with an unknown
default value, and SizeRange with 0..100). I've opted to specify
0..100 directly, but we can probably make it configurable.

Change-Id: I749bc4c703fe424099240cab822b1642e5216361
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7791
Autosubmit: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
Vincent Ambo 2023-01-07 15:23:32 +03:00 committed by tazjin
parent f27f5ef0c9
commit 940251b87f
7 changed files with 211 additions and 77 deletions

View file

@ -1,9 +1,10 @@
//! Support for configurable generation of arbitrary nix values
use imbl::proptest::{ord_map, vector};
use proptest::{prelude::*, strategy::BoxedStrategy};
use std::ffi::OsString;
use super::{NixAttrs, NixList, NixString, Value};
use super::{attrs::AttrsRep, NixAttrs, NixList, NixString, Value};
#[derive(Clone)]
pub enum Parameters {
@ -25,6 +26,39 @@ impl Default for Parameters {
}
}
impl Arbitrary for NixAttrs {
type Parameters = Parameters; // <BTreeMap<NixString, Value> as Arbitrary>::Parameters;
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
prop_oneof![
// Empty attrs representation
Just(Self(AttrsRep::Empty)),
// KV representation (name/value pairs)
(
any_with::<Value>(args.clone()),
any_with::<Value>(args.clone())
)
.prop_map(|(name, value)| Self(AttrsRep::KV { name, value })),
// Map representation
ord_map(NixString::arbitrary(), Value::arbitrary_with(args), 0..100)
.prop_map(|map| Self(AttrsRep::Im(map)))
]
.boxed()
}
}
impl Arbitrary for NixList {
type Parameters = <Value as Arbitrary>::Parameters;
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
vector(<Value as Arbitrary>::arbitrary_with(args), 0..100)
.prop_map(NixList::from)
.boxed()
}
}
impl Arbitrary for Value {
type Parameters = Parameters;
type Strategy = BoxedStrategy<Self>;
@ -65,14 +99,8 @@ fn leaf_value() -> impl Strategy<Value = Value> {
fn non_internal_value() -> impl Strategy<Value = Value> {
leaf_value().prop_recursive(3, 5, 5, |inner| {
prop_oneof![
any_with::<NixAttrs>((
Default::default(),
Default::default(),
Parameters::Strategy(inner.clone())
))
.prop_map(Value::attrs),
any_with::<NixList>((Default::default(), Parameters::Strategy(inner)))
.prop_map(Value::List)
NixAttrs::arbitrary_with(Parameters::Strategy(inner.clone())).prop_map(Value::attrs),
any_with::<NixList>(Parameters::Strategy(inner)).prop_map(Value::List)
]
})
}