Skip to content

Commit 43f5a34

Browse files
committed
TestCodable small refactor
1 parent cbed130 commit 43f5a34

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

TestFoundation/TestCodable.swift

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,24 @@ func expectRoundTripEquality<T : Codable>(of value: T, encode: (T) throws -> Dat
5151
XCTAssertEqual(value, decoded, "Decoded \(T.self) <\(decoded)> not equal to original <\(value)>")
5252
}
5353

54-
func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T,
55-
encoder: JSONEncoder = JSONEncoder(),
56-
decoder: JSONDecoder = JSONDecoder()) where T : Equatable {
54+
func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T) where T : Equatable {
55+
let inf = "INF", negInf = "-INF", nan = "NaN"
5756
let encode = { (_ value: T) throws -> Data in
57+
let encoder = JSONEncoder()
58+
encoder.nonConformingFloatEncodingStrategy = .convertToString(positiveInfinity: inf,
59+
negativeInfinity: negInf,
60+
nan: nan)
5861
return try encoder.encode(value)
5962
}
60-
63+
6164
let decode = { (_ data: Data) throws -> T in
65+
let decoder = JSONDecoder()
66+
decoder.nonConformingFloatDecodingStrategy = .convertFromString(positiveInfinity: inf,
67+
negativeInfinity: negInf,
68+
nan: nan)
6269
return try decoder.decode(T.self, from: data)
6370
}
64-
71+
6572
expectRoundTripEquality(of: value, encode: encode, decode: decode)
6673
}
6774

@@ -261,6 +268,7 @@ class TestCodable : XCTestCase {
261268
CGRect.zero,
262269
CGRect(origin: CGPoint(x: 10, y: 20), size: CGSize(width: 30, height: 40)),
263270
CGRect(origin: CGPoint(x: -10, y: -20), size: CGSize(width: -30, height: -40)),
271+
CGRect.null,
264272
// Disabled due to limit on magnitude in JSON. See SR-5346
265273
// CGRect.infinite
266274
]
@@ -269,20 +277,6 @@ class TestCodable : XCTestCase {
269277
for rect in cgrectValues {
270278
expectRoundTripEqualityThroughJSON(for: rect)
271279
}
272-
273-
do {
274-
let rect = CGRect.null
275-
let encoder = JSONEncoder()
276-
let decoder = JSONDecoder()
277-
let inf = "INF", negInf = "-INF", nan = "NaN"
278-
encoder.nonConformingFloatEncodingStrategy = .convertToString(positiveInfinity: inf,
279-
negativeInfinity: negInf,
280-
nan: nan)
281-
decoder.nonConformingFloatDecodingStrategy = .convertFromString(positiveInfinity: inf,
282-
negativeInfinity: negInf,
283-
nan: nan)
284-
expectRoundTripEqualityThroughJSON(for: rect, encoder: encoder, decoder: decoder)
285-
}
286280
}
287281

288282
}

0 commit comments

Comments
 (0)