Skip to content

Commit ea6b2e8

Browse files
authored
Merge pull request swiftlang#159 from google/diagnostic-improvements
Fail lint tests if any diagnostics are unasserted.
2 parents 77e69e9 + f2cd3a0 commit ea6b2e8

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

Tests/SwiftFormatRulesTests/DiagnosingTestCase.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public class DiagnosingTestCase: XCTestCase {
1212
/// A helper that will keep track of the number of times a specific diagnostic was emitted.
1313
private var consumer = DiagnosticTrackingConsumer()
1414

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+
1519
private class DiagnosticTrackingConsumer: DiagnosticConsumer {
1620
var registeredDiagnostics = [String]()
1721
func handle(_ diagnostic: Diagnostic) {
@@ -35,14 +39,13 @@ public class DiagnosingTestCase: XCTestCase {
3539
}
3640

3741
public override func tearDown() {
42+
guard shouldCheckForUnassertedDiagnostics else { return }
43+
3844
// 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")
4448
}
45-
#endif
4649
}
4750

4851
/// Performs a lint using the provided linter rule on the provided input.
@@ -56,7 +59,12 @@ public class DiagnosingTestCase: XCTestCase {
5659
_ type: SyntaxLintRule.Type,
5760
input: String,
5861
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+
6068
do {
6169
let syntax = try SyntaxTreeParser.parse(input)
6270
let linter = type.init(context: context!)

Tests/SwiftFormatRulesTests/IdentifiersMustBeASCIITests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public class IdentifiersMustBeASCIITests: DiagnosingTestCase {
1515
"""
1616
performLint(IdentifiersMustBeASCII.self, input: input)
1717
XCTAssertDiagnosed(.nonASCIICharsNotAllowed(["😎"],"fo😎o"))
18+
// TODO: It would be nice to allow Δ (among other mathematically meaningful symbols) without
19+
// a lot of special cases; investigate this.
20+
XCTAssertDiagnosed(.nonASCIICharsNotAllowed(["Δ"],"Δx"))
1821
XCTAssertDiagnosed(.nonASCIICharsNotAllowed(["🤩", "😆"], "🤩😆"))
1922
}
2023

Tests/SwiftFormatRulesTests/ValidateDocumentationCommentsTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class ValidateDocumentationCommentsTests: DiagnosingTestCase {
7373
/// - Parameters:
7474
/// - p1: Parameter 1.
7575
/// - p2: Parameter 2.
76+
/// - p3: Parameter 3.
7677
/// - Returns: an integer.
7778
func noReturn(p1: Int, p2: Int, p3: Int) {}
7879
@@ -81,6 +82,7 @@ public class ValidateDocumentationCommentsTests: DiagnosingTestCase {
8182
/// - Parameters:
8283
/// - p1: Parameter 1.
8384
/// - p2: Parameter 2.
85+
/// - p3: Parameter 3.
8486
func foo(p1: Int, p2: Int, p3: Int) -> Int {}
8587
"""
8688
performLint(ValidateDocumentationComments.self, input: input)

0 commit comments

Comments
 (0)