Skip to content

Commit a20d25f

Browse files
committed
JSONSerialization tests
- Move JSONSerialization tests into correct testfile and use Int64 to stop tests breaking on 32bit platforms. - Add tests for isValidJSONObject() to test the support of new types. - Add tests for Float, Double, Decimal and NSDecimalNumber.
1 parent 64b67c9 commit a20d25f

File tree

2 files changed

+115
-41
lines changed

2 files changed

+115
-41
lines changed

TestFoundation/TestJSONEncoder.swift

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -405,64 +405,30 @@ class TestJSONEncoder : XCTestCase {
405405
// UInt and Int
406406
func test_codingOfUIntMinMax() {
407407

408-
let encoder = JSONEncoder()
409-
410408
struct MyValue: Codable {
411-
let intMin:Int = Int.min
412-
let intMax:Int = Int.max
413-
let uintMin:UInt = UInt.min
414-
let uintMax:UInt = UInt.max
409+
let intMin = Int64.min
410+
let intMax = Int64.max
411+
let uintMin = UInt64.min
412+
let uintMax = UInt64.max
415413
}
416414

417-
let myValue = MyValue()
418-
let myDictI: [String:Any] = ["intMin": myValue.intMin, "intMax": myValue.intMax]
419-
let myDictU: [String:Any] = ["uintMin": myValue.uintMin, "uintMax": myValue.uintMax]
420-
let myDict1: [String:Any] = ["intMin": myValue.intMin]
421-
let myDict2: [String:Any] = ["intMax": myValue.intMax]
422-
let myDict3: [String:Any] = ["uintMin": myValue.uintMin]
423-
let myDict4: [String:Any] = ["uintMax": myValue.uintMax]
424-
425415
func compareJSON(_ s1: String, _ s2: String) {
426416
let ss1 = s1.trimmingCharacters(in: CharacterSet(charactersIn: "{}")).split(separator: Character(",")).sorted()
427417
let ss2 = s2.trimmingCharacters(in: CharacterSet(charactersIn: "{}")).split(separator: Character(",")).sorted()
428418
XCTAssertEqual(ss1, ss2)
429419
}
430420

431421
do {
422+
let encoder = JSONEncoder()
423+
let myValue = MyValue()
432424
let result = try encoder.encode(myValue)
433425
let r = String(data: result, encoding: .utf8) ?? "nil"
434426
compareJSON(r, "{\"uintMin\":0,\"uintMax\":18446744073709551615,\"intMin\":-9223372036854775808,\"intMax\":9223372036854775807}")
435-
436-
let resultI = try JSONSerialization.data(withJSONObject: myDictI)
437-
let rI = String(data: resultI, encoding: .utf8) ?? "nil"
438-
compareJSON(rI, "{\"intMin\":-9223372036854775808,\"intMax\":9223372036854775807}")
439-
440-
let resultU = try JSONSerialization.data(withJSONObject: myDictU)
441-
let rU = String(data: resultU, encoding: .utf8) ?? "nil"
442-
compareJSON(rU, "{\"uintMax\":18446744073709551615,\"uintMin\":0}")
443-
444-
let result1 = try JSONSerialization.data(withJSONObject: myDict1)
445-
let r1 = String(data: result1, encoding: .utf8) ?? "nil"
446-
XCTAssertEqual(r1, "{\"intMin\":-9223372036854775808}")
447-
448-
let result2 = try JSONSerialization.data(withJSONObject: myDict2)
449-
let r2 = String(data: result2, encoding: .utf8) ?? "nil"
450-
XCTAssertEqual(r2, "{\"intMax\":9223372036854775807}")
451-
452-
let result3 = try JSONSerialization.data(withJSONObject: myDict3)
453-
let r3 = String(data: result3, encoding: .utf8) ?? "nil"
454-
XCTAssertEqual(r3, "{\"uintMin\":0}")
455-
456-
let result4 = try JSONSerialization.data(withJSONObject: myDict4)
457-
let r4 = String(data: result4, encoding: .utf8) ?? "nil"
458-
XCTAssertEqual(r4, "{\"uintMax\":18446744073709551615}")
459427
} catch {
460428
XCTFail(String(describing: error))
461429
}
462430
}
463431

464-
465-
466432
// MARK: - Helper Functions
467433
private var _jsonEmptyDictionary: Data {
468434
return "{}".data(using: .utf8)!

TestFoundation/TestJSONSerialization.swift

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ extension TestJSONSerialization {
829829
return [
830830
("test_isValidJSONObjectTrue", test_isValidJSONObjectTrue),
831831
("test_isValidJSONObjectFalse", test_isValidJSONObjectFalse),
832+
("test_validNumericJSONObjects", test_validNumericJSONObjects)
832833
]
833834
}
834835

@@ -924,6 +925,32 @@ extension TestJSONSerialization {
924925
}
925926
}
926927

928+
func test_validNumericJSONObjects() {
929+
// All of the numeric types supported by JSONSerialization
930+
XCTAssertTrue(JSONSerialization.isValidJSONObject([nil, NSNull()]))
931+
XCTAssertTrue(JSONSerialization.isValidJSONObject([true, false]))
932+
XCTAssertTrue(JSONSerialization.isValidJSONObject([Int.min, Int8.min, Int16.min, Int32.min, Int64.min]))
933+
XCTAssertTrue(JSONSerialization.isValidJSONObject([UInt.min, UInt8.min, UInt16.min, UInt32.min, UInt64.min]))
934+
XCTAssertTrue(JSONSerialization.isValidJSONObject([Float.leastNonzeroMagnitude, Double.leastNonzeroMagnitude]))
935+
936+
XCTAssertTrue(JSONSerialization.isValidJSONObject([NSNumber(value: true), NSNumber(value: Float.greatestFiniteMagnitude), NSNumber(value: Double.greatestFiniteMagnitude)]))
937+
XCTAssertTrue(JSONSerialization.isValidJSONObject([NSNumber(value: Int.max), NSNumber(value: Int8.max), NSNumber(value: Int16.max), NSNumber(value: Int32.max), NSNumber(value: Int64.max)]))
938+
XCTAssertTrue(JSONSerialization.isValidJSONObject([NSNumber(value: UInt.max), NSNumber(value: UInt8.max), NSNumber(value: UInt16.max), NSNumber(value: UInt32.max), NSNumber(value: UInt64.max)]))
939+
XCTAssertTrue(JSONSerialization.isValidJSONObject([NSDecimalNumber(booleanLiteral: true), NSDecimalNumber(decimal: Decimal.greatestFiniteMagnitude), NSDecimalNumber(floatLiteral: Double.greatestFiniteMagnitude), NSDecimalNumber(integerLiteral: Int.min)]))
940+
XCTAssertTrue(JSONSerialization.isValidJSONObject([Decimal(123), Decimal(Double.leastNonzeroMagnitude)]))
941+
942+
XCTAssertFalse(JSONSerialization.isValidJSONObject(Float.nan))
943+
XCTAssertFalse(JSONSerialization.isValidJSONObject(Float.infinity))
944+
XCTAssertFalse(JSONSerialization.isValidJSONObject(NSNumber(value: Float.nan)))
945+
XCTAssertFalse(JSONSerialization.isValidJSONObject(NSNumber(value: Float.infinity)))
946+
947+
XCTAssertFalse(JSONSerialization.isValidJSONObject(Double.nan))
948+
XCTAssertFalse(JSONSerialization.isValidJSONObject(Double.infinity))
949+
XCTAssertFalse(JSONSerialization.isValidJSONObject(NSNumber(value: Double.nan)))
950+
XCTAssertFalse(JSONSerialization.isValidJSONObject(NSNumber(value: Double.infinity)))
951+
952+
XCTAssertFalse(JSONSerialization.isValidJSONObject(NSDecimalNumber(decimal: Decimal(floatLiteral: Double.nan))))
953+
}
927954
}
928955

