-
Notifications
You must be signed in to change notification settings - Fork 49
Additional unit tests for OpenAPIObjectContainer #24
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,4 +193,96 @@ final class Test_OpenAPIValue: Test_Runtime { | |
XCTAssertEqual(value[0] as? String, "one") | ||
XCTAssertEqual(value[1] as? [String: Int], ["two": 2]) | ||
} | ||
|
||
func testEncoding_objectNested_success() throws { | ||
|
||
struct Foo: Encodable { | ||
var bar: String | ||
var dict: OpenAPIObjectContainer = .init() | ||
} | ||
|
||
do { | ||
let value = Foo( | ||
bar: "hi", | ||
dict: try .init(unvalidatedValue: [ | ||
"baz": "bar", | ||
"number": 1, | ||
"nestedArray": [ | ||
1, | ||
[ | ||
"k": "v" | ||
], | ||
] as [(any Sendable)?], | ||
"nestedDict": [ | ||
"nested": 2 | ||
], | ||
]) | ||
) | ||
let encoder: JSONEncoder = .init() | ||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys] | ||
let data = try encoder.encode(value) | ||
XCTAssertEqual( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make it consistent with the other tests by using the utility functions that does the encoding/decoding and equality checks. |
||
String(decoding: data, as: UTF8.self), | ||
#""" | ||
{ | ||
"bar" : "hi", | ||
"dict" : { | ||
"baz" : "bar", | ||
"nestedArray" : [ | ||
1, | ||
{ | ||
"k" : "v" | ||
} | ||
], | ||
"nestedDict" : { | ||
"nested" : 2 | ||
}, | ||
"number" : 1 | ||
} | ||
} | ||
"""# | ||
) | ||
} | ||
} | ||
|
||
func testDecodeEncodeRoundTrip_objectNested_success() throws { | ||
|
||
struct Foo: Codable { | ||
var bar: String | ||
var dict: OpenAPIObjectContainer = .init() | ||
} | ||
|
||
do { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here about the do scope. |
||
let data = Data( | ||
#""" | ||
{ | ||
"bar" : "hi", | ||
"dict" : { | ||
"baz" : "bar", | ||
"nestedArray" : [ | ||
1, | ||
{ | ||
"k" : "v" | ||
} | ||
], | ||
"nestedDict" : { | ||
"nested" : 2 | ||
}, | ||
"number" : 1 | ||
} | ||
} | ||
"""# | ||
.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) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this extra do scope here?