Skip to content

Commit 3aae2c7

Browse files
committed
Update tests for URICoding
1 parent 6bdd19a commit 3aae2c7

File tree

2 files changed

+97
-32
lines changed

2 files changed

+97
-32
lines changed

Tests/OpenAPIRuntimeTests/URICoder/Parsing/Test_URIParser.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ final class Test_URIParser: Test_Runtime {
121121
)
122122
} catch {
123123
guard let expectedError = input.expectedError,
124-
let serializationError = error as? ParsingError else {
124+
let parsingError = error as? ParsingError else {
125125
XCTAssert(
126126
false,
127127
"Unexpected error thrown: \(error)",
@@ -132,7 +132,7 @@ final class Test_URIParser: Test_Runtime {
132132
}
133133
XCTAssertEqual(
134134
expectedError,
135-
serializationError,
135+
parsingError,
136136
"Failed for config: \(variant.name)",
137137
file: testCase.file,
138138
line: testCase.line

Tests/OpenAPIRuntimeTests/URICoder/Test_URICodingRoundtrip.swift

+95-30
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ final class Test_URICodingRoundtrip: Test_Runtime {
9797
simpleUnexplode: "",
9898
formDataExplode: "root=",
9999
formDataUnexplode: "root=",
100-
deepObjectExplode: "root="
100+
deepObjectExplode: .custom(
101+
"root=",
102+
expectedError: .deepObjectsWithPrimitiveValuesNotSupported
103+
)
101104
)
102105
)
103106

@@ -112,7 +115,10 @@ final class Test_URICodingRoundtrip: Test_Runtime {
112115
simpleUnexplode: "Hello%20World%21",
113116
formDataExplode: "root=Hello+World%21",
114117
formDataUnexplode: "root=Hello+World%21",
115-
deepObjectExplode: "root=Hello%20World%21"
118+
deepObjectExplode: .custom(
119+
"root=Hello%20World%21",
120+
expectedError: .deepObjectsWithPrimitiveValuesNotSupported
121+
)
116122
)
117123
)
118124

@@ -127,7 +133,10 @@ final class Test_URICodingRoundtrip: Test_Runtime {
127133
simpleUnexplode: "red",
128134
formDataExplode: "root=red",
129135
formDataUnexplode: "root=red",
130-
deepObjectExplode: "root=red"
136+
deepObjectExplode: .custom(
137+
"root=red",
138+
expectedError: .deepObjectsWithPrimitiveValuesNotSupported
139+
)
131140
)
132141
)
133142

@@ -142,7 +151,10 @@ final class Test_URICodingRoundtrip: Test_Runtime {
142151
simpleUnexplode: "1234",
143152
formDataExplode: "root=1234",
144153
formDataUnexplode: "root=1234",
145-
deepObjectExplode: "root=1234"
154+
deepObjectExplode: .custom(
155+
"root=1234",
156+
expectedError: .deepObjectsWithPrimitiveValuesNotSupported
157+
)
146158
)
147159
)
148160

@@ -157,7 +169,10 @@ final class Test_URICodingRoundtrip: Test_Runtime {
157169
simpleUnexplode: "12.34",
158170
formDataExplode: "root=12.34",
159171
formDataUnexplode: "root=12.34",
160-
deepObjectExplode: "root=12.34"
172+
deepObjectExplode: .custom(
173+
"root=12.34",
174+
expectedError: .deepObjectsWithPrimitiveValuesNotSupported
175+
)
161176
)
162177
)
163178

@@ -172,7 +187,10 @@ final class Test_URICodingRoundtrip: Test_Runtime {
172187
simpleUnexplode: "true",
173188
formDataExplode: "root=true",
174189
formDataUnexplode: "root=true",
175-
deepObjectExplode: "root=true"
190+
deepObjectExplode: .custom(
191+
"root=true",
192+
expectedError: .deepObjectsWithPrimitiveValuesNotSupported
193+
)
176194
)
177195
)
178196

