@@ -7,8 +7,8 @@ module Main where
7
7
import Dhall.JSON (Conversion (.. ))
8
8
import Test.Tasty (TestTree )
9
9
10
- import qualified Control.Exception
11
- import qualified Data.Aeson
10
+ import qualified Control.Exception as Exception
11
+ import qualified Data.Aeson as Aeson
12
12
import qualified Data.ByteString.Lazy
13
13
import qualified Data.Text
14
14
import qualified Data.Text.IO
@@ -32,6 +32,7 @@ testTree =
32
32
, yaml
33
33
, emptyAlternative
34
34
, nesting
35
+ , unionKeys
35
36
]
36
37
37
38
issue48 :: TestTree
@@ -43,7 +44,7 @@ issue48 = Test.Tasty.HUnit.testCase "Issue #48" assertion
43
44
code <- Data.Text.IO. readFile file
44
45
45
46
parsedExpression <- case Dhall.Parser. exprFromText file code of
46
- Left exception -> Control. Exception. throwIO exception
47
+ Left exception -> Exception. throwIO exception
47
48
Right parsedExpression -> return parsedExpression
48
49
49
50
resolvedExpression <- Dhall.Import. load parsedExpression
@@ -56,12 +57,12 @@ issue48 = Test.Tasty.HUnit.testCase "Issue #48" assertion
56
57
Dhall.JSON. convertToHomogeneousMaps conversion resolvedExpression
57
58
58
59
actualValue <- case Dhall.JSON. dhallToJSON convertedExpression of
59
- Left exception -> Control. Exception. throwIO exception
60
+ Left exception -> Exception. throwIO exception
60
61
Right actualValue -> return actualValue
61
62
62
63
bytes <- Data.ByteString.Lazy. readFile " ./tasty/data/issue48.json"
63
64
64
- expectedValue <- case Data. Aeson. eitherDecode bytes of
65
+ expectedValue <- case Aeson. eitherDecode bytes of
65
66
Left string -> fail string
66
67
Right expectedValue -> return expectedValue
67
68
@@ -117,7 +118,7 @@ emptyAlternative :: TestTree
117
118
emptyAlternative = Test.Tasty.HUnit. testCase " Empty alternative" $ do
118
119
let schema = Core. Union [ (" Bar" , Nothing ), (" Foo" , Nothing ) ]
119
120
120
- let json = Data. Aeson. String " Foo"
121
+ let json = Aeson. String " Foo"
121
122
122
123
let expectedResult = Core. Field schema " Foo"
123
124
@@ -132,17 +133,17 @@ nesting = Test.Tasty.testGroup "Nesting" [ nested, inline ]
132
133
where
133
134
nested =
134
135
testCase " ./tasty/data/nesting0.dhall"
135
- (Data. Aeson. Object
136
- [ (" foo" , Data. Aeson. Number 2 )
137
- , (" name" , Data. Aeson. String " Left" )
136
+ (Aeson. Object
137
+ [ (" foo" , Aeson. Number 2 )
138
+ , (" name" , Aeson. String " Left" )
138
139
]
139
140
)
140
141
141
142
inline =
142
143
testCase " ./tasty/data/nesting1.dhall"
143
- (Data. Aeson. Object
144
- [ (" name" , Data. Aeson. String " Left" )
145
- , (" value" , Data. Aeson. Object [ (" foo" , Data. Aeson. Number 2 )] )
144
+ (Aeson. Object
145
+ [ (" name" , Aeson. String " Left" )
146
+ , (" value" , Aeson. Object [ (" foo" , Aeson. Number 2 )] )
146
147
]
147
148
)
148
149
@@ -151,7 +152,7 @@ nesting = Test.Tasty.testGroup "Nesting" [ nested, inline ]
151
152
code <- Data.Text.IO. readFile file
152
153
153
154
parsedExpression <- case Dhall.Parser. exprFromText file code of
154
- Left exception -> Control. Exception. throwIO exception
155
+ Left exception -> Exception. throwIO exception
155
156
Right parsedExpression -> return parsedExpression
156
157
157
158
resolvedExpression <- Dhall.Import. load parsedExpression
@@ -164,9 +165,59 @@ nesting = Test.Tasty.testGroup "Nesting" [ nested, inline ]
164
165
Dhall.JSON. convertToHomogeneousMaps conversion resolvedExpression
165
166
166
167
actualValue <- case Dhall.JSON. dhallToJSON convertedExpression of
167
- Left exception -> Control. Exception. throwIO exception
168
+ Left exception -> Exception. throwIO exception
168
169
Right actualValue -> return actualValue
169
170
170
171
let message = " The alternative name was not nested correctly"
171
172
172
173
Test.Tasty.HUnit. assertEqual message expectedValue actualValue
174
+
175
+ unionKeys :: TestTree
176
+ unionKeys = Test.Tasty.HUnit. testCase " Empty alternative" $ do
177
+ let union = Core. Union [ (" A" , Nothing ), (" B" , Nothing ) ]
178
+ let schema =
179
+ Core. App
180
+ Core. List
181
+ (Core. Record
182
+ [ (" mapKey" , union )
183
+ , (" mapValue" , Core. Natural )
184
+ ]
185
+ )
186
+
187
+ let expectedValue =
188
+ Aeson. Object [ (" A" , Aeson. Number 1 ), (" B" , Aeson. Number 2 ) ]
189
+
190
+ let expectedDhall =
191
+ Core. ListLit
192
+ Nothing
193
+ [ Core. RecordLit
194
+ [ (" mapKey" , Core. Field union " A" )
195
+ , (" mapValue" , Core. NaturalLit 1 )
196
+ ]
197
+ , Core. RecordLit
198
+ [ (" mapKey" , Core. Field union " B" )
199
+ , (" mapValue" , Core. NaturalLit 2 )
200
+ ]
201
+ ]
202
+
203
+ actualDhall <- Core. throws (JSONToDhall. dhallFromJSON JSONToDhall. defaultConversion schema expectedValue)
204
+
205
+ let message₀ = " Union keys were not decoded from JSON correctly"
206
+
207
+ Test.Tasty.HUnit. assertEqual message₀ expectedDhall actualDhall
208
+
209
+ let mapKey = " mapKey"
210
+
211
+ let mapValue = " mapValue"
212
+
213
+ let conversion = Dhall.JSON. Conversion {.. }
214
+
215
+ let convertedExpression =
216
+ Dhall.JSON. convertToHomogeneousMaps conversion expectedDhall
217
+
218
+ let message₁ = " Union keys were not encoded as JSON correctly"
219
+
220
+ case Dhall.JSON. dhallToJSON convertedExpression of
221
+ Left exception -> Exception. throwIO exception
222
+ Right actualValue ->
223
+ Test.Tasty.HUnit. assertEqual message₁ expectedValue actualValue
0 commit comments