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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
use super::*;
mod nix_eq {
use crate::observer::NoOpObserver;
use super::*;
use proptest::prelude::ProptestConfig;
use test_strategy::proptest;
#[proptest(ProptestConfig { cases: 2, ..Default::default() })]
fn reflexive(x: NixAttrs) {
let mut observer = NoOpObserver {};
let mut vm = VM::new(Default::default(), Box::new(crate::DummyIO), &mut observer);
assert!(x.nix_eq(&x, &mut vm).unwrap())
}
#[proptest(ProptestConfig { cases: 2, ..Default::default() })]
fn symmetric(x: NixAttrs, y: NixAttrs) {
let mut observer = NoOpObserver {};
let mut vm = VM::new(Default::default(), Box::new(crate::DummyIO), &mut observer);
assert_eq!(
x.nix_eq(&y, &mut vm).unwrap(),
y.nix_eq(&x, &mut vm).unwrap()
)
}
#[proptest(ProptestConfig { cases: 2, ..Default::default() })]
fn transitive(x: NixAttrs, y: NixAttrs, z: NixAttrs) {
let mut observer = NoOpObserver {};
let mut vm = VM::new(Default::default(), Box::new(crate::DummyIO), &mut observer);
if x.nix_eq(&y, &mut vm).unwrap() && y.nix_eq(&z, &mut vm).unwrap() {
assert!(x.nix_eq(&z, &mut vm).unwrap())
}
}
}
#[test]
fn test_empty_attrs() {
let attrs = NixAttrs::construct(0, vec![]).expect("empty attr construction should succeed");
assert!(
matches!(attrs, NixAttrs(AttrsRep::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(AttrsRep::Im(_))),
"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,
forty_two_val.clone(),
name_val,
meaning_val.clone(),
],
)
.expect("constructing K/V pair attrs should succeed");
match kv_attrs {
NixAttrs(AttrsRep::KV { name, value })
if name.to_str().unwrap() == meaning_val.to_str().unwrap()
|| value.to_str().unwrap() == forty_two_val.to_str().unwrap() => {}
_ => panic!(
"K/V attribute set should use optimised representation, but got {:?}",
kv_attrs
),
}
}
#[test]
fn test_empty_attrs_iter() {
let attrs = NixAttrs::construct(0, vec![]).unwrap();
assert!(attrs.iter().next().is_none());
}
#[test]
fn test_kv_attrs_iter() {
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,
forty_two_val.clone(),
name_val,
meaning_val.clone(),
],
)
.expect("constructing K/V pair attrs should succeed");
let mut iter = kv_attrs
.iter()
.collect::<Vec<_>>()
.into_iter()
.map(|(k, v)| (k, v));
let (k, v) = iter.next().unwrap();
assert!(k == NixString::NAME_REF);
assert!(v.to_str().unwrap() == meaning_val.to_str().unwrap());
let (k, v) = iter.next().unwrap();
assert!(k == NixString::VALUE_REF);
assert!(v.as_int().unwrap() == forty_two_val.as_int().unwrap());
assert!(iter.next().is_none());
}
#[test]
fn test_map_attrs_iter() {
let attrs = NixAttrs::construct(
1,
vec![Value::String("key".into()), Value::String("value".into())],
)
.expect("simple attr construction should succeed");
let mut iter = attrs.iter().collect::<Vec<_>>().into_iter();
let (k, v) = iter.next().unwrap();
assert!(k == &NixString::from("key"));
assert!(v.to_str().unwrap().as_str() == "value");
assert!(iter.next().is_none());
}
|