From f8b380672043e9b870c4303ce47205a1435de8ee Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sat, 17 Sep 2022 14:14:33 -0400 Subject: test(tvix/eval): impl Arbitrary for Value Impl Arbitrary for Value (and NixAttrs and NixList) in the same way we did for NixString. Value currently only generates non-"internal" values (no thunks, AttrNotFound, etc.) and can't generate functions (builtins or closures), because those'd require full generation of tvix bytecode, which is a bit more work than I'd like to do now - there's a `todo!` left in the code for a place where we could allow opting-in to internal values and functions later. Change-Id: I07a59e2b1d89cfaa912d4ecebd642caf4ddb040a Reviewed-on: https://cl.tvl.fyi/c/depot/+/6627 Autosubmit: grfn Tested-by: BuildkiteCI Reviewed-by: tazjin --- tvix/eval/src/value/arbitrary.rs | 78 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tvix/eval/src/value/arbitrary.rs (limited to 'tvix/eval/src/value/arbitrary.rs') diff --git a/tvix/eval/src/value/arbitrary.rs b/tvix/eval/src/value/arbitrary.rs new file mode 100644 index 000000000000..56ccd3b28ab6 --- /dev/null +++ b/tvix/eval/src/value/arbitrary.rs @@ -0,0 +1,78 @@ +//! Support for configurable generation of arbitrary nix values + +use proptest::{prelude::*, strategy::BoxedStrategy}; +use std::{ffi::OsString, rc::Rc}; + +use super::{NixAttrs, NixList, NixString, Value}; + +#[derive(Clone)] +pub enum Parameters { + Strategy(BoxedStrategy), + Parameters { + generate_internal_values: bool, + generate_functions: bool, + generate_nested: bool, + }, +} + +impl Default for Parameters { + fn default() -> Self { + Self::Parameters { + generate_internal_values: false, + generate_functions: false, + generate_nested: true, + } + } +} + +impl Arbitrary for Value { + type Parameters = Parameters; + type Strategy = BoxedStrategy; + + fn arbitrary_with(args: Self::Parameters) -> Self::Strategy { + match args { + Parameters::Strategy(s) => s, + Parameters::Parameters { + generate_internal_values, + generate_functions, + generate_nested, + } => { + if generate_internal_values || generate_functions { + todo!("Generating internal values and functions not implemented yet") + } else if generate_nested { + non_internal_value().boxed() + } else { + leaf_value().boxed() + } + } + } + } +} + +fn leaf_value() -> impl Strategy { + use Value::*; + + prop_oneof![ + Just(Null), + any::().prop_map(Bool), + any::().prop_map(Integer), + any::().prop_map(Float), + any::().prop_map(String), + any::().prop_map(|s| Path(s.into())), + ] +} + +fn non_internal_value() -> impl Strategy { + leaf_value().prop_recursive(10, 256, 10, |inner| { + prop_oneof![ + any_with::(( + Default::default(), + Default::default(), + Parameters::Strategy(inner.clone()) + )) + .prop_map(|a| Value::Attrs(Rc::new(a))), + any_with::((Default::default(), Parameters::Strategy(inner))) + .prop_map(Value::List) + ] + }) +} -- cgit 1.4.1