File: PrettySpec.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 (141 lines) | stat: -rw-r--r-- 4,360 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
module PrettySpec (spec) where

import Test.Hspec (it, shouldBe, Spec)
import QuoteStr (quoteStr)
import Toml (encode, parse, prettyToml, Table)
import Data.Map qualified as Map

tomlString :: Table -> String
tomlString = show . prettyToml

spec :: Spec
spec =
 do it "renders example 1" $
        show (encode (Map.singleton "x" (1 :: Integer)))
        `shouldBe` [quoteStr|
            x = 1|]

    it "renders example 2" $
        fmap tomlString (parse "x=1\ny=2")
        `shouldBe` Right [quoteStr|
            x = 1
            y = 2|]

    it "renders example lists" $
        fmap tomlString (parse "x=[1,'two', [true]]")
        `shouldBe` Right [quoteStr|
        x = [1, "two", [true]]|]

    it "renders empty tables" $
        fmap tomlString (parse "x.y.z={}\nz.y.w=false")
        `shouldBe` Right [quoteStr|
            [x.y.z]

            [z]
            y.w = false|]

    it "renders empty tables in array of tables" $
        fmap tomlString (parse "ex=[{},{},{a=9}]")
        `shouldBe` Right [quoteStr|
            [[ex]]

            [[ex]]

            [[ex]]
            a = 9|]

    it "renders multiple tables" $
        fmap tomlString (parse "a.x=1\nb.x=3\na.y=2\nb.y=4")
        `shouldBe` Right [quoteStr|
            [a]
            x = 1
            y = 2

            [b]
            x = 3
            y = 4|]

    it "renders escapes in strings" $
        fmap tomlString (parse "a=\"\\\\\\b\\t\\r\\n\\f\\\"\\u007f\\U0001000c\"")
        `shouldBe` Right [quoteStr|
            a = "\\\b\t\r\n\f\"\u007F\U0001000C"|]

    it "renders multiline strings" $
        fmap tomlString (parse [quoteStr|
            Everything-I-Touch = "Everything I touch\nwith tenderness, alas,\npricks like a bramble."
            Two-More = [
                "The west wind whispered,\nAnd touched the eyelids of spring:\nHer eyes, Primroses.",
                "Plum flower temple:\nVoices rise\nFrom the foothills",
            ]|])
        `shouldBe` Right [quoteStr|
            Everything-I-Touch = """
            Everything I touch
            with tenderness, alas,
            pricks like a bramble."""
            Two-More = [ """
            The west wind whispered,
            And touched the eyelids of spring:
            Her eyes, Primroses."""
                       , "Plum flower temple:\nVoices rise\nFrom the foothills" ]|]

    it "renders floats" $
        fmap tomlString (parse "a=0.0\nb=-0.1\nc=0.1\nd=3.141592653589793\ne=4e123")
        `shouldBe` Right [quoteStr|
            a = 0.0
            b = -0.1
            c = 0.1
            d = 3.141592653589793
            e = 4.0e123|]

    it "renders special floats" $
        fmap tomlString (parse "a=inf\nb=-inf\nc=nan")
        `shouldBe` Right [quoteStr|
            a = inf
            b = -inf
            c = nan|]

    it "renders empty documents" $
        fmap tomlString (parse "")
        `shouldBe` Right ""

    it "renders dates and time" $
        fmap tomlString (parse [quoteStr|
            a = 2020-05-07
            b = 15:16:17.990
            c = 2020-05-07T15:16:17.990
            d = 2020-05-07T15:16:17.990Z
            e = 2020-05-07T15:16:17-07:00
            f = 2021-09-06T14:15:19+08:00
            g = 0008-10-11T12:13:14+15:00|])
        `shouldBe` Right [quoteStr|
            a = 2020-05-07
            b = 15:16:17.99
            c = 2020-05-07T15:16:17.99
            d = 2020-05-07T15:16:17.99Z
            e = 2020-05-07T15:16:17-07:00
            f = 2021-09-06T14:15:19+08:00
            g = 0008-10-11T12:13:14+15:00|]

    it "renders quoted keys" $
        fmap tomlString (parse "''.'a b'.'\"' = 10")
        `shouldBe` Right [quoteStr|
        ""."a b"."\"" = 10|]

    it "renders inline tables" $
        fmap tomlString (parse [quoteStr|
            x = [[{a = 'this is a longer example', b = 'and it will linewrap'},{c = 'all on its own'}]]|])
        `shouldBe` Right [quoteStr|
            x = [ [ {a = "this is a longer example", b = "and it will linewrap"}
                  , {c = "all on its own"} ] ]|]

    it "factors out unique table prefixes in leaf tables" $
        fmap tomlString (parse [quoteStr|
            [x]
            i = 1
            p.q = "a"
            y.z = "c"|])
        `shouldBe` Right [quoteStr|
            [x]
            i = 1
            p.q = "a"
            y.z = "c"|]