Skip to content

[Test] Make new OpenAPIValue tests consistent with the rest #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2023
Merged
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
43 changes: 12 additions & 31 deletions Tests/OpenAPIRuntimeTests/Base/Test_OpenAPIValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,12 @@ final class Test_OpenAPIValue: Test_Runtime {
}

func testEncoding_objectNested_success() throws {

struct Foo: Encodable {
var bar: String
var dict: OpenAPIObjectContainer = .init()
}

do {
let value = Foo(
try _testPrettyEncoded(
Foo(
bar: "hi",
dict: try .init(unvalidatedValue: [
"baz": "bar",
Expand All @@ -217,13 +215,8 @@ final class Test_OpenAPIValue: Test_Runtime {
"nested": 2
],
])
)
let encoder: JSONEncoder = .init()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let data = try encoder.encode(value)
XCTAssertEqual(
String(decoding: data, as: UTF8.self),
#"""
),
expectedJSON: #"""
{
"bar" : "hi",
"dict" : {
Expand All @@ -241,20 +234,16 @@ final class Test_OpenAPIValue: Test_Runtime {
}
}
"""#
)
}
)
}

func testDecodeEncodeRoundTrip_objectNested_success() throws {

func testDecoding_objectNested_success() throws {
struct Foo: Codable {
var bar: String
var dict: OpenAPIObjectContainer = .init()
}

do {
let data = Data(
#"""
let decoded: Foo = try _getDecoded(
json: #"""
{
"bar" : "hi",
"dict" : {
Expand All @@ -272,17 +261,9 @@ final class Test_OpenAPIValue: Test_Runtime {
}
}
"""#
.utf8
)
let decoded = try JSONDecoder().decode(Foo.self, from: data)
let nestedDict = try XCTUnwrap(decoded.dict.value["nestedDict"] as? [String: Any?])
let nestedValue = try XCTUnwrap(nestedDict["nested"] as? Int)
XCTAssertEqual(nestedValue, 2)

let encoder: JSONEncoder = .init()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let encodedData = try encoder.encode(decoded)
XCTAssertEqual(encodedData, data)
}
)
let nestedDict = try XCTUnwrap(decoded.dict.value["nestedDict"] as? [String: Any?])
let nestedValue = try XCTUnwrap(nestedDict["nested"] as? Int)
XCTAssertEqual(nestedValue, 2)
}
}