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

Commit

Permalink
Equatable fix (#71)
Browse files Browse the repository at this point in the history
* fix

* revert change of (lhs as [AnyCodable], rhs as [AnyCodable])
  • Loading branch information
ytyubox authored Sep 16, 2022
1 parent f9fda69 commit 28c3246
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
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

0 comments on commit 28c3246

Please sign in to comment.