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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
|
extern crate toml;
use toml::Value;
macro_rules! bad {
($toml:expr, $msg:expr) => {
match $toml.parse::<toml::Value>() {
Ok(s) => panic!("parsed to: {:#?}", s),
Err(e) => assert_eq!(e.to_string(), $msg),
}
};
}
#[test]
fn crlf() {
"\
[project]\r\n\
\r\n\
name = \"splay\"\r\n\
version = \"0.1.0\"\r\n\
authors = [\"alex@crichton.co\"]\r\n\
\r\n\
[[lib]]\r\n\
\r\n\
path = \"lib.rs\"\r\n\
name = \"splay\"\r\n\
description = \"\"\"\
A Rust implementation of a TAR file reader and writer. This library does not\r\n\
currently handle compression, but it is abstract over all I/O readers and\r\n\
writers. Additionally, great lengths are taken to ensure that the entire\r\n\
contents are never required to be entirely resident in memory all at once.\r\n\
\"\"\"\
"
.parse::<Value>()
.unwrap();
}
#[test]
fn fun_with_strings() {
let table = r#"
bar = "\U00000000"
key1 = "One\nTwo"
key2 = """One\nTwo"""
key3 = """
One
Two"""
key4 = "The quick brown fox jumps over the lazy dog."
key5 = """
The quick brown \
fox jumps over \
the lazy dog."""
key6 = """\
The quick brown \
fox jumps over \
the lazy dog.\
"""
# What you see is what you get.
winpath = 'C:\Users\nodejs\templates'
winpath2 = '\\ServerX\admin$\system32\'
quoted = 'Tom "Dubs" Preston-Werner'
regex = '<\i\c*\s*>'
regex2 = '''I [dw]on't need \d{2} apples'''
lines = '''
The first newline is
trimmed in raw strings.
All other whitespace
is preserved.
'''
"#
.parse::<Value>()
.unwrap();
assert_eq!(table["bar"].as_str(), Some("\0"));
assert_eq!(table["key1"].as_str(), Some("One\nTwo"));
assert_eq!(table["key2"].as_str(), Some("One\nTwo"));
assert_eq!(table["key3"].as_str(), Some("One\nTwo"));
let msg = "The quick brown fox jumps over the lazy dog.";
assert_eq!(table["key4"].as_str(), Some(msg));
assert_eq!(table["key5"].as_str(), Some(msg));
assert_eq!(table["key6"].as_str(), Some(msg));
assert_eq!(
table["winpath"].as_str(),
Some(r"C:\Users\nodejs\templates")
);
assert_eq!(
table["winpath2"].as_str(),
Some(r"\\ServerX\admin$\system32\")
);
assert_eq!(
table["quoted"].as_str(),
Some(r#"Tom "Dubs" Preston-Werner"#)
);
assert_eq!(table["regex"].as_str(), Some(r"<\i\c*\s*>"));
assert_eq!(
table["regex2"].as_str(),
Some(r"I [dw]on't need \d{2} apples")
);
assert_eq!(
table["lines"].as_str(),
Some(
"The first newline is\n\
trimmed in raw strings.\n\
All other whitespace\n\
is preserved.\n"
)
);
}
#[test]
fn tables_in_arrays() {
let table = r#"
[[foo]]
#…
[foo.bar]
#…
[[foo]] # ...
#…
[foo.bar]
#...
"#
.parse::<Value>()
.unwrap();
table["foo"][0]["bar"].as_table().unwrap();
table["foo"][1]["bar"].as_table().unwrap();
}
#[test]
fn empty_table() {
let table = r#"
[foo]"#
.parse::<Value>()
.unwrap();
table["foo"].as_table().unwrap();
}
#[test]
fn fruit() {
let table = r#"
[[fruit]]
name = "apple"
[fruit.physical]
color = "red"
shape = "round"
[[fruit.variety]]
name = "red delicious"
[[fruit.variety]]
name = "granny smith"
[[fruit]]
name = "banana"
[[fruit.variety]]
name = "plantain"
"#
.parse::<Value>()
.unwrap();
assert_eq!(table["fruit"][0]["name"].as_str(), Some("apple"));
assert_eq!(table["fruit"][0]["physical"]["color"].as_str(), Some("red"));
assert_eq!(
table["fruit"][0]["physical"]["shape"].as_str(),
Some("round")
);
assert_eq!(
table["fruit"][0]["variety"][0]["name"].as_str(),
Some("red delicious")
);
assert_eq!(
table["fruit"][0]["variety"][1]["name"].as_str(),
Some("granny smith")
);
assert_eq!(table["fruit"][1]["name"].as_str(), Some("banana"));
assert_eq!(
table["fruit"][1]["variety"][0]["name"].as_str(),
Some("plantain")
);
}
#[test]
fn stray_cr() {
bad!("\r", "unexpected character found: `\\r` at line 1 column 1");
bad!(
"a = [ \r ]",
"unexpected character found: `\\r` at line 1 column 7"
);
bad!(
"a = \"\"\"\r\"\"\"",
"invalid character in string: `\\r` at line 1 column 8"
);
bad!(
"a = \"\"\"\\ \r \"\"\"",
"invalid escape character in string: ` ` at line 1 column 9"
);
bad!(
"a = '''\r'''",
"invalid character in string: `\\r` at line 1 column 8"
);
bad!(
"a = '\r'",
"invalid character in string: `\\r` at line 1 column 6"
);
bad!(
"a = \"\r\"",
"invalid character in string: `\\r` at line 1 column 6"
);
}
#[test]
fn blank_literal_string() {
let table = "foo = ''".parse::<Value>().unwrap();
assert_eq!(table["foo"].as_str(), Some(""));
}
#[test]
fn many_blank() {
let table = "foo = \"\"\"\n\n\n\"\"\"".parse::<Value>().unwrap();
assert_eq!(table["foo"].as_str(), Some("\n\n"));
}
#[test]
fn literal_eats_crlf() {
let table = "
foo = \"\"\"\\\r\n\"\"\"
bar = \"\"\"\\\r\n \r\n \r\n a\"\"\"
"
.parse::<Value>()
.unwrap();
assert_eq!(table["foo"].as_str(), Some(""));
assert_eq!(table["bar"].as_str(), Some("a"));
}
#[test]
fn string_no_newline() {
bad!("a = \"\n\"", "newline in string found at line 1 column 6");
bad!("a = '\n'", "newline in string found at line 1 column 6");
}
#[test]
fn bad_leading_zeros() {
bad!("a = 00", "invalid number at line 1 column 6");
bad!("a = -00", "invalid number at line 1 column 7");
bad!("a = +00", "invalid number at line 1 column 7");
bad!("a = 00.0", "invalid number at line 1 column 6");
bad!("a = -00.0", "invalid number at line 1 column 7");
bad!("a = +00.0", "invalid number at line 1 column 7");
bad!(
"a = 9223372036854775808",
"invalid number at line 1 column 5"
);
bad!(
"a = -9223372036854775809",
"invalid number at line 1 column 5"
);
}
#[test]
fn bad_floats() {
bad!("a = 0.", "invalid number at line 1 column 7");
bad!("a = 0.e", "invalid number at line 1 column 7");
bad!("a = 0.E", "invalid number at line 1 column 7");
bad!("a = 0.0E", "invalid number at line 1 column 5");
bad!("a = 0.0e", "invalid number at line 1 column 5");
bad!("a = 0.0e-", "invalid number at line 1 column 9");
bad!("a = 0.0e+", "invalid number at line 1 column 5");
bad!("a = 0.0e+00", "invalid number at line 1 column 11");
}
#[test]
fn floats() {
macro_rules! t {
($actual:expr, $expected:expr) => {{
let f = format!("foo = {}", $actual);
println!("{}", f);
let a = f.parse::<Value>().unwrap();
assert_eq!(a["foo"].as_float().unwrap(), $expected);
}};
}
t!("1.0", 1.0);
t!("1.0e0", 1.0);
t!("1.0e+0", 1.0);
t!("1.0e-0", 1.0);
t!("1E-0", 1.0);
t!("1.001e-0", 1.001);
t!("2e10", 2e10);
t!("2e+10", 2e10);
t!("2e-10", 2e-10);
t!("2_0.0", 20.0);
t!("2_0.0_0e1_0", 20.0e10);
t!("2_0.1_0e1_0", 20.1e10);
}
#[test]
fn bare_key_names() {
let a = "
foo = 3
foo_3 = 3
foo_-2--3--r23f--4-f2-4 = 3
_ = 3
- = 3
8 = 8
\"a\" = 3
\"!\" = 3
\"a^b\" = 3
\"\\\"\" = 3
\"character encoding\" = \"value\"
'ʎǝʞ' = \"value\"
"
.parse::<Value>()
.unwrap();
&a["foo"];
&a["-"];
&a["_"];
&a["8"];
&a["foo_3"];
&a["foo_-2--3--r23f--4-f2-4"];
&a["a"];
&a["!"];
&a["\""];
&a["character encoding"];
&a["ʎǝʞ"];
}
#[test]
fn bad_keys() {
bad!(
"key\n=3",
"expected an equals, found a newline at line 1 column 4"
);
bad!(
"key=\n3",
"expected a value, found a newline at line 1 column 5"
);
bad!(
"key|=3",
"unexpected character found: `|` at line 1 column 4"
);
bad!("\"\"=3", "empty table key found at line 1 column 1");
bad!(
"=3",
"expected a table key, found an equals at line 1 column 1"
);
bad!("\"\"|=3", "empty table key found at line 1 column 1");
bad!("\"\n\"|=3", "newline in string found at line 1 column 2");
bad!(
"\"\r\"|=3",
"invalid character in string: `\\r` at line 1 column 2"
);
bad!(
"''''''=3",
"multiline strings are not allowed for key at line 1 column 1"
);
bad!(
"\"\"\"\"\"\"=3",
"multiline strings are not allowed for key at line 1 column 1"
);
bad!(
"'''key'''=3",
"multiline strings are not allowed for key at line 1 column 1"
);
bad!(
"\"\"\"key\"\"\"=3",
"multiline strings are not allowed for key at line 1 column 1"
);
}
#[test]
fn bad_table_names() {
bad!(
"[]",
"expected a table key, found a right bracket at line 1 column 2"
);
bad!(
"[.]",
"expected a table key, found a period at line 1 column 2"
);
bad!("[\"\".\"\"]", "empty table key found at line 1 column 2");
bad!(
"[a.]",
"expected a table key, found a right bracket at line 1 column 4"
);
bad!("[\"\"]", "empty table key found at line 1 column 2");
bad!("[!]", "unexpected character found: `!` at line 1 column 2");
bad!("[\"\n\"]", "newline in string found at line 1 column 3");
bad!(
"[a.b]\n[a.\"b\"]",
"redefinition of table `a.b` for key `a.b` at line 2 column 1"
);
bad!("[']", "unterminated string at line 1 column 2");
bad!("[''']", "unterminated string at line 1 column 2");
bad!(
"['''''']",
"multiline strings are not allowed for key at line 1 column 2"
);
bad!(
"['''foo''']",
"multiline strings are not allowed for key at line 1 column 2"
);
bad!(
"[\"\"\"bar\"\"\"]",
"multiline strings are not allowed for key at line 1 column 2"
);
bad!("['\n']", "newline in string found at line 1 column 3");
bad!("['\r\n']", "newline in string found at line 1 column 3");
}
#[test]
fn table_names() {
let a = "
[a.\"b\"]
[\"f f\"]
[\"f.f\"]
[\"\\\"\"]
['a.a']
['\"\"']
"
.parse::<Value>()
.unwrap();
println!("{:?}", a);
&a["a"]["b"];
&a["f f"];
&a["f.f"];
&a["\""];
&a["\"\""];
}
#[test]
fn invalid_bare_numeral() {
bad!("4", "expected an equals, found eof at line 1 column 2");
}
#[test]
fn inline_tables() {
"a = {}".parse::<Value>().unwrap();
"a = {b=1}".parse::<Value>().unwrap();
"a = { b = 1 }".parse::<Value>().unwrap();
"a = {a=1,b=2}".parse::<Value>().unwrap();
"a = {a=1,b=2,c={}}".parse::<Value>().unwrap();
bad!(
"a = {a=1,}",
"expected a table key, found a right brace at line 1 column 10"
);
bad!(
"a = {,}",
"expected a table key, found a comma at line 1 column 6"
);
bad!(
"a = {a=1,a=1}",
"duplicate key: `a` for key `a` at line 1 column 5"
);
bad!(
"a = {\n}",
"expected a table key, found a newline at line 1 column 6"
);
bad!(
"a = {",
"expected a table key, found eof at line 1 column 6"
);
"a = {a=[\n]}".parse::<Value>().unwrap();
"a = {\"a\"=[\n]}".parse::<Value>().unwrap();
"a = [\n{},\n{},\n]".parse::<Value>().unwrap();
}
#[test]
fn number_underscores() {
macro_rules! t {
($actual:expr, $expected:expr) => {{
let f = format!("foo = {}", $actual);
let table = f.parse::<Value>().unwrap();
assert_eq!(table["foo"].as_integer().unwrap(), $expected);
}};
}
t!("1_0", 10);
t!("1_0_0", 100);
t!("1_000", 1000);
t!("+1_000", 1000);
t!("-1_000", -1000);
}
#[test]
fn bad_underscores() {
bad!("foo = 0_", "invalid number at line 1 column 7");
bad!("foo = 0__0", "invalid number at line 1 column 7");
bad!("foo = __0", "invalid number at line 1 column 7");
bad!("foo = 1_0_", "invalid number at line 1 column 7");
}
#[test]
fn bad_unicode_codepoint() {
bad!(
"foo = \"\\uD800\"",
"invalid escape value: `55296` at line 1 column 9"
);
}
#[test]
fn bad_strings() {
bad!(
"foo = \"\\uxx\"",
"invalid hex escape character in string: `x` at line 1 column 10"
);
bad!(
"foo = \"\\u\"",
"invalid hex escape character in string: `\\\"` at line 1 column 10"
);
bad!("foo = \"\\", "unterminated string at line 1 column 7");
bad!("foo = '", "unterminated string at line 1 column 7");
}
#[test]
fn empty_string() {
assert_eq!(
"foo = \"\"".parse::<Value>().unwrap()["foo"]
.as_str()
.unwrap(),
""
);
}
#[test]
fn booleans() {
let table = "foo = true".parse::<Value>().unwrap();
assert_eq!(table["foo"].as_bool(), Some(true));
let table = "foo = false".parse::<Value>().unwrap();
assert_eq!(table["foo"].as_bool(), Some(false));
bad!(
"foo = true2",
"failed to parse datetime for key `foo` at line 1 column 7"
);
bad!("foo = false2", "invalid number at line 1 column 7");
bad!(
"foo = t1",
"failed to parse datetime for key `foo` at line 1 column 7"
);
bad!("foo = f2", "invalid number at line 1 column 7");
}
#[test]
fn bad_nesting() {
bad!(
"
a = [2]
[[a]]
b = 5
",
"duplicate key: `a` at line 3 column 9"
);
bad!(
"
a = 1
[a.b]
",
"duplicate key: `a` at line 3 column 9"
);
bad!(
"
a = []
[a.b]
",
"duplicate key: `a` at line 3 column 9"
);
bad!(
"
a = []
[[a.b]]
",
"duplicate key: `a` at line 3 column 9"
);
bad!(
"
[a]
b = { c = 2, d = {} }
[a.b]
c = 2
",
"duplicate key: `b` for key `a` at line 4 column 9"
);
}
#[test]
fn bad_table_redefine() {
bad!(
"
[a]
foo=\"bar\"
[a.b]
foo=\"bar\"
[a]
",
"redefinition of table `a` for key `a` at line 6 column 9"
);
bad!(
"
[a]
foo=\"bar\"
b = { foo = \"bar\" }
[a]
",
"redefinition of table `a` for key `a` at line 5 column 9"
);
bad!(
"
[a]
b = {}
[a.b]
",
"duplicate key: `b` for key `a` at line 4 column 9"
);
bad!(
"
[a]
b = {}
[a]
",
"redefinition of table `a` for key `a` at line 4 column 9"
);
}
#[test]
fn datetimes() {
macro_rules! t {
($actual:expr) => {{
let f = format!("foo = {}", $actual);
let toml = f.parse::<Value>().expect(&format!("failed: {}", f));
assert_eq!(toml["foo"].as_datetime().unwrap().to_string(), $actual);
}};
}
t!("2016-09-09T09:09:09Z");
t!("2016-09-09T09:09:09.1Z");
t!("2016-09-09T09:09:09.2+10:00");
t!("2016-09-09T09:09:09.123456789-02:00");
bad!(
"foo = 2016-09-09T09:09:09.Z",
"failed to parse datetime for key `foo` at line 1 column 7"
);
bad!(
"foo = 2016-9-09T09:09:09Z",
"failed to parse datetime for key `foo` at line 1 column 7"
);
bad!(
"foo = 2016-09-09T09:09:09+2:00",
"failed to parse datetime for key `foo` at line 1 column 7"
);
bad!(
"foo = 2016-09-09T09:09:09-2:00",
"failed to parse datetime for key `foo` at line 1 column 7"
);
bad!(
"foo = 2016-09-09T09:09:09Z-2:00",
"failed to parse datetime for key `foo` at line 1 column 7"
);
}
#[test]
fn require_newline_after_value() {
bad!("0=0r=false", "invalid number at line 1 column 3");
bad!(
r#"
0=""o=""m=""r=""00="0"q="""0"""e="""0"""
"#,
"expected newline, found an identifier at line 2 column 5"
);
bad!(
r#"
[[0000l0]]
0="0"[[0000l0]]
0="0"[[0000l0]]
0="0"l="0"
"#,
"expected newline, found a left bracket at line 3 column 6"
);
bad!(
r#"
0=[0]00=[0,0,0]t=["0","0","0"]s=[1000-00-00T00:00:00Z,2000-00-00T00:00:00Z]
"#,
"expected newline, found an identifier at line 2 column 6"
);
bad!(
r#"
0=0r0=0r=false
"#,
"invalid number at line 2 column 3"
);
bad!(
r#"
0=0r0=0r=falsefal=false
"#,
"invalid number at line 2 column 3"
);
}
|