Skip to content

Commit b0ea692

Browse files
committed
TestCodable: Make expectRoundTripEquality() throw instead of returning false
1 parent 7b966bf commit b0ea692

File tree

1 file changed

+95
-38
lines changed

1 file changed

+95
-38
lines changed

TestFoundation/TestCodable.swift

Lines changed: 95 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,17 @@ private func makePersonNameComponents(namePrefix: String? = nil,
3333
return result
3434
}
3535

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 {
3837
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+
}
5143
}
52-
53-
XCTAssertEqual(value, decoded, "Decoded \(T.self) <\(decoded)> not equal to original <\(value)>")
54-
return value == decoded
5544
}
5645

57-
func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) -> Bool where T : Equatable {
46+
func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) throws where T : Equatable {
5847
let inf = "INF", negInf = "-INF", nan = "NaN"
5948
let encode = { (_ value: T) throws -> Data in
6049
let encoder = JSONEncoder()
@@ -72,7 +61,7 @@ func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) -> Bool where
7261
return try decoder.decode(T.self, from: data)
7362
}
7463

75-
return expectRoundTripEquality(of: value, encode: encode, decode: decode)
64+
try expectRoundTripEquality(of: value, encode: encode, decode: decode)
7665
}
7766

7867
// MARK: - Helper Types
@@ -101,7 +90,11 @@ class TestCodable : XCTestCase {
10190

10291
func test_PersonNameComponents_JSON() {
10392
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+
}
10598
}
10699
}
107100

@@ -116,7 +109,11 @@ class TestCodable : XCTestCase {
116109
func test_UUID_JSON() {
117110
for uuid in uuidValues {
118111
// 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+
}
120117
}
121118
}
122119

@@ -131,7 +128,11 @@ class TestCodable : XCTestCase {
131128

132129
func test_URL_JSON() {
133130
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+
}
135136
}
136137
}
137138

@@ -144,7 +145,11 @@ class TestCodable : XCTestCase {
144145

145146
func test_NSRange_JSON() {
146147
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+
}
148153
}
149154
}
150155

@@ -162,7 +167,11 @@ class TestCodable : XCTestCase {
162167

163168
func test_Locale_JSON() {
164169
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+
}
166175
}
167176
}
168177

@@ -175,7 +184,11 @@ class TestCodable : XCTestCase {
175184

176185
func test_IndexSet_JSON() {
177186
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+
}
179192
}
180193
}
181194

@@ -189,7 +202,11 @@ class TestCodable : XCTestCase {
189202

190203
func test_IndexPath_JSON() {
191204
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+
}
193210
}
194211
}
195212

@@ -213,7 +230,11 @@ class TestCodable : XCTestCase {
213230

214231
func test_AffineTransform_JSON() {
215232
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+
}
217238
}
218239
}
219240

@@ -229,7 +250,11 @@ class TestCodable : XCTestCase {
229250

230251
func test_Decimal_JSON() {
231252
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+
}
233258
}
234259
}
235260

@@ -245,7 +270,11 @@ class TestCodable : XCTestCase {
245270

246271
func test_CGPoint_JSON() {
247272
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+
}
249278
}
250279
}
251280

@@ -261,7 +290,11 @@ class TestCodable : XCTestCase {
261290

262291
func test_CGSize_JSON() {
263292
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+
}
265298
}
266299
}
267300

@@ -278,7 +311,11 @@ class TestCodable : XCTestCase {
278311

279312
func test_CGRect_JSON() {
280313
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+
}
282319
}
283320
}
284321

@@ -304,7 +341,11 @@ class TestCodable : XCTestCase {
304341

305342
func test_CharacterSet_JSON() {
306343
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+
}
308349
}
309350
}
310351

@@ -333,7 +374,11 @@ class TestCodable : XCTestCase {
333374

334375
func test_TimeZone_JSON() {
335376
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+
}
337382
}
338383
}
339384

@@ -369,7 +414,11 @@ class TestCodable : XCTestCase {
369414

370415
func test_Calendar_JSON() {
371416
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+
}
373422
}
374423
}
375424

@@ -406,14 +455,22 @@ class TestCodable : XCTestCase {
406455
#endif
407456

408457
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+
}
410463
}
411464

412465
// MARK: - Measurement
413466
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+
}
417474
}
418475
}
419476

0 commit comments

Comments
 (0)