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
|
/*
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 TutorialArticleTests: XCTestCase {
func testEmpty() throws {
let source = "@Article"
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0) as? BlockDirective
XCTAssertNotNil(directive)
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
directive.map { directive in
var problems = [Problem]()
XCTAssertEqual(TutorialArticle.directiveName, directive.name)
let article = TutorialArticle(from: directive, source: nil, for: bundle, in: context, problems: &problems)
XCTAssertNotNil(article)
XCTAssertEqual(2, problems.count)
XCTAssertEqual([
"org.swift.docc.HasArgument.time",
"org.swift.docc.HasExactlyOne<Article, \(Intro.self)>.Missing",
],
problems.map { $0.diagnostic.identifier })
}
}
func testSimpleNoIntro() throws {
let source = """
@Article {
## The first section
This is content in the first section.
## The second section
This is content in the section section.
### A subsection
This article has a subsection in the second section.
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0) as? BlockDirective
XCTAssertNotNil(directive)
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
directive.map { directive in
var problems = [Problem]()
XCTAssertEqual(TutorialArticle.directiveName, directive.name)
let article = TutorialArticle(from: directive, source: nil, for: bundle, in: context, problems: &problems)
XCTAssertNotNil(article)
XCTAssertEqual(2, problems.count)
article.map { article in
let expectedDump = """
TutorialArticle @1:1-13:2
└─ MarkupContainer (6 elements)
"""
XCTAssertEqual(expectedDump, article.dump())
}
}
}
/// Tests that we parse correctly and emit proper warnings when the author provides non-sequential headers.
func testHeaderMix() throws {
let source = """
@Article {
## The first section
This is content in the first section.
## Another section
asdf
#### Level 4 section
This is content in the section section.
The second section skips the H3 and goes directly to the H4
## Jump back to 2
This is ok
# This goes up to an H1
An H1 even though you should only use H2s or below.
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0) as? BlockDirective
XCTAssertNotNil(directive)
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
directive.map { directive in
var problems = [Problem]()
XCTAssertEqual(TutorialArticle.directiveName, directive.name)
let article = TutorialArticle(from: directive, source: nil, for: bundle, in: context, problems: &problems)
XCTAssertNotNil(article)
XCTAssertEqual(4, problems.count)
article.map { article in
let expectedDump = """
TutorialArticle @1:1-23:2
└─ MarkupContainer (11 elements)
"""
XCTAssertEqual(expectedDump, article.dump())
}
}
}
func testIntroAndContent() throws {
let source = """
@Article(time: 20) {
@Intro(title: "Basic Augmented Reality App") {
This is some text in an intro.
This is another paragraph of **styled text**.
@Image(source: myimage.png, alt: image)
}
## The first section
This is content in the first section.
## The second section
This is content in the section section.
### A subsection
This article has a subsection in the second section.
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0) as? BlockDirective
XCTAssertNotNil(directive)
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
directive.map { directive in
var problems = [Problem]()
XCTAssertEqual(TutorialArticle.directiveName, directive.name)
let article = TutorialArticle(from: directive, source: nil, for: bundle, in: context, problems: &problems)
XCTAssertNotNil(article)
XCTAssertEqual(0, problems.count)
article.map { article in
let expectedDump = """
TutorialArticle @1:1-23:2 title: 'Basic Augmented Reality App' time: '20'
├─ Intro @3:4-10:5 title: 'Basic Augmented Reality App'
│ ├─ MarkupContainer (2 elements)
│ └─ ImageMedia @9:7-9:46 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "myimage.png")' altText: 'image'
└─ MarkupContainer (6 elements)
"""
XCTAssertEqual(expectedDump, article.dump())
}
}
}
func testLayouts() throws {
let source = """
@Article {
@ContentAndMedia {
@Image(source: customize-text-view.png, alt: "alt")
You can customize a view's display by changing your code,
or by using the inspector to discover what's available and to help you write code.
As you build the Landmarks app, you can use any combination of editors:
the source editor, the canvas, or the inspectors.
Your code stays updated, regardless of which tool you use.
}
@ContentAndMedia {
You can customize a view's display by changing your code,
or by using the inspector to discover what's available and to help you write code.
As you build the Landmarks app, you can use any combination of editors:
the source editor, the canvas, or the inspectors.
Your code stays updated, regardless of which tool you use.
@Image(source: customize-text-view.png, alt: "alt")
}
Full width inbetween other layouts.
@Stack {
@ContentAndMedia {
You can customize a view's display by changing your code,
or by using the inspector to discover what's available and to help you write code.
@Image(source: this-is-still-trailing.png, alt: "alt")
As you build the Landmarks app, you can use any combination of editors:
the source editor, the canvas, or the inspectors.
Your code stays updated, regardless of which tool you use.
}
Arbitrary markup between directives is not allowed.
@ContentAndMedia {
You can customize a view's display by changing your code,
or by using the inspector to discover what's available and to help you write code.
As you build the Landmarks app, you can use any combination of editors:
the source editor, the canvas, or the inspectors.
Your code stays updated, regardless of which tool you use.
@Image(source: this-is-trailing.png, alt: "alt")
}
}
## A Section
Some full width stuff.
- foo
- bar
- baz
@Stack {
@ContentAndMedia {
You can customize a view's display by changing your code,
or by using the inspector to discover what's available and to help you write code.
@Image(source: this-is-still-trailing.png, alt: "alt")
As you build the Landmarks app, you can use any combination of editors:
the source editor, the canvas, or the inspectors.
Your code stays updated, regardless of which tool you use.
}
@ContentAndMedia {
@Image(source: this-is-leading.png, alt: "alt")
}
@ContentAndMedia {
@Image(source: this-is-leading.png, alt: "alt")
}
}
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0) as? BlockDirective
XCTAssertNotNil(directive)
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
directive.map { directive in
var problems = [Problem]()
XCTAssertEqual(TutorialArticle.directiveName, directive.name)
let article = TutorialArticle(from: directive, source: nil, for: bundle, in: context, problems: &problems)
XCTAssertNotNil(article)
XCTAssertEqual(3, problems.count)
let arbitraryMarkupProblem = problems.first(where: { $0.diagnostic.identifier == "org.swift.docc.Stack.UnexpectedContent" })
XCTAssertNotNil(arbitraryMarkupProblem)
XCTAssertEqual(arbitraryMarkupProblem?.diagnostic.summary, "'Stack' contains unexpected content")
XCTAssertEqual(arbitraryMarkupProblem?.diagnostic.explanation, "Arbitrary markup content is not allowed as a child of the 'Stack' directive.")
article.map { article in
let expectedDump = """
TutorialArticle @1:1-81:2
├─ ContentAndMedia @3:4-12:5 mediaPosition: 'leading'
│ ├─ MarkupContainer (2 elements)
│ └─ ImageMedia @4:7-4:58 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "customize-text-view.png")' altText: 'alt'
├─ ContentAndMedia @14:4-23:5 mediaPosition: 'trailing'
│ ├─ MarkupContainer (2 elements)
│ └─ ImageMedia @22:7-22:58 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "customize-text-view.png")' altText: 'alt'
├─ MarkupContainer (1 element)
├─ Stack @27:4-51:5
│ ├─ ContentAndMedia @28:7-37:8 mediaPosition: 'trailing'
│ │ ├─ MarkupContainer (2 elements)
│ │ └─ ImageMedia @32:10-32:64 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "this-is-still-trailing.png")' altText: 'alt'
│ └─ ContentAndMedia @41:7-50:8 mediaPosition: 'trailing'
│ ├─ MarkupContainer (2 elements)
│ └─ ImageMedia @49:10-49:58 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "this-is-trailing.png")' altText: 'alt'
├─ MarkupContainer (3 elements)
└─ Stack @61:4-80:5
├─ ContentAndMedia @62:7-71:8 mediaPosition: 'trailing'
│ ├─ MarkupContainer (2 elements)
│ └─ ImageMedia @66:10-66:64 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "this-is-still-trailing.png")' altText: 'alt'
├─ ContentAndMedia @73:7-75:8 mediaPosition: 'leading'
│ ├─ MarkupContainer (empty)
│ └─ ImageMedia @74:10-74:57 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "this-is-leading.png")' altText: 'alt'
└─ ContentAndMedia @77:7-79:8 mediaPosition: 'leading'
├─ MarkupContainer (empty)
└─ ImageMedia @78:10-78:57 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "this-is-leading.png")' altText: 'alt'
"""
XCTAssertEqual(expectedDump, article.dump())
}
}
}
func testAssessment() throws {
let source = """
@Article(time: 20) {
@Intro(title: "Basic Augmented Reality App") {
This is some text in an intro.
@Image(source: myimage.png, alt: image)
}
## The first section
This is content in the first section.
## The second section
This is content in the section section.
### A subsection
This article has a subsection in the second section.
@Assessments {
@MultipleChoice {
This is the first question's phrasing.
@Choice(isCorrect: true) {
This the correct answer.
@Justification(reaction: "Nice work!") {
This is correct because it is.
}
}
@Choice(isCorrect: false) {
`anchor.intersects(view)`
@Justification(reaction: "Maybe next time.") {
This is incorrect because it is.
}
}
}
}
}
"""
let document = Document(parsing: source, options: .parseBlockDirectives)
let directive = document.child(at: 0) as? BlockDirective
XCTAssertNotNil(directive)
let (bundle, context) = try testBundleAndContext(named: "TestBundle")
directive.map { directive in
var problems = [Problem]()
XCTAssertEqual(TutorialArticle.directiveName, directive.name)
let article = TutorialArticle(from: directive, source: nil, for: bundle, in: context, problems: &problems)
XCTAssertNotNil(article)
XCTAssertEqual(0, problems.count)
article.map { article in
let expectedDump = """
TutorialArticle @1:1-42:2 title: 'Basic Augmented Reality App' time: '20'
├─ Intro @2:4-7:5 title: 'Basic Augmented Reality App'
│ ├─ MarkupContainer (1 element)
│ └─ ImageMedia @6:7-6:46 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "myimage.png")' altText: 'image'
├─ MarkupContainer (6 elements)
└─ Assessments @21:4-41:5
└─ MultipleChoice @22:7-40:8 title: 'SwiftDocC.MarkupContainer'
├─ MarkupContainer (empty)
├─ Choice @25:10-31:11 isCorrect: true
│ ├─ MarkupContainer (1 element)
│ └─ Justification @28:13-30:14 reaction: 'Nice work!'
│ └─ MarkupContainer (1 element)
└─ Choice @33:10-39:11 isCorrect: false
├─ MarkupContainer (1 element)
└─ Justification @36:13-38:14 reaction: 'Maybe next time.'
└─ MarkupContainer (1 element)
"""
XCTAssertEqual(expectedDump, article.dump())
}
}
}
func testAnalyzeNode() throws {
let title = "unreferenced-tutorial"
let reference = ResolvedTopicReference(bundleIdentifier: "org.swift.docc.TopicGraphTests", path: "/\(title)", sourceLanguage: .swift)
let node = TopicGraph.Node(reference: reference, kind: .technology, source: .file(url: URL(fileURLWithPath: "/path/to/\(title)")), title: title)
let (_, context) = try testBundleAndContext(named: "TestBundle")
context.topicGraph.addNode(node)
let engine = DiagnosticEngine()
TutorialArticle.analyze(node, completedContext: context, engine: engine)
XCTAssertEqual(engine.problems.count, 1)
XCTAssertEqual(engine.problems.map { $0.diagnostic.identifier }, ["org.swift.docc.UnreferencedTutorialArticle"])
XCTAssertTrue(engine.problems.allSatisfy { $0.diagnostic.severity == .warning })
let problem = try XCTUnwrap(engine.problems.first)
let source = try XCTUnwrap(problem.diagnostic.source)
XCTAssertTrue(source.isFileURL)
}
func testAnalyzeExternalNode() throws {
let title = "unreferenced-tutorial"
let reference = ResolvedTopicReference(bundleIdentifier: "org.swift.docc.TopicGraphTests", path: "/\(title)", sourceLanguage: .swift)
let node = TopicGraph.Node(reference: reference, kind: .technology, source: .external, title: title)
let (_, context) = try testBundleAndContext(named: "TestBundle")
context.topicGraph.addNode(node)
let engine = DiagnosticEngine()
TutorialArticle.analyze(node, completedContext: context, engine: engine)
XCTAssertEqual(engine.problems.count, 1)
XCTAssertEqual(engine.problems.map { $0.diagnostic.identifier }, ["org.swift.docc.UnreferencedTutorialArticle"])
XCTAssertTrue(engine.problems.allSatisfy { $0.diagnostic.severity == .warning })
let problem = try XCTUnwrap(engine.problems.first)
XCTAssertNil(problem.diagnostic.source)
}
func testAnalyzeFragmentNode() throws {
let title = "unreferenced-tutorial"
let url = URL(fileURLWithPath: "/path/to/\(title)")
let reference = ResolvedTopicReference(bundleIdentifier: "org.swift.docc.TopicGraphTests", path: "/\(title)", sourceLanguage: .swift)
let range = SourceLocation(line: 1, column: 1, source: url)..<SourceLocation(line: 1, column: 1, source: url)
let node = TopicGraph.Node(reference: reference, kind: .technology, source: .range(range, url: url) , title: title)
let (_, context) = try testBundleAndContext(named: "TestBundle")
context.topicGraph.addNode(node)
let engine = DiagnosticEngine()
TutorialArticle.analyze(node, completedContext: context, engine: engine)
XCTAssertEqual(engine.problems.count, 1)
XCTAssertEqual(engine.problems.map { $0.diagnostic.identifier }, ["org.swift.docc.UnreferencedTutorialArticle"])
XCTAssertTrue(engine.problems.allSatisfy { $0.diagnostic.severity == .warning })
let problem = try XCTUnwrap(engine.problems.first)
XCTAssertNil(problem.diagnostic.source)
}
/// Verify that a `TutorialArticle` only recognizes chapter, volume, or technology nodes as valid parents.
func testAnalyzeForValidParent() throws {
func node(withTitle title: String, ofKind kind: DocumentationNode.Kind) -> TopicGraph.Node {
let url = URL(fileURLWithPath: "/path/to/\(title)")
let reference = ResolvedTopicReference(bundleIdentifier: "org.swift.docc.TutorialArticleTests", path: "/\(title)", sourceLanguage: .swift)
let range = SourceLocation(line: 1, column: 1, source: url)..<SourceLocation(line: 1, column: 1, source: url)
return TopicGraph.Node(reference: reference, kind: kind, source: .range(range, url: url) , title: title)
}
let (_, context) = try testBundleAndContext(named: "TestBundle")
let tutorialArticleNode = node(withTitle: "tutorial-article", ofKind: .tutorialArticle)
let validParents: Set<DocumentationNode.Kind> = [.chapter, .technology, .volume]
let otherKinds: Set<DocumentationNode.Kind> = Set(DocumentationNode.Kind.allKnownValues).subtracting(validParents)
for kind in validParents {
let parentNode = node(withTitle: "technology-x", ofKind: kind)
context.topicGraph.addEdge(from: parentNode, to: tutorialArticleNode)
let engine = DiagnosticEngine()
TutorialArticle.analyze(tutorialArticleNode, completedContext: context, engine: engine)
XCTAssertEqual(engine.problems.count, 0)
context.topicGraph.removeEdges(from: parentNode)
context.topicGraph.nodes.removeValue(forKey: parentNode.reference)
XCTAssert(context.parents(of: tutorialArticleNode.reference).isEmpty)
}
for kind in otherKinds {
let parentNode = node(withTitle: "technology-x", ofKind: kind)
context.topicGraph.addEdge(from: parentNode, to: tutorialArticleNode)
let engine = DiagnosticEngine()
TutorialArticle.analyze(tutorialArticleNode, completedContext: context, engine: engine)
XCTAssertEqual(engine.problems.count, 1)
XCTAssertTrue(engine.problems.allSatisfy { $0.diagnostic.severity == .warning })
let problem = try XCTUnwrap(engine.problems.first)
XCTAssertEqual(problem.diagnostic.identifier, "org.swift.docc.UnreferencedTutorialArticle")
context.topicGraph.removeEdges(from: parentNode)
context.topicGraph.nodes.removeValue(forKey: parentNode.reference)
XCTAssert(context.parents(of: tutorialArticleNode.reference).isEmpty)
}
}
}
|