@@ -12,6 +12,10 @@ public class DiagnosingTestCase: XCTestCase {
12
12
/// A helper that will keep track of the number of times a specific diagnostic was emitted.
13
13
private var consumer = DiagnosticTrackingConsumer ( )
14
14
15
+ /// Set during lint tests to indicate that we should check for any unasserted diagnostics when the
16
+ /// test is torn down.
17
+ private var shouldCheckForUnassertedDiagnostics = false
18
+
15
19
private class DiagnosticTrackingConsumer : DiagnosticConsumer {
16
20
var registeredDiagnostics = [ String] ( )
17
21
func handle( _ diagnostic: Diagnostic ) {
@@ -35,14 +39,13 @@ public class DiagnosingTestCase: XCTestCase {
35
39
}
36
40
37
41
public override func tearDown( ) {
42
+ guard shouldCheckForUnassertedDiagnostics else { return }
43
+
38
44
// This will emit a test failure if a diagnostic is thrown but we don't explicitly call
39
- // XCTAssertDiagnosed for it. I (hbh) am personally on the fence about whether to include
40
- // this test.
41
- #if false
42
- for (diag, count) in consumer. registeredDiagnostics where count > 0 {
43
- XCTFail ( " unexpected diagnostic ' \( diag) ' thrown \( count) time \( count == 1 ? " " : " s " ) " )
45
+ // XCTAssertDiagnosed for it.
46
+ for diag in consumer. registeredDiagnostics {
47
+ XCTFail ( " unexpected diagnostic ' \( diag) ' emitted " )
44
48
}
45
- #endif
46
49
}
47
50
48
51
/// Performs a lint using the provided linter rule on the provided input.
@@ -56,7 +59,12 @@ public class DiagnosingTestCase: XCTestCase {
56
59
_ type: SyntaxLintRule . Type ,
57
60
input: String ,
58
61
file: StaticString = #file,
59
- line: UInt = #line) {
62
+ line: UInt = #line
63
+ ) {
64
+ // If we're linting, then indicate that we want to fail for unasserted diagnostics when the test
65
+ // is torn down.
66
+ shouldCheckForUnassertedDiagnostics = true
67
+
60
68
do {
61
69
let syntax = try SyntaxTreeParser . parse ( input)
62
70
let linter = type. init ( context: context!)
0 commit comments