Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/ParseSwift/Coding/AnyDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Foundation
let json = """
{
"boolean": true,
"integer": 1,
"integer": 42,
"double": 3.14159265358979323846,
"string": "string",
"array": [1, 2, 3],
Expand Down
7 changes: 5 additions & 2 deletions Sources/ParseSwift/Coding/AnyEncodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import Foundation

let dictionary: [String: AnyEncodable] = [
"boolean": true,
"integer": 1,
"integer": 42,
"double": 3.14159265358979323846,
"string": "string",
"array": [1, 2, 3],
"nested": [
"a": "alpha",
"b": "bravo",
"c": "charlie"
]
],
"null": nil
]

let encoder = JSONEncoder()
Expand Down Expand Up @@ -99,6 +100,8 @@ extension _AnyEncodable {
try container.encode(array.map { AnyEncodable($0) })
case let dictionary as [String: Any?]:
try container.encode(dictionary.mapValues { AnyEncodable($0) })
case let encodable as Encodable:
try encodable.encode(to: encoder)
default:
let context = EncodingError.Context(codingPath: container.codingPath,
debugDescription: "AnyEncodable value cannot be encoded")
Expand Down
41 changes: 36 additions & 5 deletions Tests/ParseSwiftTests/AnyCodableTests/AnyCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ import XCTest
@testable import ParseSwift

class AnyCodableTests: XCTestCase {

struct SomeCodable: Codable {
var string: String
var int: Int
var bool: Bool
var hasUnderscore: String

// swiftlint:disable:next nesting
enum CodingKeys: String, CodingKey {
case string
case int
case bool
case hasUnderscore = "has_underscore"
}
}

func testJSONDecoding() {
guard let json = """
{
Expand Down Expand Up @@ -42,17 +58,25 @@ class AnyCodableTests: XCTestCase {
//Test has objective-c
#if !os(Linux) && !os(Android) && !os(Windows)
func testJSONEncoding() {

let someCodable = AnyCodable(SomeCodable(string: "String",
int: 100,
bool: true,
hasUnderscore: "another string"))

let dictionary: [String: AnyCodable] = [
"boolean": true,
"integer": 1,
"integer": 42,
"double": 3.14159265358979323846,
"string": "string",
"array": [1, 2, 3],
"nested": [
"a": "alpha",
"b": "bravo",
"c": "charlie"
]
],
"someCodable": someCodable,
"null": nil
]
do {
let encoder = JSONEncoder()
Expand All @@ -64,16 +88,23 @@ class AnyCodableTests: XCTestCase {
}
guard let expected = """
{
"boolean": true,
"integer": 1,
"boolean": 1,
"integer": 42,
"double": 3.14159265358979323846,
"string": "string",
"array": [1, 2, 3],
"nested": {
"a": "alpha",
"b": "bravo",
"c": "charlie"
}
},
"someCodable": {
"string":"String",
"int":100,
"bool": true,
"has_underscore":"another string"
},
"null": null
}
""".data(using: .utf8) else {
XCTFail("Should unrap data to utf8")
Expand Down
143 changes: 137 additions & 6 deletions Tests/ParseSwiftTests/AnyCodableTests/AnyEncodableTests.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
import XCTest
@testable import ParseSwift

//Test has objective-c
// Test has objective-c
#if !os(Linux) && !os(Android) && !os(Windows)
class AnyEncodableTests: XCTestCase {

struct SomeEncodable: Encodable {
var string: String
var int: Int
var bool: Bool
var hasUnderscore: String

// swiftlint:disable:next nesting
enum CodingKeys: String, CodingKey {
case string
case int
case bool
case hasUnderscore = "has_underscore"
}
}

func testJSONEncoding() {

let someEncodable = AnyEncodable(SomeEncodable(string: "String",
int: 100,
bool: true,
hasUnderscore: "another string"))

let dictionary: [String: AnyEncodable] = [
"boolean": true,
"integer": 1,
"integer": 42,
"double": 3.14159265358979323846,
"string": "string",
"array": [1, 2, 3],
"nested": [
"a": "alpha",
"b": "bravo",
"c": "charlie"
]
],
"someCodable": someEncodable,
"null": nil
]

do {
let encoder = JSONEncoder()
let json = try encoder.encode(dictionary)
Expand All @@ -27,16 +52,23 @@ class AnyEncodableTests: XCTestCase {
}
guard let expected = """
{
"boolean": true,
"integer": 1,
"boolean": 1,
"integer": 42,
"double": 3.14159265358979323846,
"string": "string",
"array": [1, 2, 3],
"nested": {
"a": "alpha",
"b": "bravo",
"c": "charlie"
}
},
"someCodable": {
"string": "String",
"int": 100,
"bool": true,
"has_underscore": "another string"
},
"null": null
}
""".data(using: .utf8) else {
XCTFail("Should unrap data to utf8")
Expand All @@ -52,5 +84,104 @@ class AnyEncodableTests: XCTestCase {
XCTFail(error.localizedDescription)
}
}

func testEncodeNSNumber() throws {
let dictionary: [String: NSNumber] = [
"boolean": true,
"char": -127,
"int": -32767,
"short": -32767,
"long": -2147483647,
"longlong": -9223372036854775807,
"uchar": 255,
"uint": 65535,
"ushort": 65535,
"ulong": 4294967295,
"ulonglong": 18446744073709615,
"double": 3.141592653589793
]

let encoder = JSONEncoder()

let json = try encoder.encode(AnyEncodable(dictionary))
guard let encodedJSONObject = try JSONSerialization.jsonObject(with: json, options: []) as? NSDictionary else {
XCTFail("Should have unwrapped")
return
}

let expected = """
{
"boolean": 1,
"char": -127,
"int": -32767,
"short": -32767,
"long": -2147483647,
"longlong": -9223372036854775807,
"uchar": 255,
"uint": 65535,
"ushort": 65535,
"ulong": 4294967295,
"ulonglong": 18446744073709615,
"double": 3.141592653589793,
}
""".data(using: .utf8)!
// swiftlint:disable:next line_length
guard let expectedJSONObject = try JSONSerialization.jsonObject(with: expected, options: []) as? NSDictionary else {
XCTFail("Should have unwrapped")
return
}

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)
}

func testStringInterpolationEncoding() throws {
let dictionary: [String: AnyEncodable] = [
"boolean": "\(true)",
"integer": "\(42)",
"double": "\(3.141592653589793)",
"string": "\("string")",
"array": "\([1, 2, 3])"
]

let encoder = JSONEncoder()

let json = try encoder.encode(dictionary)
guard let encodedJSONObject = try JSONSerialization.jsonObject(with: json, options: []) as? NSDictionary else {
XCTFail("Should have unwrapped")
return
}

let expected = """
{
"boolean": "true",
"integer": "42",
"double": "3.141592653589793",
"string": "string",
"array": "[1, 2, 3]",
}
""".data(using: .utf8)!
// swiftlint:disable:next line_length
guard let expectedJSONObject = try JSONSerialization.jsonObject(with: expected, options: []) as? NSDictionary else {
XCTFail("Should have unwrapped")
return
}

XCTAssertEqual(encodedJSONObject, expectedJSONObject)
}
}
#endif