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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
mod serde {
use serde::{Deserialize, Serialize};
use serde_dhall::{from_str, serialize, FromDhall, StaticType, ToDhall};
fn assert_de<T>(s: &str, x: T)
where
T: FromDhall + StaticType + PartialEq + std::fmt::Debug,
{
assert_eq!(
from_str(s)
.static_type_annotation()
.parse::<T>()
.map_err(|e| e.to_string()),
Ok(x)
);
}
fn assert_ser<T>(s: &str, x: T)
where
T: ToDhall + StaticType + PartialEq + std::fmt::Debug,
{
assert_eq!(
serialize(&x)
.static_type_annotation()
.to_string()
.map_err(|e| e.to_string()),
Ok(s.to_string())
);
}
fn assert_serde<T>(s: &str, x: T)
where
T: ToDhall
+ FromDhall
+ StaticType
+ PartialEq
+ std::fmt::Debug
+ Clone,
{
assert_de(s, x.clone());
assert_ser(s, x);
}
#[test]
fn numbers() {
assert_serde("True", true);
assert_serde("1", 1u64);
assert_serde("1", 1u32);
assert_serde("1", 1usize);
assert_serde("+1", 1i64);
assert_serde("+1", 1i32);
assert_serde("+1", 1isize);
assert_serde("1.0", 1.0f64);
assert_serde("1.0", 1.0f32);
}
#[test]
fn text() {
assert_serde(r#""foo""#, "foo".to_owned());
assert_ser(r#""foo""#, "foo");
}
#[test]
fn list() {
assert_serde("[] : List Natural", <Vec<u64>>::new());
assert_serde("[] : List Text", <Vec<String>>::new());
assert_serde(
r#"["foo", "bar"]"#,
vec!["foo".to_owned(), "bar".to_owned()],
);
assert_ser(r#"["foo", "bar"]"#, vec!["foo", "bar"]);
assert_serde::<Vec<u64>>("[1, 2]", vec![1, 2]);
}
#[test]
fn optional() {
assert_serde("None Natural", None::<u64>);
assert_serde("None Text", None::<String>);
assert_serde("Some 1", Some(1u64));
assert_eq!(
serialize(&None::<u64>).to_string().map_err(|e| e.to_string()),
Err("cannot serialize value without a type annotation: Optional(None)".to_string())
);
}
#[test]
fn tuple() {
assert_serde::<()>(r#"{=}"#, ());
assert_serde::<(u64, String)>(
r#"{ _1 = 1, _2 = "foo" }"#,
(1, "foo".to_owned()),
);
assert_serde::<(u64, u64, u64, u64)>(
r#"{ _1 = 1, _2 = 2, _3 = 3, _4 = 4 }"#,
(1, 2, 3, 4),
);
}
#[test]
fn structs() {
// #[derive(
// Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType,
// )]
// struct Foo;
// assert_serde::<Foo>("{=}", Foo);
// #[derive(
// Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType,
// )]
// struct Bar(u64);
// assert_serde::<Bar>("{ _1 = 1 }", Bar (1));
#[derive(
Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType,
)]
struct Baz {
x: u64,
y: i64,
}
assert_serde::<Baz>("{ x = 1, y = -2 }", Baz { x: 1, y: -2 });
}
#[test]
fn enums() {
#[derive(
Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType,
)]
enum Foo {
X(u64),
Y(i64),
}
assert_serde::<Foo>("< X: Natural | Y: Integer >.X 1", Foo::X(1));
#[derive(
Debug, Clone, PartialEq, Eq, Deserialize, Serialize, StaticType,
)]
enum Bar {
X,
Y(i64),
}
assert_serde::<Bar>("< X | Y: Integer >.X", Bar::X);
assert!(from_str("< X | Y: Integer >.Y")
.static_type_annotation()
.parse::<Bar>()
.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. Fields are just taken in alphabetic order.
assert_eq!(
parse::<(u64, String, i64)>(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, u64>>("{ x = 1, y = 2 }"),
expected_map
);
assert_eq!(
parse::<HashMap<String, u64>>("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, u64>>(
"{ `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, u64>>("{ 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());
}
// TODO: test various builder configurations
// In particular test cloning and reusing builder
}
|