about summary refs log tree commit diff
path: root/tvix/eval/src/value/attrs.rs
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2022-09-17T18·14-0400
committerclbot <clbot@tvl.fyi>2022-09-18T17·55+0000
commitf8b380672043e9b870c4303ce47205a1435de8ee (patch)
tree6132b8ccbf869634ff7e4ec7dca9667c3e495520 /tvix/eval/src/value/attrs.rs
parent221d3b9485a1d84fc4d9f06d864242d3c393d0ba (diff)
test(tvix/eval): impl Arbitrary for Value r/4902
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 <grfn@gws.fyi>
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/eval/src/value/attrs.rs')
-rw-r--r--tvix/eval/src/value/attrs.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs
index 86e52206b8..0551fe20d9 100644
--- a/tvix/eval/src/value/attrs.rs
+++ b/tvix/eval/src/value/attrs.rs
@@ -149,6 +149,35 @@ impl PartialEq for NixAttrs {
     }
 }
 
+#[cfg(feature = "arbitrary")]
+mod arbitrary {
+    use super::*;
+
+    use proptest::prelude::*;
+    use proptest::prop_oneof;
+    use proptest::strategy::{BoxedStrategy, Just, Strategy};
+
+    impl Arbitrary for NixAttrs {
+        type Parameters = <BTreeMap<NixString, Value> as Arbitrary>::Parameters;
+
+        type Strategy = BoxedStrategy<Self>;
+
+        fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
+            prop_oneof![
+                Just(Self(AttrsRep::Empty)),
+                (
+                    any_with::<Value>(args.2.clone()),
+                    any_with::<Value>(args.2.clone())
+                )
+                    .prop_map(|(name, value)| Self(AttrsRep::KV { name, value })),
+                any_with::<BTreeMap<NixString, Value>>(args)
+                    .prop_map(|map| Self(AttrsRep::Map(map)))
+            ]
+            .boxed()
+        }
+    }
+}
+
 impl NixAttrs {
     /// Return an attribute set containing the merge of the two
     /// provided sets. Keys from the `other` set have precedence.