929956
// MARK: - serializationTests
@@ -941,6 +968,14 @@ extension TestJSONSerialization {
941968
("test_serialize_IntMin", test_serialize_IntMin),
942969
("test_serialize_UIntMax", test_serialize_UIntMax),
943970
("test_serialize_UIntMin", test_serialize_UIntMin),
971+
("test_serialize_8BitSizes", test_serialize_8BitSizes),
972+
("test_serialize_16BitSizes", test_serialize_16BitSizes),
973+
("test_serialize_32BitSizes", test_serialize_32BitSizes),
974+
("test_serialize_64BitSizes", test_serialize_64BitSizes),
975+
("test_serialize_Float", test_serialize_Float),
976+
("test_serialize_Double", test_serialize_Double),
977+
("test_serialize_Decimal", test_serialize_Decimal),
978+
("test_serialize_NSDecimalNumber", test_serialize_NSDecimalNumber),
944979
("test_serialize_stringEscaping", test_serialize_stringEscaping),
945980
("test_jsonReadingOffTheEndOfBuffers", test_jsonReadingOffTheEndOfBuffers),
946981
("test_jsonObjectToOutputStreamBuffer", test_jsonObjectToOutputStreamBuffer),
@@ -1172,7 +1207,80 @@ extension TestJSONSerialization {
11721207
let json: [Any] = [UInt.min]
11731208
XCTAssertEqual(try trySerialize(json), "[\(UInt.min)]")
11741209
}
1175-
1210+
1211+
func test_serialize_8BitSizes() {
1212+
let json1 = [Int8.min, Int8(-1), Int8(0), Int8(1), Int8.max]
1213+
XCTAssertEqual(try trySerialize(json1), "[-128,-1,0,1,127]")
1214+
let json2 = [UInt8.min, UInt8(0), UInt8(1), UInt8.max]
1215+
XCTAssertEqual(try trySerialize(json2), "[0,0,1,255]")
1216+
}
1217+
1218+
func test_serialize_16BitSizes() {
1219+
let json1 = [Int16.min, Int16(-1), Int16(0), Int16(1), Int16.max]
1220+
XCTAssertEqual(try trySerialize(json1), "[-32768,-1,0,1,32767]")
1221+
let json2 = [UInt16.min, UInt16(0), UInt16(1), UInt16.max]
1222+
XCTAssertEqual(try trySerialize(json2), "[0,0,1,65535]")
1223+
}
1224+
1225+
func test_serialize_32BitSizes() {
1226+
let json1 = [Int32.min, Int32(-1), Int32(0), Int32(1), Int32.max]
1227+
XCTAssertEqual(try trySerialize(json1), "[-2147483648,-1,0,1,2147483647]")
1228+
let json2 = [UInt32.min, UInt32(0), UInt32(1), UInt32.max]
1229+
XCTAssertEqual(try trySerialize(json2), "[0,0,1,4294967295]")
1230+
}
1231+
1232+
func test_serialize_64BitSizes() {
1233+
let json1 = [Int64.min, Int64(-1), Int64(0), Int64(1), Int64.max]
1234+
XCTAssertEqual(try trySerialize(json1), "[-9223372036854775808,-1,0,1,9223372036854775807]")
1235+
let json2 = [UInt64.min, UInt64(0), UInt64(1), UInt64.max]
1236+
XCTAssertEqual(try trySerialize(json2), "[0,0,1,18446744073709551615]")
1237+
}
1238+
1239+
func test_serialize_Float() {
1240+
XCTAssertEqual(try trySerialize([-Float.leastNonzeroMagnitude, Float.leastNonzeroMagnitude]), "[-0,0]")
1241+
XCTAssertEqual(try trySerialize([-Float.greatestFiniteMagnitude]), "[-340282346638529000000000000000000000000]")
1242+
XCTAssertEqual(try trySerialize([Float.greatestFiniteMagnitude]), "[340282346638529000000000000000000000000]")
1243+
XCTAssertEqual(try trySerialize([Float(-1), Float.leastNonzeroMagnitude, Float(1)]), "[-1,0,1]")
1244+
}
1245+
1246+
func test_serialize_Double() {
1247+
XCTAssertEqual(try trySerialize([-Double.leastNonzeroMagnitude, Double.leastNonzeroMagnitude]), "[-0,0]")
1248+
XCTAssertEqual(try trySerialize([-Double.leastNormalMagnitude, Double.leastNormalMagnitude]), "[-0,0]")
1249+
XCTAssertEqual(try trySerialize([-Double.greatestFiniteMagnitude]), "[-179769313486232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
1250+
XCTAssertEqual(try trySerialize([Double.greatestFiniteMagnitude]), "[179769313486232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
1251+
XCTAssertEqual(try trySerialize([Double(-1.0), Double(1.0)]), "[-1,1]")
1252+
}
1253+
1254+
func test_serialize_Decimal() {
1255+
XCTAssertEqual(try trySerialize([-Decimal.leastFiniteMagnitude]), "[3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
1256+
XCTAssertEqual(try trySerialize([Decimal.leastFiniteMagnitude]), "[-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
1257+
XCTAssertEqual(try trySerialize([-Decimal.leastNonzeroMagnitude]), "[-0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001]")
1258+
XCTAssertEqual(try trySerialize([Decimal.leastNonzeroMagnitude]), "[0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001]")
1259+
XCTAssertEqual(try trySerialize([-Decimal.greatestFiniteMagnitude]), "[-3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
1260+
XCTAssertEqual(try trySerialize([Decimal.greatestFiniteMagnitude]), "[3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]")
1261+
XCTAssertEqual(try trySerialize([Decimal(Int8.min), Decimal(Int8(0)), Decimal(Int8.max)]), "[-128,0,127]")
1262+
XCTAssertEqual(try trySerialize([Decimal(string: "-0.0"), Decimal(string: "0.000"), Decimal(string: "1.0000")]), "[0,0,1]")
1263+
}
1264+
1265+
func test_serialize_NSDecimalNumber() {
1266+
let dn0: [Any] = [NSDecimalNumber(floatLiteral: -Double.leastNonzeroMagnitude)]
1267+
let dn1: [Any] = [NSDecimalNumber(floatLiteral: Double.leastNonzeroMagnitude)]
1268+
let dn2: [Any] = [NSDecimalNumber(floatLiteral: -Double.leastNormalMagnitude)]
1269+
let dn3: [Any] = [NSDecimalNumber(floatLiteral: Double.leastNormalMagnitude)]
1270+
let dn4: [Any] = [NSDecimalNumber(floatLiteral: -Double.greatestFiniteMagnitude)]
1271+
let dn5: [Any] = [NSDecimalNumber(floatLiteral: Double.greatestFiniteMagnitude)]
1272+
1273+
XCTAssertEqual(try trySerialize(dn0), "[-0.00000000000000000000000000000000000000000000000000000000000000000004940656458412464128]")
1274+
XCTAssertEqual(try trySerialize(dn1), "[0.00000000000000000000000000000000000000000000000000000000000000000004940656458412464128]")
1275+
XCTAssertEqual(try trySerialize(dn2), "[-0.0000000000000000000000000000000000000000000000000002225073858507201792]")
1276+
XCTAssertEqual(try trySerialize(dn3), "[0.0000000000000000000000000000000000000000000000000002225073858507201792]")
1277+
XCTAssertEqual(try trySerialize(dn4), "[-17976931348623167488000000000000000000000000000000000]")
1278+
XCTAssertEqual(try trySerialize(dn5), "[17976931348623167488000000000000000000000000000000000]")
1279+
XCTAssertEqual(try trySerialize([NSDecimalNumber(string: "0.0001"), NSDecimalNumber(string: "0.00"), NSDecimalNumber(string: "-0.0")]), "[0.0001,0,0]")
1280+
XCTAssertEqual(try trySerialize([NSDecimalNumber(integerLiteral: Int(Int16.min)), NSDecimalNumber(integerLiteral: 0), NSDecimalNumber(integerLiteral: Int(Int16.max))]), "[-32768,0,32767]")
1281+
XCTAssertEqual(try trySerialize([NSDecimalNumber(booleanLiteral: true), NSDecimalNumber(booleanLiteral: false)]), "[1,0]")
1282+
}
1283+
11761284
func test_serialize_stringEscaping() {
11771285
var json = ["foo"]
11781286
XCTAssertEqual(try trySerialize(json), "[\"foo\"]")

0 commit comments

Comments
 (0)