@@ -187,7 +205,10 @@ final class Test_URICodingRoundtrip: Test_Runtime {
187205
simpleUnexplode: "2023-08-25T07%3A34%3A59Z",
188206
formDataExplode: "root=2023-08-25T07%3A34%3A59Z",
189207
formDataUnexplode: "root=2023-08-25T07%3A34%3A59Z",
190-
deepObjectExplode: "root=2023-08-25T07%3A34%3A59Z"
208+
deepObjectExplode: .custom(
209+
"root=2023-08-25T07%3A34%3A59Z",
210+
expectedError: .deepObjectsWithPrimitiveValuesNotSupported
211+
)
191212
)
192213
)
193214

@@ -202,7 +223,10 @@ final class Test_URICodingRoundtrip: Test_Runtime {
202223
simpleUnexplode: "a,b,c",
203224
formDataExplode: "list=a&list=b&list=c",
204225
formDataUnexplode: "list=a,b,c",
205-
deepObjectExplode: nil
226+
deepObjectExplode: .custom(
227+
"list=a&list=b&list=c",
228+
expectedError: .deepObjectsArrayNotSupported
229+
)
206230
)
207231
)
208232

@@ -217,7 +241,10 @@ final class Test_URICodingRoundtrip: Test_Runtime {
217241
simpleUnexplode: "2023-08-25T07%3A34%3A59Z,2023-08-25T07%3A35%3A01Z",
218242
formDataExplode: "list=2023-08-25T07%3A34%3A59Z&list=2023-08-25T07%3A35%3A01Z",
219243
formDataUnexplode: "list=2023-08-25T07%3A34%3A59Z,2023-08-25T07%3A35%3A01Z",
220-
deepObjectExplode: nil
244+
deepObjectExplode: .custom(
245+
"list=2023-08-25T07%3A34%3A59Z&list=2023-08-25T07%3A35%3A01Z",
246+
expectedError: .deepObjectsArrayNotSupported
247+
)
221248
)
222249
)
223250

@@ -232,7 +259,10 @@ final class Test_URICodingRoundtrip: Test_Runtime {
232259
simpleUnexplode: .custom("", value: [""]),
233260
formDataExplode: "",
234261
formDataUnexplode: "",
235-
deepObjectExplode: nil
262+
deepObjectExplode: .custom(
263+
"",
264+
expectedError: .deepObjectsArrayNotSupported
265+
)
236266
)
237267
)
238268

@@ -247,7 +277,10 @@ final class Test_URICodingRoundtrip: Test_Runtime {
247277
simpleUnexplode: "red,green,blue",
248278
formDataExplode: "list=red&list=green&list=blue",
249279
formDataUnexplode: "list=red,green,blue",
250-
deepObjectExplode: nil
280+
deepObjectExplode: .custom(
281+
"list=red&list=green&list=blue",
282+
expectedError: .deepObjectsArrayNotSupported
283+
)
251284
)
252285
)
253286

