forked from apple/swift-docc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiagnosticTests.swift
238 lines (189 loc) · 10.4 KB
/
DiagnosticTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import XCTest
@testable import SwiftDocC
import Markdown
@testable import SymbolKit
import SwiftDocCTestUtilities
class DiagnosticTests: XCTestCase {
fileprivate let nonexistentDiagnostic = Diagnostic(source: nil, severity: .error, range: nil, identifier: "blah", summary: "blah")
fileprivate let basicDiagnostic = Diagnostics.diagnostic(identifier: "org.swift.docc.testdiagnostic")!
func testLocalizedSummary() {
let expectedDump = "This is a test diagnostic"
XCTAssertEqual(expectedDump, basicDiagnostic.summary)
}
func testLocalizedExplanation() {
XCTAssertNil(nonexistentDiagnostic.explanation)
guard let explanation = basicDiagnostic.explanation else {
XCTFail("basicDiagnostic.explanation was nil.")
return
}
let expectedDump = """
This is the test diagnostic's abstract.
Further discussion would go here:
- don't
- use
- jargon
- or
- be
- opaque!
## Example
```swift
func foo() {}
```
## Solution
You should do *this* and **that**.
## Solution Example
```swift
func bar() {}
```
"""
XCTAssertEqual(expectedDump, explanation)
}
func testFilenameAndPosition() {
let path = "/tmp/foo.md"
let range = SourceLocation(line: 1, column: 1, source: URL(fileURLWithPath: path))..<SourceLocation(line: 2, column: 2, source: URL(fileURLWithPath: path))
let diagnostic = Diagnostic(source: URL(fileURLWithPath: path), severity: .error, range: range, identifier: "org.swift.docc.test.Diagnostic.localizedDescription", summary: "This is a test diagnostic")
let expectedDescription = "\(path):1:1: error: This is a test diagnostic"
XCTAssertEqual(expectedDescription, DiagnosticConsoleWriter.formattedDescription(for: diagnostic))
}
/// Test that the file path is printed even when range is nil, indicating a whole-file diagnostic or a diagnostic where the range is unknown.
func testWholeFileDiagnosticDescription() {
let path = "/tmp/foo.md"
let diagnostic = Diagnostic(source: URL(fileURLWithPath: path), severity: .error, range: nil, identifier: "org.swift.docc.test.Diagnostic.localizedDescription", summary: "This is a test diagnostic")
let expectedDescription = "\(path): error: This is a test diagnostic"
XCTAssertEqual(expectedDescription, DiagnosticConsoleWriter.formattedDescription(for: diagnostic))
}
/// Test offsetting diagnostic ranges
func testOffsetDiagnostics() throws {
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
let source = "Test a ``Reference`` in a sentence."
let markup = Document(parsing: source, options: .parseSymbolLinks)
var resolver = ReferenceResolver(context: context, bundle: bundle, source: URL(string: "/tmp/foo.symbols.json"), rootReference: ResolvedTopicReference(bundleIdentifier: "org.swift.docc.example", path: "/documentation/MyKit", sourceLanguage: .swift))
// Resolve references
_ = resolver.visitMarkup(markup)
if LinkResolutionMigrationConfiguration.shouldUseHierarchyBasedLinkResolver {
XCTAssertEqual(resolver.problems.first?.diagnostic.range, SourceLocation(line: 1, column: 10, source: nil)..<SourceLocation(line: 1, column: 19, source: nil))
} else {
XCTAssertEqual(resolver.problems.first?.diagnostic.range, SourceLocation(line: 1, column: 8, source: nil)..<SourceLocation(line: 1, column: 21, source: nil))
}
let offset = SymbolGraph.LineList.SourceRange(start: .init(line: 10, character: 10), end: .init(line: 10, character: 20))
var problem = try XCTUnwrap(resolver.problems.first)
problem.offsetWithRange(offset)
if LinkResolutionMigrationConfiguration.shouldUseHierarchyBasedLinkResolver {
XCTAssertEqual(problem.diagnostic.range, SourceLocation(line: 11, column: 20, source: nil)..<SourceLocation(line: 11, column: 29, source: nil))
} else {
XCTAssertEqual(problem.diagnostic.range, SourceLocation(line: 11, column: 18, source: nil)..<SourceLocation(line: 11, column: 31, source: nil))
}
}
func testFormattedDescription() {
let source = URL(string: "/path/to/file.md")!
let range = SourceLocation(line: 1, column: 8, source: source)..<SourceLocation(line: 10, column: 21, source: source)
let identifier = "org.swift.docc.test-identifier"
let summary = "Test diagnostic summary"
let explanation = "Test diagnostic explanation."
let expectedLocation = "/path/to/file.md:1:8"
let error = Diagnostic(source: source, severity: .error, range: range, identifier: identifier, summary: summary, explanation: explanation)
XCTAssertEqual(DiagnosticConsoleWriter.formattedDescription(for: error), "\(expectedLocation): error: \(summary)\n\(explanation)")
let warning = Diagnostic(source: source, severity: .warning, range: range, identifier: identifier, summary: summary, explanation: explanation)
XCTAssertEqual(DiagnosticConsoleWriter.formattedDescription(for: warning), "\(expectedLocation): warning: \(summary)\n\(explanation)")
let note = Diagnostic(source: source, severity: .information, range: range, identifier: identifier, summary: summary, explanation: explanation)
XCTAssertEqual(DiagnosticConsoleWriter.formattedDescription(for: note), "\(expectedLocation): note: \(summary)\n\(explanation)")
let notice = Diagnostic(source: source, severity: .hint, range: range, identifier: identifier, summary: summary, explanation: explanation)
XCTAssertEqual(DiagnosticConsoleWriter.formattedDescription(for: notice), "\(expectedLocation): notice: \(summary)\n\(explanation)")
}
func testLocalizedDescriptionWithNote() {
let source = URL(string: "/path/to/file.md")!
let range = SourceLocation(line: 1, column: 8, source: source)..<SourceLocation(line: 10, column: 21, source: source)
let identifier = "org.swift.docc.test-identifier"
let summary = "Test diagnostic summary"
let explanation = "Test diagnostic explanation."
let expectedLocation = "/path/to/file.md:1:8"
let noteSource = URL(string: "/a/file/path.md")!
let noteRange = SourceLocation(line: 1, column: 1, source: source)..<SourceLocation(line: 9, column: 7, source: noteSource)
let note = DiagnosticNote(
source: noteSource,
range: noteRange,
message: "The message of the note."
)
let expectedNoteLocation = "/a/file/path.md:1:1"
let diagnostic = Diagnostic(source: source, severity: .error, range: range, identifier: identifier, summary: summary, explanation: explanation, notes: [note])
XCTAssertEqual(DiagnosticConsoleWriter.formattedDescription(for: diagnostic), """
\(expectedLocation): error: \(summary)
\(explanation)
\(expectedNoteLocation): note: The message of the note.
""")
}
func createTestSymbol(commentText: String) -> SymbolGraph.Symbol {
let emptyRange = SymbolGraph.LineList.SourceRange(
start: .init(line: 0, character: 0),
end: .init(line: 0, character: 0)
)
let docCommentLines = commentText.components(separatedBy: .newlines).map { SymbolGraph.LineList.Line(text: $0, range: emptyRange) }
return SymbolGraph.Symbol(
identifier: .init(precise: "s:5MyKit0A5ClassC10myFunctionyyF", interfaceLanguage: "occ"),
names: .init(title: "", navigator: nil, subHeading: nil, prose: nil),
pathComponents: ["path", "to", "my file.h"],
docComment: SymbolGraph.LineList(docCommentLines),
accessLevel: .init(rawValue: "public"),
kind: .init(parsedIdentifier: .func, displayName: "myFunction"),
mixins: [:]
)
}
func testDoxygenDiagnostic() throws {
let commentText = """
Brief description of this method
@param something Description of this parameter
@returns Description of return value
"""
let symbol = createTestSymbol(commentText: commentText)
let engine = DiagnosticEngine()
let _ = DocumentationNode.contentFrom(documentedSymbol: symbol, documentationExtension: nil, engine: engine)
XCTAssertEqual(engine.problems.count, 0)
// testing scenario with known directive
let commentWithKnownDirective = """
Brief description of this method
@TitleHeading("Fancy Type of Article")
@returns Description of return value
"""
let symbolWithKnownDirective = createTestSymbol(commentText: commentWithKnownDirective)
let engine1 = DiagnosticEngine()
let _ = DocumentationNode.contentFrom(documentedSymbol: symbolWithKnownDirective, documentationExtension: nil, engine: engine1)
// count should be 1 for the known directive '@TitleHeading'
// TODO: Consider adding a diagnostic for Doxygen tags (rdar://92184094)
XCTAssertEqual(engine1.problems.count, 1)
XCTAssertEqual(engine1.problems.map { $0.diagnostic.identifier }, ["org.swift.docc.UnsupportedDocCommentDirective"])
}
}
fileprivate let diagnostics: [String: Diagnostic] = [
"org.swift.docc.testdiagnostic": Diagnostic(source: nil, severity: .error, range: nil, identifier: "org.swift.docc.testdiagnostic", summary: "This is a test diagnostic", explanation: """
This is the test diagnostic's abstract.
Further discussion would go here:
- don't
- use
- jargon
- or
- be
- opaque!
## Example
```swift
func foo() {}
```
## Solution
You should do *this* and **that**.
## Solution Example
```swift
func bar() {}
```
"""),
]
enum Diagnostics {
static func diagnostic(identifier: String) -> Diagnostic? {
return diagnostics[identifier]
}
}