From 77f78b3e50ae9cb3beded5bbd34fb48bd8612d6e Mon Sep 17 00:00:00 2001 From: dankinsoid <30962149+dankinsoid@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:09:23 +0400 Subject: [PATCH] pr fixes --- .../OpenAPIKit/AnyCodable/AnyCodable.swift | 13 +- .../AnyCodable/AnyCodableEncoder.swift | 1 + Tests/AnyCodableTests/AnyCodableTests.swift | 140 ++++++++++++------ 3 files changed, 100 insertions(+), 54 deletions(-) diff --git a/Sources/OpenAPIKit/AnyCodable/AnyCodable.swift b/Sources/OpenAPIKit/AnyCodable/AnyCodable.swift index 53563b810..93923271d 100644 --- a/Sources/OpenAPIKit/AnyCodable/AnyCodable.swift +++ b/Sources/OpenAPIKit/AnyCodable/AnyCodable.swift @@ -87,7 +87,7 @@ extension AnyCodable: CustomStringConvertible { public var description: String { switch self { case .string(let string): - return string.description + return "\"\(string.description)\"" case .bool(let bool): return bool.description case .int(let int): @@ -95,9 +95,9 @@ extension AnyCodable: CustomStringConvertible { case .double(let double): return double.description case .object(let dictionary): - return dictionary.description + return "[\(dictionary.sorted(by: { $0.key < $1.key }).map { "\"\($0)\": \($1.description)" }.joined(separator: ", "))]" case .array(let array): - return array.description + return "[\(array.map { $0.description }.joined(separator: ", "))]" case .null: return "nil" } @@ -106,12 +106,7 @@ extension AnyCodable: CustomStringConvertible { extension AnyCodable: CustomDebugStringConvertible { public var debugDescription: String { - switch value { - case let value as CustomDebugStringConvertible: - return "AnyCodable(\(value.debugDescription))" - default: - return "AnyCodable(\(description))" - } + "AnyCodable(\(description))" } } diff --git a/Sources/OpenAPIKit/AnyCodable/AnyCodableEncoder.swift b/Sources/OpenAPIKit/AnyCodable/AnyCodableEncoder.swift index 67b220c6f..7852ae3ae 100644 --- a/Sources/OpenAPIKit/AnyCodable/AnyCodableEncoder.swift +++ b/Sources/OpenAPIKit/AnyCodable/AnyCodableEncoder.swift @@ -8,6 +8,7 @@ public extension AnyCodable { /// - dateFormat: The date encoding format to use. /// - keyEncodingStrategy: The key encoding strategy to use. /// - Returns: A new instance of `AnyCodable` or `nil` if the given value cannot be encoded. + @_disfavoredOverload init?( _ value: Encodable, dateFormat: DateEncodingFormat = .default, diff --git a/Tests/AnyCodableTests/AnyCodableTests.swift b/Tests/AnyCodableTests/AnyCodableTests.swift index 4632d02ab..decc94faf 100644 --- a/Tests/AnyCodableTests/AnyCodableTests.swift +++ b/Tests/AnyCodableTests/AnyCodableTests.swift @@ -2,46 +2,54 @@ import XCTest class AnyCodableTests: XCTestCase { - func test_init() throws { - let _ = AnyCodable("hi") - let _: AnyCodable = nil - let _: AnyCodable = true - let _: AnyCodable = 10 - let _: AnyCodable = 3.4 - let _: AnyCodable = "hello" - let _: AnyCodable = ["hi", "there"] - let _: AnyCodable = ["hi": "there"] - } - func test_equality() throws { - XCTAssertEqual(AnyCodable(()), AnyCodable(())) - XCTAssertEqual(AnyCodable(true), AnyCodable(true)) - XCTAssertEqual(AnyCodable(2), AnyCodable(2)) - XCTAssertEqual(AnyCodable(Int8(2)), AnyCodable(Int8(2))) - XCTAssertEqual(AnyCodable(Int16(2)), AnyCodable(Int16(2))) - XCTAssertEqual(AnyCodable(Int32(2)), AnyCodable(Int32(2))) - XCTAssertEqual(AnyCodable(Int64(2)), AnyCodable(Int64(2))) - XCTAssertEqual(AnyCodable(UInt(2)), AnyCodable(UInt(2))) - XCTAssertEqual(AnyCodable(UInt8(2)), AnyCodable(UInt8(2))) - XCTAssertEqual(AnyCodable(UInt16(2)), AnyCodable(UInt16(2))) - XCTAssertEqual(AnyCodable(UInt32(2)), AnyCodable(UInt32(2))) - XCTAssertEqual(AnyCodable(UInt64(2)), AnyCodable(UInt64(2))) - XCTAssertEqual(AnyCodable(Float(2)), AnyCodable(Float(2))) - XCTAssertEqual(AnyCodable(Double(2)), AnyCodable(Double(2))) - XCTAssertEqual(AnyCodable("hi"), AnyCodable("hi")) - XCTAssertEqual(AnyCodable(["hi": AnyCodable(2)]), AnyCodable(["hi": AnyCodable(2)])) - XCTAssertEqual(AnyCodable([AnyCodable("hi"), AnyCodable("there")]), AnyCodable([AnyCodable("hi"), AnyCodable("there")])) - XCTAssertEqual(AnyCodable(["hi": 1]), AnyCodable(["hi": 1])) - XCTAssertEqual(AnyCodable(["hi": 1.2]), AnyCodable(["hi": 1.2])) - XCTAssertEqual(AnyCodable(["hi"]), AnyCodable(["hi"])) - XCTAssertEqual(AnyCodable([1]), AnyCodable([1])) - XCTAssertEqual(AnyCodable([1.2]), AnyCodable([1.2])) - XCTAssertEqual(AnyCodable([true]), AnyCodable([true])) - - XCTAssertNotEqual(AnyCodable(()), AnyCodable(true)) - XCTAssertNotEqual(AnyCodable(()), AnyCodable(2)) - XCTAssertNotEqual(AnyCodable(4), AnyCodable("hi")) - XCTAssertNotEqual(AnyCodable("hi"), AnyCodable(["hi"])) + XCTAssertEqual(AnyCodable.null, AnyCodable.null) + XCTAssertEqual(AnyCodable.bool(true), AnyCodable.bool(true)) + XCTAssertEqual(AnyCodable.int(2), AnyCodable.int(2)) + XCTAssertEqual(AnyCodable.double(2), AnyCodable.double(2)) + XCTAssertEqual(AnyCodable.string("hi"), AnyCodable.string("hi")) + XCTAssertEqual(AnyCodable.object(["hi": .int(2)]), AnyCodable.object(["hi": .int(2)])) + XCTAssertEqual(AnyCodable.array([.string("hi")]), AnyCodable.array([.string("hi")])) + XCTAssertEqual(AnyCodable.array([.int(1)]), AnyCodable.array([.int(1)])) + + XCTAssertNotEqual(AnyCodable.null, AnyCodable.bool(true)) + XCTAssertNotEqual(AnyCodable.null, AnyCodable.int(2)) + XCTAssertNotEqual(AnyCodable.int(4), AnyCodable.string("hi")) + XCTAssertNotEqual(AnyCodable.string("hi"), AnyCodable.array([.string("hi")])) + XCTAssertNotEqual(AnyCodable.object(["hi": .int(2)]), AnyCodable.object(["hi": .double(3)])) + } + + func test_inits() throws { + XCTAssertEqual(AnyCodable(()), AnyCodable.null) + XCTAssertEqual(AnyCodable(true), AnyCodable.bool(true)) + XCTAssertEqual(AnyCodable(2), AnyCodable.int(2)) + XCTAssertEqual(AnyCodable(Int8(2)), AnyCodable.int(2)) + XCTAssertEqual(AnyCodable(Int16(2)), AnyCodable.int(2)) + XCTAssertEqual(AnyCodable(Int32(2)), AnyCodable.int(2)) + XCTAssertEqual(AnyCodable(Int64(2)), AnyCodable.int(2)) + XCTAssertEqual(AnyCodable(UInt(2)), AnyCodable.int(2)) + XCTAssertEqual(AnyCodable(UInt8(2)), AnyCodable.int(2)) + XCTAssertEqual(AnyCodable(UInt16(2)), AnyCodable.int(2)) + XCTAssertEqual(AnyCodable(UInt32(2)), AnyCodable.int(2)) + XCTAssertEqual(AnyCodable(UInt64(2)), AnyCodable.int(2)) + XCTAssertEqual(AnyCodable(Float(2)), AnyCodable.double(2)) + XCTAssertEqual(AnyCodable(Double(2)), AnyCodable.double(2)) + XCTAssertEqual(AnyCodable("hi"), AnyCodable.string("hi")) + XCTAssertEqual(AnyCodable(["hi": 2]), AnyCodable.object(["hi": .int(2)])) + XCTAssertEqual(AnyCodable(["hi", "there"]), AnyCodable.array([.string("hi"), .string("there")])) + XCTAssertEqual(AnyCodable(["hi": 1]), AnyCodable.object(["hi": .int(1)])) + XCTAssertEqual(AnyCodable([1]), AnyCodable.array([.int(1)])) + XCTAssertEqual(AnyCodable([1.2]), AnyCodable.array([.double(1.2)])) + XCTAssertEqual(AnyCodable([true]), AnyCodable.array([.bool(true)])) + } + + func test_expressible() throws { + XCTAssertEqual(AnyCodable.string("hi"), "hi") + XCTAssertEqual(AnyCodable.bool(true), true) + XCTAssertEqual(AnyCodable.null, nil) + XCTAssertEqual(AnyCodable.int(2), 2) + XCTAssertEqual(AnyCodable.double(3.4), 3.4) + XCTAssertEqual(AnyCodable.object(["hi": .string("there")]), ["hi": "there"]) } func test_equalityFromJSON() throws { @@ -233,6 +241,26 @@ class AnyCodableTests: XCTestCase { let decodedValue = try JSONDecoder().decode(EncodableStruct.self, from: data) XCTAssertEqual(decodedValue, EncodableStruct()) } + + func test_dateEncodableInit() throws { + let anyCodable = AnyCodable( + EncodableStruct(), + dateFormat: .default, + keyEncodingStrategy: .default + ) + let expectedAnyCodable: AnyCodable = [ + "int": 1, + "string": "hello", + "bool": true, + "array": [1, 2, 3], + "dictionary": ["a": 1, "b": 2, "c": 3], + ] + XCTAssertEqual(anyCodable, expectedAnyCodable) + + let data = try JSONEncoder().encode(anyCodable) + let decodedValue = try JSONDecoder().decode(EncodableStruct.self, from: data) + XCTAssertEqual(decodedValue, EncodableStruct()) + } func test_keyEncodingStrategy() throws { let camelCaseString = "thisIsCamelCase" @@ -251,25 +279,25 @@ class AnyCodableTests: XCTestCase { let expectedAnyCodable: AnyCodable = 0.0 XCTAssertEqual(anyCodable, expectedAnyCodable) - let data = try JSONEncoder().encode(anyCodable) + let data = try JSONEncoder().encode([anyCodable]) // Use array for swift 5.1 support let string = String(data: data, encoding: .utf8) - XCTAssertEqual(string, #"0"#) + XCTAssertEqual(string, "[0]") let anyCodable3 = try AnyCodable.encodable(date, dateFormat: .dateTime) let expectedAnyCodable3: AnyCodable = "1970-01-01T00:00:00.000Z" XCTAssertEqual(anyCodable3, expectedAnyCodable3) - let data3 = try JSONEncoder().encode(anyCodable3) + let data3 = try JSONEncoder().encode([anyCodable3]) // Use array for swift 5.1 support let string3 = String(data: data3, encoding: .utf8) - XCTAssertEqual(string3, #""1970-01-01T00:00:00.000Z""#) + XCTAssertEqual(string3, #"["1970-01-01T00:00:00.000Z"]"#) let anyCodable4 = try AnyCodable.encodable(date, dateFormat: .date) let expectedAnyCodable4: AnyCodable = "1970-01-01" XCTAssertEqual(anyCodable4, expectedAnyCodable4) - let data4 = try JSONEncoder().encode(anyCodable4) + let data4 = try JSONEncoder().encode([anyCodable4]) // Use array for swift 5.1 support let string4 = String(data: data4, encoding: .utf8) - XCTAssertEqual(string4, #""1970-01-01""#) + XCTAssertEqual(string4, #"["1970-01-01"]"#) } func test_RefInit() { @@ -298,6 +326,28 @@ class AnyCodableTests: XCTestCase { ref.wrappedValue = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" XCTAssertEqual(ref.wrappedValue, "yyyy-MM-dd'T'HH:mm:ss.SSSZ") } + + func test_description() { + XCTAssertEqual(AnyCodable(0).description, "0") + XCTAssertEqual(AnyCodable(0.0).description, "0.0") + XCTAssertEqual(AnyCodable(()).description, "nil") + XCTAssertEqual(AnyCodable("hello").description, "\"hello\"") + XCTAssertEqual(AnyCodable(true).description, "true") + XCTAssertEqual(AnyCodable(false).description, "false") + XCTAssertEqual(AnyCodable([1, 2, 3]).description, "[1, 2, 3]") + XCTAssertEqual(AnyCodable(["a": 1, "b": 2]).description, "[\"a\": 1, \"b\": 2]") + } + + func test_debugDescription() { + XCTAssertEqual(AnyCodable(0).debugDescription, "AnyCodable(0)") + XCTAssertEqual(AnyCodable(0.0).debugDescription, "AnyCodable(0.0)") + XCTAssertEqual(AnyCodable(()).debugDescription, "AnyCodable(nil)") + XCTAssertEqual(AnyCodable("hello").debugDescription, "AnyCodable(\"hello\")") + XCTAssertEqual(AnyCodable(true).debugDescription, "AnyCodable(true)") + XCTAssertEqual(AnyCodable(false).debugDescription, "AnyCodable(false)") + XCTAssertEqual(AnyCodable([1, 2, 3]).debugDescription, "AnyCodable([1, 2, 3])") + XCTAssertEqual(AnyCodable(["a": 1, "b": 2]).debugDescription, "AnyCodable([\"a\": 1, \"b\": 2])") + } } private struct Wrapper: Codable {