]> git.proxmox.com Git - rustc.git/blob - vendor/basic-toml/tests/valid.rs
New upstream version 1.70.0+dfsg1
[rustc.git] / vendor / basic-toml / tests / valid.rs
1 #![allow(
2 clippy::match_like_matches_macro,
3 clippy::needless_pass_by_value,
4 clippy::uninlined_format_args
5 )]
6
7 use serde_json::{json, Value};
8
9 fn to_json(toml: Value) -> Value {
10 fn doit(s: &str, json: Value) -> Value {
11 json!({ "type": s, "value": json })
12 }
13
14 match toml {
15 Value::Null => unreachable!(),
16 Value::String(s) => doit("string", Value::String(s)),
17 Value::Number(n) => {
18 let repr = n.to_string();
19 if repr.contains('.') {
20 let float: f64 = repr.parse().unwrap();
21 let mut repr = format!("{:.15}", float);
22 repr.truncate(repr.trim_end_matches('0').len());
23 if repr.ends_with('.') {
24 repr.push('0');
25 }
26 doit("float", Value::String(repr))
27 } else {
28 doit("integer", Value::String(repr))
29 }
30 }
31 Value::Bool(b) => doit("bool", Value::String(format!("{}", b))),
32 Value::Array(arr) => {
33 let is_table = match arr.first() {
34 Some(&Value::Object(_)) => true,
35 _ => false,
36 };
37 let json = Value::Array(arr.into_iter().map(to_json).collect());
38 if is_table {
39 json
40 } else {
41 doit("array", json)
42 }
43 }
44 Value::Object(table) => {
45 let mut map = serde_json::Map::new();
46 for (k, v) in table {
47 map.insert(k, to_json(v));
48 }
49 Value::Object(map)
50 }
51 }
52 }
53
54 fn run(toml_raw: &str, json_raw: &str) {
55 println!("parsing:\n{}", toml_raw);
56 let toml: Value = basic_toml::from_str(toml_raw).unwrap();
57 let json: Value = serde_json::from_str(json_raw).unwrap();
58
59 // Assert toml == json
60 let toml_json = to_json(toml.clone());
61 assert!(
62 json == toml_json,
63 "expected\n{}\ngot\n{}\n",
64 serde_json::to_string_pretty(&json).unwrap(),
65 serde_json::to_string_pretty(&toml_json).unwrap()
66 );
67
68 // Assert round trip
69 println!("round trip parse: {}", toml);
70 let toml2: Value = basic_toml::from_str(&basic_toml::to_string(&toml).unwrap()).unwrap();
71 assert_eq!(toml, toml2);
72 }
73
74 macro_rules! test( ($name:ident, $toml:expr, $json:expr) => (
75 #[test]
76 fn $name() { run($toml, $json); }
77 ) );
78
79 test!(
80 array_empty,
81 include_str!("valid/array-empty.toml"),
82 include_str!("valid/array-empty.json")
83 );
84 test!(
85 array_nospaces,
86 include_str!("valid/array-nospaces.toml"),
87 include_str!("valid/array-nospaces.json")
88 );
89 test!(
90 arrays_hetergeneous,
91 include_str!("valid/arrays-hetergeneous.toml"),
92 include_str!("valid/arrays-hetergeneous.json")
93 );
94 #[cfg(any())]
95 test!(
96 arrays,
97 include_str!("valid/arrays.toml"),
98 include_str!("valid/arrays.json")
99 );
100 test!(
101 arrays_nested,
102 include_str!("valid/arrays-nested.toml"),
103 include_str!("valid/arrays-nested.json")
104 );
105 test!(
106 array_mixed_types_ints_and_floats,
107 include_str!("valid/array-mixed-types-ints-and-floats.toml"),
108 include_str!("valid/array-mixed-types-ints-and-floats.json")
109 );
110 test!(
111 array_mixed_types_arrays_and_ints,
112 include_str!("valid/array-mixed-types-arrays-and-ints.toml"),
113 include_str!("valid/array-mixed-types-arrays-and-ints.json")
114 );
115 test!(
116 array_mixed_types_strings_and_ints,
117 include_str!("valid/array-mixed-types-strings-and-ints.toml"),
118 include_str!("valid/array-mixed-types-strings-and-ints.json")
119 );
120 test!(
121 empty,
122 include_str!("valid/empty.toml"),
123 include_str!("valid/empty.json")
124 );
125 test!(
126 bool,
127 include_str!("valid/bool.toml"),
128 include_str!("valid/bool.json")
129 );
130 test!(
131 comments_everywhere,
132 include_str!("valid/comments-everywhere.toml"),
133 include_str!("valid/comments-everywhere.json")
134 );
135 #[cfg(any())]
136 test!(
137 datetime,
138 include_str!("valid/datetime.toml"),
139 include_str!("valid/datetime.json")
140 );
141 #[cfg(any())]
142 test!(
143 example,
144 include_str!("valid/example.toml"),
145 include_str!("valid/example.json")
146 );
147 test!(
148 float,
149 include_str!("valid/float.toml"),
150 include_str!("valid/float.json")
151 );
152 #[cfg(any())]
153 test!(
154 implicit_and_explicit_after,
155 include_str!("valid/implicit-and-explicit-after.toml"),
156 include_str!("valid/implicit-and-explicit-after.json")
157 );
158 #[cfg(any())]
159 test!(
160 implicit_and_explicit_before,
161 include_str!("valid/implicit-and-explicit-before.toml"),
162 include_str!("valid/implicit-and-explicit-before.json")
163 );
164 test!(
165 implicit_groups,
166 include_str!("valid/implicit-groups.toml"),
167 include_str!("valid/implicit-groups.json")
168 );
169 test!(
170 integer,
171 include_str!("valid/integer.toml"),
172 include_str!("valid/integer.json")
173 );
174 test!(
175 key_equals_nospace,
176 include_str!("valid/key-equals-nospace.toml"),
177 include_str!("valid/key-equals-nospace.json")
178 );
179 test!(
180 key_space,
181 include_str!("valid/key-space.toml"),
182 include_str!("valid/key-space.json")
183 );
184 test!(
185 key_special_chars,
186 include_str!("valid/key-special-chars.toml"),
187 include_str!("valid/key-special-chars.json")
188 );
189 test!(
190 key_with_pound,
191 include_str!("valid/key-with-pound.toml"),
192 include_str!("valid/key-with-pound.json")
193 );
194 test!(
195 key_empty,
196 include_str!("valid/key-empty.toml"),
197 include_str!("valid/key-empty.json")
198 );
199 test!(
200 long_float,
201 include_str!("valid/long-float.toml"),
202 include_str!("valid/long-float.json")
203 );
204 test!(
205 long_integer,
206 include_str!("valid/long-integer.toml"),
207 include_str!("valid/long-integer.json")
208 );
209 test!(
210 multiline_string,
211 include_str!("valid/multiline-string.toml"),
212 include_str!("valid/multiline-string.json")
213 );
214 test!(
215 raw_multiline_string,
216 include_str!("valid/raw-multiline-string.toml"),
217 include_str!("valid/raw-multiline-string.json")
218 );
219 test!(
220 raw_string,
221 include_str!("valid/raw-string.toml"),
222 include_str!("valid/raw-string.json")
223 );
224 test!(
225 string_empty,
226 include_str!("valid/string-empty.toml"),
227 include_str!("valid/string-empty.json")
228 );
229 test!(
230 string_escapes,
231 include_str!("valid/string-escapes.toml"),
232 include_str!("valid/string-escapes.json")
233 );
234 test!(
235 string_simple,
236 include_str!("valid/string-simple.toml"),
237 include_str!("valid/string-simple.json")
238 );
239 test!(
240 string_with_pound,
241 include_str!("valid/string-with-pound.toml"),
242 include_str!("valid/string-with-pound.json")
243 );
244 test!(
245 table_array_implicit,
246 include_str!("valid/table-array-implicit.toml"),
247 include_str!("valid/table-array-implicit.json")
248 );
249 test!(
250 table_array_many,
251 include_str!("valid/table-array-many.toml"),
252 include_str!("valid/table-array-many.json")
253 );
254 test!(
255 table_array_nest,
256 include_str!("valid/table-array-nest.toml"),
257 include_str!("valid/table-array-nest.json")
258 );
259 test!(
260 table_array_one,
261 include_str!("valid/table-array-one.toml"),
262 include_str!("valid/table-array-one.json")
263 );
264 test!(
265 table_empty,
266 include_str!("valid/table-empty.toml"),
267 include_str!("valid/table-empty.json")
268 );
269 test!(
270 table_sub_empty,
271 include_str!("valid/table-sub-empty.toml"),
272 include_str!("valid/table-sub-empty.json")
273 );
274 test!(
275 table_multi_empty,
276 include_str!("valid/table-multi-empty.toml"),
277 include_str!("valid/table-multi-empty.json")
278 );
279 test!(
280 table_whitespace,
281 include_str!("valid/table-whitespace.toml"),
282 include_str!("valid/table-whitespace.json")
283 );
284 test!(
285 table_with_pound,
286 include_str!("valid/table-with-pound.toml"),
287 include_str!("valid/table-with-pound.json")
288 );
289 test!(
290 unicode_escape,
291 include_str!("valid/unicode-escape.toml"),
292 include_str!("valid/unicode-escape.json")
293 );
294 test!(
295 unicode_literal,
296 include_str!("valid/unicode-literal.toml"),
297 include_str!("valid/unicode-literal.json")
298 );
299 #[cfg(any())]
300 test!(
301 hard_example,
302 include_str!("valid/hard_example.toml"),
303 include_str!("valid/hard_example.json")
304 );
305 #[cfg(any())]
306 test!(
307 example2,
308 include_str!("valid/example2.toml"),
309 include_str!("valid/example2.json")
310 );
311 #[cfg(any())]
312 test!(
313 example3,
314 include_str!("valid/example-v0.3.0.toml"),
315 include_str!("valid/example-v0.3.0.json")
316 );
317 #[cfg(any())]
318 test!(
319 example4,
320 include_str!("valid/example-v0.4.0.toml"),
321 include_str!("valid/example-v0.4.0.json")
322 );
323 #[cfg(any())]
324 test!(
325 example_bom,
326 include_str!("valid/example-bom.toml"),
327 include_str!("valid/example.json")
328 );
329
330 #[cfg(any())]
331 test!(
332 datetime_truncate,
333 include_str!("valid/datetime-truncate.toml"),
334 include_str!("valid/datetime-truncate.json")
335 );
336 test!(
337 key_quote_newline,
338 include_str!("valid/key-quote-newline.toml"),
339 include_str!("valid/key-quote-newline.json")
340 );
341 test!(
342 table_array_nest_no_keys,
343 include_str!("valid/table-array-nest-no-keys.toml"),
344 include_str!("valid/table-array-nest-no-keys.json")
345 );
346 test!(
347 dotted_keys,
348 include_str!("valid/dotted-keys.toml"),
349 include_str!("valid/dotted-keys.json")
350 );
351
352 test!(
353 quote_surrounded_value,
354 include_str!("valid/quote-surrounded-value.toml"),
355 include_str!("valid/quote-surrounded-value.json")
356 );
357
358 test!(
359 float_exponent,
360 include_str!("valid/float-exponent.toml"),
361 include_str!("valid/float-exponent.json")
362 );
363
364 test!(
365 string_delim_end,
366 include_str!("valid/string-delim-end.toml"),
367 include_str!("valid/string-delim-end.json")
368 );