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 pathOptimizeTest.elm
129 lines (126 loc) · 4.67 KB
/
OptimizeTest.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
module OptimizeTest exposing (optimize)
import Elm.AST.Typed as Typed exposing (Expr_(..))
import Elm.Data.Type as Type exposing (TypeOrId(..))
import Expect
import Stage.Optimize
import Test exposing (Test, describe, test)
import TestHelpers
exposing
( located
, typedBool
, typedInt
, typedIntList
)
optimize : Test
optimize =
describe "Stage.Optimize"
[ let
runTest : ( String, Typed.LocatedExpr, Typed.LocatedExpr ) -> Test
runTest ( description, input, output ) =
test description <|
\() ->
input
|> Stage.Optimize.optimizeExpr
|> Expect.equal output
in
describe "optimizeExpr"
[ describe "optimizePlus"
(List.map runTest
[ ( "works with two literal ints"
, located
( Plus
(typedInt 2)
(typedInt 5)
, Type Type.Int
)
, typedInt 7
)
, ( "doesn't work if left is not int"
, located
( Plus
(located ( Argument "x", Type Type.Int ))
(typedInt 5)
, Type Type.Int
)
, located
( Plus
(located ( Argument "x", Type Type.Int ))
(typedInt 5)
, Type Type.Int
)
)
, ( "doesn't work if right is not int"
, located
( Plus
(typedInt 5)
(located ( Argument "x", Type Type.Int ))
, Type Type.Int
)
, located
( Plus
(typedInt 5)
(located ( Argument "x", Type Type.Int ))
, Type Type.Int
)
)
]
)
, describe "optimizeCons"
(List.map runTest
[ ( "works with one value"
, located
( Cons
(typedInt 1)
(typedIntList [ 2, 3 ])
, Type Type.Int
)
, typedIntList [ 1, 2, 3 ]
)
]
)
, describe "optimizeIfLiteralBool"
(List.map runTest
[ ( "folds to then if true"
, located
( If
{ test = typedBool True
, then_ = typedInt 42
, else_ = typedInt 0
}
, Type Type.Int
)
, typedInt 42
)
, ( "folds to else if false"
, located
( If
{ test = typedBool False
, then_ = typedInt 0
, else_ = typedInt 42
}
, Type Type.Int
)
, typedInt 42
)
, ( "doesn't work if the bool is not literal"
, located
( If
{ test = located ( Argument "x", Type Type.Bool )
, then_ = typedInt 0
, else_ = typedInt 42
}
, Type Type.Int
)
, located
( If
{ test = located ( Argument "x", Type Type.Bool )
, then_ = typedInt 0
, else_ = typedInt 42
}
, Type Type.Int
)
)
]
)
]
]