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
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021 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
class SectionExtractionTests: XCTestCase {
func testSectionHeadingIndex() {
// No headings -> nil
XCTAssertNil(Document(parsing: "", options: []).sectionHeadingIndex(level: 2, named: "Topics"))
// Wrong level -> nil
XCTAssertNil(Document(parsing: "# Topics", options: []).sectionHeadingIndex(level: 2, named: "Topics"))
// Wrong name -> nil
XCTAssertNil(Document(parsing: "## Topicz", options: []).sectionHeadingIndex(level: 2, named: "Topics"))
// Correct level and name -> index 0
XCTAssertEqual(0, Document(parsing: "## Topics", options: []).sectionHeadingIndex(level: 2, named: "Topics"))
// Correct level and name -> index 1
XCTAssertEqual(1, Document(parsing: "# Title\n\n## Topics", options: []).sectionHeadingIndex(level: 2, named: "Topics"))
}
func testIndexToNextHeading() {
// No headings -> nil
XCTAssertNil(Document(parsing: "", options: []).indexToNextHeading(from: 0, level: 1))
XCTAssertNil(Document(parsing: "", options: []).indexToNextHeading(from: 0, level: 2))
// Start -> wrong level -> nil
XCTAssertNil(Document(parsing: "# Title", options: []).indexToNextHeading(from: 0, level: 2))
// Start -> correct level -> index 0
XCTAssertEqual(0, Document(parsing: "# Title", options: []).indexToNextHeading(from: 0, level: 1))
// Middle -> correct level -> index 2
do {
let markupSource = """
# Title
This is a paragraph.
## Topics
Topics would go here.
"""
XCTAssertEqual(2, Document(parsing: markupSource, options: []).indexToNextHeading(from: 1, level: 2))
}
// End -> correct level -> index n
do {
let markupSource = """
# Title
This is a paragraph.
## Topics
"""
XCTAssertEqual(2, Document(parsing: markupSource, options: []).indexToNextHeading(from: 2, level: 2))
}
}
private func testNode(with document: Document) -> DocumentationNode {
return DocumentationNode(reference: ResolvedTopicReference(bundleIdentifier: "org.swift.docc", path: "/blah", sourceLanguage: .swift), kind: .article, sourceLanguage: .swift, name: .conceptual(title: "Title"), markup: document, semantic: Semantic())
}
func testSection() {
// Empty -> nil
do {
let document = Document(parsing: "", options: [])
let markupModel = DocumentationMarkup(markup: document)
XCTAssertNil(markupModel.discussionSection)
}
// Level != 2 -> nil
do {
let document = Document(parsing: "# Topics\n\nHey.\n", options: [])
let markupModel = DocumentationMarkup(markup: document)
XCTAssertEqual("Hey.", Paragraph(markupModel.abstractSection?.content.compactMap { $0 as? InlineMarkup } ?? []).format())
XCTAssertNil(markupModel.discussionSection)
XCTAssertNil(markupModel.topicsSection)
XCTAssertNil(markupModel.seeAlsoSection)
}
// Correct heading at end, empty content
do {
let markupSource = """
# Title
Abstract.
Some stuff.
## Topics
"""
let document = Document(parsing: markupSource, options: [])
let markupModel = DocumentationMarkup(markup: document)
XCTAssertEqual("Abstract.", Paragraph(markupModel.abstractSection?.content.compactMap { $0 as? InlineMarkup } ?? []).format())
XCTAssertEqual("Some stuff.", Document(markupModel.discussionSection?.content.compactMap { $0 as? Paragraph } ?? []).format())
XCTAssertNil(markupModel.topicsSection)
XCTAssertNil(markupModel.seeAlsoSection)
}
// Correct heading, has content
do {
let markupSource = """
# Title
Abstract.
Some stuff.
## Topics
### A
This is a topic about A.
## See Also
This stuff.
"""
let document = Document(parsing: markupSource, options: [.parseBlockDirectives, .parseSymbolLinks])
let markupModel = DocumentationMarkup(markup: document)
XCTAssertEqual("Abstract.", Paragraph(markupModel.abstractSection?.content.compactMap { $0 as? InlineMarkup } ?? []).detachedFromParent.format())
XCTAssertEqual("Some stuff.", Document(markupModel.discussionSection?.content.compactMap { $0 as? Paragraph } ?? []).detachedFromParent.format())
XCTAssertEqual("### A\nThis is a topic about A.", markupModel.topicsSection?.content.map { $0.detachedFromParent.format() }.joined(separator: "\n"))
XCTAssertEqual("This stuff.", markupModel.seeAlsoSection?.content.map { $0.detachedFromParent.format() }.joined(separator: "\n"))
}
}
}
class TaskGroupTests: XCTestCase {
func testTopicLinks() {
// Empty content -> no links
XCTAssertTrue(TaskGroup(heading: Heading(level: 1), content: []).links.isEmpty)
// Content without links -> []
do {
let markupSource = """
# Title
# Topics
- Not a link? Uh oh!
- Not a link either!
OWO what's this? A paragraph?
"""
let taskGroup = TaskGroup(heading: Heading(level: 1), content: Array(Document(parsing: markupSource, options: [.parseBlockDirectives, .parseSymbolLinks]).children))
XCTAssertTrue(taskGroup.links.isEmpty)
}
// Content with links at the wrong level -> []
do {
let markupSource = """
# Title
# Topics
- Not a link? Uh oh!
- <doc:/foo>
- <doc:/foo>
"""
let taskGroup = TaskGroup(heading: Heading(level: 1), content: Array(Document(parsing: markupSource, options: [.parseBlockDirectives, .parseSymbolLinks]).children))
XCTAssertTrue(taskGroup.links.isEmpty)
}
// Content with links
do {
let markupSource = """
# Title
# Topics
- <doc:/foo>
- <notadoc:/foo>
"""
let taskGroup = TaskGroup(heading: Heading(level: 1), content: Array(Document(parsing: markupSource, options: [.parseBlockDirectives, .parseSymbolLinks]).children))
let links = taskGroup.links
XCTAssertEqual(1, links.count)
for link in links {
XCTAssertEqual("doc:/foo", link.destination)
}
XCTAssertEqual(3, taskGroup.content.count)
guard let list = taskGroup.content[2] as? UnorderedList,
list.childCount == 1,
let remainingItem = (list.child(at: 0) as? ListItem)?.format(),
remainingItem == "- <notadoc:/foo>" else {
XCTFail("Stripping task group links didn't preserve non-topic-link bullet")
return
}
}
// Content with link links
do {
let markupSource = """
# Title
# Topics
- [Example](http://www.example.com)
- [Example](https://www.example.com)
"""
let taskGroup = TaskGroup(heading: Heading(level: 1), content: Array(Document(parsing: markupSource, options: [.parseBlockDirectives, .parseSymbolLinks]).children))
let links = taskGroup.links
XCTAssertEqual(2, links.count)
XCTAssertEqual("http://www.example.com", links[0].destination)
XCTAssertEqual("https://www.example.com", links[1].destination)
}
}
func testSupportedLanguages() throws {
let markupSource = """
# Title
Abstract.
## Topics
### Something Swift only
This link is only for Swift
@SupportedLanguage(swift)
- ``Link1``
### Something Objective-C only
This link is only for Objective-C
@SupportedLanguage(objc)
- ``Link1``
### Something for both
This link is for both Swift and Objective-C
@SupportedLanguage(objc)
@SupportedLanguage(swift)
- ``Link3``
### Something without a language filter
This link is for all languages
- ``Link4``
"""
let document = Document(parsing: markupSource, options: [.parseBlockDirectives, .parseSymbolLinks])
let markupModel = DocumentationMarkup(markup: document)
XCTAssertEqual("Abstract.", Paragraph(markupModel.abstractSection?.content.compactMap { $0 as? InlineMarkup } ?? []).detachedFromParent.format())
let topicSection = try XCTUnwrap(markupModel.topicsSection)
XCTAssertEqual(topicSection.taskGroups.count, 4)
do {
let taskGroup = try XCTUnwrap(topicSection.taskGroups.first)
XCTAssertEqual(taskGroup.heading?.detachedFromParent.format(), "### Something Swift only")
XCTAssertEqual(taskGroup.abstract?.paragraph.detachedFromParent.format(), "This link is only for Swift")
XCTAssertEqual(taskGroup.directives.count, 1)
XCTAssertEqual(taskGroup.directives[SupportedLanguage.directiveName]?.count, 1)
for directive in taskGroup.directives[SupportedLanguage.directiveName] ?? [] {
XCTAssertEqual(directive.name, "SupportedLanguage")
XCTAssertEqual(directive.arguments().count, 1)
}
XCTAssertEqual(taskGroup.links.count, 1)
}
do {
let taskGroup = try XCTUnwrap(topicSection.taskGroups.dropFirst().first)
XCTAssertEqual(taskGroup.heading?.detachedFromParent.format(), "### Something Objective-C only")
XCTAssertEqual(taskGroup.abstract?.paragraph.detachedFromParent.format(), "This link is only for Objective-C")
XCTAssertEqual(taskGroup.directives.count, 1)
XCTAssertEqual(taskGroup.directives[SupportedLanguage.directiveName]?.count, 1)
for directive in taskGroup.directives[SupportedLanguage.directiveName] ?? [] {
XCTAssertEqual(directive.name, "SupportedLanguage")
XCTAssertEqual(directive.arguments().count, 1)
}
XCTAssertEqual(taskGroup.links.count, 1)
}
do {
let taskGroup = try XCTUnwrap(topicSection.taskGroups.dropFirst(2).first)
XCTAssertEqual(taskGroup.heading?.detachedFromParent.format(), "### Something for both")
XCTAssertEqual(taskGroup.abstract?.paragraph.detachedFromParent.format(), "This link is for both Swift and Objective-C")
XCTAssertEqual(taskGroup.directives.count, 1)
XCTAssertEqual(taskGroup.directives[SupportedLanguage.directiveName]?.count, 2)
for directive in taskGroup.directives[SupportedLanguage.directiveName] ?? [] {
XCTAssertEqual(directive.name, "SupportedLanguage")
XCTAssertEqual(directive.arguments().count, 1)
}
XCTAssertEqual(taskGroup.links.count, 1)
}
do {
let taskGroup = try XCTUnwrap(topicSection.taskGroups.dropFirst(3).first)
XCTAssertEqual(taskGroup.heading?.detachedFromParent.format(), "### Something without a language filter")
XCTAssertEqual(taskGroup.abstract?.paragraph.detachedFromParent.format(), "This link is for all languages")
XCTAssert(taskGroup.directives.isEmpty)
XCTAssertEqual(taskGroup.links.count, 1)
}
}
func testOtherDirectivesAreIgnored() throws {
let markupSource = """
# Title
Abstract.
## Topics
### Something
A mix of different directives that aren't supported in task groups.
@Comment {
Some commented out markup
}
@Metadata {
}
@SomeUnknownDirective()
@SupportedLanguage(swift)
- ``SomeLink``
"""
let document = Document(parsing: markupSource, options: [.parseBlockDirectives, .parseSymbolLinks])
let markupModel = DocumentationMarkup(markup: document)
XCTAssertEqual("Abstract.", Paragraph(markupModel.abstractSection?.content.compactMap { $0 as? InlineMarkup } ?? []).detachedFromParent.format())
let topicSection = try XCTUnwrap(markupModel.topicsSection)
XCTAssertEqual(topicSection.taskGroups.count, 1)
let taskGroup = try XCTUnwrap(topicSection.taskGroups.first)
XCTAssertEqual(taskGroup.heading?.detachedFromParent.format(), "### Something")
XCTAssertEqual(taskGroup.abstract?.paragraph.detachedFromParent.format(), "A mix of different directives that aren’t supported in task groups.")
XCTAssertEqual(taskGroup.directives.count, 4)
XCTAssertEqual(taskGroup.directives[Comment.directiveName]?.count, 1)
if let comment = taskGroup.directives[Comment.directiveName]?.first {
XCTAssertEqual(comment.name, "Comment")
XCTAssertEqual(comment.childCount, 1)
XCTAssert(comment.arguments().isEmpty)
}
XCTAssertEqual(taskGroup.directives[Metadata.directiveName]?.count, 1)
if let metadata = taskGroup.directives[Metadata.directiveName]?.first {
XCTAssertEqual(metadata.name, "Metadata")
XCTAssertEqual(metadata.childCount, 0)
XCTAssertEqual(metadata.childCount, 0)
XCTAssert(metadata.arguments().isEmpty)
}
if let directive = taskGroup.directives["SomeUnknownDirective"]?.first {
XCTAssertEqual(directive.childCount, 0)
XCTAssert(directive.arguments().isEmpty)
}
XCTAssertEqual(taskGroup.directives[SupportedLanguage.directiveName]?.count, 1)
if let supportedLanguage = taskGroup.directives[SupportedLanguage.directiveName]?.first {
XCTAssertEqual(supportedLanguage.name, "SupportedLanguage")
XCTAssertEqual(supportedLanguage.childCount, 0)
XCTAssertEqual(supportedLanguage.arguments().count, 1)
}
XCTAssertEqual(taskGroup.links.count, 1)
}
func testTopicContentOrder() throws {
func assertExpectedParsedTaskGroupContent(_ content: String, file: StaticString = #file, line: UInt = #line) throws {
let document = Document(parsing: """
# Title
Abstract.
## Topics
\(content)
""", options: [.parseBlockDirectives, .parseSymbolLinks])
let markupModel = DocumentationMarkup(markup: document)
let topicSection = try XCTUnwrap(markupModel.topicsSection, file: file, line: line)
XCTAssertEqual(topicSection.taskGroups.count, 1, file: file, line: line)
let taskGroup = try XCTUnwrap(topicSection.taskGroups.first, file: file, line: line)
XCTAssertEqual(taskGroup.heading?.title, "Topic name", file: file, line: line)
XCTAssertEqual(taskGroup.abstract?.paragraph.detachedFromParent.format(), "Abstract paragraph", file: file, line: line)
XCTAssertEqual(taskGroup.discussion?.content.map { $0.detachedFromParent.format() }, [
"Discussion paragraph 1",
"Discussion paragraph 2",
], file: file, line: line)
XCTAssertEqual(taskGroup.directives.count, 1, file: file, line: line)
XCTAssertEqual(taskGroup.directives.keys.first, SupportedLanguage.directiveName, file: file, line: line)
XCTAssertEqual(taskGroup.directives[SupportedLanguage.directiveName]?.first?.name, "SupportedLanguage", file: file, line: line)
XCTAssertEqual(taskGroup.directives[SupportedLanguage.directiveName]?.first?.arguments().count, 1, file: file, line: line)
XCTAssertEqual(taskGroup.links.map(\.destination), ["Link1", "Link2"], file: file, line: line)
}
try assertExpectedParsedTaskGroupContent("""
### Topic name
Abstract paragraph
Discussion paragraph 1
Discussion paragraph 2
@SupportedLanguage(swift)
- ``Link1``
- ``Link2``
""")
try assertExpectedParsedTaskGroupContent("""
### Topic name
Abstract paragraph
@SupportedLanguage(swift)
Discussion paragraph 1
Discussion paragraph 2
- ``Link1``
- ``Link2``
""")
try assertExpectedParsedTaskGroupContent("""
### Topic name
@SupportedLanguage(swift)
Abstract paragraph
Discussion paragraph 1
Discussion paragraph 2
- ``Link1``
- ``Link2``
""")
try assertExpectedParsedTaskGroupContent("""
### Topic name
Abstract paragraph
Discussion paragraph 1
@SupportedLanguage(swift)
Discussion paragraph 2
- ``Link1``
- ``Link2``
""")
try assertExpectedParsedTaskGroupContent("""
### Topic name
Abstract paragraph
Discussion paragraph 1
Discussion paragraph 2
- ``Link1``
- ``Link2``
@SupportedLanguage(swift)
""")
}
}
|