@@ -278,7 +311,9 @@ final class Test_URICodingRoundtrip: Test_Runtime {
278311
simpleUnexplode: "2023-01-18T10%3A04%3A11Z",
279312
formDataExplode: "root=2023-01-18T10%3A04%3A11Z",
280313
formDataUnexplode: "root=2023-01-18T10%3A04%3A11Z",
281-
deepObjectExplode: "root=2023-01-18T10%3A04%3A11Z"
314+
deepObjectExplode: .custom(
315+
"root=2023-01-18T10%3A04%3A11Z",
316+
expectedError: .deepObjectsWithPrimitiveValuesNotSupported)
282317
)
283318
)
284319
try _test(
@@ -291,7 +326,9 @@ final class Test_URICodingRoundtrip: Test_Runtime {
291326
simpleUnexplode: "green",
292327
formDataExplode: "root=green",
293328
formDataUnexplode: "root=green",
294-
deepObjectExplode: "root=green"
329+
deepObjectExplode: .custom(
330+
"root=green",
331+
expectedError: .deepObjectsWithPrimitiveValuesNotSupported)
295332
)
296333
)
297334
try _test(
@@ -372,15 +409,25 @@ final class Test_URICodingRoundtrip: Test_Runtime {
372409
struct Input: ExpressibleByStringLiteral {
373410
var string: String
374411
var customValue: T?
375-
376-
init(string: String, customValue: T?) {
412+
var expectedError: URISerializer.SerializationError?
413+
414+
init(string: String, customValue: T?, expectedError: URISerializer.SerializationError?) {
377415
self.string = string
378416
self.customValue = customValue
417+
self.expectedError = expectedError
379418
}
380419

381-
init(stringLiteral value: String) { self.init(string: value, customValue: nil) }
420+
init(stringLiteral value: String) {
421+
self.init(string: value, customValue: nil, expectedError: nil)
422+
}
382423

383-
static func custom(_ string: String, value: T) -> Self { .init(string: string, customValue: value) }
424+
static func custom(_ string: String, value: T) -> Self {
425+
.init(string: string, customValue: value, expectedError: nil)
426+
}
427+
428+
static func custom(_ string: String, expectedError: URISerializer.SerializationError) -> Self {
429+
.init(string: string, customValue: nil, expectedError: expectedError)
430+
}
384431
}
385432

386433
var formExplode: Input
@@ -389,7 +436,7 @@ final class Test_URICodingRoundtrip: Test_Runtime {
389436
var simpleUnexplode: Input
390437
var formDataExplode: Input
391438
var formDataUnexplode: Input
392-
var deepObjectExplode: Input?
439+
var deepObjectExplode: Input
393440
}
394441

395442
func _test<T: Codable & Equatable>(
@@ -401,11 +448,31 @@ final class Test_URICodingRoundtrip: Test_Runtime {
401448
) throws {
402449
func testVariant(name: String, configuration: URICoderConfiguration, variant: Variants<T>.Input) throws {
403450
let encoder = URIEncoder(configuration: configuration)
404-
let encodedString = try encoder.encode(value, forKey: key)
405-
XCTAssertEqual(encodedString, variant.string, "Variant: \(name)", file: file, line: line)
406-
let decoder = URIDecoder(configuration: configuration)
407-
let decodedValue = try decoder.decode(T.self, forKey: key, from: encodedString[...])
408-
XCTAssertEqual(decodedValue, variant.customValue ?? value, "Variant: \(name)", file: file, line: line)
451+
do {
452+
let encodedString = try encoder.encode(value, forKey: key)
453+
XCTAssertEqual(encodedString, variant.string, "Variant: \(name)", file: file, line: line)
454+
let decoder = URIDecoder(configuration: configuration)
455+
let decodedValue = try decoder.decode(T.self, forKey: key, from: encodedString[...])
456+
XCTAssertEqual(decodedValue, variant.customValue ?? value, "Variant: \(name)", file: file, line: line)
457+
} catch {
458+
guard let expectedError = variant.expectedError,
459+
let serializationError = error as? URISerializer.SerializationError else {
460+
XCTAssert(
461+
false,
462+
"Unexpected error thrown: \(error)",
463+
file: file,
464+
line: line
465+
)
466+
return
467+
}
468+
XCTAssertEqual(
469+
expectedError,
470+
serializationError,
471+
"Failed for config: \(variant.string)",
472+
file: file,
473+
line: line
474+
)
475+
}
409476
}
410477
try testVariant(name: "formExplode", configuration: .formExplode, variant: variants.formExplode)
411478
try testVariant(name: "formUnexplode", configuration: .formUnexplode, variant: variants.formUnexplode)
@@ -418,12 +485,10 @@ final class Test_URICodingRoundtrip: Test_Runtime {
418485
variant: variants.formDataUnexplode
419486
)
420487

421-
if let deepObjectExplode = variants.deepObjectExplode {
422-
try testVariant(
423-
name: "deepObjectExplode",
424-
configuration: .deepObjectExplode,
425-
variant: deepObjectExplode
426-
)
427-
}
488+
try testVariant(
489+
name: "deepObjectExplode",
490+
configuration: .deepObjectExplode,
491+
variant: variants.deepObjectExplode
492+
)
428493
}
429494
}

0 commit comments

Comments
 (0)