about summary refs log tree commit diff
path: root/tvix/eval
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-08-10T14·34+0300
committertazjin <tazjin@tvl.su>2022-08-24T18·19+0000
commitc7ba2dec04c9c0bf7558285ed7d434e61ee5e5b5 (patch)
treec8551d50822d7c8f1cda78505c189f361449b9e3 /tvix/eval
parent08b4d65fbd0ab1f5809aa2f6eb5da819d61299c4 (diff)
test(tvix/value): add simple attrset construction tests r/4456
These do not yet test nested attribute sets; we need to add some more
inspection primitives first.

Change-Id: Icfc99bf17c73ebefc0d882a84f0ca73ec688a54d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6110
Reviewed-by: eta <tvl@eta.st>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval')
-rw-r--r--tvix/eval/src/value/attrs.rs3
-rw-r--r--tvix/eval/src/value/attrs/tests.rs53
-rw-r--r--tvix/eval/src/value/string.rs6
3 files changed, 62 insertions, 0 deletions
diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs
index 209f97778c..75ade6a89a 100644
--- a/tvix/eval/src/value/attrs.rs
+++ b/tvix/eval/src/value/attrs.rs
@@ -14,6 +14,9 @@ use crate::errors::{Error, EvalResult};
 use super::string::NixString;
 use super::Value;
 
+#[cfg(test)]
+mod tests;
+
 #[derive(Clone, Debug)]
 pub enum NixAttrs {
     Empty,
diff --git a/tvix/eval/src/value/attrs/tests.rs b/tvix/eval/src/value/attrs/tests.rs
new file mode 100644
index 0000000000..9bd8305482
--- /dev/null
+++ b/tvix/eval/src/value/attrs/tests.rs
@@ -0,0 +1,53 @@
+use super::*;
+
+#[test]
+fn test_empty_attrs() {
+    let attrs = NixAttrs::construct(0, vec![]).expect("empty attr construction should succeed");
+
+    assert!(
+        matches!(attrs, NixAttrs::Empty),
+        "empty attribute set should use optimised representation"
+    );
+}
+
+#[test]
+fn test_simple_attrs() {
+    let attrs = NixAttrs::construct(
+        1,
+        vec![Value::String("key".into()), Value::String("value".into())],
+    )
+    .expect("simple attr construction should succeed");
+
+    assert!(
+        matches!(attrs, NixAttrs::Map(_)),
+        "simple attribute set should use map representation",
+    )
+}
+
+#[test]
+fn test_kv_attrs() {
+    let name_val = Value::String("name".into());
+    let value_val = Value::String("value".into());
+    let meaning_val = Value::String("meaning".into());
+    let forty_two_val = Value::Integer(42);
+
+    let kv_attrs = NixAttrs::construct(
+        2,
+        vec![
+            value_val.clone(),
+            forty_two_val.clone(),
+            name_val.clone(),
+            meaning_val.clone(),
+        ],
+    )
+    .expect("constructing K/V pair attrs should succeed");
+
+    match kv_attrs {
+        NixAttrs::KV { name, value } if name == meaning_val || value == forty_two_val => {}
+
+        _ => panic!(
+            "K/V attribute set should use optimised representation, but got {:?}",
+            kv_attrs
+        ),
+    }
+}
diff --git a/tvix/eval/src/value/string.rs b/tvix/eval/src/value/string.rs
index 531bcf547b..d4776caea4 100644
--- a/tvix/eval/src/value/string.rs
+++ b/tvix/eval/src/value/string.rs
@@ -11,3 +11,9 @@ impl Display for NixString {
         f.write_str(self.0.as_str())
     }
 }
+
+impl From<&str> for NixString {
+    fn from(s: &str) -> Self {
+        NixString(s.to_string())
+    }
+}