summaryrefslogtreecommitdiff
path: root/test/Spec.hs
blob: ef41bdd5bfc15d03ab993c113796214073b79127 (plain)
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
{-# LANGUAGE OverloadedStrings #-}

import           Control.Monad.Writer
import           Elm.Classes
import           Elm.Declaration
import           Elm.Expression
import           Elm.GenError
import           Elm.Import
import           Elm.Program
import           Elm.Type
import           Renderer
import           Test.Hspec
import           Text.PrettyPrint     hiding (Str, render)

main = hspec $ do
    describe "Setup" $ do
        it "Should work" $ do
            5 `shouldBe` 5
    describe "Expression" $ do
        describe "Bool" $ do
            it "Should render true properly" $ do
                let expr = Bool True
                renderExpr expr `shouldBe` "True"
            it "Should render false properly" $ do
                let expr = Bool False
                renderExpr expr `shouldBe` "False"
        describe "Int" $ do
            it "Should work" $ do
                renderExpr (Int 5) `shouldBe` "5"
        describe "Float" $ do
            it "Should work" $ do
                renderExpr (Float 1.2) `shouldBe` "1.2"
        describe "String" $ do
            it "Should work" $ do
                renderExpr (Str "hello") `shouldBe` "\"hello\""
        describe "Tuple" $ do
            it "Should work for 0 tuples" $ do
                renderExpr (Tuple []) `shouldBe` "()"
            it "Should work for larger tuples" $ do
                renderExpr (Tuple [Int 5, Int 6]) `shouldBe` "( 5, 6 )"
                renderExpr (Tuple [Int 2, Float 2.5]) `shouldBe` "( 2, 2.5 )"
        describe "Var" $ do
            it "Should work" $ do
                renderExpr (Var "a") `shouldBe` "a"
                renderExpr (Var "abc") `shouldBe` "abc"
            it "Should work with overloadedstrings" $ do
                renderExpr "a" `shouldBe` "a"
                renderExpr "abc" `shouldBe` "abc"
            it "Should error on an empty string" $ do
                (runWriter . generate $ Var "") `shouldBe` (text "", Error "An empty string is not a valid variable name")
            it "Should wrap operators in parens" $ do
                pending
            it "Should only allow operators with valid characters" $ do
                pending
            it "Should only allow variables with valid characters" $ do
                pending
        describe "Under" $ do
            it "Does this I guess" $ do
                renderExpr Under `shouldBe` "_"
        describe "List" $ do
            it "Should work for empty lists" $ do
                renderExpr (List []) `shouldBe` "[]"
            it "Should work for single item lists" $ do
                renderExpr (List ["a"]) `shouldBe` "[a]"
            it "Should work for multi item lists" $ do
                renderExpr (List ["a", "b", "c"]) `shouldBe` "[a, b, c]"
            it "Should work with overloaded lists" $ do
                renderExpr (List ["a", "b", "c"]) `shouldBe` "[a, b, c]"
        describe "Function Application" $ do
            it "Should work for 0 params" $ do
                renderExpr (App ["a"]) `shouldBe` "a"
            it "Should work for multiple params" $ do
                renderExpr (App ["a", "b", "c"]) `shouldBe` "a b c"
                renderExpr (App ["a", Int 5, "b", Int 7]) `shouldBe` "a 5 b 7"
            it "Should nest properly" $ do
                renderExpr (App ["a", App ["b", "c"]]) `shouldBe` "a (b c)"
        describe "Inline operators" $ do
            it "Should work" $ do
                renderExpr (Op "+" "a" "b") `shouldBe` "a + b"
            it "Should error on invalid operator characters" $ do
                pending
        describe "Record" $ do
            it "Should work when empty" $ do
                renderExpr (Record Nothing []) `shouldBe` "{}"
            it "Should work with key value pairs" $ do
                renderExpr (Record Nothing [("a", Int 5), ("b", Int 6)])
                    `shouldBe` "{ a = 5, b = 6 }"
            it "Should allow updates" $ do
                renderExpr (Record (Just "a") [("b", Int 5)])
                    `shouldBe` "{ a | b = 5 }"
            it "Should warn here" $ do
                (runWriter . generate $ Record (Just "data") [])
                    `shouldBe` ("data", WarningList ["Trying to update record data with no changed fields"])
            it "Should error here" $ do
                (runWriter . generate $ Record (Just $ App ["f", Int 5]) [])
                    `shouldBe` ("", Error "You are unable to update a record with a non constant")
        describe "Let" $ do
            it "Should work with one dec" $ do
                text <- liftIO . readFile $ "test/spec/let1.txt"
                let
                    ast = Let "a" [("a", Int 5)]
                renderExpr ast ++ "\n" `shouldBe` text
            it "Should work with more than one dec" $ do
                text <- liftIO . readFile $ "test/spec/let2.txt"
                let
                    ast = Let (Op "+" "a" "b") [("a", Int 5), ("b", Int 6)]
                renderExpr ast ++ "\n" `shouldBe` text
            it "Should error with zero declerations" $ do
                (runWriter . generate $ Let "a" [])
                    `shouldBe` ("", Error "Unable to create let expression with 0 bindings")
        describe "Case" $ do
            it "Should work" $ do
                text <- liftIO . readFile $ "test/spec/case1.txt"
                let
                    ast =
                        Case "m"
                            [(App ["Just", "x"], "x")
                            ,("Nothing", Int 0)
                            ]
                renderExpr ast ++ "\n" `shouldBe` text
            it "Should generate an error with no cases" $ do
                let
                    ast = Case "m" []
                (runWriter . generate $ ast)
                    `shouldBe` ("", Error "Unable to create case expression with 0 cases")

    describe "Type" $ do
        describe "Params" $ do
            it "Should work for without params" $ do
                render (Params "a" [])
                    `shouldBe` "a"
            it "Should work with params" $ do
                render (Params "a" [Params "b" []])
                    `shouldBe` "a b"
            it "Should put params in parens if needed" $ do
                render (Params "a" [Params "b" [Params "c" []]])
                    `shouldBe` "a (b c)"

        describe "TApp" $ do
            it "Should work" $ do
                render (TApp [Params "a" [], Params "b" []])
                    `shouldBe` "a -> b"

            it "Should put nested tapps in parens" $ do
                render (TApp [Params "a" [], TApp [Params "a" [], Params "b" []]])
                    `shouldBe` "a -> (a -> b)"

        describe "TTuple" $ do
            it "Should work for unit" $ do
                render (TTuple []) `shouldBe` "()"

            it "Should return the value and warn with a one item tuple" $ do
                let
                    ast =
                        TTuple [Params "a" []]
                (runWriter . generate $ ast)
                    `shouldBe` ("(a)", WarningList ["Attempt to create a one item tuple"])
            it "Should work for multiple item tuples" $ do
                render (TTuple [Params "a" [], Params "b" []])
                    `shouldBe` "(a, b)"

        describe "TRecord" $ do
            it "Should error when passed nothing" $ do
                let
                    ast =
                        TRecord Nothing []
                (runWriter . generate $ ast)
                    `shouldBe` ("", Error "Unable to create a record type with no base and no constraints")

            it "Should warn when there are no constraints" $ do
                let
                    ast =
                        TRecord (Just "a") []
                (runWriter . generate $ ast)
                    `shouldBe` ("a", WarningList ["You are creating a record type from a with no constraints"])
            it "Should work with no base" $ do
                let
                    ast =
                        TRecord Nothing
                            [ ("a", Params "Int" [])
                            , ("b", Params "Int" [])
                            ]
                render ast `shouldBe` "{ a : Int, b : Int }"

            it "Should work with a base" $ do
                let
                    ast =
                        TRecord (Just "a")
                            [ ("b", Params "Int" [])
                            , ("c", Params "Int" [])
                            ]
                render ast `shouldBe` "{ a | b : Int, c : Int }"
    describe "Import" $ do
        describe "ImportItem" $ do
            it "Should work for single items" $ do
                render (Item "Maybe")
                    `shouldBe` "Maybe"
            it "Should work for sub importing" $ do
                render (ItemExposing "Maybe" ["Just", "Nothing"])
                    `shouldBe` "Maybe(Just, Nothing)"
            it "Should working for sub importing everything" $ do
                render (ItemEvery "Maybe")
                    `shouldBe` "Maybe(..)"
        describe "ImportType" $ do
            it "Should properly select everything" $ do
                render (Everything) `shouldBe` "(..)"
            it "Should properly select only types" $ do
                render (Select []) `shouldBe` "()"
            it "Should properly select items" $ do
                render (Select [Item "Just", Item "withDefault"])
                    `shouldBe` "(Just, withDefault)"
                render (Select [ItemEvery "Just", Item "withDefault"])
                    `shouldBe` "(Just(..), withDefault)"
        describe "Import" $ do
            it "Should import stuff" $ do
                render (Import "List" Nothing Nothing)
                    `shouldBe` "import List"
            it "Should render with an alias" $ do
                render (Import "Maybe" (Just "M") Nothing)
                    `shouldBe` "import Maybe as M"
            it "Should expose from the imports" $ do
                render (Import "Maybe" Nothing $ Just $ Select [Item "withDefault"])
                    `shouldBe` "import Maybe exposing (withDefault)"
        describe "Dec" $ do
            it "Should work for simple delcerations" $ do
                let
                    ast =
                        Dec "add5"
                            (TApp ["Int", "Int"])
                            [Var "x"]
                            (Op "+" (Var "x") (Int 5))
                file <- readFile "test/spec/dec1.txt"
                render ast ++ "\n" `shouldBe` file
            it "Should work for more complex declerations" $ do
                let
                    ast =
                        Dec "withDefault"
                            (TApp ["a", Params "Maybe" ["a"], "a"])
                            ["default", "m"]
                            (Case "m"
                                [ (App ["Just", "x"], "x")
                                , ("Nothing", "default")
                                ])
                file <- readFile "test/spec/dec2.txt"
                render ast ++ "\n" `shouldBe` file
            it "Should properly handle type declerations" $ do
                -- \_(*_*)_/
                let
                    ast =
                        DecType "Maybe"
                        ["a"]
                        [ ("Nothing", [])
                        , ("Just", ["a"])
                        ]
                file <- readFile "test/spec/dec3.txt"
                render ast ++ "\n" `shouldBe` file
            it "Should properly handle type aliases" $ do
                let
                    ast =
                        DecTypeAlias "Model" ["a"] (Params "Maybe" ["a"])
                file <- readFile "test/spec/dec4.txt"
                render ast ++ "\n" `shouldBe` file
    describe "Program" $ do
        it "Should work" $ do
            let
                ast =
                    Program "Maybe"
                        (Select
                            [ ItemExposing "Maybe" ["Just", "Nothing"]
                            , Item "andThen"
                            , Item "map"
                            , Item "map2"
                            , Item "map3"
                            , Item "map4"
                            , Item "map5"
                            , Item "withDefault"
                            ])
                        []
                        [ DecType "Maybe" ["a"]
                            [ ("Nothing", [])
                            , ("Just", ["a"])
                            ]
                        , Dec "withDefault"
                            (TApp ["a", Params "Maybe" ["a"], "a"])
                            ["default", "maybe"]
                            (Case "maybe"
                                [ (App ["Just", "value"], "value")
                                , ("Nothing", "default")
                                ])
                        , Dec "map"
                            (TApp [TApp ["a", "b"], Params "Maybe" ["a"], Params "Maybe" ["b"]])
                            ["f", "maybe"]
                            (Case "maybe"
                                [ (App ["Just", "value"], App ["Just", App ["f", "value"]])
                                , ("Nothing", "Nothing")
                                ])
                        , Dec "map2"
                            (TApp
                                [ TApp ["a", "b", "value"]
                                , Params "Maybe" ["a"]
                                , Params "Maybe" ["b"]
                                , Params "Maybe" ["value"]
                                ])
                            ["func", "ma", "mb"]
                            (Case (Tuple ["ma", "mb"])
                                [ (Tuple [App ["Just", "a"], App ["Just", "b"]],
                                    App ["Just", App ["func", "a", "b"]])
                                , (Under, "Nothing")
                                ])
                        , Dec "map3"
                            (TApp
                                [ TApp ["a", "b", "c", "value"]
                                , Params "Maybe" ["a"]
                                , Params "Maybe" ["b"]
                                , Params "Maybe" ["c"]
                                , Params "Maybe" ["value"]
                                ])
                            ["func", "ma", "mb", "mc"]
                            (Case (Tuple ["ma", "mb", "mc"])
                                [ (Tuple
                                    [ App ["Just", "a"]
                                    , App ["Just", "b"]
                                    , App ["Just", "c"]
                                    ],
                                    App ["Just", App ["func", "a", "b", "c"]])
                                , (Under, "Nothing")
                                ])
                        ]
            file <- readFile "test/program1.elm"
            let (str, err) = renderProgram ast
            err `shouldBe` WarningList []
            str `shouldBe` file