This repository was archived by the owner on Oct 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathDesugarTest.elm
346 lines (297 loc) · 11.6 KB
/
DesugarTest.elm
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
module DesugarTest exposing (desugarTest)
import Dict
import Elm.AST.Canonical as Canonical
import Elm.AST.Canonical.Unwrapped as CanonicalU
import Elm.AST.Frontend as Frontend
import Elm.Compiler.Error as Error exposing (DesugarError)
import Elm.Data.Declaration as Declaration exposing (Declaration)
import Elm.Data.Exposing as Exposing
import Elm.Data.Import exposing (Import)
import Elm.Data.Located as Located
import Elm.Data.Module as Module exposing (Module)
import Elm.Data.ModuleName exposing (ModuleName)
import Elm.Data.Qualifiedness exposing (PossiblyQualified(..))
import Elm.Data.TypeAnnotation exposing (TypeAnnotation)
import Elm.Data.VarName exposing (VarName)
import Expect
import Stage.Desugar as Desugar
import Test exposing (Test, describe, test)
import TestHelpers exposing (located)
desugarTest : Test
desugarTest =
describe "Stage.Desugar"
[ test "desugar \\a b -> a + b into \\a -> \\b -> a + b " <|
\_ ->
frontendLambda "a" "b"
|> Desugar.desugarExpr Dict.empty (moduleFromName "A")
|> mapUnwrap
|> Expect.equal (Ok <| canonicalLambda "a" "b")
, toTest
{ description = "desugar variable name in module"
, thisModuleName = "A"
, thisModuleVars = [ "a" ]
, thisModuleImports = []
, availableModules = []
, inputVar = ( Nothing, "a" )
, expectedResult = Ok ( "A", "a" )
}
, toTest
{ description = "desugar variable name NOT in this module"
, thisModuleName = "A"
, thisModuleVars = [ "a" ]
, thisModuleImports = []
, availableModules = []
, inputVar = ( Nothing, "b" )
, expectedResult =
Err
(Error.VarNameNotFound
{ insideModule = "A"
, var =
{ qualifiedness = PossiblyQualified Nothing
, name = "b"
}
}
)
}
, toTest
{ description = "desugar prefixed variable name: import B ; B.a"
, thisModuleName = "A"
, thisModuleVars = [ "a" ]
, thisModuleImports = [ importFromName "B" ]
, availableModules = [ { name = "B", exposedVars = [ "a" ] } ]
, inputVar = ( Just "B", "a" )
, expectedResult = Ok ( "B", "a" )
}
, toTest
{ description = "desugar prefixed variable name with aliased module: import B as C; C.a"
, thisModuleName = "A"
, thisModuleVars = [ "a" ]
, thisModuleImports = [ importFromName "B" |> as_ "C" ]
, availableModules = [ { name = "B", exposedVars = [ "a" ] } ]
, inputVar = ( Just "C", "a" )
, expectedResult = Ok ( "B", "a" )
}
, toTest
{ description = "desugar exposed variable name: import B exposing (b); b"
, thisModuleName = "A"
, thisModuleVars = [ "a" ]
, thisModuleImports = [ importFromName "B" |> exposingValuesInImport [ "b" ] ]
, availableModules = [ { name = "B", exposedVars = [ "b" ] } ]
, inputVar = ( Nothing, "b" )
, expectedResult = Ok ( "B", "b" )
}
, toTest
{ description = "desugar variable name when exposed from import and defined in module: import B exposing (a); a = 42; a"
, thisModuleName = "A"
, thisModuleVars = [ "a" ]
, thisModuleImports = [ importFromName "B" |> exposingValuesInImport [ "a" ] ]
, availableModules = [ { name = "B", exposedVars = [ "a" ] } ]
, inputVar = ( Nothing, "a" )
, expectedResult = Ok ( "A", "a" )
}
, toTest
{ description = "desugar ambiguous variable names: import A as exposing (a); import B exposing (a) ; a"
, thisModuleName = "A"
, thisModuleVars = []
, thisModuleImports =
[ importFromName "B" |> exposingValuesInImport [ "a" ]
, importFromName "C" |> exposingValuesInImport [ "a" ]
]
, availableModules =
[ { name = "B", exposedVars = [ "a" ] }
, { name = "C", exposedVars = [ "a" ] }
]
, inputVar = ( Nothing, "a" )
, expectedResult =
Err <|
Error.AmbiguousName
{ name = "a"
, insideModule = "A"
, possibleModules = [ "B", "C" ]
}
}
, toTest
{ description = "referencing unknown variable"
, thisModuleName = "A"
, thisModuleVars = []
, thisModuleImports = []
, availableModules = []
, inputVar = ( Nothing, "a" )
, expectedResult =
Err <|
Error.VarNameNotFound
{ var =
{ qualifiedness = PossiblyQualified Nothing
, name = "a"
}
, insideModule = "A"
}
}
, test "desugar duplicate record field" <|
\_ ->
let
aRegion =
{ start = { row = 1, col = 1 }, end = { row = 2, col = 2 } }
bRegion =
{ start = { row = 3, col = 3 }, end = { row = 4, col = 4 } }
in
[ { name = "aaa", body = Located.located aRegion Frontend.Unit }
, { name = "aaa", body = Located.located bRegion Frontend.Unit }
]
|> Frontend.Record
|> located
|> Desugar.desugarExpr Dict.empty (moduleFromName "A")
|> mapUnwrap
|> Expect.equal
({ name = "aaa"
, insideModule = "A"
, firstOccurrence = Located.located aRegion ()
, secondOccurrence = Located.located bRegion ()
}
|> Error.DuplicateRecordField
|> Err
)
]
{-| `frontendLambda "a" "b"` builds `\a b -> a + b`.
-}
frontendLambda : String -> String -> Frontend.LocatedExpr
frontendLambda arg1 arg2 =
located <|
Frontend.Lambda
{ arguments = [ arg1, arg2 ]
, body =
located <|
Frontend.Plus
(located <| Frontend.Argument arg1)
(located <| Frontend.Argument arg2)
}
{-| `canonicalLambda "a" "b"` builds `\a -> \b -> a + b`.
-}
canonicalLambda : String -> String -> CanonicalU.Expr
canonicalLambda arg1 arg2 =
CanonicalU.Lambda
{ argument = arg1
, body =
CanonicalU.Lambda
{ argument = arg2
, body =
CanonicalU.Plus
(CanonicalU.Argument arg1)
(CanonicalU.Argument arg2)
}
}
type alias NameResolutionTestCase =
{ description : String
, thisModuleName : ModuleName
, thisModuleVars : List VarName
, thisModuleImports : List ( ModuleName, Import )
, availableModules : List { name : ModuleName, exposedVars : List VarName }
, inputVar : ( Maybe ModuleName, VarName )
, expectedResult : Result DesugarError ( ModuleName, VarName )
}
toTest : NameResolutionTestCase -> Test
toTest r =
test r.description <|
\_ ->
Desugar.desugarExpr
(r.availableModules
|> List.map
(\m ->
( m.name
, moduleFromName m.name
|> addDeclarations m.exposedVars
|> exposingValuesInModule m.exposedVars
)
)
|> Dict.fromList
)
(moduleFromName r.thisModuleName
|> addDeclarations r.thisModuleVars
|> addImports r.thisModuleImports
)
(var r.inputVar)
|> mapUnwrap
|> Expect.equal (r.expectedResult |> Result.map buildExpectedResult)
var : ( Maybe ModuleName, VarName ) -> Frontend.LocatedExpr
var ( maybeModuleName, varName ) =
located <|
Frontend.Var
{ qualifiedness = PossiblyQualified maybeModuleName
, name = varName
}
buildExpectedResult : ( ModuleName, VarName ) -> CanonicalU.Expr
buildExpectedResult ( moduleName, varName ) =
CanonicalU.Var
{ module_ = moduleName
, name = varName
}
{- |
module ModuleWithVarA exposing (a)
a = 42
-}
importFromName : ModuleName -> ( ModuleName, Import )
importFromName moduleName =
( moduleName
, { moduleName = moduleName
, as_ = Nothing
, exposing_ = Nothing
}
)
exposingValuesInImport : List VarName -> ( ModuleName, Import ) -> ( ModuleName, Import )
exposingValuesInImport vars ( moduleName, import_ ) =
( moduleName
, { import_ | exposing_ = Just <| Exposing.ExposingSome <| List.map Exposing.ExposedValue vars }
)
as_ : ModuleName -> ( ModuleName, Import ) -> ( ModuleName, Import )
as_ alias_ ( moduleName, import_ ) =
( moduleName, { import_ | as_ = Just alias_ } )
moduleFromName : ModuleName -> Module expr ann qual
moduleFromName name =
{ imports = Dict.empty
, name = name
, filePath = "/"
, declarations = Dict.empty
, type_ = Module.PlainModule
, exposing_ = Exposing.ExposingSome []
}
{- | add the following declaration to a module:
a = 42
-}
addDeclaration :
String
-> Module Frontend.LocatedExpr TypeAnnotation PossiblyQualified
-> Module Frontend.LocatedExpr TypeAnnotation PossiblyQualified
addDeclaration varName module_ =
let
decl : Declaration Frontend.LocatedExpr TypeAnnotation PossiblyQualified
decl =
{ module_ = module_.name
, name = varName
, body =
Declaration.Value
{ typeAnnotation = Nothing
, expression = located <| Frontend.Int 42
}
}
in
{ module_ | declarations = Dict.insert varName decl module_.declarations }
addDeclarations :
List String
-> Module Frontend.LocatedExpr TypeAnnotation PossiblyQualified
-> Module Frontend.LocatedExpr TypeAnnotation PossiblyQualified
addDeclarations varNames module_ =
List.foldr addDeclaration module_ varNames
{- | add a list of exposed values to a module -}
exposingValuesInModule : List VarName -> Module expr ann qual -> Module expr ann qual
exposingValuesInModule varNames exposable =
{ exposable | exposing_ = Exposing.ExposingSome (List.map Exposing.ExposedValue varNames) }
{- | add an import to a module -}
addImport : ( ModuleName, Import ) -> Module expr ann qual -> Module expr ann qual
addImport ( moduleName, import_ ) module_ =
{ module_ | imports = Dict.insert moduleName import_ module_.imports }
addImports : List ( ModuleName, Import ) -> Module expr ann qual -> Module expr ann qual
addImports imports module_ =
List.foldr addImport module_ imports
mapUnwrap : Result x Canonical.LocatedExpr -> Result x CanonicalU.Expr
mapUnwrap =
Result.map Canonical.unwrap