File: TomlSpec.hs

package info (click to toggle)
haskell-toml-parser 1.3.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312 kB
  • sloc: haskell: 2,980; yacc: 116; makefile: 3
file content (781 lines) | stat: -rw-r--r-- 26,490 bytes parent folder | download
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
{-# Language QuasiQuotes #-}
{-|
Module      : TomlSpec
Description : Unit tests
Copyright   : (c) Eric Mertens, 2023
License     : ISC
Maintainer  : emertens@gmail.com

TOML parser and validator unit tests (primarily drawn from the
specification document).

-}
module TomlSpec (spec) where

import Data.Map qualified as Map
import Data.Time (Day)
import QuoteStr (quoteStr)
import Test.Hspec (describe, it, shouldBe, shouldSatisfy, Spec)
import Toml (Value(..), parse, decode, Result(Success))
import Toml.ToValue (table, (.=))

spec :: Spec
spec =
 do describe "comment"
     do it "ignores comments" $
          parse [quoteStr|
            # This is a full-line comment
            key = "value"  # This is a comment at the end of a line
            another = "# This is not a comment"|]
          `shouldBe`
          Right (table [("another",String "# This is not a comment"),("key",String "value")])

    describe "key/value pair"
     do it "supports the most basic assignments" $
          parse "key = \"value\"" `shouldBe` Right (Map.singleton "key" (String "value"))

        it "requires a value after equals" $
          parse "key = # INVALID"
          `shouldBe`
          Left "1:16: parse error: unexpected end-of-input"

        it "requires newlines between assignments" $
          parse "first = \"Tom\" last = \"Preston-Werner\" # INVALID"
          `shouldBe`
          Left "1:15: parse error: unexpected bare key"

    describe "keys"
     do it "allows bare keys" $
          parse [quoteStr|
            key = "value"
            bare_key = "value"
            bare-key = "value"
            1234 = "value"|]
          `shouldBe`
          Right (table [
            "1234"     .= "value",
            "bare-key" .= "value",
            "bare_key" .= "value",
            "key"      .= "value"])

        it "allows quoted keys" $
          parse [quoteStr|
            "127.0.0.1" = "value"
            "character encoding" = "value"
            "ʎǝʞ" = "value"
            'key2' = "value"
            'quoted "value"' = "value"|]
          `shouldBe`
          Right (table [
            "127.0.0.1"          .= "value",
            "character encoding" .= "value",
            "key2"               .= "value",
            "quoted \"value\""   .= "value",
            "ʎǝʞ"                .= "value"])

        it "allows dotted keys" $
          parse [quoteStr|
            name = "Orange"
            physical.color = "orange"
            physical.shape = "round"
            site."google.com" = true|]
          `shouldBe`
          Right (table [
            "name"     .= "Orange",
            "physical" .= table ["color" .= "orange", "shape" .= "round"],
            "site"     .= table ["google.com" .= True]])

        it "prevents duplicate keys" $
          parse [quoteStr|
            name = "Tom"
            name = "Pradyun"|]
          `shouldBe` Left "2:1: key error: name is already assigned"

        it "prevents duplicate keys even between bare and quoted" $
          parse [quoteStr|
            spelling = "favorite"
            "spelling" = "favourite"|]
          `shouldBe` Left "2:1: key error: spelling is already assigned"

        it "allows out of order definitions" $
          parse [quoteStr|
            apple.type = "fruit"
            orange.type = "fruit"

            apple.skin = "thin"
            orange.skin = "thick"

            apple.color = "red"
            orange.color = "orange"|]
          `shouldBe`
          Right (table [
            "apple" .= table [
                "color" .= "red",
                "skin"  .= "thin",
                "type"  .= "fruit"],
            "orange" .= table [
                "color" .= "orange",
                "skin"  .= "thick",
                "type"  .= "fruit"]])

        it "allows numeric bare keys" $
          parse "3.14159 = 'pi'" `shouldBe` Right (table [
            "3" .= table [("14159", String "pi")]])

        it "allows keys that look like other values" $
          parse [quoteStr|
            true = true
            false = false
            1900-01-01 = 1900-01-01
            1_2 = 2_3|]
          `shouldBe`
          Right (table [
            "1900-01-01" .= (read "1900-01-01" :: Day),
            "1_2"        .= (23::Int),
            "false"      .= False,
            "true"       .= True])

    describe "string"
     do it "parses escapes" $
          parse [quoteStr|
            str = "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."|]
          `shouldBe`
          Right (Map.singleton "str" (String "I'm a string. \"You can quote me\". Name\tJos\xe9\nLocation\tSF."))

        it "strips the initial newline from multiline strings" $
          parse [quoteStr|
            str1 = """
            Roses are red
            Violets are blue"""|]
          `shouldBe` Right (Map.singleton "str1" (String "Roses are red\nViolets are blue"))

        it "strips whitespace with a trailing escape" $
          parse [quoteStr|
            # The following strings are byte-for-byte equivalent:
            str1 = "The quick brown fox jumps over the lazy dog."

            str2 = """
            The quick brown \


            fox jumps over \
                the lazy dog."""

            str3 = """\
                The quick brown \
                fox jumps over \
                the lazy dog.\
                """|]
          `shouldBe`
          Right (table [
            "str1" .= "The quick brown fox jumps over the lazy dog.",
            "str2" .= "The quick brown fox jumps over the lazy dog.",
            "str3" .= "The quick brown fox jumps over the lazy dog."])

        it "allows quotes inside multiline quoted strings" $
          parse [quoteStr|
            str4 = """Here are two quotation marks: "". Simple enough."""
            str5 = """Here are three quotation marks: ""\"."""
            str6 = """Here are fifteen quotation marks: ""\"""\"""\"""\"""\"."""

            # "This," she said, "is just a pointless statement."
            str7 = """"This," she said, "is just a pointless statement.""""|]
          `shouldBe`
          Right (table [
            "str4" .= "Here are two quotation marks: \"\". Simple enough.",
            "str5" .= "Here are three quotation marks: \"\"\".",
            "str6" .= "Here are fifteen quotation marks: \"\"\"\"\"\"\"\"\"\"\"\"\"\"\".",
            "str7" .= "\"This,\" she said, \"is just a pointless statement.\""])

        it "disallows triple quotes inside a multiline string" $
          parse [quoteStr|
            str5 = """Here are three quotation marks: """."""  # INVALID|]
          `shouldBe` Left "1:46: parse error: unexpected '.'"

        it "ignores escapes in literal strings" $
          parse [quoteStr|
            # 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*>'|]
          `shouldBe`
          Right (table [
            "quoted"   .= "Tom \"Dubs\" Preston-Werner",
            "regex"    .= "<\\i\\c*\\s*>",
            "winpath"  .= "C:\\Users\\nodejs\\templates",
            "winpath2" .= "\\\\ServerX\\admin$\\system32\\"])

        it "handles multiline literal strings" $
          parse [quoteStr|
            regex2 = '''I [dw]on't need \d{2} apples'''
            lines  = '''
            The first newline is
            trimmed in raw strings.
            All other whitespace
            is preserved.
            '''|]
          `shouldBe`
          Right (table [
            "lines"  .= "The first newline is\ntrimmed in raw strings.\nAll other whitespace\nis preserved.\n",
            "regex2" .= "I [dw]on't need \\d{2} apples"])

        it "parses all the other escapes" $
          parse [quoteStr|
            x = "\\\b\f\r\U0010abcd"
            y = """\\\b\f\r\u7bca\U0010abcd\n\r\t"""|]
          `shouldBe`
          Right (table [
            "x" .= "\\\b\f\r\x0010abcd",
            "y" .= "\\\b\f\r\x7bca\x0010abcd\n\r\t"])

        it "rejects out of range unicode escapes" $
          parse [quoteStr|
            x = "\U11111111"|]
          `shouldBe` Left "1:6: lexical error: unicode escape too large"

        it "handles unexpected end of line" $
          parse [quoteStr|
            x = "example
            y = 42|]
          `shouldBe` Left "1:13: lexical error: unexpected end-of-line"

    describe "integer"
     do it "parses literals correctly" $
          parse [quoteStr|
            int1 = +99
            int2 = 42
            int3 = 0
            int4 = -17
            int5 = 1_000
            int6 = 5_349_221
            int7 = 53_49_221  # Indian number system grouping
            int8 = 1_2_3_4_5  # VALID but discouraged
            # hexadecimal with prefix `0x`
            hex1 = 0xDEADBEEF
            hex2 = 0xdeadbeef
            hex3 = 0xdead_beef

            # octal with prefix `0o`
            oct1 = 0o01234567
            oct2 = 0o755 # useful for Unix file permissions

            # binary with prefix `0b`
            bin1 = 0b11010110|]
          `shouldBe` Right
          (table [
              "bin1" .= Integer 214,
              "hex1" .= Integer 0xDEADBEEF,
              "hex2" .= Integer 0xDEADBEEF,
              "hex3" .= Integer 0xDEADBEEF,
              "int1" .= Integer 99,
              "int2" .= Integer 42,
              "int3" .= Integer 0,
              "int4" .= Integer (-17),
              "int5" .= Integer 1000,
              "int6" .= Integer 5349221,
              "int7" .= Integer 5349221,
              "int8" .= Integer 12345,
              "oct1" .= Integer 0o01234567,
              "oct2" .= Integer 0o755])

    it "handles leading zeros gracefully" $
      parse "x = 01"
      `shouldBe`
      Left "1:5: lexical error: leading zero prohibited"


    describe "float"
     do it "parses floats" $
          parse [quoteStr|
            # fractional
            flt1 = +1.0
            flt2 = 3.1415
            flt3 = -0.01

            # exponent
            flt4 = 5e+22
            flt5 = 1e06
            flt6 = -2E-2

            # both
            flt7 = 6.626e-34
            flt8 = 224_617.445_991_228
            # infinity
            sf1 = inf  # positive infinity
            sf2 = +inf # positive infinity
            sf3 = -inf # negative infinity|]
          `shouldBe`
          Right (table [
            "flt1" .= Float 1.0,
            "flt2" .= Float 3.1415,
            "flt3" .= Float (-1.0e-2),
            "flt4" .= Float 4.9999999999999996e22,
            "flt5" .= Float 1000000.0,
            "flt6" .= Float (-2.0e-2),
            "flt7" .= Float 6.626e-34,
            "flt8" .= Float 224617.445991228,
            "sf1"  .= Float (1/0),
            "sf2"  .= Float (1/0),
            "sf3"  .= Float (-1/0)])

        it "parses nan correctly" $
          let checkNaN (Float x) = isNaN x
              checkNaN _         = False
          in
          parse [quoteStr|
            # not a number
            sf4 = nan  # actual sNaN/qNaN encoding is implementation-specific
            sf5 = +nan # same as `nan`
            sf6 = -nan # valid, actual encoding is implementation-specific|]
          `shouldSatisfy` \case
            Left{} -> False
            Right x -> all checkNaN x

        -- code using Numeric.readFloat can use significant
        -- resources. this makes sure this doesn't start happening
        -- in the future
        it "parses huge floats without great delays" $
          parse "x = 1e1000000000000"
          `shouldBe`
          Right (Map.singleton "x" (Float (1/0)))

    describe "boolean"
     do it "parses boolean literals" $
          parse [quoteStr|
            bool1 = true
            bool2 = false|]
          `shouldBe`
          Right (table [
            "bool1" .= True,
            "bool2" .= False])

    describe "offset date-time"
     do it "parses offset date times" $
          parse [quoteStr|
            odt1 = 1979-05-27T07:32:00Z
            odt2 = 1979-05-27T00:32:00-07:00
            odt3 = 1979-05-27T00:32:00.999999-07:00
            odt4 = 1979-05-27 07:32:00Z|]
          `shouldBe`
          Right (table [
            "odt1" .= ZonedTime (read "1979-05-27 07:32:00 +0000"),
            "odt2" .= ZonedTime (read "1979-05-27 00:32:00 -0700"),
            "odt3" .= ZonedTime (read "1979-05-27 00:32:00.999999 -0700"),
            "odt4" .= ZonedTime (read "1979-05-27 07:32:00 +0000")])

    describe "local date-time"
     do it "parses local date-times" $
          parse [quoteStr|
            ldt1 = 1979-05-27T07:32:00
            ldt2 = 1979-05-27T00:32:00.999999
            ldt3 = 1979-05-28 00:32:00.999999|]
          `shouldBe`
          Right (table [
            "ldt1" .= LocalTime (read "1979-05-27 07:32:00"),
            "ldt2" .= LocalTime (read "1979-05-27 00:32:00.999999"),
            "ldt3" .= LocalTime (read "1979-05-28 00:32:00.999999")])

        it "catches invalid date-times" $
          parse [quoteStr|
            ldt = 9999-99-99T99:99:99|]
          `shouldBe`
          Left "1:7: lexical error: malformed local date-time"

    describe "local date"
     do it "parses dates" $
          parse [quoteStr|
            ld1 = 1979-05-27|]
          `shouldBe`
          Right (Map.singleton "ld1" (Day (read "1979-05-27")))

    describe "local time"
     do it "parses times" $
          parse [quoteStr|
            lt1 = 07:32:00
            lt2 = 00:32:00.999999|]
          `shouldBe`
          Right (table [
            "lt1" .= TimeOfDay (read "07:32:00"),
            "lt2" .= TimeOfDay (read "00:32:00.999999")])

    describe "array"
     do it "parses array examples" $
          parse [quoteStr|
            integers = [ 1, 2, 3 ]
            colors = [ "red", "yellow", "green" ]
            nested_arrays_of_ints = [ [ 1, 2 ], [3, 4, 5] ]
            nested_mixed_array = [ [ 1, 2 ], ["a", "b", "c"] ]
            string_array = [ "all", 'strings', """are the same""", '''type''' ]

            # Mixed-type arrays are allowed
            numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]
            contributors = [
            "Foo Bar <foo@example.com>",
            { name = "Baz Qux", email = "bazqux@example.com", url = "https://example.com/bazqux" }
            ]|]
            `shouldBe`
            Right (table [
                "colors" .= ["red", "yellow", "green"],
                "contributors" .= [
                    String "Foo Bar <foo@example.com>",
                    Table (table [
                        "email" .= "bazqux@example.com",
                        "name" .= "Baz Qux",
                        "url" .= "https://example.com/bazqux"])],
                "integers" .= [1, 2, 3 :: Integer],
                "nested_arrays_of_ints" .= [[1, 2], [3, 4, 5 :: Integer]],
                "nested_mixed_array" .= [[Integer 1, Integer 2], [String "a", String "b", String "c"]],
                "numbers" .= [Float 0.1, Float 0.2, Float 0.5, Integer 1, Integer 2, Integer 5],
                "string_array" .= ["all", "strings", "are the same", "type"]])

        it "handles newlines and comments" $
          parse [quoteStr|
            integers2 = [
            1, 2, 3
            ]

            integers3 = [
            1,
            2, # this is ok
            ]|]
            `shouldBe`
            Right (table [
                "integers2" .= [1, 2, 3 :: Int],
                "integers3" .= [1, 2 :: Int]])

        it "disambiguates double brackets from array tables" $
          parse "x = [[1]]" `shouldBe` Right (Map.singleton "x" (Array [Array [Integer 1]]))

    describe "table"
     do it "allows empty tables" $
          parse "[table]" `shouldBe` Right (table ["table" .= table []])

        it "parses simple tables" $
          parse [quoteStr|
            [table-1]
            key1 = "some string"
            key2 = 123

            [table-2]
            key1 = "another string"
            key2 = 456|]
          `shouldBe`
          Right (table [
            "table-1" .= table [
                "key1" .= "some string",
                "key2" .= Integer 123],
            "table-2" .= table [
                "key1" .= "another string",
                "key2" .= Integer 456]])

        it "allows quoted keys" $
          parse [quoteStr|
            [dog."tater.man"]
            type.name = "pug"|]
          `shouldBe`
          Right (table ["dog" .= table ["tater.man" .= table ["type" .= table ["name" .= "pug"]]]])

        it "allows whitespace around keys" $
          parse [quoteStr|
            [a.b.c]            # this is best practice
            [ d.e.f ]          # same as [d.e.f]
            [ g .  h  . i ]    # same as [g.h.i]
            [ j . "ʞ" . 'l' ]  # same as [j."ʞ".'l']|]
          `shouldBe`
          Right (table [
            "a" .= table ["b" .= table ["c" .= table []]],
            "d" .= table ["e" .= table ["f" .= table []]],
            "g" .= table ["h" .= table ["i" .= table []]],
            "j" .= table ["ʞ" .= table ["l" .= table []]]])

        it "allows supertables to be defined after subtables" $
          parse [quoteStr|
            # [x] you
            # [x.y] don't
            # [x.y.z] need these
            [x.y.z.w] # for this to work

            [x] # defining a super-table afterward is ok
            q=1|]
          `shouldBe`
          Right (table [
            "x" .= table [
                "q" .= Integer 1,
                "y" .= table [
                    "z" .= table [
                        "w" .= table []]]]])

        it "prevents using a [table] to open a table defined with dotted keys" $
          parse [quoteStr|
            [fruit]
            apple.color = 'red'
            apple.taste.sweet = true
            [fruit.apple]|]
          `shouldBe` Left "4:8: key error: apple is a closed table"

        it "can add subtables" $
          parse [quoteStr|
            [fruit]
            apple.color = "red"
            apple.taste.sweet = true
            [fruit.apple.texture]  # you can add sub-tables
            smooth = true|]
          `shouldBe`
          Right (table [
            "fruit" .= table [
                "apple" .= table [
                    "color" .= "red",
                    "taste" .= table [
                        "sweet" .= True],
                        "texture" .= table [
                            "smooth" .= True]]]])

    describe "inline table"
     do it "parses inline tables" $
          parse [quoteStr|
            name = { first = "Tom", last = "Preston-Werner" }
            point = { x = 1, y = 2 }
            animal = { type.name = "pug" }|]
          `shouldBe`
          Right (table [
            "animal" .= table ["type" .= table ["name" .= "pug"]],
            "name"   .= table ["first" .= "Tom", "last" .= "Preston-Werner"],
            "point"  .= table ["x" .= Integer 1, "y" .= Integer 2]])

        it "prevents altering inline tables with dotted keys" $
          parse [quoteStr|
            [product]
            type = { name = "Nail" }
            type.edible = false  # INVALID|]
          `shouldBe` Left "3:1: key error: type is already assigned"

        it "prevents using inline tables to add keys to existing tables" $
          parse [quoteStr|
            [product]
            type.name = "Nail"
            type = { edible = false }  # INVALID|]
          `shouldBe` Left "3:1: key error: type is already assigned"

        it "checks that inline keys aren't reassigned" $
          parse [quoteStr|
            x = {a = 1, a = 2}|]
          `shouldBe` Left "1:13: key error: a is already assigned"

        it "checks that inline keys don't overlap with implicit inline tables" $
          parse [quoteStr|
            x = {a.b = 1, a = 2}|]
          `shouldBe` Left "1:15: key error: a is already assigned"
        
        it "checks for overwrites from other inline tables" $
          parse [quoteStr|
            tab = { inner = { dog = "best" }, inner.cat = "worst" }|]
          `shouldBe` Left "1:35: key error: inner is already assigned"
        
        it "checks for overlaps of other inline tables" $
          parse [quoteStr|
            tbl = { fruit = { apple.color = "red" }, fruit.apple.texture = { smooth = true } }|]
          `shouldBe` Left "1:42: key error: fruit is already assigned"

    describe "array of tables"
     do it "supports array of tables syntax" $
          decode [quoteStr|
            [[products]]
            name = "Hammer"
            sku = 738594937

            [[products]]  # empty table within the array

            [[products]]
            name = "Nail"
            sku = 284758393

            color = "gray"|]
          `shouldBe`
          Success mempty (Map.singleton "products" [
            table [
              "name" .= "Hammer",
              "sku"  .= Integer 738594937],
            Map.empty,
            table [
                "color" .= "gray",
                "name"  .= "Nail",
                "sku"   .= Integer 284758393]])

        it "handles subtables under array of tables" $
          parse [quoteStr|
            [[fruits]]
            name = "apple"

            [fruits.physical]  # subtable
            color = "red"
            shape = "round"

            [[fruits.varieties]]  # nested array of tables
            name = "red delicious"

            [[fruits.varieties]]
            name = "granny smith"


            [[fruits]]
            name = "banana"

            [[fruits.varieties]]
            name = "plantain"|]
          `shouldBe`
          Right (table [
            "fruits" .= [
                table [
                    "name" .= "apple",
                    "physical" .= table [
                        "color" .= "red",
                        "shape" .= "round"],
                    "varieties" .= [
                        table ["name" .= "red delicious"],
                        table ["name" .= "granny smith"]]],
                table [
                    "name" .= "banana",
                    "varieties" .= [
                        table ["name" .= "plantain"]]]]])

        it "prevents redefining a supertable with an array of tables" $
          parse [quoteStr|
            # INVALID TOML DOC
            [fruit.physical]  # subtable, but to which parent element should it belong?
            color = "red"
            shape = "round"

            [[fruit]]  # parser must throw an error upon discovering that "fruit" is
                    # an array rather than a table
            name = "apple"|]
            `shouldBe` Left "6:3: key error: fruit is already implicitly defined to be a table"

        it "prevents redefining an inline array" $
          parse [quoteStr|
            # INVALID TOML DOC
            fruits = []

            [[fruits]] # Not allowed|]
          `shouldBe` Left "4:3: key error: fruits is already assigned"

    -- these cases are needed to complete coverage checking on Semantics module
    describe "corner cases"
     do it "stays open" $
          parse [quoteStr|
            [x.y.z]
            [x]
            [x.y]|]
          `shouldBe`
          parse "x.y.z={}"

        it "stays closed" $
          parse [quoteStr|
            [x.y]
            [x]
            [x.y]|] `shouldBe` Left "3:4: key error: y is a closed table"

        it "super tables of array tables preserve array tables" $
          parse [quoteStr|
            [[x.y]]
            [x]
            [[x.y]]|]
          `shouldBe`
          parse "x.y=[{},{}]"

        it "super tables of array tables preserve array tables" $
          parse [quoteStr|
            [[x.y]]
            [x]
            [x.y.z]|]
          `shouldBe`
          parse "x.y=[{z={}}]"

        it "detects conflicting inline keys" $
          parse [quoteStr|
            x = { y = 1, y.z = 2}|]
          `shouldBe` Left "1:14: key error: y is already assigned"

        it "handles merging dotted inline table keys" $
          parse [quoteStr|
            t = { a.x.y = 1, a.x.z = 2, a.q = 3}|]
          `shouldBe`
          Right (table [
            "t" .= table [
                "a" .= table [
                    "q" .= Integer 3,
                    "x" .= table [
                        ("y",Integer 1),
                        ("z",Integer 2)]]]])

        it "disallows overwriting assignments with tables" $
          parse [quoteStr|
            x = 1
            [x.y]|]
          `shouldBe` Left "2:2: key error: x is already assigned"

        it "handles super super tables" $
          parse [quoteStr|
            [x.y.z]
            [x.y]
            [x]|]
          `shouldBe`
          parse "x.y.z={}"

        it "You can dot into open supertables" $
          parse [quoteStr|
            [x.y.z]
            [x]
            y.q = 1|]
          `shouldBe`
          parse "x.y={z={},q=1}"

        it "dotted tables close previously open tables" $
          parse [quoteStr|
            [x.y.z]
            [x]
            y.q = 1
            [x.y]|]
          `shouldBe` Left "4:4: key error: y is a closed table"

        it "dotted tables can't assign through closed tables!" $
          parse [quoteStr|
            [x.y]
            [x]
            y.z.w = 1|]
          `shouldBe` Left "3:1: key error: y is a closed table"

        it "super tables can't add new subtables to array tables via dotted keys" $
          parse [quoteStr|
            [[x.y]]
            [x]
            y.z.a = 1
            y.z.b = 2|]
          `shouldBe` Left "3:1: key error: y is a closed table"

        it "the previous example preserves closeness" $
          parse [quoteStr|
            [[x.y]]
            [x]
            y.z.a = 1
            y.w = 2|]
          `shouldBe` Left "3:1: key error: y is a closed table"

        it "defining a supertable closes the supertable" $
          parse [quoteStr|
            [x.y]
            [x]
            [x]|]
          `shouldBe` Left "3:2: key error: x is a closed table"

        it "prevents redefining an array of tables" $
          parse [quoteStr|
            [[x.y]]
            [x.y]|]
          `shouldBe` Left "2:4: key error: y is a closed table"

        it "quotes table names in semantic errors" $
          parse [quoteStr|
            [[x.""]]
            [x.""]|]
          `shouldBe` Left "2:4: key error: \"\" is a closed table"