From c9ed971b8ceaf2d6b3ed93176ea450e69d08af65 Mon Sep 17 00:00:00 2001 From: Apostolos Giokas Date: Thu, 15 Dec 2022 09:55:36 +0100 Subject: [PATCH] Fix Equatable --- Sources/AnyCodable/AnyDecodable.swift | 4 ++ Tests/AnyCodableTests/AnyDecodableTests.swift | 41 +++++++++++++------ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Sources/AnyCodable/AnyDecodable.swift b/Sources/AnyCodable/AnyDecodable.swift index 9b42228..e96a6f3 100644 --- a/Sources/AnyCodable/AnyDecodable.swift +++ b/Sources/AnyCodable/AnyDecodable.swift @@ -114,8 +114,12 @@ extension AnyDecodable: Equatable { return lhs == rhs case let (lhs as [String: AnyDecodable], rhs as [String: AnyDecodable]): return lhs == rhs + case let (lhs as [String: AnyHashable], rhs as [String: AnyHashable]): + return lhs == rhs case let (lhs as [AnyDecodable], rhs as [AnyDecodable]): return lhs == rhs + case let (lhs as [AnyHashable], rhs as [AnyHashable]): + return lhs == rhs default: return false } diff --git a/Tests/AnyCodableTests/AnyDecodableTests.swift b/Tests/AnyCodableTests/AnyDecodableTests.swift index 147dc39..728d7f6 100644 --- a/Tests/AnyCodableTests/AnyDecodableTests.swift +++ b/Tests/AnyCodableTests/AnyDecodableTests.swift @@ -3,7 +3,26 @@ import XCTest class AnyDecodableTests: XCTestCase { func testJSONDecoding() throws { - let json = """ + let dictionary = try createDictionary() + + XCTAssertEqual(dictionary["boolean"]?.value as! Bool, true) + XCTAssertEqual(dictionary["integer"]?.value as! Int, 42) + XCTAssertEqual(dictionary["double"]?.value as! Double, 3.141592653589793, accuracy: 0.001) + XCTAssertEqual(dictionary["string"]?.value as! String, "string") + XCTAssertEqual(dictionary["array"]?.value as! [Int], [1, 2, 3]) + XCTAssertEqual(dictionary["nested"]?.value as! [String: String], ["a": "alpha", "b": "bravo", "c": "charlie"]) + XCTAssertEqual(dictionary["null"]?.value as! NSNull, NSNull()) + } + + func testEquals() throws { + let dictionary1 = try createDictionary() + let dictionary2 = try createDictionary() + + XCTAssertEqual(dictionary1, dictionary2) + } + + private func createDictionary() throws -> [String: AnyDecodable] { + guard let json = """ { "boolean": true, "integer": 42, @@ -17,17 +36,13 @@ class AnyDecodableTests: XCTestCase { }, "null": null } - """.data(using: .utf8)! - - let decoder = JSONDecoder() - let dictionary = try decoder.decode([String: AnyDecodable].self, from: json) - - XCTAssertEqual(dictionary["boolean"]?.value as! Bool, true) - XCTAssertEqual(dictionary["integer"]?.value as! Int, 42) - XCTAssertEqual(dictionary["double"]?.value as! Double, 3.141592653589793, accuracy: 0.001) - XCTAssertEqual(dictionary["string"]?.value as! String, "string") - XCTAssertEqual(dictionary["array"]?.value as! [Int], [1, 2, 3]) - XCTAssertEqual(dictionary["nested"]?.value as! [String: String], ["a": "alpha", "b": "bravo", "c": "charlie"]) - XCTAssertEqual(dictionary["null"]?.value as! NSNull, NSNull()) + """.data(using: .utf8) else { + throw AnyDecodableTestError.toDataConversionFailed + } + return try JSONDecoder().decode([String: AnyDecodable].self, from: json) + } + + private enum AnyDecodableTestError: Error { + case toDataConversionFailed } }