Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Equatable fix #71

Merged
merged 2 commits into from
Sep 16, 2022
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
7 changes: 7 additions & 0 deletions Sources/AnyCodable/AnyCodable.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
/**
A type-erased `Codable` value.

Expand Down Expand Up @@ -58,6 +59,12 @@ extension AnyCodable: Equatable {
return lhs == rhs
case let (lhs as [AnyCodable], rhs as [AnyCodable]):
return lhs == rhs
case let (lhs as [String: Any], rhs as [String: Any]):
return NSDictionary(dictionary: lhs) == NSDictionary(dictionary: rhs)
case let (lhs as [Any], rhs as [Any]):
return NSArray(array: lhs) == NSArray(array: rhs)
case is (NSNull, NSNull):
return true
default:
return false
}
Expand Down
30 changes: 30 additions & 0 deletions Tests/AnyCodableTests/AnyCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ class AnyCodableTests: XCTestCase {
XCTAssertEqual(dictionary["null"]?.value as! NSNull, NSNull())
}

func testJSONDecodingEquatable() throws {
let json = """
{
"boolean": true,
"integer": 42,
"double": 3.141592653589793,
"string": "string",
"array": [1, 2, 3],
"nested": {
"a": "alpha",
"b": "bravo",
"c": "charlie"
},
"null": null
}
""".data(using: .utf8)!

let decoder = JSONDecoder()
let dictionary1 = try decoder.decode([String: AnyCodable].self, from: json)
let dictionary2 = try decoder.decode([String: AnyCodable].self, from: json)

XCTAssertEqual(dictionary1["boolean"], dictionary2["boolean"])
XCTAssertEqual(dictionary1["integer"], dictionary2["integer"])
XCTAssertEqual(dictionary1["double"], dictionary2["double"])
XCTAssertEqual(dictionary1["string"], dictionary2["string"])
XCTAssertEqual(dictionary1["array"], dictionary2["array"])
XCTAssertEqual(dictionary1["nested"], dictionary2["nested"])
XCTAssertEqual(dictionary1["null"], dictionary2["null"])
}

func testJSONEncoding() throws {

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