Skip to content

Commit

Permalink
pr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Aug 30, 2023
1 parent 8675584 commit 77f78b3
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 54 deletions.
13 changes: 4 additions & 9 deletions Sources/OpenAPIKit/AnyCodable/AnyCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ 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):
return int.description
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"
}
Expand All @@ -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))"
}
}

Expand Down
1 change: 1 addition & 0 deletions Sources/OpenAPIKit/AnyCodable/AnyCodableEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
140 changes: 95 additions & 45 deletions Tests/AnyCodableTests/AnyCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 77f78b3

Please sign in to comment.