@@ -15,6 +15,17 @@ import XCTest
15
15
16
16
final class JSONTests : XCTestCase {
17
17
18
+ func testPrimitive( ) {
19
+ _testRoundTrip ( of: true , expectedJSON: " true " )
20
+ _testRoundTrip ( of: false , expectedJSON: " false " )
21
+ _testRoundTrip ( of: Bool ? . none, expectedJSON: " null " )
22
+ _testRoundTrip ( of: " " , expectedJSON: " \" \" " )
23
+ _testRoundTrip ( of: 0 , expectedJSON: " 0 " )
24
+ _testRoundTrip ( of: 0 as Int8 , expectedJSON: " 0 " )
25
+ _testRoundTrip ( of: 0.0 as Float , expectedJSON: " 0.0 " )
26
+ _testRoundTrip ( of: 0.0 as Double , expectedJSON: " 0.0 " )
27
+ }
28
+
18
29
func testEmptyStruct( ) {
19
30
let value = EmptyStruct ( )
20
31
_testRoundTrip ( of: value, expectedJSON: " {} " )
@@ -73,12 +84,12 @@ final class JSONTests: XCTestCase {
73
84
data: [ nil , 42 ]
74
85
)
75
86
] ,
76
- elapsed: 42.3
87
+ elapsed: 42.3e32
77
88
)
78
89
_testRoundTrip (
79
90
of: value,
80
91
expectedJSON: #"""
81
- {"diagnostics":[{"animal":"cat","data":[null,42],"message":"error 🛑"}],"elapsed":42.3 ,"result":"\tresult\nfoo"}
92
+ {"diagnostics":[{"animal":"cat","data":[null,42],"message":"error 🛑"}],"elapsed":4.23e+33 ,"result":"\tresult\nfoo"}
82
93
"""#
83
94
)
84
95
}
@@ -92,6 +103,33 @@ final class JSONTests: XCTestCase {
92
103
)
93
104
}
94
105
106
+ func testParseError( ) {
107
+ _assertParseError (
108
+ #"{"foo": 1"# ,
109
+ message: " unexpected end of file "
110
+ )
111
+ _assertParseError (
112
+ #""foo"# ,
113
+ message: " unexpected end of file "
114
+ )
115
+ _assertParseError (
116
+ " \n " ,
117
+ message: " unexpected end of file "
118
+ )
119
+ _assertParseError (
120
+ " trua " ,
121
+ message: " unexpected character 'a'; expected 'e' "
122
+ )
123
+ _assertParseError (
124
+ " [true, #foo] " ,
125
+ message: " unexpected character '#'; value start "
126
+ )
127
+ _assertParseError (
128
+ " {}true " ,
129
+ message: " unexpected character 't'; after top-level value "
130
+ )
131
+ }
132
+
95
133
func testTypeCoercion( ) {
96
134
_testRoundTripTypeCoercionFailure ( of: [ false , true ] , as: [ Int ] . self)
97
135
_testRoundTripTypeCoercionFailure ( of: [ false , true ] , as: [ Int8 ] . self)
@@ -150,7 +188,25 @@ final class JSONTests: XCTestCase {
150
188
let data = try JSONEncoder ( ) . encode ( value)
151
189
let _ = try JSONDecoder ( ) . decode ( U . self, from: data)
152
190
XCTFail ( " Coercion from \( T . self) to \( U . self) was expected to fail. " )
153
- } catch { }
191
+ } catch DecodingError . typeMismatch( _, _) {
192
+ // Success
193
+ } catch {
194
+ XCTFail ( " unexpected error " )
195
+ }
196
+ }
197
+
198
+ private func _assertParseError( _ json: String , message: String ) {
199
+ do {
200
+ var json = json
201
+ _ = try json. withUTF8 { try JSON . decode ( Bool . self, from: $0) }
202
+ } catch DecodingError . dataCorrupted( let context) {
203
+ XCTAssertEqual (
204
+ String ( describing: try XCTUnwrap ( context. underlyingError) ) ,
205
+ message
206
+ )
207
+ } catch {
208
+ XCTFail ( " unexpected error " )
209
+ }
154
210
}
155
211
}
156
212
0 commit comments