Skip to content

Commit d79dbc9

Browse files
authored
[Test] Make new OpenAPIValue tests consistent with the rest (#33)
[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
1 parent 6a37848 commit d79dbc9

File tree

1 file changed

+12
-31
lines changed

1 file changed

+12
-31
lines changed

Tests/OpenAPIRuntimeTests/Base/Test_OpenAPIValue.swift

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,12 @@ final class Test_OpenAPIValue: Test_Runtime {
195195
}
196196

197197
func testEncoding_objectNested_success() throws {
198-
199198
struct Foo: Encodable {
200199
var bar: String
201200
var dict: OpenAPIObjectContainer = .init()
202201
}
203-
204-
do {
205-
let value = Foo(
202+
try _testPrettyEncoded(
203+
Foo(
206204
bar: "hi",
207205
dict: try .init(unvalidatedValue: [
208206
"baz": "bar",
@@ -217,13 +215,8 @@ final class Test_OpenAPIValue: Test_Runtime {
217215
"nested": 2
218216
],
219217
])
220-
)
221-
let encoder: JSONEncoder = .init()
222-
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
223-
let data = try encoder.encode(value)
224-
XCTAssertEqual(
225-
String(decoding: data, as: UTF8.self),
226-
#"""
218+
),
219+
expectedJSON: #"""
227220
{
228221
"bar" : "hi",
229222
"dict" : {
@@ -241,20 +234,16 @@ final class Test_OpenAPIValue: Test_Runtime {
241234
}
242235
}
243236
"""#
244-
)
245-
}
237+
)
246238
}
247239

248-
func testDecodeEncodeRoundTrip_objectNested_success() throws {
249-
240+
func testDecoding_objectNested_success() throws {
250241
struct Foo: Codable {
251242
var bar: String
252243
var dict: OpenAPIObjectContainer = .init()
253244
}
254-
255-
do {
256-
let data = Data(
257-
#"""
245+
let decoded: Foo = try _getDecoded(
246+
json: #"""
258247
{
259248
"bar" : "hi",
260249
"dict" : {
@@ -272,17 +261,9 @@ final class Test_OpenAPIValue: Test_Runtime {
272261
}
273262
}
274263
"""#
275-
.utf8
276-
)
277-
let decoded = try JSONDecoder().decode(Foo.self, from: data)
278-
let nestedDict = try XCTUnwrap(decoded.dict.value["nestedDict"] as? [String: Any?])
279-
let nestedValue = try XCTUnwrap(nestedDict["nested"] as? Int)
280-
XCTAssertEqual(nestedValue, 2)
281-
282-
let encoder: JSONEncoder = .init()
283-
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
284-
let encodedData = try encoder.encode(decoded)
285-
XCTAssertEqual(encodedData, data)
286-
}
264+
)
265+
let nestedDict = try XCTUnwrap(decoded.dict.value["nestedDict"] as? [String: Any?])
266+
let nestedValue = try XCTUnwrap(nestedDict["nested"] as? Int)
267+
XCTAssertEqual(nestedValue, 2)
287268
}
288269
}

0 commit comments

Comments
 (0)