@@ -33,25 +33,17 @@ private func makePersonNameComponents(namePrefix: String? = nil,
33
33
return result
34
34
}
35
35
36
- func expectRoundTripEquality< T : Codable > ( of value: T , encode: ( T ) throws -> Data , decode: ( Data ) throws -> T ) where T : Equatable {
37
- let data : Data
36
+ func expectRoundTripEquality< T : Codable > ( of value: T , encode: ( T ) throws -> Data , decode: ( Data ) throws -> T ) throws where T : Equatable {
38
37
do {
39
- data = try encode ( value)
40
- } catch {
41
- fatalError ( " Unable to encode \( T . self) < \( value) >: \( error) " )
42
- }
43
-
44
- let decoded : T
45
- do {
46
- decoded = try decode ( data)
47
- } catch {
48
- fatalError ( " Unable to decode \( T . self) < \( value) >: \( error) " )
38
+ let data = try encode ( value)
39
+ let decoded : T = try decode ( data)
40
+ if value != decoded {
41
+ throw NSError ( domain: " Decode mismatch " , code: 0 , userInfo: [ " msg " : " Decoded \( T . self) < \( decoded) > not equal to original < \( value) > " ] )
42
+ }
49
43
}
50
-
51
- XCTAssertEqual ( value, decoded, " Decoded \( T . self) < \( decoded) > not equal to original < \( value) > " )
52
44
}
53
45
54
- func expectRoundTripEqualityThroughJSON< T : Codable > ( for value: T ) where T : Equatable {
46
+ func expectRoundTripEqualityThroughJSON< T : Codable > ( for value: T ) throws where T : Equatable {
55
47
let inf = " INF " , negInf = " -INF " , nan = " NaN "
56
48
let encode = { ( _ value: T ) throws -> Data in
57
49
let encoder = JSONEncoder ( )
@@ -69,7 +61,7 @@ func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) where T : Equ
69
61
return try decoder. decode ( T . self, from: data)
70
62
}
71
63
72
- expectRoundTripEquality ( of: value, encode: encode, decode: decode)
64
+ try expectRoundTripEquality ( of: value, encode: encode, decode: decode)
73
65
}
74
66
75
67
// MARK: - Helper Types
@@ -98,7 +90,11 @@ class TestCodable : XCTestCase {
98
90
99
91
func test_PersonNameComponents_JSON( ) {
100
92
for components in personNameComponentsValues {
101
- expectRoundTripEqualityThroughJSON ( for: components)
93
+ do {
94
+ try expectRoundTripEqualityThroughJSON ( for: components)
95
+ } catch let error {
96
+ XCTFail ( " \( error) for \( components) " )
97
+ }
102
98
}
103
99
}
104
100
@@ -113,7 +109,11 @@ class TestCodable : XCTestCase {
113
109
func test_UUID_JSON( ) {
114
110
for uuid in uuidValues {
115
111
// We have to wrap the UUID since we cannot have a top-level string.
116
- expectRoundTripEqualityThroughJSON ( for: UUIDCodingWrapper ( uuid) )
112
+ do {
113
+ try expectRoundTripEqualityThroughJSON ( for: UUIDCodingWrapper ( uuid) )
114
+ } catch let error {
115
+ XCTFail ( " \( error) for \( uuid) " )
116
+ }
117
117
}
118
118
}
119
119
@@ -128,7 +128,11 @@ class TestCodable : XCTestCase {
128
128
129
129
func test_URL_JSON( ) {
130
130
for url in urlValues {
131
- expectRoundTripEqualityThroughJSON ( for: url)
131
+ do {
132
+ try expectRoundTripEqualityThroughJSON ( for: url)
133
+ } catch let error {
134
+ XCTFail ( " \( error) for \( url) " )
135
+ }
132
136
}
133
137
}
134
138
@@ -141,7 +145,11 @@ class TestCodable : XCTestCase {
141
145
142
146
func test_NSRange_JSON( ) {
143
147
for range in nsrangeValues {
144
- expectRoundTripEqualityThroughJSON ( for: range)
148
+ do {
149
+ try expectRoundTripEqualityThroughJSON ( for: range)
150
+ } catch let error {
151
+ XCTFail ( " \( error) for \( range) " )
152
+ }
145
153
}
146
154
}
147
155
@@ -159,7 +167,11 @@ class TestCodable : XCTestCase {
159
167
160
168
func test_Locale_JSON( ) {
161
169
for locale in localeValues {
162
- expectRoundTripEqualityThroughJSON ( for: locale)
170
+ do {
171
+ try expectRoundTripEqualityThroughJSON ( for: locale)
172
+ } catch let error {
173
+ XCTFail ( " \( error) for \( locale) " )
174
+ }
163
175
}
164
176
}
165
177
@@ -172,7 +184,11 @@ class TestCodable : XCTestCase {
172
184
173
185
func test_IndexSet_JSON( ) {
174
186
for indexSet in indexSetValues {
175
- expectRoundTripEqualityThroughJSON ( for: indexSet)
187
+ do {
188
+ try expectRoundTripEqualityThroughJSON ( for: indexSet)
189
+ } catch let error {
190
+ XCTFail ( " \( error) for \( indexSet) " )
191
+ }
176
192
}
177
193
}
178
194
@@ -186,7 +202,11 @@ class TestCodable : XCTestCase {
186
202
187
203
func test_IndexPath_JSON( ) {
188
204
for indexPath in indexPathValues {
189
- expectRoundTripEqualityThroughJSON ( for: indexPath)
205
+ do {
206
+ try expectRoundTripEqualityThroughJSON ( for: indexPath)
207
+ } catch let error {
208
+ XCTFail ( " \( error) for \( indexPath) " )
209
+ }
190
210
}
191
211
}
192
212
@@ -210,7 +230,11 @@ class TestCodable : XCTestCase {
210
230
211
231
func test_AffineTransform_JSON( ) {
212
232
for transform in affineTransformValues {
213
- expectRoundTripEqualityThroughJSON ( for: transform)
233
+ do {
234
+ try expectRoundTripEqualityThroughJSON ( for: transform)
235
+ } catch let error {
236
+ XCTFail ( " \( error) for \( transform) " )
237
+ }
214
238
}
215
239
}
216
240
@@ -226,7 +250,11 @@ class TestCodable : XCTestCase {
226
250
227
251
func test_Decimal_JSON( ) {
228
252
for decimal in decimalValues {
229
- expectRoundTripEqualityThroughJSON ( for: decimal)
253
+ do {
254
+ try expectRoundTripEqualityThroughJSON ( for: decimal)
255
+ } catch let error {
256
+ XCTFail ( " \( error) for \( decimal) " )
257
+ }
230
258
}
231
259
}
232
260
@@ -242,7 +270,11 @@ class TestCodable : XCTestCase {
242
270
243
271
func test_CGPoint_JSON( ) {
244
272
for point in cgpointValues {
245
- expectRoundTripEqualityThroughJSON ( for: point)
273
+ do {
274
+ try expectRoundTripEqualityThroughJSON ( for: point)
275
+ } catch let error {
276
+ XCTFail ( " \( error) for \( point) " )
277
+ }
246
278
}
247
279
}
248
280
@@ -258,7 +290,11 @@ class TestCodable : XCTestCase {
258
290
259
291
func test_CGSize_JSON( ) {
260
292
for size in cgsizeValues {
261
- expectRoundTripEqualityThroughJSON ( for: size)
293
+ do {
294
+ try expectRoundTripEqualityThroughJSON ( for: size)
295
+ } catch let error {
296
+ XCTFail ( " \( error) for \( size) " )
297
+ }
262
298
}
263
299
}
264
300
@@ -275,7 +311,11 @@ class TestCodable : XCTestCase {
275
311
276
312
func test_CGRect_JSON( ) {
277
313
for rect in cgrectValues {
278
- expectRoundTripEqualityThroughJSON ( for: rect)
314
+ do {
315
+ try expectRoundTripEqualityThroughJSON ( for: rect)
316
+ } catch let error {
317
+ XCTFail ( " \( error) for \( rect) " )
318
+ }
279
319
}
280
320
}
281
321
@@ -301,7 +341,11 @@ class TestCodable : XCTestCase {
301
341
302
342
func test_CharacterSet_JSON( ) {
303
343
for characterSet in characterSetValues {
304
- expectRoundTripEqualityThroughJSON ( for: characterSet)
344
+ do {
345
+ try expectRoundTripEqualityThroughJSON ( for: characterSet)
346
+ } catch let error {
347
+ XCTFail ( " \( error) for \( characterSet) " )
348
+ }
305
349
}
306
350
}
307
351
@@ -330,7 +374,11 @@ class TestCodable : XCTestCase {
330
374
331
375
func test_TimeZone_JSON( ) {
332
376
for timeZone in timeZoneValues {
333
- expectRoundTripEqualityThroughJSON ( for: timeZone)
377
+ do {
378
+ try expectRoundTripEqualityThroughJSON ( for: timeZone)
379
+ } catch let error {
380
+ XCTFail ( " \( error) for \( timeZone) " )
381
+ }
334
382
}
335
383
}
336
384
@@ -366,7 +414,11 @@ class TestCodable : XCTestCase {
366
414
367
415
func test_Calendar_JSON( ) {
368
416
for calendar in calendarValues {
369
- expectRoundTripEqualityThroughJSON ( for: calendar)
417
+ do {
418
+ try expectRoundTripEqualityThroughJSON ( for: calendar)
419
+ } catch let error {
420
+ XCTFail ( " \( error) for \( calendar) " )
421
+ }
370
422
}
371
423
}
372
424
@@ -403,14 +455,22 @@ class TestCodable : XCTestCase {
403
455
#endif
404
456
405
457
let components = calendar. dateComponents ( dateComponents, from: Date ( timeIntervalSince1970: 1501283776 ) )
406
- expectRoundTripEqualityThroughJSON ( for: components)
458
+ do {
459
+ try expectRoundTripEqualityThroughJSON ( for: components)
460
+ } catch let error {
461
+ XCTFail ( " \( error) " )
462
+ }
407
463
}
408
464
409
465
// MARK: - Measurement
410
466
func test_Measurement_JSON( ) {
411
- expectRoundTripEqualityThroughJSON ( for: Measurement ( value: 42 , unit: UnitAcceleration . metersPerSecondSquared) )
412
- expectRoundTripEqualityThroughJSON ( for: Measurement ( value: 42 , unit: UnitMass . kilograms) )
413
- expectRoundTripEqualityThroughJSON ( for: Measurement ( value: 42 , unit: UnitLength . miles) )
467
+ do {
468
+ try expectRoundTripEqualityThroughJSON ( for: Measurement ( value: 42 , unit: UnitAcceleration . metersPerSecondSquared) )
469
+ try expectRoundTripEqualityThroughJSON ( for: Measurement ( value: 42 , unit: UnitMass . kilograms) )
470
+ try expectRoundTripEqualityThroughJSON ( for: Measurement ( value: 42 , unit: UnitLength . miles) )
471
+ } catch let error {
472
+ XCTFail ( " \( error) " )
473
+ }
414
474
}
415
475
416
476
// MARK: - URLComponents
0 commit comments