Skip to content

Commit 7b966bf

Browse files
committed
TestCodable: Replace fatalError() with XCTFail
1 parent 1b4bb4e commit 7b966bf

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

TestFoundation/TestCodable.swift

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,28 @@ 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) where T : Equatable {
36+
func expectRoundTripEquality<T : Codable>(of value: T, encode: (T) throws -> Data, decode: (Data) throws -> T) -> Bool where T : Equatable {
3737
let data: Data
3838
do {
3939
data = try encode(value)
4040
} catch {
41-
fatalError("Unable to encode \(T.self) <\(value)>: \(error)")
41+
XCTFail("Unable to encode \(T.self) <\(value)>: \(error)")
42+
return false
4243
}
4344

4445
let decoded: T
4546
do {
4647
decoded = try decode(data)
4748
} catch {
48-
fatalError("Unable to decode \(T.self) <\(value)>: \(error)")
49+
XCTFail("Unable to decode \(T.self) <\(value)>: \(error)")
50+
return false
4951
}
5052

5153
XCTAssertEqual(value, decoded, "Decoded \(T.self) <\(decoded)> not equal to original <\(value)>")
54+
return value == decoded
5255
}
5356

54-
func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) where T : Equatable {
57+
func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) -> Bool where T : Equatable {
5558
let inf = "INF", negInf = "-INF", nan = "NaN"
5659
let encode = { (_ value: T) throws -> Data in
5760
let encoder = JSONEncoder()
@@ -69,7 +72,7 @@ func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) where T : Equ
6972
return try decoder.decode(T.self, from: data)
7073
}
7174

72-
expectRoundTripEquality(of: value, encode: encode, decode: decode)
75+
return expectRoundTripEquality(of: value, encode: encode, decode: decode)
7376
}
7477

7578
// MARK: - Helper Types
@@ -98,7 +101,7 @@ class TestCodable : XCTestCase {
98101

99102
func test_PersonNameComponents_JSON() {
100103
for components in personNameComponentsValues {
101-
expectRoundTripEqualityThroughJSON(for: components)
104+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: components))
102105
}
103106
}
104107

@@ -113,7 +116,7 @@ class TestCodable : XCTestCase {
113116
func test_UUID_JSON() {
114117
for uuid in uuidValues {
115118
// We have to wrap the UUID since we cannot have a top-level string.
116-
expectRoundTripEqualityThroughJSON(for: UUIDCodingWrapper(uuid))
119+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: UUIDCodingWrapper(uuid)))
117120
}
118121
}
119122

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

129132
func test_URL_JSON() {
130133
for url in urlValues {
131-
expectRoundTripEqualityThroughJSON(for: url)
134+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: url))
132135
}
133136
}
134137

@@ -141,7 +144,7 @@ class TestCodable : XCTestCase {
141144

142145
func test_NSRange_JSON() {
143146
for range in nsrangeValues {
144-
expectRoundTripEqualityThroughJSON(for: range)
147+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: range))
145148
}
146149
}
147150

@@ -159,7 +162,7 @@ class TestCodable : XCTestCase {
159162

160163
func test_Locale_JSON() {
161164
for locale in localeValues {
162-
expectRoundTripEqualityThroughJSON(for: locale)
165+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: locale))
163166
}
164167
}
165168

@@ -172,7 +175,7 @@ class TestCodable : XCTestCase {
172175

173176
func test_IndexSet_JSON() {
174177
for indexSet in indexSetValues {
175-
expectRoundTripEqualityThroughJSON(for: indexSet)
178+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: indexSet))
176179
}
177180
}
178181

@@ -186,7 +189,7 @@ class TestCodable : XCTestCase {
186189

187190
func test_IndexPath_JSON() {
188191
for indexPath in indexPathValues {
189-
expectRoundTripEqualityThroughJSON(for: indexPath)
192+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: indexPath))
190193
}
191194
}
192195

@@ -210,7 +213,7 @@ class TestCodable : XCTestCase {
210213

211214
func test_AffineTransform_JSON() {
212215
for transform in affineTransformValues {
213-
expectRoundTripEqualityThroughJSON(for: transform)
216+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: transform))
214217
}
215218
}
216219

@@ -226,7 +229,7 @@ class TestCodable : XCTestCase {
226229

227230
func test_Decimal_JSON() {
228231
for decimal in decimalValues {
229-
expectRoundTripEqualityThroughJSON(for: decimal)
232+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: decimal))
230233
}
231234
}
232235

@@ -242,7 +245,7 @@ class TestCodable : XCTestCase {
242245

243246
func test_CGPoint_JSON() {
244247
for point in cgpointValues {
245-
expectRoundTripEqualityThroughJSON(for: point)
248+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: point))
246249
}
247250
}
248251

@@ -258,7 +261,7 @@ class TestCodable : XCTestCase {
258261

259262
func test_CGSize_JSON() {
260263
for size in cgsizeValues {
261-
expectRoundTripEqualityThroughJSON(for: size)
264+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: size))
262265
}
263266
}
264267

@@ -275,7 +278,7 @@ class TestCodable : XCTestCase {
275278

276279
func test_CGRect_JSON() {
277280
for rect in cgrectValues {
278-
expectRoundTripEqualityThroughJSON(for: rect)
281+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: rect))
279282
}
280283
}
281284

@@ -301,7 +304,7 @@ class TestCodable : XCTestCase {
301304

302305
func test_CharacterSet_JSON() {
303306
for characterSet in characterSetValues {
304-
expectRoundTripEqualityThroughJSON(for: characterSet)
307+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: characterSet))
305308
}
306309
}
307310

@@ -330,7 +333,7 @@ class TestCodable : XCTestCase {
330333

331334
func test_TimeZone_JSON() {
332335
for timeZone in timeZoneValues {
333-
expectRoundTripEqualityThroughJSON(for: timeZone)
336+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: timeZone))
334337
}
335338
}
336339

@@ -366,7 +369,7 @@ class TestCodable : XCTestCase {
366369

367370
func test_Calendar_JSON() {
368371
for calendar in calendarValues {
369-
expectRoundTripEqualityThroughJSON(for: calendar)
372+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: calendar))
370373
}
371374
}
372375

@@ -403,14 +406,14 @@ class TestCodable : XCTestCase {
403406
#endif
404407

405408
let components = calendar.dateComponents(dateComponents, from: Date(timeIntervalSince1970: 1501283776))
406-
expectRoundTripEqualityThroughJSON(for: components)
409+
XCTAssertTrue(expectRoundTripEqualityThroughJSON(for: components))
407410
}
408411

409412
// MARK: - Measurement
410413
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))
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)))
414417
}
415418
}
416419

0 commit comments

Comments
 (0)