diff --git a/Sources/AnyCodable/AnyCodable.swift b/Sources/AnyCodable/AnyCodable.swift index 7d25ed1..ec41b8a 100644 --- a/Sources/AnyCodable/AnyCodable.swift +++ b/Sources/AnyCodable/AnyCodable.swift @@ -21,49 +21,6 @@ extension AnyCodable: _AnyEncodable, _AnyDecodable {} -extension AnyCodable: Equatable { - public static func == (lhs: AnyCodable, rhs: AnyCodable) -> Bool { - switch (lhs.value, rhs.value) { - case is (Void, Void): - return true - case let (lhs as Bool, rhs as Bool): - return lhs == rhs - case let (lhs as Int, rhs as Int): - return lhs == rhs - case let (lhs as Int8, rhs as Int8): - return lhs == rhs - case let (lhs as Int16, rhs as Int16): - return lhs == rhs - case let (lhs as Int32, rhs as Int32): - return lhs == rhs - case let (lhs as Int64, rhs as Int64): - return lhs == rhs - case let (lhs as UInt, rhs as UInt): - return lhs == rhs - case let (lhs as UInt8, rhs as UInt8): - return lhs == rhs - case let (lhs as UInt16, rhs as UInt16): - return lhs == rhs - case let (lhs as UInt32, rhs as UInt32): - return lhs == rhs - case let (lhs as UInt64, rhs as UInt64): - return lhs == rhs - case let (lhs as Float, rhs as Float): - return lhs == rhs - case let (lhs as Double, rhs as Double): - return lhs == rhs - case let (lhs as String, rhs as String): - return lhs == rhs - case let (lhs as [String: AnyCodable], rhs as [String: AnyCodable]): - return lhs == rhs - case let (lhs as [AnyCodable], rhs as [AnyCodable]): - return lhs == rhs - default: - return false - } - } -} - extension AnyCodable: CustomStringConvertible { public var description: String { switch value { @@ -136,4 +93,8 @@ extension AnyCodable: Hashable { break } } + + public static func ==(lhs: AnyCodable, rhs: AnyCodable) -> Bool { + return lhs.hashValue == rhs.hashValue + } } diff --git a/Sources/AnyCodable/AnyDecodable.swift b/Sources/AnyCodable/AnyDecodable.swift index 9b42228..67e1259 100644 --- a/Sources/AnyCodable/AnyDecodable.swift +++ b/Sources/AnyCodable/AnyDecodable.swift @@ -77,51 +77,6 @@ extension _AnyDecodable { } } -extension AnyDecodable: Equatable { - public static func == (lhs: AnyDecodable, rhs: AnyDecodable) -> Bool { - switch (lhs.value, rhs.value) { -#if canImport(Foundation) - case is (NSNull, NSNull), is (Void, Void): - return true -#endif - case let (lhs as Bool, rhs as Bool): - return lhs == rhs - case let (lhs as Int, rhs as Int): - return lhs == rhs - case let (lhs as Int8, rhs as Int8): - return lhs == rhs - case let (lhs as Int16, rhs as Int16): - return lhs == rhs - case let (lhs as Int32, rhs as Int32): - return lhs == rhs - case let (lhs as Int64, rhs as Int64): - return lhs == rhs - case let (lhs as UInt, rhs as UInt): - return lhs == rhs - case let (lhs as UInt8, rhs as UInt8): - return lhs == rhs - case let (lhs as UInt16, rhs as UInt16): - return lhs == rhs - case let (lhs as UInt32, rhs as UInt32): - return lhs == rhs - case let (lhs as UInt64, rhs as UInt64): - return lhs == rhs - case let (lhs as Float, rhs as Float): - return lhs == rhs - case let (lhs as Double, rhs as Double): - return lhs == rhs - case let (lhs as String, rhs as String): - return lhs == rhs - case let (lhs as [String: AnyDecodable], rhs as [String: AnyDecodable]): - return lhs == rhs - case let (lhs as [AnyDecodable], rhs as [AnyDecodable]): - return lhs == rhs - default: - return false - } - } -} - extension AnyDecodable: CustomStringConvertible { public var description: String { switch value { @@ -185,4 +140,8 @@ extension AnyDecodable: Hashable { break } } + + public static func ==(lhs: AnyDecodable, rhs: AnyDecodable) -> Bool { + return lhs.hashValue == rhs.hashValue + } } diff --git a/Sources/AnyCodable/AnyEncodable.swift b/Sources/AnyCodable/AnyEncodable.swift index bda5cfa..1647820 100644 --- a/Sources/AnyCodable/AnyEncodable.swift +++ b/Sources/AnyCodable/AnyEncodable.swift @@ -137,49 +137,6 @@ extension _AnyEncodable { #endif } -extension AnyEncodable: Equatable { - public static func == (lhs: AnyEncodable, rhs: AnyEncodable) -> Bool { - switch (lhs.value, rhs.value) { - case is (Void, Void): - return true - case let (lhs as Bool, rhs as Bool): - return lhs == rhs - case let (lhs as Int, rhs as Int): - return lhs == rhs - case let (lhs as Int8, rhs as Int8): - return lhs == rhs - case let (lhs as Int16, rhs as Int16): - return lhs == rhs - case let (lhs as Int32, rhs as Int32): - return lhs == rhs - case let (lhs as Int64, rhs as Int64): - return lhs == rhs - case let (lhs as UInt, rhs as UInt): - return lhs == rhs - case let (lhs as UInt8, rhs as UInt8): - return lhs == rhs - case let (lhs as UInt16, rhs as UInt16): - return lhs == rhs - case let (lhs as UInt32, rhs as UInt32): - return lhs == rhs - case let (lhs as UInt64, rhs as UInt64): - return lhs == rhs - case let (lhs as Float, rhs as Float): - return lhs == rhs - case let (lhs as Double, rhs as Double): - return lhs == rhs - case let (lhs as String, rhs as String): - return lhs == rhs - case let (lhs as [String: AnyEncodable], rhs as [String: AnyEncodable]): - return lhs == rhs - case let (lhs as [AnyEncodable], rhs as [AnyEncodable]): - return lhs == rhs - default: - return false - } - } -} - extension AnyEncodable: CustomStringConvertible { public var description: String { switch value { @@ -286,4 +243,8 @@ extension AnyEncodable: Hashable { break } } + + public static func ==(lhs: AnyEncodable, rhs: AnyEncodable) -> Bool { + return lhs.hashValue == rhs.hashValue + } } diff --git a/Tests/AnyCodableTests/AnyCodableTests.swift b/Tests/AnyCodableTests/AnyCodableTests.swift index dbc5d11..3508e8c 100644 --- a/Tests/AnyCodableTests/AnyCodableTests.swift +++ b/Tests/AnyCodableTests/AnyCodableTests.swift @@ -2,6 +2,28 @@ import XCTest class AnyCodableTests: XCTestCase { + + func testEquivalency() { + let boolean = true + let integer = 42 + let double = 3.141592653589793 + let string = "string" + let array = [1, 2, 3] + let nested = [ + "a": "alpha", + "b": "bravo", + "c": "charlie", + ] + XCTAssertEqual(AnyCodable(boolean), AnyCodable(boolean)) + XCTAssertEqual(AnyCodable(integer), AnyCodable(integer)) + XCTAssertEqual(AnyCodable(double), AnyCodable(double)) + XCTAssertEqual(AnyCodable(string), AnyCodable(string)) + XCTAssertEqual(AnyCodable(array), AnyCodable(array)) + XCTAssertEqual(AnyCodable(nested), AnyCodable(nested)) + XCTAssertNotEqual(AnyCodable(array), AnyCodable([1, 2, 4])) + XCTAssertNotEqual(AnyCodable(nested), AnyCodable(["a": "apple", "b": "banana", "c": "cherry"])) + } + func testJSONDecoding() throws { let json = """ { diff --git a/Tests/AnyCodableTests/AnyDecodableTests.swift b/Tests/AnyCodableTests/AnyDecodableTests.swift index 147dc39..c67bebf 100644 --- a/Tests/AnyCodableTests/AnyDecodableTests.swift +++ b/Tests/AnyCodableTests/AnyDecodableTests.swift @@ -2,6 +2,28 @@ import XCTest class AnyDecodableTests: XCTestCase { + + func testEquivalency() { + let boolean = true + let integer = 42 + let double = 3.141592653589793 + let string = "string" + let array = [1, 2, 3] + let nested = [ + "a": "alpha", + "b": "bravo", + "c": "charlie", + ] + XCTAssertEqual(AnyDecodable(boolean), AnyDecodable(boolean)) + XCTAssertEqual(AnyDecodable(integer), AnyDecodable(integer)) + XCTAssertEqual(AnyDecodable(double), AnyDecodable(double)) + XCTAssertEqual(AnyDecodable(string), AnyDecodable(string)) + XCTAssertEqual(AnyDecodable(array), AnyDecodable(array)) + XCTAssertEqual(AnyDecodable(nested), AnyDecodable(nested)) + XCTAssertNotEqual(AnyDecodable(array), AnyDecodable([1, 2, 4])) + XCTAssertNotEqual(AnyDecodable(nested), AnyDecodable(["a": "apple", "b": "banana", "c": "cherry"])) + } + func testJSONDecoding() throws { let json = """ { diff --git a/Tests/AnyCodableTests/AnyEncodableTests.swift b/Tests/AnyCodableTests/AnyEncodableTests.swift index cb08818..eba5dff 100644 --- a/Tests/AnyCodableTests/AnyEncodableTests.swift +++ b/Tests/AnyCodableTests/AnyEncodableTests.swift @@ -2,6 +2,28 @@ import XCTest class AnyEncodableTests: XCTestCase { + + func testEquivalency() { + let boolean = true + let integer = 42 + let double = 3.141592653589793 + let string = "string" + let array = [1, 2, 3] + let nested = [ + "a": "alpha", + "b": "bravo", + "c": "charlie", + ] + XCTAssertEqual(AnyEncodable(boolean), AnyEncodable(boolean)) + XCTAssertEqual(AnyEncodable(integer), AnyEncodable(integer)) + XCTAssertEqual(AnyEncodable(double), AnyEncodable(double)) + XCTAssertEqual(AnyEncodable(string), AnyEncodable(string)) + XCTAssertEqual(AnyEncodable(array), AnyEncodable(array)) + XCTAssertEqual(AnyEncodable(nested), AnyEncodable(nested)) + XCTAssertNotEqual(AnyEncodable(array), AnyEncodable([1, 2, 4])) + XCTAssertNotEqual(AnyEncodable(nested), AnyEncodable(["a": "apple", "b": "banana", "c": "cherry"])) + } + func testJSONEncoding() throws { let dictionary: [String: AnyEncodable] = [ "boolean": true,