-
-
Notifications
You must be signed in to change notification settings - Fork 136
Improve tests and fix encoding of NSNumber #84
base: master
Are you sure you want to change the base?
Conversation
Also related: #76, but this PR should do a better job at preventing future regressions |
try container.encode(nsnumber.boolValue) | ||
case "c": | ||
try container.encode(nsnumber.int8Value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this case is now mapped to a boolean
try container.encode(nsnumber.int64Value) | ||
case "C": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this case is now mapped to a boolean
@@ -20,10 +20,12 @@ class AnyEncodableTests: XCTestCase { | |||
func testJSONEncoding() throws { | |||
|
|||
let someEncodable = AnyEncodable(SomeEncodable(string: "String", int: 100, bool: true, hasUnderscore: "another string")) | |||
|
|||
let nsNumber = AnyEncodable(1 as NSNumber) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added NSNumber
here to ensure there is no future regressions
|
||
XCTAssertEqual(encodedJSONObject, expectedJSONObject) | ||
XCTAssert(encodedJSONObject["boolean"] is Bool) | ||
|
||
XCTAssert(encodedJSONObject["char"] is Int8) | ||
XCTAssert(encodedJSONObject["int"] is Int16) | ||
XCTAssert(encodedJSONObject["short"] is Int32) | ||
XCTAssert(encodedJSONObject["long"] is Int32) | ||
XCTAssert(encodedJSONObject["longlong"] is Int64) | ||
|
||
XCTAssert(encodedJSONObject["uchar"] is UInt8) | ||
XCTAssert(encodedJSONObject["uint"] is UInt16) | ||
XCTAssert(encodedJSONObject["ushort"] is UInt32) | ||
XCTAssert(encodedJSONObject["ulong"] is UInt32) | ||
XCTAssert(encodedJSONObject["ulonglong"] is UInt64) | ||
|
||
XCTAssert(encodedJSONObject["double"] is Double) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed this part that is not testing decoding
|
||
|
||
|
||
func XCTAssertJsonAreIdentical(_ expression1: String, _ expression2: String, options: JSONSerialization.WritingOptions? = nil) throws { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Helper functions to test that two serialized JSON (either String
or Data
) are equivalent (ie ignoring keys ordering and spaces).
@@ -108,28 +108,24 @@ extension _AnyEncodable { | |||
|
|||
#if canImport(Foundation) | |||
private func encode(nsnumber: NSNumber, into container: inout SingleValueEncodingContainer) throws { | |||
switch Character(Unicode.Scalar(UInt8(nsnumber.objCType.pointee))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for applications where performance matters, this change should help make the code run faster.
Changes
NSDictionary
is very permissive. For instance this test will pass:Instead if we compare the serialized value (
{ "k": true }
) to that of the expectation ({ "k": 1 }
) this test will logically fail.I added util function to ease the comparison of serialized JSON strings.
NSNumber
. There were two issues:as? Bool
before testingas? NSNumber
. ANSNumber
can always be casted as aBool
so the later case was never hitSwift.Bool
are represented as a char type in ObjC and the mapping was incorrectNSNumber
's serialization.NSNumber
, which after this fix might be more frequent