]> git.proxmox.com Git - rustc.git/blob - src/vendor/toml/tests/valid.rs
New upstream version 1.17.0+dfsg1
[rustc.git] / src / vendor / toml / tests / valid.rs
1 extern crate toml;
2 extern crate serde_json;
3
4 use toml::Value as Toml;
5 use serde_json::Value as Json;
6
7 fn to_json(toml: toml::Value) -> Json {
8 fn doit(s: &str, json: Json) -> Json {
9 let mut map = serde_json::Map::new();
10 map.insert("type".to_string(), Json::String(s.to_string()));
11 map.insert("value".to_string(), json);
12 Json::Object(map)
13 }
14
15 match toml {
16 Toml::String(s) => doit("string", Json::String(s)),
17 Toml::Integer(i) => doit("integer", Json::String(i.to_string())),
18 Toml::Float(f) => doit("float", Json::String({
19 let s = format!("{:.15}", f);
20 let s = format!("{}", s.trim_right_matches('0'));
21 if s.ends_with(".") {format!("{}0", s)} else {s}
22 })),
23 Toml::Boolean(b) => doit("bool", Json::String(format!("{}", b))),
24 Toml::Datetime(s) => doit("datetime", Json::String(s.to_string())),
25 Toml::Array(arr) => {
26 let is_table = match arr.first() {
27 Some(&Toml::Table(..)) => true,
28 _ => false,
29 };
30 let json = Json::Array(arr.into_iter().map(to_json).collect());
31 if is_table {json} else {doit("array", json)}
32 }
33 Toml::Table(table) => {
34 let mut map = serde_json::Map::new();
35 for (k, v) in table {
36 map.insert(k, to_json(v));
37 }
38 Json::Object(map)
39 }
40 }
41 }
42
43 fn run(toml: &str, json: &str) {
44 println!("parsing:\n{}", toml);
45 let toml: Toml = toml.parse().unwrap();
46 let json: Json = json.parse().unwrap();
47
48 // Assert toml == json
49 let toml_json = to_json(toml.clone());
50 assert!(json == toml_json,
51 "expected\n{}\ngot\n{}\n",
52 serde_json::to_string_pretty(&json).unwrap(),
53 serde_json::to_string_pretty(&toml_json).unwrap());
54
55 // Assert round trip
56 println!("round trip parse: {}", toml);
57 let toml2 = toml.to_string().parse().unwrap();
58 assert_eq!(toml, toml2);
59 }
60
61 macro_rules! test( ($name:ident, $toml:expr, $json:expr) => (
62 #[test]
63 fn $name() { run($toml, $json); }
64 ) );
65
66 test!(array_empty,
67 include_str!("valid/array-empty.toml"),
68 include_str!("valid/array-empty.json"));
69 test!(array_nospaces,
70 include_str!("valid/array-nospaces.toml"),
71 include_str!("valid/array-nospaces.json"));
72 test!(arrays_hetergeneous,
73 include_str!("valid/arrays-hetergeneous.toml"),
74 include_str!("valid/arrays-hetergeneous.json"));
75 test!(arrays,
76 include_str!("valid/arrays.toml"),
77 include_str!("valid/arrays.json"));
78 test!(arrays_nested,
79 include_str!("valid/arrays-nested.toml"),
80 include_str!("valid/arrays-nested.json"));
81 test!(empty,
82 include_str!("valid/empty.toml"),
83 include_str!("valid/empty.json"));
84 test!(bool,
85 include_str!("valid/bool.toml"),
86 include_str!("valid/bool.json"));
87 test!(datetime,
88 include_str!("valid/datetime.toml"),
89 include_str!("valid/datetime.json"));
90 test!(example,
91 include_str!("valid/example.toml"),
92 include_str!("valid/example.json"));
93 test!(float,
94 include_str!("valid/float.toml"),
95 include_str!("valid/float.json"));
96 test!(implicit_and_explicit_after,
97 include_str!("valid/implicit-and-explicit-after.toml"),
98 include_str!("valid/implicit-and-explicit-after.json"));
99 test!(implicit_and_explicit_before,
100 include_str!("valid/implicit-and-explicit-before.toml"),
101 include_str!("valid/implicit-and-explicit-before.json"));
102 test!(implicit_groups,
103 include_str!("valid/implicit-groups.toml"),
104 include_str!("valid/implicit-groups.json"));
105 test!(integer,
106 include_str!("valid/integer.toml"),
107 include_str!("valid/integer.json"));
108 test!(key_equals_nospace,
109 include_str!("valid/key-equals-nospace.toml"),
110 include_str!("valid/key-equals-nospace.json"));
111 test!(key_space,
112 include_str!("valid/key-space.toml"),
113 include_str!("valid/key-space.json"));
114 test!(key_special_chars,
115 include_str!("valid/key-special-chars.toml"),
116 include_str!("valid/key-special-chars.json"));
117 test!(key_with_pound,
118 include_str!("valid/key-with-pound.toml"),
119 include_str!("valid/key-with-pound.json"));
120 test!(long_float,
121 include_str!("valid/long-float.toml"),
122 include_str!("valid/long-float.json"));
123 test!(long_integer,
124 include_str!("valid/long-integer.toml"),
125 include_str!("valid/long-integer.json"));
126 test!(multiline_string,
127 include_str!("valid/multiline-string.toml"),
128 include_str!("valid/multiline-string.json"));
129 test!(raw_multiline_string,
130 include_str!("valid/raw-multiline-string.toml"),
131 include_str!("valid/raw-multiline-string.json"));
132 test!(raw_string,
133 include_str!("valid/raw-string.toml"),
134 include_str!("valid/raw-string.json"));
135 test!(string_empty,
136 include_str!("valid/string-empty.toml"),
137 include_str!("valid/string-empty.json"));
138 test!(string_escapes,
139 include_str!("valid/string-escapes.toml"),
140 include_str!("valid/string-escapes.json"));
141 test!(string_simple,
142 include_str!("valid/string-simple.toml"),
143 include_str!("valid/string-simple.json"));
144 test!(string_with_pound,
145 include_str!("valid/string-with-pound.toml"),
146 include_str!("valid/string-with-pound.json"));
147 test!(table_array_implicit,
148 include_str!("valid/table-array-implicit.toml"),
149 include_str!("valid/table-array-implicit.json"));
150 test!(table_array_many,
151 include_str!("valid/table-array-many.toml"),
152 include_str!("valid/table-array-many.json"));
153 test!(table_array_nest,
154 include_str!("valid/table-array-nest.toml"),
155 include_str!("valid/table-array-nest.json"));
156 test!(table_array_one,
157 include_str!("valid/table-array-one.toml"),
158 include_str!("valid/table-array-one.json"));
159 test!(table_empty,
160 include_str!("valid/table-empty.toml"),
161 include_str!("valid/table-empty.json"));
162 test!(table_sub_empty,
163 include_str!("valid/table-sub-empty.toml"),
164 include_str!("valid/table-sub-empty.json"));
165 test!(table_whitespace,
166 include_str!("valid/table-whitespace.toml"),
167 include_str!("valid/table-whitespace.json"));
168 test!(table_with_pound,
169 include_str!("valid/table-with-pound.toml"),
170 include_str!("valid/table-with-pound.json"));
171 test!(unicode_escape,
172 include_str!("valid/unicode-escape.toml"),
173 include_str!("valid/unicode-escape.json"));
174 test!(unicode_literal,
175 include_str!("valid/unicode-literal.toml"),
176 include_str!("valid/unicode-literal.json"));
177 test!(hard_example,
178 include_str!("valid/hard_example.toml"),
179 include_str!("valid/hard_example.json"));
180 test!(example2,
181 include_str!("valid/example2.toml"),
182 include_str!("valid/example2.json"));
183 test!(example3,
184 include_str!("valid/example-v0.3.0.toml"),
185 include_str!("valid/example-v0.3.0.json"));
186 test!(example4,
187 include_str!("valid/example-v0.4.0.toml"),
188 include_str!("valid/example-v0.4.0.json"));
189 test!(example_bom,
190 include_str!("valid/example-bom.toml"),
191 include_str!("valid/example.json"));