-
-
Couldn't load subscription status.
- Fork 148
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 |
| case cpp_or_c99_bool_objc_encoding, char_objc_encoding, unsigned_char_objc_encoding: | ||
| 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
| case "q": | ||
| case long_long_objc_encoding: | ||
| 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
|
|
||
| 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).
|
|
||
| #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
NSDictionaryis 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? Boolbefore testingas? NSNumber. ANSNumbercan always be casted as aBoolso the later case was never hitSwift.Boolare represented as a char type in ObjC and the mapping was incorrectNSNumber's serialization.NSNumber, which after this fix might be more frequent