about summary refs log tree commit diff
path: root/tvix/eval/src/value/attrs/tests.rs
blob: 9bd8305482abb67a0fb3945f61c254b548f25b48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
        ),
    }
}