Skip to content

Commit cbed130

Browse files
committed
Added Codable unit tests
1 parent 3cb480a commit cbed130

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

TestFoundation/TestCodable.swift

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,17 @@ 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) where T : Equatable {
54+
func expectRoundTripEqualityThroughJSON<T : Codable>(for value: T,
55+
encoder: JSONEncoder = JSONEncoder(),
56+
decoder: JSONDecoder = JSONDecoder()) where T : Equatable {
5557
let encode = { (_ value: T) throws -> Data in
56-
return try JSONEncoder().encode(value)
58+
return try encoder.encode(value)
5759
}
58-
60+
5961
let decode = { (_ data: Data) throws -> T in
60-
return try JSONDecoder().decode(T.self, from: data)
62+
return try decoder.decode(T.self, from: data)
6163
}
62-
64+
6365
expectRoundTripEquality(of: value, encode: encode, decode: decode)
6466
}
6567

@@ -224,6 +226,7 @@ class TestCodable : XCTestCase {
224226
// MARK: - CGPoint
225227
lazy var cgpointValues: [CGPoint] = [
226228
CGPoint(),
229+
CGPoint.zero,
227230
CGPoint(x: 10, y: 20),
228231
CGPoint(x: -10, y: -20),
229232
// Disabled due to limit on magnitude in JSON. See SR-5346
@@ -239,6 +242,7 @@ class TestCodable : XCTestCase {
239242
// MARK: - CGSize
240243
lazy var cgsizeValues: [CGSize] = [
241244
CGSize(),
245+
CGSize.zero,
242246
CGSize(width: 30, height: 40),
243247
CGSize(width: -30, height: -40),
244248
// Disabled due to limit on magnitude in JSON. See SR-5346
@@ -254,19 +258,31 @@ class TestCodable : XCTestCase {
254258
// MARK: - CGRect
255259
lazy var cgrectValues: [CGRect] = [
256260
CGRect(),
261+
CGRect.zero,
257262
CGRect(origin: CGPoint(x: 10, y: 20), size: CGSize(width: 30, height: 40)),
258263
CGRect(origin: CGPoint(x: -10, y: -20), size: CGSize(width: -30, height: -40)),
259264
// Disabled due to limit on magnitude in JSON. See SR-5346
260-
// CGRect(origin: CGPoint(x: -.greatestFiniteMagnitude / 2,
261-
// y: -.greatestFiniteMagnitude / 2),
262-
// size: CGSize(width: .greatestFiniteMagnitude,
263-
// height: .greatestFiniteMagnitude)),
265+
// CGRect.infinite
264266
]
265267

266268
func test_CGRect_JSON() {
267269
for rect in cgrectValues {
268270
expectRoundTripEqualityThroughJSON(for: rect)
269271
}
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+
}
270286
}
271287

272288
}

0 commit comments

Comments
 (0)