-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathMetadataTests.swift
464 lines (390 loc) · 19.3 KB
/
MetadataTests.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2025 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 Foundation
import XCTest
@testable import SwiftDocC
import Markdown
class MetadataTests: XCTestCase {
func testEmpty() throws {
let source = "@Metadata"
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0)! as! BlockDirective
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let metadata = Metadata(from: directive, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(metadata, "Even if a Metadata directive is empty we can create it")
XCTAssertEqual(1, problems.count)
XCTAssertEqual("org.swift.docc.Metadata.NoConfiguration", problems.first?.diagnostic.identifier)
XCTAssertEqual(.information, problems.first?.diagnostic.severity)
XCTAssertNotNil(problems.first?.possibleSolutions.first)
}
func testUnexpectedArgument() throws {
let source = "@Metadata(argument: value)"
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0)! as! BlockDirective
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let metadata = Metadata(from: directive, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(metadata, "Even if there are warnings we can create a metadata value")
XCTAssertEqual(2, problems.count)
XCTAssertEqual("org.swift.docc.UnknownArgument", problems.first?.diagnostic.identifier)
XCTAssertEqual("org.swift.docc.Metadata.NoConfiguration", problems.last?.diagnostic.identifier)
}
func testUnexpectedDirective() throws {
let source = """
@Metadata {
@Image
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0)! as! BlockDirective
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let metadata = Metadata(from: directive, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(metadata, "Even if there are warnings we can create a Metadata value")
XCTAssertEqual(3, problems.count)
XCTAssertEqual("org.swift.docc.HasOnlyKnownDirectives", problems.first?.diagnostic.identifier)
XCTAssertEqual("org.swift.docc.Metadata.UnexpectedContent", problems.dropFirst().first?.diagnostic.identifier)
XCTAssertEqual("org.swift.docc.Metadata.NoConfiguration", problems.last?.diagnostic.identifier)
}
func testExtraContent() throws {
let source = """
@Metadata {
Some text
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0)! as! BlockDirective
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let metadata = Metadata(from: directive, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(metadata, "Even if there are warnings we can create a Metadata value")
XCTAssertEqual(2, problems.count)
XCTAssertEqual("org.swift.docc.Metadata.UnexpectedContent", problems.first?.diagnostic.identifier)
XCTAssertEqual("org.swift.docc.Metadata.NoConfiguration", problems.last?.diagnostic.identifier)
}
// MARK: - Supported metadata directives
func testDocumentationExtensionSupport() throws {
let source = """
@Metadata {
@DocumentationExtension(mergeBehavior: override)
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0)! as! BlockDirective
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let metadata = Metadata(from: directive, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(metadata)
XCTAssertEqual(0, problems.count)
XCTAssertEqual(metadata?.documentationOptions?.behavior, .override)
}
func testRepeatDocumentationExtension() throws {
let source = """
@Metadata {
@DocumentationExtension(mergeBehavior: append)
@DocumentationExtension(mergeBehavior: override)
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0)! as! BlockDirective
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let metadata = Metadata(from: directive, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(metadata)
XCTAssertEqual(2, problems.count)
XCTAssertEqual(problems.map(\.diagnostic.identifier).sorted(), [
"org.swift.docc.DocumentationExtension.NoConfiguration",
"org.swift.docc.HasAtMostOne<Metadata, DocumentationExtension>.DuplicateChildren",
])
XCTAssertEqual(metadata?.documentationOptions?.behavior, .append)
}
func testDisplayNameSupport() throws {
let source = """
@Metadata {
@DisplayName("Custom Name")
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0)! as! BlockDirective
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let metadata = Metadata(from: directive, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(metadata)
XCTAssert(problems.isEmpty, "There shouldn't be any problems. Got:\n\(problems.map { $0.diagnostic.summary })")
XCTAssertEqual(metadata?.displayName?.name, "Custom Name")
}
func testTitleHeadingSupport() throws {
let source = """
@Metadata {
@TitleHeading("Custom Heading")
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0)! as! BlockDirective
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let metadata = Metadata(from: directive, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(metadata)
XCTAssert(problems.isEmpty, "There shouldn't be any problems. Got:\n\(problems.map { $0.diagnostic.summary })")
XCTAssertEqual(metadata?.titleHeading?.heading, "Custom Heading")
}
func testCustomMetadataSupport() throws {
let source = """
@Metadata {
@CustomMetadata(key: "country", value: "Belgium")
@CustomMetadata(key: "continent", value: "Europe")
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0)! as! BlockDirective
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let metadata = Metadata(from: directive, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(metadata)
XCTAssertEqual(metadata?.customMetadata.count, 2)
XCTAssertEqual(problems.count, 0)
}
func testRedirectSupport() throws {
let source = """
@Metadata {
@Redirected(from: "some/other/path")
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0)! as! BlockDirective
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let metadata = Metadata(from: directive, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(metadata)
XCTAssertEqual(0, problems.count)
XCTAssertEqual(metadata?.redirects?.first?.oldPath.relativePath, "some/other/path")
}
// MARK: - Metadata Support
func testArticleSupportsMetadata() throws {
let source = """
# Plain article
@Metadata {
@DocumentationExtension(mergeBehavior: override)
}
The abstract of this article
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let article = Article(from: document, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(article, "An Article value can be created with a Metadata child.")
XCTAssert(problems.isEmpty, "There shouldn't be any problems. Got:\n\(problems.map { $0.diagnostic.summary })")
var analyzer = SemanticAnalyzer(source: nil, bundle: bundle)
_ = analyzer.visit(document)
XCTAssert(analyzer.problems.isEmpty, "Expected no problems. Got:\n \(DiagnosticConsoleWriter.formattedDescription(for: analyzer.problems))")
}
func testSymbolArticleSupportsMetadataDisplayName() throws {
let source = """
# ``SomeSymbol``
@Metadata {
@DisplayName("Custom Name")
}
The abstract of this documentation extension
"""
let document = Document(parsing: source, options: [.parseBlockDirectives, .parseSymbolLinks])
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let article = Article(from: document, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(article, "An Article value can be created with a Metadata child with a DisplayName child.")
XCTAssertNotNil(article?.metadata?.displayName, "The Article has the parsed DisplayName metadata.")
XCTAssert(problems.isEmpty, "There shouldn't be any problems. Got:\n\(problems.map { $0.diagnostic.summary })")
var analyzer = SemanticAnalyzer(source: nil, bundle: bundle)
_ = analyzer.visit(document)
XCTAssert(analyzer.problems.isEmpty, "Expected no problems. Got:\n \(DiagnosticConsoleWriter.formattedDescription(for: analyzer.problems))")
}
func testArticleDoesNotSupportsMetadataDisplayName() throws {
let source = """
# Article title
@Metadata {
@DisplayName("Custom Name")
}
The abstract of this documentation extension
"""
let document = Document(parsing: source, options: [.parseBlockDirectives, .parseSymbolLinks])
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let article = Article(from: document, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(article, "An Article value can be created with a Metadata child with a DisplayName child.")
XCTAssertNotNil(article?.metadata, "The Article has the parsed Metadata")
XCTAssertNil(article?.metadata?.displayName, "The Article doesn't have the DisplayName")
XCTAssertEqual(problems.count, 1)
let problem = try XCTUnwrap(problems.first)
XCTAssertEqual(problem.diagnostic.identifier, "org.swift.docc.Article.DisplayName.NotSupported")
XCTAssertEqual(problem.diagnostic.summary, "A 'DisplayName' directive is only supported in documentation extension files. To customize the display name of an article, change the content of the level-1 heading.")
XCTAssertEqual(problem.possibleSolutions.count, 1)
let solution = try XCTUnwrap(problem.possibleSolutions.first)
XCTAssertEqual(solution.summary, "Change the title")
XCTAssertEqual(solution.replacements.count, 2)
XCTAssertEqual(solution.replacements.first?.range, SourceLocation(line: 4, column: 4, source: nil) ..< SourceLocation(line: 4, column: 31, source: nil))
XCTAssertEqual(solution.replacements.first?.replacement, "")
XCTAssertEqual(solution.replacements.last?.range, SourceLocation(line: 1, column: 1, source: nil) ..< SourceLocation(line: 1, column: 16, source: nil))
XCTAssertEqual(solution.replacements.last?.replacement, "# Custom Name")
}
func testArticleSupportsMetadataTitleHeading() throws {
let source = """
# Article title
@Metadata {
@TitleHeading("Custom Heading")
}
The abstract of this documentation extension
"""
let document = Document(parsing: source, options: [.parseBlockDirectives, .parseSymbolLinks])
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let article = Article(from: document, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(article, "An Article value can be created with a Metadata child with a TitleHeading child.")
XCTAssertNotNil(article?.metadata?.titleHeading, "The Article has the parsed TitleHeading metadata.")
XCTAssertEqual(article?.metadata?.titleHeading?.heading, "Custom Heading")
XCTAssert(problems.isEmpty, "There shouldn't be any problems. Got:\n\(problems.map { $0.diagnostic.summary })")
var analyzer = SemanticAnalyzer(source: nil, bundle: bundle)
_ = analyzer.visit(document)
XCTAssert(analyzer.problems.isEmpty, "Expected no problems. Got:\n \(DiagnosticConsoleWriter.formattedDescription(for: analyzer.problems))")
}
func testDuplicateMetadata() throws {
let source = """
# Article title
@Metadata {
@DocumentationExtension(mergeBehavior: append)
}
@Metadata {
@DocumentationExtension(mergeBehavior: override)
}
The abstract of this documentation extension
"""
let document = Document(parsing: source, options: [.parseBlockDirectives, .parseSymbolLinks])
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let article = Article(from: document, source: nil, for: bundle, problems: &problems)
XCTAssertNotNil(article, "An Article value can be created with a Metadata child with a DisplayName child.")
XCTAssertNotNil(article?.metadata, "The Article has the parsed Metadata")
XCTAssertNil(article?.metadata?.displayName, "The Article doesn't have the DisplayName")
XCTAssertEqual(
problems.map(\.diagnostic.identifier),
[
"org.swift.docc.DocumentationExtension.NoConfiguration",
"org.swift.docc.HasAtMostOne<Article, Metadata>.DuplicateChildren",
]
)
}
func testPageImageSupport() throws {
let (problems, metadata) = try parseMetadataFromSource(
"""
# Article title
@Metadata {
@PageImage(source: "plus", purpose: icon)
@PageImage(source: "sloth", alt: "A sloth on a branch.", purpose: card)
}
The abstract of this article.
"""
)
XCTAssertEqual(problems, [])
XCTAssertEqual(metadata.pageImages.count, 2)
let plusImage = metadata.pageImages.first { pageImage in
pageImage.source.path == "plus"
}
XCTAssertEqual(plusImage?.purpose, .icon)
XCTAssertEqual(plusImage?.alt, nil)
let slothImage = metadata.pageImages.first { pageImage in
pageImage.source.path == "sloth"
}
XCTAssertEqual(slothImage?.purpose, .card)
XCTAssertEqual(slothImage?.alt, "A sloth on a branch.")
}
func testDuplicatePageImage() throws {
let (problems, _) = try parseMetadataFromSource(
"""
# Article title
@Metadata {
@PageImage(source: "plus", purpose: icon)
@PageImage(source: "sloth", alt: "A sloth on a branch.", purpose: icon)
}
The abstract of this article.
"""
)
XCTAssertEqual(
problems,
[
"4: warning – org.swift.docc.DuplicatePageImage",
"5: warning – org.swift.docc.DuplicatePageImage",
]
)
}
func testPageColorSupport() throws {
do {
let (problems, metadata) = try parseMetadataFromSource(
"""
# Article title
@Metadata {
@PageColor(blue)
}
The abstract of this article.
"""
)
XCTAssertEqual(problems, [])
XCTAssertEqual(metadata.pageColor, .blue)
}
do {
let (problems, metadata) = try parseMetadataFromSource(
"""
# Article title
@Metadata {
@PageColor(green)
}
The abstract of this article.
"""
)
XCTAssertEqual(problems, [])
XCTAssertEqual(metadata.pageColor, .green)
}
}
func parseMetadataFromSource(
_ source: String,
file: StaticString = #filePath,
line: UInt = #line
) throws -> (problems: [String], metadata: Metadata) {
let document = Document(parsing: source, options: [.parseBlockDirectives, .parseSymbolLinks])
let (bundle, _) = try testBundleAndContext(named: "LegacyBundle_DoNotUseInNewTests")
var problems = [Problem]()
let article = Article(from: document, source: nil, for: bundle, problems: &problems)
let problemIDs = problems.map { problem -> String in
let line = problem.diagnostic.range?.lowerBound.line.description ?? "unknown-line"
return "\(line): \(problem.diagnostic.severity) – \(problem.diagnostic.identifier)"
}.sorted()
let metadata = try XCTUnwrap(article?.metadata, file: file, line: line)
return (problemIDs, metadata)
}
func testInvalidMetadataDirectivesInDocumentationCommentHaveSolution() throws {
let source = """
@Metadata {
@DisplayName("Custom Name")
@TechnologyRoot
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0)! as! BlockDirective
let (bundle, _) = try testBundleAndContext()
var problems = [Problem]()
let metadata = Metadata(from: directive, source: nil, for: bundle, problems: &problems)
metadata?.validateForUseInDocumentationComment(symbolSource: nil, problems: &problems)
XCTAssertEqual(problems.count, 2)
// Verify that each problem has a solution to remove the directive
for problem in problems {
XCTAssertNotNil(problem.possibleSolutions)
XCTAssertEqual(problem.possibleSolutions?.count, 1)
XCTAssertEqual(problem.possibleSolutions?.first?.summary, "Remove this \(problem.diagnostic.identifier.split(separator: ".").last!) directive.")
XCTAssertEqual(problem.possibleSolutions?.first?.replacements.count, 1)
XCTAssertEqual(problem.possibleSolutions?.first?.replacements.first?.replacement, "")
}
}
}