-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser_test.mbt
More file actions
656 lines (619 loc) · 14.5 KB
/
parser_test.mbt
File metadata and controls
656 lines (619 loc) · 14.5 KB
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
///|
/// Tests for the TOML parser
test "parse simple key-value string" {
let result = @toml.parse(
(
#|name = "John"
),
)
debug_inspect(
result,
content=(
#|TomlTable({ "name": TomlString("John") })
),
)
}
///|
test "parse simple key-value integer" {
let result = @toml.parse(
(
#|age = 25
),
)
debug_inspect(
result,
content=(
#|TomlTable({ "age": TomlInteger(25) })
),
)
}
///|
test "parse simple key-value boolean" {
let result = @toml.parse(
(
#|enabled = true
),
)
debug_inspect(
result,
content=(
#|TomlTable({ "enabled": TomlBoolean(true) })
),
)
}
///|
test "parse simple key-value float" {
let result = @toml.parse(
(
#|pi = 3.14
),
)
debug_inspect(
result,
content=(
#|TomlTable({ "pi": TomlFloat(3.14) })
),
)
}
///|
test "parse multiple key-value pairs" {
let multiple_kvp_toml =
#|name = "Alice"
#|age = 30
#|enabled = false
#|
let result = @toml.parse(multiple_kvp_toml)
debug_inspect(
result,
content=(
#|TomlTable(
#| {
#| "name": TomlString("Alice"),
#| "age": TomlInteger(30),
#| "enabled": TomlBoolean(false),
#| },
#|)
),
)
}
///|
test "parse array" {
let result = @toml.parse(
(
#|numbers = [1, 2, 3]
),
)
debug_inspect(
result,
content=(
#|TomlTable({ "numbers": TomlArray([TomlInteger(1), TomlInteger(2), TomlInteger(3)]) })
),
)
}
///|
test "parse array ending with exponent float" {
let result = try? @toml.parse(
(
#|numbers = [-1e2]
),
)
debug_inspect(
result,
content=(
#|Ok(TomlTable({ "numbers": TomlArray([TomlFloat(-100)]) }))
),
)
}
///|
test "parse inline table" {
let inline_table_toml =
#|person = {name = "Bob", age = 25}
#|
let result = @toml.parse(inline_table_toml)
debug_inspect(
result,
content=(
#|TomlTable({ "person": TomlTable({ "name": TomlString("Bob"), "age": TomlInteger(25) }) })
),
)
}
///|
/// Tests for uncovered parser error conditions
test "test parser expect method failure" {
inspect(
parse_expect_to_fail("key = [missing_bracket"),
content=(
#|Expected value at { start: { line: 1, column: 8 }, end: { line: 1, column: 23 } }
),
)
}
///|
test "test parser EOF handling in peek" {
debug_inspect(
try? @toml.parse(""),
content=(
#|Ok(TomlTable({}))
),
)
}
///|
test "test parser EOF handling in advance" {
inspect(
parse_expect_to_fail("key ="),
content=(
#|Expected value at { start: { line: 1, column: 6 }, end: { line: 1, column: 6 } }
),
)
}
///|
test "test parser rejects dash-starting identifier as value" {
inspect(
parse_expect_to_fail("key = -abc"),
content=(
#|Expected value at { start: { line: 1, column: 7 }, end: { line: 1, column: 11 } }
),
)
}
///|
test "test inline table with string keys" {
debug_inspect(
try? @toml.parse("table = {\"string key\" = \"value\"}"),
content=(
#|Ok(TomlTable({ "table": TomlTable({ "string key": TomlString("value") }) }))
),
)
}
///|
test "test inline table invalid syntax" {
inspect(
parse_expect_to_fail("table = {key value}"),
content=(
#|Expected '=' at { start: { line: 1, column: 14 }, end: { line: 1, column: 19 } }
),
)
}
///|
test "test inline table missing comma or closing brace" {
inspect(
parse_expect_to_fail("table = {key = \"value\" invalid}"),
content=(
#|Expected ',' or '}' in inline table at { start: { line: 1, column: 24 }, end: { line: 1, column: 31 } }
),
)
}
///|
test "test table header with string name" {
let string_table_name_toml =
#|["table name"]
#|key = "value"
#|
debug_inspect(
try? @toml.parse(string_table_name_toml),
content=(
#|Ok(TomlTable({ "table name": TomlTable({ "key": TomlString("value") }) }))
),
)
}
///|
test "test table header numeric name" {
let numeric_table_name_toml =
#|[123]
#|key = "value"
#|
debug_inspect(
try? @toml.parse(numeric_table_name_toml),
content=(
#|Ok(TomlTable({ "123": TomlTable({ "key": TomlString("value") }) }))
),
)
}
///|
test "tokenize exponent-like bare keys" {
// Each line shows how the tokenizer sees an exponent-like key followed by `= "val"`
debug_inspect(
@tokenize.tokenize("10e-1 = \"a\""),
content=(
#|[
#| Identifier(
#| "10e-1",
#| loc={ start: { line: 1, column: 1 }, end: { line: 1, column: 6 } },
#| ),
#| Equals(loc={ start: { line: 1, column: 7 }, end: { line: 1, column: 8 } }),
#| StringToken(
#| "a",
#| loc={ start: { line: 1, column: 9 }, end: { line: 1, column: 12 } },
#| multiline=false,
#| ),
#| EOF(loc={ start: { line: 1, column: 12 }, end: { line: 1, column: 12 } }),
#|]
),
)
debug_inspect(
@tokenize.tokenize("10e1 = \"b\""),
content=(
#|[
#| Identifier(
#| "10e1",
#| loc={ start: { line: 1, column: 1 }, end: { line: 1, column: 5 } },
#| ),
#| Equals(loc={ start: { line: 1, column: 6 }, end: { line: 1, column: 7 } }),
#| StringToken(
#| "b",
#| loc={ start: { line: 1, column: 8 }, end: { line: 1, column: 11 } },
#| multiline=false,
#| ),
#| EOF(loc={ start: { line: 1, column: 11 }, end: { line: 1, column: 11 } }),
#|]
),
)
debug_inspect(
@tokenize.tokenize("-e-1 = \"c\""),
content=(
#|[
#| Identifier(
#| "-e-1",
#| loc={ start: { line: 1, column: 1 }, end: { line: 1, column: 5 } },
#| ),
#| Equals(loc={ start: { line: 1, column: 6 }, end: { line: 1, column: 7 } }),
#| StringToken(
#| "c",
#| loc={ start: { line: 1, column: 8 }, end: { line: 1, column: 11 } },
#| multiline=false,
#| ),
#| EOF(loc={ start: { line: 1, column: 11 }, end: { line: 1, column: 11 } }),
#|]
),
)
debug_inspect(
@tokenize.tokenize("-10e-1 = \"d\""),
content=(
#|[
#| FloatToken(
#| -1,
#| loc={ start: { line: 1, column: 1 }, end: { line: 1, column: 7 } },
#| raw="-10e-1",
#| ),
#| Equals(loc={ start: { line: 1, column: 8 }, end: { line: 1, column: 9 } }),
#| StringToken(
#| "d",
#| loc={ start: { line: 1, column: 10 }, end: { line: 1, column: 13 } },
#| multiline=false,
#| ),
#| EOF(loc={ start: { line: 1, column: 13 }, end: { line: 1, column: 13 } }),
#|]
),
)
}
///|
test "test exponent-like bare keys" {
let exponent_like_keys_toml =
#|10e-1 = "a"
#|10e1 = "b"
#|-e-1 = "c"
#|-10e-1 = "d"
#|
debug_inspect(
try? @toml.parse(exponent_like_keys_toml),
content=(
#|Ok(
#| TomlTable(
#| {
#| "10e-1": TomlString("a"),
#| "10e1": TomlString("b"),
#| "-e-1": TomlString("c"),
#| "-10e-1": TomlString("d"),
#| },
#| ),
#|)
),
)
}
///|
test "test exponent-like bare table headers" {
let exponent_like_table_names_toml =
#|[10e-1]
#|value = "a"
#|[10e1]
#|value = "b"
#|[-e-1]
#|value = "c"
#|[-10e-1]
#|value = "d"
#|
debug_inspect(
try? @toml.parse(exponent_like_table_names_toml),
content=(
#|Ok(
#| TomlTable(
#| {
#| "10e-1": TomlTable({ "value": TomlString("a") }),
#| "10e1": TomlTable({ "value": TomlString("b") }),
#| "-e-1": TomlTable({ "value": TomlString("c") }),
#| "-10e-1": TomlTable({ "value": TomlString("d") }),
#| },
#| ),
#|)
),
)
}
///|
test "overflow float rejected in value position" {
// 1e100000 overflows Double but should NOT silently become infinity.
// TOML reserves inf/nan as the only valid non-finite spellings.
inspect(
parse_expect_to_fail("x = 1e100000\n"),
content="Invalid float: 1e100000 at { start: { line: 1, column: 5 }, end: { line: 1, column: 13 } }",
)
inspect(
parse_expect_to_fail("x = -31e368\n"),
content="Invalid float: -31e368 at { start: { line: 1, column: 5 }, end: { line: 1, column: 12 } }",
)
}
///|
test "overflow float accepted as bare key" {
// The same overflow token is valid as a bare key (all chars are A-Za-z0-9_-)
debug_inspect(
try? @toml.parse("-31e368 = \"ok\"\n"),
content=(
#|Ok(TomlTable({ "-31e368": TomlString("ok") }))
),
)
}
///|
test "special float keywords as keys" {
// inf, nan, +inf, -inf, etc. are valid bare keys in TOML
debug_inspect(
try? @toml.parse("+inf = 1\n"),
content=(
#|Ok(TomlTable({ "+inf": TomlInteger(1) }))
),
)
debug_inspect(
try? @toml.parse("-inf = 2\n"),
content=(
#|Ok(TomlTable({ "-inf": TomlInteger(2) }))
),
)
debug_inspect(
try? @toml.parse("nan = 3\n"),
content=(
#|Ok(TomlTable({ "nan": TomlInteger(3) }))
),
)
}
///|
test "dotted key with float-like segments" {
// Float token with dot in raw string should split into dotted keys
debug_inspect(
try? @toml.parse("1.2 = \"a\"\n"),
content=(
#|Ok(TomlTable({ "1": TomlTable({ "2": TomlString("a") }) }))
),
)
// Float-like key followed by dotted sub-key
debug_inspect(
try? @toml.parse("[-80e-2.sub]\nval = 1\n"),
content=(
#|Ok(TomlTable({ "-80e-2": TomlTable({ "sub": TomlTable({ "val": TomlInteger(1) }) }) }))
),
)
}
///|
test "test duplicate table redefinition" {
let duplicate_table_toml =
#|[table]
#|key1 = "value1"
#|[table]
#|key2 = "value2"
#|
inspect(
parse_expect_to_fail(duplicate_table_toml),
content=(
#|Duplicate table definition: [table] at { start: { line: 3, column: 8 }, end: { line: 4, column: 1 } }
),
)
}
///|
test "test table name conflicts with existing value" {
let conflicting_table_toml =
#|table = "string"
#|[table]
#|key = "value"
#|
inspect(
parse_expect_to_fail(conflicting_table_toml),
content=(
#|ExpectedTable(\"table\", \"string\") at { start: { line: 2, column: 8 }, end: { line: 3, column: 1 } }
),
)
}
///|
/// Tests for inline table edge cases
test "test empty inline table" {
debug_inspect(
try? @toml.parse("empty = {}"),
content=(
#|Ok(TomlTable({ "empty": TomlTable({}) }))
),
)
}
///|
test "test nested inline tables" {
debug_inspect(
try? @toml.parse("table = {inner = {key = \"value\"}}"),
content=(
#|Ok(TomlTable({ "table": TomlTable({ "inner": TomlTable({ "key": TomlString("value") }) }) }))
),
)
}
///|
test "test inline table with multiple key types" {
debug_inspect(
try? @toml.parse("mixed = {\"quoted\" = 1, unquoted = 2}"),
content=(
#|Ok(TomlTable({ "mixed": TomlTable({ "quoted": TomlInteger(1), "unquoted": TomlInteger(2) }) }))
),
)
}
///|
test "test inline table with array values" {
debug_inspect(
try? @toml.parse("table = {arr = [1, 2, 3], str = \"test\"}"),
content=(
#|Ok(
#| TomlTable(
#| {
#| "table": TomlTable(
#| {
#| "arr": TomlArray([TomlInteger(1), TomlInteger(2), TomlInteger(3)]),
#| "str": TomlString("test"),
#| },
#| ),
#| },
#| ),
#|)
),
)
}
///|
/// Test dotted key notation - simple case
test "dotted key notation simple" {
let result = @toml.parse(
(
#|a.b.c = "value"
),
)
debug_inspect(
result,
content=(
#|TomlTable({ "a": TomlTable({ "b": TomlTable({ "c": TomlString("value") }) }) })
),
)
}
///|
/// Test dotted key notation - multiple values
test "dotted key notation multiple" {
let dotted_multiple_toml =
#|a.b.c = "value1"
#|a.b.d = "value2"
#|a.e = "value3"
#|f = "value4"
#|
debug_inspect(
@toml.parse(dotted_multiple_toml),
content=(
#|TomlTable(
#| {
#| "a": TomlTable(
#| {
#| "b": TomlTable({ "c": TomlString("value1"), "d": TomlString("value2") }),
#| "e": TomlString("value3"),
#| },
#| ),
#| "f": TomlString("value4"),
#| },
#|)
),
)
}
///|
/// Test dotted key notation - different data types
test "dotted key notation data types" {
let dotted_types_toml =
#|server.host = "localhost"
#|server.port = 8080
#|server.enabled = true
#|server.timeout = 30.5
#|server.tags = ["web", "api"]
#|
debug_inspect(
@toml.parse(dotted_types_toml),
content=(
#|TomlTable(
#| {
#| "server": TomlTable(
#| {
#| "host": TomlString("localhost"),
#| "port": TomlInteger(8080),
#| "enabled": TomlBoolean(true),
#| "timeout": TomlFloat(30.5),
#| "tags": TomlArray([TomlString("web"), TomlString("api")]),
#| },
#| ),
#| },
#|)
),
)
}
///|
/// Test dotted key notation - quoted keys
test "dotted key notation quoted keys" {
let result = @toml.parse(
(
#|"first.part"."second.part" = "value"
),
)
debug_inspect(
result,
content=(
#|TomlTable({ "first.part": TomlTable({ "second.part": TomlString("value") }) })
),
)
}
///|
/// Test dotted key notation - integer keys
test "dotted key notation integer keys" {
let result = @toml.parse(
(
#|1.2.3 = "numeric path"
),
)
debug_inspect(
result,
content=(
#|TomlTable({ "1": TomlTable({ "2": TomlTable({ "3": TomlString("numeric path") }) }) })
),
)
}
///|
/// Test dotted key notation - conflict with table headers
test "dotted key notation table conflict" {
let conflict_toml =
#|a.b = "dotted"
#|[a]
#|c = "table"
#|
debug_inspect(
@toml.parse(conflict_toml),
content=(
#|TomlTable({ "a": TomlTable({ "b": TomlString("dotted"), "c": TomlString("table") }) })
),
)
}
///|
/// Test dotted key notation - deep nesting
test "dotted key notation deep nesting" {
let result = @toml.parse(
(
#|a.b.c.d.e.f = "deep"
),
)
// TODO:
// TOML::to_string is not really good,
debug_inspect(
result,
content=(
#|TomlTable({ "a": TomlTable({ "b": TomlTable({ "c": TomlTable({ "d": TomlTable({ "e": TomlTable({ "f": TomlString("deep") }) }) }) }) }) })
),
)
}
///|
#skip("this is a bug to be fixed")
test "complex dotted keys" {
let data =
#| [x]
#|server."port number".8080.config = 3
#|
// TODO: fix
debug_inspect(try? @toml.parse(data), content="")
}