Skip to content

Commit

Permalink
[Test] Make new OpenAPIValue tests consistent with the rest (#33)
Browse files Browse the repository at this point in the history
[Test] Make new OpenAPIValue tests consistent with the rest

### Motivation

Two new OpenAPIValue tests were using a different style, and had some repetition around encoding/decoding.

### Modifications

Fixed up by bringing them to the same style as other OpenAPIValue tests.

### Result

Consistent style of tests, less code and easier to read.

### Test Plan

All tests passed.


Reviewed by: simonjbeaumont

Builds:
     ✔︎ pull request validation (5.8) - Build finished. 
     ✔︎ pull request validation (5.9) - Build finished. 
     ✔︎ pull request validation (api breakage) - Build finished. 
     ✔︎ pull request validation (docc test) - Build finished. 
     ✔︎ pull request validation (integration test) - Build finished. 
     ✔︎ pull request validation (nightly) - Build finished. 
     ✔︎ pull request validation (soundness) - Build finished. 

#33
  • Loading branch information
czechboy0 authored Aug 4, 2023
1 parent 6a37848 commit d79dbc9
Showing 1 changed file with 12 additions and 31 deletions.
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)
}
}

0 comments on commit d79dbc9

Please sign in to comment.