Skip to content

Commit a511fb5

Browse files
author
Jesse Haigh
committed
WIP diagnostics: tests
1 parent 828ee55 commit a511fb5

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

Sources/SwiftDocC/Checker/Checkers/InvalidCodeBlockOption.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@ public struct InvalidCodeBlockOption: Checker {
4949
let matches = NearMiss.bestMatches(for: knownOptions, against: token)
5050

5151
if !matches.isEmpty {
52-
let diagnostic = Diagnostic(source: sourceFile, severity: .warning, range: codeBlock.range, identifier: "org.swift.docc.InvalidCodeBlockOption", summary: "Unknown option \(token) in code block. Did you mean \(matches)?", explanation: nil)
52+
let diagnostic = Diagnostic(source: sourceFile, severity: .warning, range: codeBlock.range, identifier: "org.swift.docc.InvalidCodeBlockOption", summary: "Unknown option \(token.singleQuoted) in code block.")
53+
let possibleSolutions = matches.map { candidate in
54+
Solution(
55+
summary: "Replace \(token.singleQuoted) with \(candidate.singleQuoted).",
56+
replacements: []
57+
)
58+
}
5359
// FIXME: figure out the position of 'token' and provide solutions
54-
problems.append(Problem(diagnostic: diagnostic, possibleSolutions: []))
60+
problems.append(Problem(diagnostic: diagnostic, possibleSolutions: possibleSolutions))
5561
}
5662
}
5763
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2025 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import XCTest
12+
@testable import SwiftDocC
13+
import Markdown
14+
15+
class InvalidCodeBlockOptionTests: XCTestCase {
16+
17+
func testOption() {
18+
let markupSource = """
19+
```nocopy
20+
let a = 1
21+
```
22+
"""
23+
let document = Document(parsing: markupSource, options: [])
24+
var checker = InvalidCodeBlockOption(sourceFile: nil)
25+
checker.visit(document)
26+
XCTAssertTrue(checker.problems.isEmpty)
27+
}
28+
29+
func testMultipleOptionTypos() {
30+
let markupSource = """
31+
```nocoy
32+
let b = 2
33+
```
34+
35+
```nocoy
36+
let c = 3
37+
```
38+
"""
39+
let document = Document(parsing: markupSource, options: [])
40+
var checker = InvalidCodeBlockOption(sourceFile: URL(fileURLWithPath: #file))
41+
checker.visit(document)
42+
XCTAssertEqual(2, checker.problems.count)
43+
44+
for problem in checker.problems {
45+
XCTAssertEqual("org.swift.docc.InvalidCodeBlockOption", problem.diagnostic.identifier)
46+
XCTAssertEqual(problem.diagnostic.summary, "Unknown option 'nocoy' in code block.")
47+
XCTAssertEqual(problem.possibleSolutions.map(\.summary), ["Replace 'nocoy' with 'nocopy'."])
48+
}
49+
}
50+
51+
func testOptionDifferentTypos() throws {
52+
let markupSource = """
53+
```swift, nocpy
54+
let d = 4
55+
```
56+
57+
```unknown, nocpoy
58+
let e = 5
59+
```
60+
61+
```nocopy
62+
let f = 6
63+
```
64+
65+
```ncopy
66+
let g = 7
67+
```
68+
"""
69+
let document = Document(parsing: markupSource, options: [])
70+
var checker = InvalidCodeBlockOption(sourceFile: URL(fileURLWithPath: #file))
71+
checker.visit(document)
72+
73+
XCTAssertEqual(3, checker.problems.count)
74+
75+
let summaries = checker.problems.map { $0.diagnostic.summary }
76+
XCTAssertEqual(summaries, [
77+
"Unknown option 'nocpy' in code block.",
78+
"Unknown option 'nocpoy' in code block.",
79+
"Unknown option 'ncopy' in code block.",
80+
])
81+
82+
for problem in checker.problems {
83+
XCTAssertEqual(
84+
"org.swift.docc.InvalidCodeBlockOption",
85+
problem.diagnostic.identifier
86+
)
87+
88+
XCTAssertEqual(problem.possibleSolutions.count, 1)
89+
let solution = try XCTUnwrap(problem.possibleSolutions.first)
90+
XCTAssert(solution.summary.hasSuffix("with 'nocopy'."))
91+
92+
}
93+
}
94+
}
95+

0 commit comments

Comments
 (0)