@@ -33,28 +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 ) -> Bool 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
- XCTFail ( " Unable to encode \( T . self) < \( value) >: \( error) " )
42
- return false
43
- }
44
-
45
- let decoded : T
46
- do {
47
- decoded = try decode ( data)
48
- } catch {
49
- XCTFail ( " Unable to decode \( T . self) < \( value) >: \( error) " )
50
- return false
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
+ }
51
43
}
52
-
53
- XCTAssertEqual ( value, decoded, " Decoded \( T . self) < \( decoded) > not equal to original < \( value) > " )
54
- return value == decoded
55
44
}
56
45
57
- func expectRoundTripEqualityThroughJSON< T : Codable > ( for value: T ) -> Bool where T : Equatable {
46
+ func expectRoundTripEqualityThroughJSON< T : Codable > ( for value: T ) throws where T : Equatable {
58
47
let inf = " INF " , negInf = " -INF " , nan = " NaN "
59
48
let encode = { ( _ value: T ) throws -> Data in
60
49
let encoder = JSONEncoder ( )
@@ -72,7 +61,7 @@ func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) -> Bool where
72
61
return try decoder. decode ( T . self, from: data)
73
62
}
74
63
75
- return expectRoundTripEquality ( of: value, encode: encode, decode: decode)
64
+ try expectRoundTripEquality ( of: value, encode: encode, decode: decode)
76
65
}
77
66
78
67
// MARK: - Helper Types
@@ -101,7 +90,11 @@ class TestCodable : XCTestCase {
101
90
102
91
func test_PersonNameComponents_JSON( ) {
103
92
for components in personNameComponentsValues {
104
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: components) )
93
+ do {
94
+ try expectRoundTripEqualityThroughJSON ( for: components)
95
+ } catch let error {
96
+ XCTFail ( " \( error) for \( components) " )
97
+ }
105
98
}
106
99
}
107
100
@@ -116,7 +109,11 @@ class TestCodable : XCTestCase {
116
109
func test_UUID_JSON( ) {
117
110
for uuid in uuidValues {
118
111
// We have to wrap the UUID since we cannot have a top-level string.
119
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: UUIDCodingWrapper ( uuid) ) )
112
+ do {
113
+ try expectRoundTripEqualityThroughJSON ( for: UUIDCodingWrapper ( uuid) )
114
+ } catch let error {
115
+ XCTFail ( " \( error) for \( uuid) " )
116
+ }
120
117
}
121
118
}
122
119
@@ -131,7 +128,11 @@ class TestCodable : XCTestCase {
131
128
132
129
func test_URL_JSON( ) {
133
130
for url in urlValues {
134
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: url) )
131
+ do {
132
+ try expectRoundTripEqualityThroughJSON ( for: url)
133
+ } catch let error {
134
+ XCTFail ( " \( error) for \( url) " )
135
+ }
135
136
}
136
137
}
137
138
@@ -144,7 +145,11 @@ class TestCodable : XCTestCase {
144
145
145
146
func test_NSRange_JSON( ) {
146
147
for range in nsrangeValues {
147
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: range) )
148
+ do {
149
+ try expectRoundTripEqualityThroughJSON ( for: range)
150
+ } catch let error {
151
+ XCTFail ( " \( error) for \( range) " )
152
+ }
148
153
}
149
154
}
150
155
@@ -162,7 +167,11 @@ class TestCodable : XCTestCase {
162
167
163
168
func test_Locale_JSON( ) {
164
169
for locale in localeValues {
165
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: locale) )
170
+ do {
171
+ try expectRoundTripEqualityThroughJSON ( for: locale)
172
+ } catch let error {
173
+ XCTFail ( " \( error) for \( locale) " )
174
+ }
166
175
}
167
176
}
168
177
@@ -175,7 +184,11 @@ class TestCodable : XCTestCase {
175
184
176
185
func test_IndexSet_JSON( ) {
177
186
for indexSet in indexSetValues {
178
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: indexSet) )
187
+ do {
188
+ try expectRoundTripEqualityThroughJSON ( for: indexSet)
189
+ } catch let error {
190
+ XCTFail ( " \( error) for \( indexSet) " )
191
+ }
179
192
}
180
193
}
181
194
@@ -189,7 +202,11 @@ class TestCodable : XCTestCase {
189
202
190
203
func test_IndexPath_JSON( ) {
191
204
for indexPath in indexPathValues {
192
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: indexPath) )
205
+ do {
206
+ try expectRoundTripEqualityThroughJSON ( for: indexPath)
207
+ } catch let error {
208
+ XCTFail ( " \( error) for \( indexPath) " )
209
+ }
193
210
}
194
211
}
195
212
@@ -213,7 +230,11 @@ class TestCodable : XCTestCase {
213
230
214
231
func test_AffineTransform_JSON( ) {
215
232
for transform in affineTransformValues {
216
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: transform) )
233
+ do {
234
+ try expectRoundTripEqualityThroughJSON ( for: transform)
235
+ } catch let error {
236
+ XCTFail ( " \( error) for \( transform) " )
237
+ }
217
238
}
218
239
}
219
240
@@ -229,7 +250,11 @@ class TestCodable : XCTestCase {
229
250
230
251
func test_Decimal_JSON( ) {
231
252
for decimal in decimalValues {
232
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: decimal) )
253
+ do {
254
+ try expectRoundTripEqualityThroughJSON ( for: decimal)
255
+ } catch let error {
256
+ XCTFail ( " \( error) for \( decimal) " )
257
+ }
233
258
}
234
259
}
235
260
@@ -245,7 +270,11 @@ class TestCodable : XCTestCase {
245
270
246
271
func test_CGPoint_JSON( ) {
247
272
for point in cgpointValues {
248
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: point) )
273
+ do {
274
+ try expectRoundTripEqualityThroughJSON ( for: point)
275
+ } catch let error {
276
+ XCTFail ( " \( error) for \( point) " )
277
+ }
249
278
}
250
279
}
251
280
@@ -261,7 +290,11 @@ class TestCodable : XCTestCase {
261
290
262
291
func test_CGSize_JSON( ) {
263
292
for size in cgsizeValues {
264
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: size) )
293
+ do {
294
+ try expectRoundTripEqualityThroughJSON ( for: size)
295
+ } catch let error {
296
+ XCTFail ( " \( error) for \( size) " )
297
+ }
265
298
}
266
299
}
267
300
@@ -278,7 +311,11 @@ class TestCodable : XCTestCase {
278
311
279
312
func test_CGRect_JSON( ) {
280
313
for rect in cgrectValues {
281
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: rect) )
314
+ do {
315
+ try expectRoundTripEqualityThroughJSON ( for: rect)
316
+ } catch let error {
317
+ XCTFail ( " \( error) for \( rect) " )
318
+ }
282
319
}
283
320
}
284
321
@@ -304,7 +341,11 @@ class TestCodable : XCTestCase {
304
341
305
342
func test_CharacterSet_JSON( ) {
306
343
for characterSet in characterSetValues {
307
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: characterSet) )
344
+ do {
345
+ try expectRoundTripEqualityThroughJSON ( for: characterSet)
346
+ } catch let error {
347
+ XCTFail ( " \( error) for \( characterSet) " )
348
+ }
308
349
}
309
350
}
310
351
@@ -333,7 +374,11 @@ class TestCodable : XCTestCase {
333
374
334
375
func test_TimeZone_JSON( ) {
335
376
for timeZone in timeZoneValues {
336
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: timeZone) )
377
+ do {
378
+ try expectRoundTripEqualityThroughJSON ( for: timeZone)
379
+ } catch let error {
380
+ XCTFail ( " \( error) for \( timeZone) " )
381
+ }
337
382
}
338
383
}
339
384
@@ -369,7 +414,11 @@ class TestCodable : XCTestCase {
369
414
370
415
func test_Calendar_JSON( ) {
371
416
for calendar in calendarValues {
372
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: calendar) )
417
+ do {
418
+ try expectRoundTripEqualityThroughJSON ( for: calendar)
419
+ } catch let error {
420
+ XCTFail ( " \( error) for \( calendar) " )
421
+ }
373
422
}
374
423
}
375
424
@@ -406,14 +455,22 @@ class TestCodable : XCTestCase {
406
455
#endif
407
456
408
457
let components = calendar. dateComponents ( dateComponents, from: Date ( timeIntervalSince1970: 1501283776 ) )
409
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: components) )
458
+ do {
459
+ try expectRoundTripEqualityThroughJSON ( for: components)
460
+ } catch let error {
461
+ XCTFail ( " \( error) " )
462
+ }
410
463
}
411
464
412
465
// MARK: - Measurement
413
466
func test_Measurement_JSON( ) {
414
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: Measurement ( value: 42 , unit: UnitAcceleration . metersPerSecondSquared) ) )
415
- XCTAssertTrue ( expectRoundTripEqualityThroughJSON ( for: Measurement ( value: 42 , unit: UnitMass . kilograms) ) )
416
- XCTAssertTrue ( 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
+ }
417
474
}
418
475
}
419
476
0 commit comments