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

Fix Equatable #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Sources/AnyCodable/AnyDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
41 changes: 28 additions & 13 deletions Tests/AnyCodableTests/AnyDecodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
}