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 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2024 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 SymbolKit
@testable import SwiftDocC
import SwiftDocCTestUtilities
class AutomaticCurationTests: XCTestCase {
private let (availableExtensionSymbolKinds, availableNonExtensionSymbolKinds) = Set(AutomaticCuration.groupKindOrder).union(SymbolGraph.Symbol.KindIdentifier.allCases)
.filter { $0.symbolGeneratesPage() }
.categorize(where: { $0.identifier.hasSuffix(".extension") })
func testAutomaticTopicsGenerationForSameModuleTypes() throws {
for kind in availableNonExtensionSymbolKinds {
let containerID = "some-container-id"
let memberID = "some-member-id"
let tempURL = try createTempFolder(content: [
Folder(name: "unit-test.docc", content: [
JSONFile(name: "ModuleName.symbols.json", content: makeSymbolGraph(
moduleName: "ModuleName",
symbols: [
makeSymbol(identifier: containerID, kind: .class, pathComponents: ["SomeClass"]),
makeSymbol(identifier: memberID, kind: kind, pathComponents: ["SomeClass", "someMember"]),
],
relationships: [
.init(source: memberID, target: containerID, kind: .memberOf, targetFallback: nil),
]
))
])
])
let (_, bundle, context) = try loadBundle(from: tempURL)
try assertRenderedPage(atPath: "/documentation/ModuleName/SomeClass", containsAutomaticTopicSectionFor: kind, context: context, bundle: bundle)
}
}
func testAutomaticTopicsGenerationForExtensionSymbols() throws {
// The extended module behavior is already verified for each extended symbol kind in the module.
for kind in availableExtensionSymbolKinds where kind != .extendedModule {
let containerID = "some-container-id"
let extensionID = "some-extension-id"
let memberID = "some-member-id"
let nonExtensionKind = SymbolGraph.Symbol.KindIdentifier(identifier: String(kind.identifier.dropLast(".extension".count)))
let tempURL = try createTempFolder(content: [
Folder(name: "unit-test.docc", content: [
// Add an empty main symbol graph file so that the extension symbol graph file is processed
JSONFile(name: "ModuleName.symbols.json", content: makeSymbolGraph(moduleName: "ModuleName")),
JSONFile(name: "ModuleName@ExtendedModule.symbols.json", content: makeSymbolGraph(
moduleName: "ModuleName",
symbols: [
// public extension Something {
// func someFunction() { }
// }
makeSymbol(
identifier: extensionID,
kind: .extension,
// The extension has the path component of the extended type
pathComponents: ["Something"],
// Specify the extended symbol's symbol kind
swiftExtension: .init(extendedModule: "ExtendedModule", typeKind: nonExtensionKind, constraints: [])
),
// No matter what type `ExtendedModule.Something` is, always add a function in the extension
makeSymbol(identifier: memberID, kind: .func, pathComponents: ["Something", "someFunction()"]),
],
relationships: [
.init(source: extensionID, target: containerID, kind: .extensionTo, targetFallback: "ExtendedModule.Something"),
.init(source: memberID, target: extensionID, kind: .memberOf, targetFallback: "ExtendedModule.Something"),
]
)),
])
])
let (_, bundle, context) = try loadBundle(from: tempURL)
try assertRenderedPage(atPath: "/documentation/ModuleName", containsAutomaticTopicSectionFor: .extendedModule, context: context, bundle: bundle)
try assertRenderedPage(atPath: "/documentation/ModuleName/ExtendedModule", containsAutomaticTopicSectionFor: kind, context: context, bundle: bundle)
}
}
private func assertRenderedPage(
atPath path: String,
containsAutomaticTopicSectionFor kind: SymbolGraph.Symbol.KindIdentifier,
context: DocumentationContext,
bundle: DocumentationBundle,
file: StaticString = #file,
line: UInt = #line
) throws {
let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: path, sourceLanguage: .swift))
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = try XCTUnwrap(translator.visit(node.semantic) as? RenderNode, file: file, line: line)
for section in renderNode.topicSections {
XCTAssert(section.generated, "\(section.title?.singleQuoted ?? "Untitled topic section") was not marked as generated.", file: file, line: line)
}
XCTAssert(
renderNode.topicSections.contains(where: { group in group.title == AutomaticCuration.groupTitle(for: kind) }),
"""
Missing automatic \(AutomaticCuration.groupTitle(for: kind).singleQuoted) topic group.
Add \(kind.identifier) to either 'AutomaticCuration.groupKindOrder or 'SymbolGraph.Symbol.KindIdentifier.noPageKinds'.
""",
file: file, line: line
)
}
func testAutomaticTopicsSkippingCustomCuratedSymbols() throws {
let (_, bundle, context) = try testBundleAndContext(copying: "TestBundle", excludingPaths: [], codeListings: [:], configureBundle: { url in
// Curate some of `SideClass`'s children under SideKit.
let sideKit = """
# ``SideKit``
SideKit framework
## Topics
### SideKit Basics
- ``SideClass/path``
- ``SideClass/url``
"""
try sideKit.write(to: url.appendingPathComponent("documentation").appendingPathComponent("sidekit.md"), atomically: true, encoding: .utf8)
})
let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/SideKit/SideClass", sourceLanguage: .swift))
// Compile the render node to flex the automatic curator
let symbol = node.semantic as! Symbol
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = translator.visit(symbol) as! RenderNode
// Verify that uncurated element `SideKit/SideClass/Element` is
// automatically curated in `SideClass`'s "Topics"
XCTAssertFalse(renderNode.topicSections.filter({ section -> Bool in
return section.identifiers.contains("doc://org.swift.docc.example/documentation/SideKit/SideClass/Element")
}).isEmpty)
// Verify that element `SideKit/SideClass/path` curated in sidecar under `SideKit`
// is NOT automatically curated in `SideClass`'s "Topics"
XCTAssertTrue(renderNode.topicSections.filter({ section -> Bool in
return section.identifiers.contains("doc://org.swift.docc.example/documentation/SideKit/SideClass/path")
}).isEmpty)
}
func testMergingAutomaticTopics() throws {
let allExpectedChildren = [
"doc://org.swift.docc.example/documentation/SideKit/SideClass/Element",
"doc://org.swift.docc.example/documentation/SideKit/SideClass/Value(_:)",
"doc://org.swift.docc.example/documentation/SideKit/SideClass/init()",
"doc://org.swift.docc.example/documentation/SideKit/SideClass/myFunction()",
"doc://org.swift.docc.example/documentation/SideKit/SideClass/path",
"doc://org.swift.docc.example/documentation/SideKit/SideClass/url",
]
// Curate one or two of the children at a time and leave the rest for automatic curation.
let variationsOfChildrenToCurate: [Set<Int>] = allExpectedChildren.indices.flatMap { first in allExpectedChildren.indices.map { second in Set([first, second]) } }
for curatedIndices in variationsOfChildrenToCurate {
let manualCuration = curatedIndices.map { "- <\(allExpectedChildren[$0])>" }.joined(separator: "\n")
let (_, bundle, context) = try testBundleAndContext(copying: "TestBundle") { url in
try """
# ``SideKit/SideClass``
Curate some of the children and leave the rest for automatic curation.
## Topics
### Manually curated
\(manualCuration)
""".write(to: url.appendingPathComponent("documentation/sideclass.md"), atomically: true, encoding: .utf8)
}
let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/SideKit/SideClass", sourceLanguage: .swift))
// Compile docs and verify the generated Topics section
let symbol = node.semantic as! Symbol
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = translator.visit(symbol) as! RenderNode
// Verify that all the symbols are curated, either manually or automatically
let curatedSymbols = renderNode.topicSections.flatMap { $0.identifiers }
XCTAssertEqual(allExpectedChildren.sorted(), curatedSymbols.sorted())
// The manual topic section is listed before any automatic topic sections
XCTAssertEqual(renderNode.topicSections.first?.title, "Manually curated")
if let firstSection = renderNode.topicSections.first {
XCTAssertFalse(firstSection.generated, "The first topic section is manually authored.")
}
for section in renderNode.topicSections.dropFirst() {
XCTAssert(section.generated, "The other topic sections are generated")
}
// Check that the automatic topic sections only exist if its elements weren't manually curated
XCTAssertEqual(
!curatedIndices.contains(0),
renderNode.topicSections.contains(where: { $0.title == "Type Aliases" })
)
XCTAssertEqual(
!curatedIndices.contains(1),
renderNode.topicSections.contains(where: { $0.title == "Enumeration Cases" })
)
XCTAssertEqual(
!curatedIndices.contains(2),
renderNode.topicSections.contains(where: { $0.title == "Initializers" })
)
XCTAssertEqual(
!curatedIndices.contains(3),
renderNode.topicSections.contains(where: { $0.title == "Instance Methods" })
)
XCTAssertEqual(
!curatedIndices.contains(4) || !curatedIndices.contains(5),
renderNode.topicSections.contains(where: { $0.title == "Instance Properties" })
)
}
}
func testSeeAlsoSectionForAutomaticallyCuratedTopics() throws {
let (_, bundle, context) = try testBundleAndContext(copying: "TestBundle") { url in
var graph = try JSONDecoder().decode(SymbolGraph.self, from: Data(contentsOf: url.appendingPathComponent("sidekit.symbols.json")))
// Copy `SideClass` a handful of times
let sideClassIdentifier = "s:7SideKit0A5ClassC"
let sideClassSymbol = graph.symbols[sideClassIdentifier]!
for suffix in ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"] {
// Duplicate the symbol
var duplicateSymbol = sideClassSymbol
let newClassName = duplicateSymbol.names.title + suffix
duplicateSymbol.names.title = newClassName
duplicateSymbol.identifier.precise = "s:7SideKit0A\(newClassName.count)\(newClassName)C"
// Update the declaration fragment to use the new name
let declarationFragments: [SymbolGraph.Symbol.DeclarationFragments.Fragment] = [
.init(kind: .keyword, spelling: "class", preciseIdentifier: nil),
.init(kind: .text, spelling: " ", preciseIdentifier: nil),
.init(kind: .identifier, spelling: newClassName, preciseIdentifier: nil),
]
let declaration = try JSONDecoder().decode(SymbolGraph.Symbol.DeclarationFragments.self, from: JSONEncoder().encode(declarationFragments))
duplicateSymbol.mixins[SymbolGraph.Symbol.DeclarationFragments.mixinKey] = declaration
duplicateSymbol.pathComponents = [newClassName]
graph.symbols[duplicateSymbol.identifier.precise] = duplicateSymbol
// Duplicate all the edges and nodes to and from the symbol
for relationship in graph.relationships where relationship.source == sideClassIdentifier {
var newRelationship = relationship
newRelationship.source = duplicateSymbol.identifier.precise
// Duplicate the target symbol to avoid symbols being members of more than one other symbol.
let newTarget = relationship.target + suffix
newRelationship.target = newTarget
if let targetSymbol = graph.symbols[relationship.target] {
graph.symbols[newTarget] = targetSymbol
}
graph.relationships.append(newRelationship)
}
for relationship in graph.relationships where relationship.target == sideClassIdentifier {
var newRelationship = relationship
newRelationship.target = duplicateSymbol.identifier.precise
// Duplicate the source symbol to avoid symbols being members of more than one other symbol.
let newSource = relationship.source + suffix
newRelationship.source = newSource
if let targetSymbol = graph.symbols[relationship.target] {
graph.symbols[newSource] = targetSymbol
}
graph.relationships.append(newRelationship)
}
// Add a sidecar file for this symbol
try """
# ``SideKit/SideClass\(suffix)``
Curate some of the children and leave the rest for automatic curation.
## Topics
### Manually curated
- ``init()``
- ``path``
- ``Value(_:)``
""".write(to: url.appendingPathComponent("documentation/sidekit\(suffix).md"), atomically: true, encoding: .utf8)
}
let newGraphData = try JSONEncoder().encode(graph)
try newGraphData.write(to: url.appendingPathComponent("sidekit.symbols.json"))
try """
# ``SideKit``
Curate the duplicate symbols into different Topic sections and leave some (nr 8, 9, and 10) for automatic curation.
## Topics
### First topic
- ``SideClass``
- ``SideClassOne``
- ``SideClassTwo``
### Second topic
- ``SideClassThree``
- ``SideClassFour``
- ``SideClassFive``
### Third topic
- ``SideClassSix``
- ``SideClassSeven``
""".write(to: url.appendingPathComponent("documentation/sidekit.md"), atomically: true, encoding: .utf8)
try """
# ``SideKit/SideClass``
Curate some of the children and leave the rest for automatic curation.
## Topics
### Manually curated
- ``init()``
- ``path``
- ``Value(_:)``
""".write(to: url.appendingPathComponent("documentation/sideclass.md"), atomically: true, encoding: .utf8)
}
// The first topic section
do {
let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/SideKit/SideClass", sourceLanguage: .swift))
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = translator.visit(node.semantic as! Symbol) as! RenderNode
// SideKit includes the "Manually curated" task group and additional automatically created groups.
XCTAssertEqual(renderNode.topicSections.map { $0.title }, ["Manually curated", "Instance Properties", "Instance Methods", "Type Aliases"])
// The other symbols in the same topic section appear in this See Also section
XCTAssertEqual(renderNode.seeAlsoSections.first?.identifiers, [
"doc://org.swift.docc.example/documentation/SideKit/SideClassOne",
"doc://org.swift.docc.example/documentation/SideKit/SideClassTwo",
])
}
// The second topic section
do {
let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/SideKit/SideClassFour", sourceLanguage: .swift))
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = translator.visit(node.semantic as! Symbol) as! RenderNode
// The other symbols in the same topic section appear in this See Also section
XCTAssertEqual(renderNode.seeAlsoSections.first?.identifiers, [
"doc://org.swift.docc.example/documentation/SideKit/SideClassThree",
"doc://org.swift.docc.example/documentation/SideKit/SideClassFive",
])
}
// The second topic section
do {
let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/SideKit/SideClassSix", sourceLanguage: .swift))
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = translator.visit(node.semantic as! Symbol) as! RenderNode
// The other symbols in the same topic section appear in this See Also section
XCTAssertEqual(renderNode.seeAlsoSections.first?.identifiers, [
"doc://org.swift.docc.example/documentation/SideKit/SideClassSeven",
])
}
// The automatically curated symbols shouldn't have a See Also section
do {
let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/SideKit/SideClassEight", sourceLanguage: .swift))
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = translator.visit(node.semantic as! Symbol) as! RenderNode
XCTAssertNil(renderNode.seeAlsoSections.first, "This symbol was automatically curated and shouldn't have a See Also section")
}
do {
let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/SideKit/SideClassNine", sourceLanguage: .swift))
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = translator.visit(node.semantic as! Symbol) as! RenderNode
XCTAssertNil(renderNode.seeAlsoSections.first, "This symbol was automatically curated and shouldn't have a See Also section")
}
do {
let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/SideKit/SideClassTen", sourceLanguage: .swift))
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = translator.visit(node.semantic as! Symbol) as! RenderNode
XCTAssertNil(renderNode.seeAlsoSections.first, "This symbol was automatically curated and shouldn't have a See Also section")
}
}
func testTopLevelSymbolsAreNotAutomaticallyCuratedIfManuallyCuratedElsewhere() throws {
// A symbol graph that defines symbol hierarchy of:
// TestBed -> A
// -> B -> C
// But curation as:
// TestBed -> A -> B -> C
let topLevelCurationSGFURL = Bundle.module.url(
forResource: "TopLevelCuration.symbols", withExtension: "json", subdirectory: "Test Resources")!
// Create a test bundle copy with the symbol graph from above
let (bundleURL, bundle, context) = try testBundleAndContext(copying: "TestBundle", excludingPaths: [], codeListings: [:]) { url in
try? FileManager.default.copyItem(at: topLevelCurationSGFURL, to: url.appendingPathComponent("TopLevelCuration.symbols.json"))
}
defer {
try? FileManager.default.removeItem(at: bundleURL)
}
do {
// Get the framework render node
let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/TestBed", sourceLanguage: .swift))
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = translator.visit(node.semantic as! Symbol) as! RenderNode
// Verify that `B` isn't automatically curated under the framework node
XCTAssertEqual(
["doc://org.swift.docc.example/documentation/TestBed/A"],
renderNode.topicSections.first?.identifiers
)
}
do {
// Get the `A` render node
let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/TestBed/A", sourceLanguage: .swift))
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = translator.visit(node.semantic as! Symbol) as! RenderNode
// Verify that `B` was in fact curated under `A`
XCTAssertEqual(
["doc://org.swift.docc.example/documentation/TestBed/B"],
renderNode.topicSections.first?.identifiers
)
}
}
func testRelevantLanguagesAreAutoCuratedInMixedLanguageFramework() throws {
let (bundle, context) = try testBundleAndContext(named: "MixedLanguageFramework")
let frameworkDocumentationNode = try context.entity(
with: ResolvedTopicReference(
bundleIdentifier: bundle.identifier,
path: "/documentation/MixedLanguageFramework",
sourceLanguages: [.swift, .objectiveC]
)
)
let swiftTopics = try AutomaticCuration.topics(
for: frameworkDocumentationNode,
withTraits: [.swift],
context: context
)
XCTAssertEqual(
swiftTopics.flatMap { taskGroup in
[taskGroup.title] + taskGroup.references.map(\.path)
},
[
"Classes",
// 'Bar' is manually curated in a task group titled "Some Swift-only APIs, some Objective-C–only APIs, some mixed" in MixedLanguageFramework.md.
"/documentation/MixedLanguageFramework/MixedLanguageClassConformingToProtocol",
// 'SwiftOnlyClass' is manually curated in a task group titled "Some Swift-only APIs, some Objective-C–only APIs, some mixed" in MixedLanguageFramework.md.
"Protocols",
"/documentation/MixedLanguageFramework/MixedLanguageProtocol",
"Structures",
"/documentation/MixedLanguageFramework/Foo-swift.struct",
// 'SwiftOnlyStruct' is manually curated in a task group titled "Swift-only APIs" in MixedLanguageFramework.md.
]
)
let objectiveCTopics = try AutomaticCuration.topics(
for: frameworkDocumentationNode,
withTraits: [DocumentationDataVariantsTrait(interfaceLanguage: "occ")],
context: context
)
XCTAssertEqual(
objectiveCTopics.flatMap { taskGroup in
[taskGroup.title] + taskGroup.references.map(\.path)
},
[
"Classes",
// 'Bar' is manually curated in a task group titled "Some Swift-only APIs, some Objective-C–only APIs, some mixed" in MixedLanguageFramework.md.
"/documentation/MixedLanguageFramework/MixedLanguageClassConformingToProtocol",
"Protocols",
"/documentation/MixedLanguageFramework/MixedLanguageProtocol",
// "Variables",
// '_MixedLanguageFrameworkVersionNumber is manually curated in a task group titled "Objective-C–only APIs" in MixedLanguageFramework.md.
// '_MixedLanguageFrameworkVersionString' is manually curated in a task group titled "Some Swift-only APIs, some Objective-C–only APIs, some mixed" in MixedLanguageFramework.md.
// 'MixedLanguageFramework/Foo-c.typealias' is manually curated in a task group titled "Custom" under 'MixedLanguageFramework/Bar/myStringFunction:error:'
"Enumerations",
"/documentation/MixedLanguageFramework/Foo-swift.struct",
]
)
}
func testIvarsAndMacrosAreCuratedProperly() throws {
let whatsitSymbols = Bundle.module.url(
forResource: "Whatsit-Objective-C.symbols", withExtension: "json", subdirectory: "Test Resources")!
let (bundleURL, bundle, context) = try testBundleAndContext(copying: "TestBundle") { url in
try? FileManager.default.copyItem(at: whatsitSymbols, to: url.appendingPathComponent("Whatsit-Objective-C.symbols.json"))
}
defer {
try? FileManager.default.removeItem(at: bundleURL)
}
let frameworkDocumentationNode = try context.entity(
with: ResolvedTopicReference(
bundleIdentifier: bundle.identifier,
path: "/documentation/Whatsit",
sourceLanguages: [.objectiveC]
)
)
let objectiveCTopics = try AutomaticCuration.topics(
for: frameworkDocumentationNode,
withTraits: [DocumentationDataVariantsTrait(interfaceLanguage: "occ")],
context: context
)
XCTAssertEqual(
objectiveCTopics.flatMap { taskGroup in
[taskGroup.title] + taskGroup.references.map(\.path)
},
[
"Classes",
"/documentation/Whatsit/Whatsit",
"Macros",
"/documentation/Whatsit/IS_COOL",
]
)
let classDocumentationNode = try context.entity(
with: ResolvedTopicReference(
bundleIdentifier: bundle.identifier,
path: "/documentation/Whatsit/Whatsit",
sourceLanguages: [.objectiveC]
)
)
let classTopics = try AutomaticCuration.topics(
for: classDocumentationNode,
withTraits: [DocumentationDataVariantsTrait(interfaceLanguage: "occ")],
context: context
)
XCTAssertEqual(
classTopics.flatMap { taskGroup in
[taskGroup.title] + taskGroup.references.map(\.path)
},
[
"Instance Variables",
"/documentation/Whatsit/Whatsit/Ivar",
]
)
}
func testTypeSubscriptsAreCuratedProperly() throws {
let symbolURL = Bundle.module.url(
forResource: "TypeSubscript.symbols", withExtension: "json", subdirectory: "Test Resources")!
let (bundleURL, bundle, context) = try testBundleAndContext(copying: "TestBundle") { url in
try? FileManager.default.copyItem(at: symbolURL, to: url.appendingPathComponent("TypeSubscript.symbols.json"))
}
defer {
try? FileManager.default.removeItem(at: bundleURL)
}
let containerDocumentationNode = try context.entity(
with: ResolvedTopicReference(
bundleIdentifier: bundle.identifier,
path: "/documentation/ThirdOrder/SomeStruct",
sourceLanguages: [.swift]
)
)
let topics = try AutomaticCuration.topics(
for: containerDocumentationNode,
withTraits: [DocumentationDataVariantsTrait(interfaceLanguage: "swift")],
context: context
)
XCTAssertEqual(
topics.flatMap { taskGroup in
[taskGroup.title] + taskGroup.references.map(\.path)
},
[
"Type Subscripts",
"/documentation/ThirdOrder/SomeStruct/subscript(_:)",
]
)
}
func testCPlusPlusSymbolsAreCuratedProperly() throws {
let (bundle, context) = try testBundleAndContext(named: "CxxSymbols")
let rootDocumentationNode = try context.entity(
with: .init(
bundleIdentifier: bundle.identifier,
path: "/documentation/CxxSymbols",
sourceLanguage: .objectiveC
)
)
let topics = try AutomaticCuration.topics(
for: rootDocumentationNode,
withTraits: [.objectiveC],
context: context
)
XCTAssertEqual(
topics.flatMap { taskGroup in
[taskGroup.title] + taskGroup.references.map(\.path)
},
[
"Namespaces",
"/documentation/CxxSymbols/Foo",
"Unions",
"/documentation/CxxSymbols/MyUnion",
]
)
}
// Ensures that manually curated sample code articles are not also
// automatically curated.
func testSampleCodeArticlesRespectManualCuration() throws {
let renderNode = try renderNode(atPath: "/documentation/SomeSample", fromTestBundleNamed: "SampleBundle")
guard renderNode.topicSections.count == 2 else {
XCTFail("Expected to find '2' topic sections. Found: \(renderNode.topicSections.count.description.singleQuoted).")
return
}
XCTAssertEqual(renderNode.topicSections[0].title, "Examples")
XCTAssertEqual(
renderNode.topicSections[0].identifiers,
[
"doc://org.swift.docc.sample/documentation/SampleBundle/MySample",
"doc://org.swift.docc.sample/documentation/SampleBundle/MyLocalSample",
"doc://org.swift.docc.sample/documentation/SampleBundle/RelativeURLSample",
"doc://org.swift.docc.sample/documentation/SampleBundle/MyArticle",
"doc://org.swift.docc.sample/documentation/SampleBundle/MyExternalSample",
]
)
XCTAssertEqual(renderNode.topicSections[1].title, "Articles")
XCTAssertEqual(
renderNode.topicSections[1].identifiers,
[
"doc://org.swift.docc.sample/documentation/SampleBundle/MyUncuratedSample",
]
)
}
func testOverloadedSymbolsAreCuratedUnderGroup() throws {
enableFeatureFlag(\.isExperimentalOverloadedSymbolPresentationEnabled)
let protocolRenderNode = try renderNode(
atPath: "/documentation/ShapeKit/OverloadedProtocol",
fromTestBundleNamed: "OverloadedSymbols")
guard protocolRenderNode.topicSections.count == 1, let protocolTopicSection = protocolRenderNode.topicSections.first else {
XCTFail("Expected to find 1 topic section, found \(protocolRenderNode.topicSections.count): \(protocolRenderNode.topicSections.map(\.title?.singleQuoted))")
return
}
XCTAssertEqual(protocolTopicSection.title, "Instance Methods")
XCTAssertEqual(protocolTopicSection.identifiers, [
"doc://com.shapes.ShapeKit/documentation/ShapeKit/OverloadedProtocol/fourthTestMemberName(test:)-9b6be"
])
let overloadGroupRenderNode = try renderNode(
atPath: "/documentation/ShapeKit/OverloadedProtocol/fourthTestMemberName(test:)-9b6be",
fromTestBundleNamed: "OverloadedSymbols")
XCTAssertEqual(
overloadGroupRenderNode.topicSections.count, 0,
"Expected no topic sections, found \(overloadGroupRenderNode.topicSections.map(\.title?.singleQuoted))"
)
}
func testAutomaticCurationHandlesOverloadsWithLanguageFilters() throws {
enableFeatureFlag(\.isExperimentalOverloadedSymbolPresentationEnabled)
let (bundle, context) = try testBundleAndContext(named: "OverloadedSymbols")
let protocolDocumentationNode = try context.entity(
with: .init(
bundleIdentifier: bundle.identifier,
path: "/documentation/ShapeKit/OverloadedProtocol",
sourceLanguage: .swift))
func assertAutomaticCuration(
variants: Set<DocumentationDataVariantsTrait>,
file: StaticString = #file,
line: UInt = #line
) throws {
let topics = try AutomaticCuration.topics(
for: protocolDocumentationNode,
withTraits: variants,
context: context)
guard topics.count == 1, let overloadTopic = topics.first else {
XCTFail(
"Expected one automatic curation topic, found \(topics.count): \(topics.map(\.title?.singleQuoted))",
file: file, line: line)
return
}
XCTAssertEqual(overloadTopic.title, "Instance Methods", file: file, line: line)
XCTAssertEqual(overloadTopic.references.map(\.absoluteString), [
"doc://com.shapes.ShapeKit/documentation/ShapeKit/OverloadedProtocol/fourthTestMemberName(test:)-9b6be"
], file: file, line: line)
}
// AutomaticCuration uses a different method for collecting child nodes when the variant
// traits set is empty and when it's not. Ensure that in both cases, we only see the
// overload group symbol curated under the protocol symbol.
try assertAutomaticCuration(variants: [])
try assertAutomaticCuration(variants: [.swift])
}
func testAutomaticCurationDropsOverloadGroupWhenOverloadsAreCurated() throws {
enableFeatureFlag(\.isExperimentalOverloadedSymbolPresentationEnabled)
let (_, bundle, context) = try testBundleAndContext(copying: "OverloadedSymbols") { url in
try """
# ``OverloadedProtocol``
This is a protocol's docs.
## Topics
- ``fourthTestMemberName(test:)-1h173``
- ``fourthTestMemberName(test:)-8iuz7``
- ``fourthTestMemberName(test:)-91hxs``
- ``fourthTestMemberName(test:)-961zx``
""".write(to: url.appendingPathComponent("OverloadedProtocol.md"), atomically: true, encoding: .utf8)
}
let protocolDocumentationNode = try context.entity(
with: .init(
bundleIdentifier: bundle.identifier,
path: "/documentation/ShapeKit/OverloadedProtocol",
sourceLanguage: .swift))
// Compile the render node to flex the automatic curator
let symbol = protocolDocumentationNode.semantic as! Symbol
var translator = RenderNodeTranslator(
context: context,
bundle: bundle,
identifier: protocolDocumentationNode.reference,
source: nil)
let renderNode = translator.visit(symbol) as! RenderNode
XCTAssertEqual(renderNode.topicSections.count, 1)
// The page should not contain a reference to the overload group node, which would otherwise
// be automatically curated into an "Instance Methods" topic group with a hash suffix of 9b6be
let curatedTopic = try XCTUnwrap(renderNode.topicSections.first)
XCTAssertEqual(curatedTopic.title, nil)
XCTAssertEqual(curatedTopic.identifiers, [
"doc://com.shapes.ShapeKit/documentation/ShapeKit/OverloadedProtocol/fourthTestMemberName(test:)-1h173",
"doc://com.shapes.ShapeKit/documentation/ShapeKit/OverloadedProtocol/fourthTestMemberName(test:)-8iuz7",
"doc://com.shapes.ShapeKit/documentation/ShapeKit/OverloadedProtocol/fourthTestMemberName(test:)-91hxs",
"doc://com.shapes.ShapeKit/documentation/ShapeKit/OverloadedProtocol/fourthTestMemberName(test:)-961zx",
])
}
}
private func makeSymbol(
identifier: String,
kind: SymbolGraph.Symbol.KindIdentifier,
pathComponents: [String],
swiftExtension: SymbolGraph.Symbol.Swift.Extension? = nil
) -> SymbolGraph.Symbol {
var mixins = [String: Mixin]()
if let swiftExtension {
mixins[SymbolGraph.Symbol.Swift.Extension.mixinKey] = swiftExtension
}
return SymbolGraph.Symbol(
identifier: .init(precise: identifier, interfaceLanguage: SourceLanguage.swift.id),
names: .init(title: pathComponents.last!, navigator: nil, subHeading: nil, prose: nil),
pathComponents: pathComponents,
docComment: nil,
accessLevel: .public,
kind: .init(parsedIdentifier: kind, displayName: "Kind Display Name"),
mixins: mixins
)
}
|