Skip to content

Commit 914560c

Browse files
Add finer-grain reference tests for translation phase (#103)
### Motivation We have a reference test, which we've been making use of to validate end-to-end generation from OpenAPI document matches handwritten Swift files. This started out life as the canonical Petstore example from the OpenAPI specification but has since been augmented in various ways to test out some edge cases. This approach isn't scaling too well as we have an all-or-nothing test. We also don't have great test coverage over the translation phase of the generator pipeline, which translates between the parsed OpenAPI document to an intermediate representation. ### Modifications Add test harnesses for finer grain reference tests that exercise smaller sections of the translation phase. It should allow us to test a small OpenAPI document renders to _equivalent_ Swift source (stripping comments and reformatting). ### Result Hopefully a more modular and maintainable testing strategy as we try to support more of the OpenAPI specification. ### Test Plan This PR _is_ tests. --------- Signed-off-by: Si Beaumont <[email protected]>
1 parent 8c5258b commit 914560c

File tree

3 files changed

+813
-31
lines changed

3 files changed

+813
-31
lines changed

Tests/OpenAPIGeneratorReferenceTests/ReferenceTest.swift renamed to Tests/OpenAPIGeneratorReferenceTests/FileBasedReferenceTests.swift

+5-31
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extension TestConfig {
3333
}
3434

3535
/// Tests that the generator produces Swift files that match a reference.
36-
class ReferenceTests: XCTestCase {
36+
class FileBasedReferenceTests: XCTestCase {
3737
override func setUp() {
3838
super.setUp()
3939
continueAfterFailure = false
@@ -82,10 +82,8 @@ class ReferenceTests: XCTestCase {
8282
)
8383

8484
// Run the requested generator invocation
85-
let diagnostics: any DiagnosticCollector = strictDiagnosticsCollector
8685
let generatorPipeline = self.makeGeneratorPipeline(
87-
config: referenceTest.asConfig,
88-
diagnostics: diagnostics
86+
config: referenceTest.asConfig
8987
)
9088
let generatedOutputSource = try generatorPipeline.run(input)
9189

@@ -143,32 +141,8 @@ class ReferenceTests: XCTestCase {
143141
}
144142
}
145143

146-
struct StrictDiagnosticsCollector: DiagnosticCollector {
147-
var test: XCTestCase
148-
149-
func emit(_ diagnostic: Diagnostic) {
150-
print("Test emitted diagnostic: \(diagnostic.description)")
151-
switch diagnostic.severity {
152-
case .note:
153-
// no need to fail, just print
154-
break
155-
case .warning, .error:
156-
XCTFail("Failing with a diagnostic: \(diagnostic.description)")
157-
}
158-
}
159-
}
160-
161-
extension ReferenceTests {
162-
163-
var strictDiagnosticsCollector: any DiagnosticCollector {
164-
StrictDiagnosticsCollector(test: self)
165-
}
166-
167-
private func makeGeneratorPipeline(
168-
config: Config,
169-
diagnostics: any DiagnosticCollector
170-
) -> GeneratorPipeline {
171-
144+
extension FileBasedReferenceTests {
145+
private func makeGeneratorPipeline(config: Config) -> GeneratorPipeline {
172146
let parser = YamsParser()
173147
let translator = MultiplexTranslator()
174148
let renderer = TextBasedRenderer()
@@ -183,7 +157,7 @@ extension ReferenceTests {
183157
return newFile
184158
},
185159
config: config,
186-
diagnostics: diagnostics
160+
diagnostics: XCTestDiagnosticCollector(test: self)
187161
)
188162
}
189163

0 commit comments

Comments
 (0)