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
146
147
148
149
150
151
152
|
use serde::Deserialize;
use serde_dhall::{from_str, FromDhall, NumKind, SimpleValue, StaticType};
#[test]
fn test_de_typed() {
fn parse<T: FromDhall + StaticType>(s: &str) -> T {
from_str(s).static_type_annotation().parse().unwrap()
}
assert_eq!(parse::<bool>("True"), true);
assert_eq!(parse::<u64>("1"), 1);
assert_eq!(parse::<u32>("1"), 1);
assert_eq!(parse::<usize>("1"), 1);
assert_eq!(parse::<i64>("+1"), 1);
assert_eq!(parse::<i32>("+1"), 1);
assert_eq!(parse::<isize>("+1"), 1);
assert_eq!(parse::<f64>("1.0"), 1.0);
assert_eq!(parse::<f32>("1.0"), 1.0);
assert_eq!(parse::<String>(r#""foo""#), "foo".to_owned());
assert_eq!(parse::<Vec<u64>>("[] : List Natural"), <Vec<u64>>::new());
assert_eq!(parse::<Vec<u64>>("[1, 2]"), vec![1, 2]);
assert_eq!(parse::<Option<u64>>("None Natural"), None);
assert_eq!(parse::<Option<u64>>("Some 1"), Some(1));
assert_eq!(
parse::<(u64, String)>(r#"{ _1 = 1, _2 = "foo" }"#),
(1, "foo".to_owned())
);
#[derive(Debug, PartialEq, Eq, Deserialize, StaticType)]
struct Foo {
x: u64,
y: i64,
}
assert_eq!(parse::<Foo>("{ x = 1, y = -2 }"), Foo { x: 1, y: -2 });
#[derive(Debug, PartialEq, Eq, Deserialize, StaticType)]
enum Bar {
X(u64),
Y(i64),
}
assert_eq!(parse::<Bar>("< X: Natural | Y: Integer >.X 1"), Bar::X(1));
#[derive(Debug, PartialEq, Eq, Deserialize, StaticType)]
enum Baz {
X,
Y(i64),
}
assert_eq!(parse::<Baz>("< X | Y: Integer >.X"), Baz::X);
assert!(from_str("< X | Y: Integer >.Y")
.static_type_annotation()
.parse::<Baz>()
.is_err());
}
#[test]
fn test_de_untyped() {
use std::collections::BTreeMap;
use std::collections::HashMap;
fn parse<T: FromDhall>(s: &str) -> T {
from_str(s).parse().unwrap()
}
// Test tuples on record of wrong type
assert_eq!(
parse::<(u64, String, isize)>(r#"{ y = "foo", x = 1, z = +42 }"#),
(1, "foo".to_owned(), 42)
);
let mut expected_map = HashMap::new();
expected_map.insert("x".to_string(), 1);
expected_map.insert("y".to_string(), 2);
assert_eq!(
parse::<HashMap<String, usize>>("{ x = 1, y = 2 }"),
expected_map
);
assert_eq!(
parse::<HashMap<String, usize>>("toMap { x = 1, y = 2 }"),
expected_map
);
let mut expected_map = HashMap::new();
expected_map.insert("if".to_string(), 1);
expected_map.insert("FOO_BAR".to_string(), 2);
expected_map.insert("baz-kux".to_string(), 3);
assert_eq!(
parse::<HashMap<String, usize>>(
"{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }"
),
expected_map
);
let mut expected_map = BTreeMap::new();
expected_map.insert("x".to_string(), 1);
expected_map.insert("y".to_string(), 2);
assert_eq!(
parse::<BTreeMap<String, usize>>("{ x = 1, y = 2 }"),
expected_map
);
#[derive(Debug, PartialEq, Eq, Deserialize)]
struct Foo {
x: u64,
y: Option<u64>,
}
// Omit optional field
assert_eq!(parse::<Foo>("{ x = 1 }"), Foo { x: 1, y: None });
// https://github.com/Nadrieril/dhall-rust/issues/155
assert!(from_str("List/length [True, 42]").parse::<bool>().is_err());
}
#[test]
fn test_de_simplevalue() {
let bool_true = SimpleValue::Num(NumKind::Bool(true));
// https://github.com/Nadrieril/dhall-rust/issues/184
assert_eq!(
from_str("[ True ]").parse::<Vec<SimpleValue>>().unwrap(),
vec![bool_true.clone()]
);
assert_eq!(
from_str("< Foo >.Foo").parse::<SimpleValue>().unwrap(),
SimpleValue::Union("Foo".into(), None)
);
assert_eq!(
from_str("< Foo: Bool >.Foo True")
.parse::<SimpleValue>()
.unwrap(),
SimpleValue::Union("Foo".into(), Some(Box::new(bool_true.clone())))
);
#[derive(Debug, PartialEq, Eq, Deserialize)]
struct Foo {
foo: SimpleValue,
}
assert_eq!(
from_str("{ foo = True }").parse::<Foo>().unwrap(),
Foo {
foo: bool_true.clone()
},
);
}
// TODO: test various builder configurations
// In particular test cloning and reusing builder
|