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
|
/*
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 TutorialTests: XCTestCase {
func testEmpty() throws {
let source = "@Tutorial"
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(Tutorial.directiveName, directive.name)
let tutorial = Tutorial(from: directive, source: nil, for: bundle, in: context, problems: &problems)
XCTAssertNil(tutorial)
XCTAssertEqual(
[
"org.swift.docc.HasExactlyOne<\(Tutorial.self), \(Intro.self)>.Missing",
"org.swift.docc.HasAtLeastOne<\(Tutorial.self), \(TutorialSection.self)>",
],
problems.map { $0.diagnostic.identifier }
)
XCTAssert(problems.map { $0.diagnostic.severity }.allSatisfy { $0 == .warning })
}
}
func testValid() throws {
let source = """
@Tutorial(time: 20) {
@XcodeRequirement(title: "Xcode X.Y Beta Z", destination: "https://www.example.com/download")
@Intro(title: "Basic Augmented Reality App") {
@Video(source: test.mp4, poster: poster.png)
@Image(source: myimage.png, alt: image)
}
@Section(title: "Create a New AR Project") {
@ContentAndMedia {
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Phasellus faucibus scelerisque eleifend donec pretium.
Ultrices dui sapien eget mi proin sed libero enim. Quis auctor elit sed vulputate mi sit amet.
@Image(source: figure1.png, alt: figure1)

Quis auctor elit sed vulputate mi sit amet.
}
@Steps {
Let's get started building the Augmented Reality app.
@Step {
Lorem ipsum dolor sit amet, consectetur.
@Image(source: xcode1.png, alt: xcode1)
@Code(file: code1.swift, name: MyCode.swift) {
@Image(source: screenshot.png, alt: screenshot)
}
}
@Step {
Lorem ipsum dolor sit amet, consectetur.
@Video(source: app.mov)
@Code(file: code2.swift, name: MyCode.swift, reset: true) {
@Image(source: screenshot.png, alt: screenshot)
}
}
> Experiment: Do something cool.
@Step {
Lorem ipsum dolor sit amet, consectetur.
@Video(source: app2.mov)
@Code(file: othercode1.swift, name: OtherCode.swift)
}
}
}
@Section(title: "Initiate ARKit Plane Detection") {
@ContentAndMedia {
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Phasellus faucibus scelerisque eleifend donec pretium.
Ultrices dui sapien eget mi proin sed libero enim. Quis auctor elit sed vulputate mi sit amet.
@Image(source: xcode.png, alt: xcode)
}
@Steps {
Let's get started building the Augmented Reality app.
@Step {
Lorem ipsum dolor sit amet, consectetur.
@Image(source: xcode1.png, alt: xcode)
}
@Step {
Lorem ipsum dolor sit amet, consectetur.
@Video(source: app.mov)
}
@Step {
Lorem ipsum dolor sit amet, consectetur.
@Video(source: app2.mov)
}
}
}
@Assessments {
@MultipleChoice {
Lorem ipsum dolor sit amet?
Phasellus faucibus scelerisque eleifend donec pretium.
```swift
let scene = ARSCNView()
let anchor = scene.anchor(for: node)
```
@Choice(isCorrect: true) {
`anchor.hitTest(view)`
@Justification(reaction: "Correct!") {
This is correct because it is.
}
}
@Choice(isCorrect: false) {
`anchor.intersects(view)`
@Justification {
This is incorrect because it is.
}
}
@Choice(isCorrect: false) {
`anchor.intersects(view)`
@Justification(reaction: Sorry) {
This is incorrect because it is.
}
}
}
@MultipleChoice {
Lorem ipsum dolor sit amet?
Phasellus faucibus scelerisque eleifend donec pretium.
```swift
let scene = ARSCNView()
let anchor = scene.anchor(for: node)
```
@Choice(isCorrect: true) {
`anchor.hitTest(view)`
@Justification {
This is correct because it is.
}
}
@Choice(isCorrect: false) {
`anchor.intersects(view)`
@Justification {
This is incorrect because it is.
}
}
@Choice(isCorrect: false) {
`anchor.intersects(view)`
@Justification {
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(Tutorial.directiveName, directive.name)
let tutorial = Tutorial(from: directive, source: nil, for: bundle, in: context, problems: &problems)
XCTAssertNotNil(tutorial)
XCTAssertTrue(problems.isEmpty)
tutorial.map { tutorial in
let expectedDump = """
Tutorial @1:1-150:2 projectFiles: nil
├─ Intro @3:4-6:5 title: 'Basic Augmented Reality App'
│ ├─ MarkupContainer (empty)
│ ├─ ImageMedia @5:7-5:46 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "myimage.png")' altText: 'image'
│ └─ VideoMedia @4:7-4:51 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "test.mp4")' poster: 'Optional(SwiftDocC.ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "poster.png"))'
├─ XcodeRequirement @2:4-2:97 title: 'Xcode X.Y Beta Z' destination: 'https://www.example.com/download'
├─ TutorialSection @8:4-48:5
│ ├─ ContentAndMedia @9:7-19:8 mediaPosition: 'trailing'
│ │ ├─ MarkupContainer (3 elements)
│ │ └─ ImageMedia @14:10-14:51 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "figure1.png")' altText: 'figure1'
│ └─ Steps @21:7-47:8
│ ├─ MarkupContainer (1 element)
│ ├─ Step @25:10-31:11
│ │ ├─ MarkupContainer (1 element)
│ │ ├─ MarkupContainer (empty)
│ │ └─ Code @28:13-30:14 fileReference: ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "code1.swift") fileName: 'MyCode.swift' shouldResetDiff: false preview: Optional(SwiftDocC.ImageMedia)
│ ├─ Step @32:10-38:11
│ │ ├─ MarkupContainer (1 element)
│ │ ├─ MarkupContainer (empty)
│ │ └─ Code @35:13-37:14 fileReference: ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "code2.swift") fileName: 'MyCode.swift' shouldResetDiff: true preview: Optional(SwiftDocC.ImageMedia)
│ ├─ MarkupContainer (1 element)
│ └─ Step @42:10-46:11
│ ├─ MarkupContainer (1 element)
│ ├─ MarkupContainer (empty)
│ └─ Code @45:13-45:65 fileReference: ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "othercode1.swift") fileName: 'OtherCode.swift' shouldResetDiff: false preview: nil
├─ TutorialSection @50:4-77:5
│ ├─ ContentAndMedia @51:7-58:8 mediaPosition: 'trailing'
│ │ ├─ MarkupContainer (2 elements)
│ │ └─ ImageMedia @57:10-57:47 source: 'ResourceReference(bundleIdentifier: "org.swift.docc.example", path: "xcode.png")' altText: 'xcode'
│ └─ Steps @60:7-76:8
│ ├─ MarkupContainer (1 element)
│ ├─ Step @64:10-67:11
│ │ ├─ MarkupContainer (1 element)
│ │ └─ MarkupContainer (empty)
│ ├─ Step @68:10-71:11
│ │ ├─ MarkupContainer (1 element)
│ │ └─ MarkupContainer (empty)
│ └─ Step @72:10-75:11
│ ├─ MarkupContainer (1 element)
│ └─ MarkupContainer (empty)
└─ Assessments @79:4-149:5
├─ MultipleChoice @80:7-113:8 title: 'SwiftDocC.MarkupContainer'
│ ├─ MarkupContainer (2 elements)
│ ├─ Choice @90:10-96:11 isCorrect: true
│ │ ├─ MarkupContainer (1 element)
│ │ └─ Justification @93:13-95:14 reaction: 'Correct!'
│ │ └─ MarkupContainer (1 element)
│ ├─ Choice @98:10-104:11 isCorrect: false
│ │ ├─ MarkupContainer (1 element)
│ │ └─ Justification @101:13-103:14
│ │ └─ MarkupContainer (1 element)
│ └─ Choice @106:10-112:11 isCorrect: false
│ ├─ MarkupContainer (1 element)
│ └─ Justification @109:13-111:14 reaction: 'Sorry'
│ └─ MarkupContainer (1 element)
└─ MultipleChoice @115:7-148:8 title: 'SwiftDocC.MarkupContainer'
├─ MarkupContainer (2 elements)
├─ Choice @125:10-131:11 isCorrect: true
│ ├─ MarkupContainer (1 element)
│ └─ Justification @128:13-130:14
│ └─ MarkupContainer (1 element)
├─ Choice @133:10-139:11 isCorrect: false
│ ├─ MarkupContainer (1 element)
│ └─ Justification @136:13-138:14
│ └─ MarkupContainer (1 element)
└─ Choice @141:10-147:11 isCorrect: false
├─ MarkupContainer (1 element)
└─ Justification @144:13-146:14
└─ MarkupContainer (1 element)
"""
XCTAssertEqual(expectedDump, tutorial.dump())
}
}
}
func testDuplicateSectionTitle() throws {
let source = """
@Tutorial(time: 20) {
@XcodeRequirement(title: "Xcode X.Y Beta Z", destination: "https://www.example.com/download")
@Intro(title: "Basic Augmented Reality App") {
@Video(source: test.mp4, poster: poster.png)
@Image(source: myimage.png, alt: image)
}
@Section(title: "Duplicate Title") {
@ContentAndMedia {
Quis auctor elit sed vulputate mi sit amet.
}
@Steps {
@Step {
Lorem ipsum dolor sit amet, consectetur.
}
}
}
@Section(title: "Duplicate Title") {
@ContentAndMedia {
Quis auctor elit sed vulputate mi sit amet.
}
@Steps {
@Step {
Lorem ipsum dolor sit amet, consectetur.
}
}
}
@Assessments {
@MultipleChoice {
Lorem ipsum dolor sit amet?
Phasellus faucibus scelerisque eleifend donec pretium.
```swift
let scene = ARSCNView()
let anchor = scene.anchor(for: node)
```
@Choice(isCorrect: true) {
`anchor.hitTest(view)`
@Justification(reaction: "Correct!") {
This is correct because it is.
}
}
@Choice(isCorrect: false) {
`anchor.intersects(view)`
@Justification {
This is incorrect because it is.
}
}
@Choice(isCorrect: false) {
`anchor.intersects(view)`
@Justification(reaction: Sorry) {
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(Tutorial.directiveName, directive.name)
let tutorial = Tutorial(from: directive, source: nil, for: bundle, in: context, problems: &problems)
XCTAssertNotNil(tutorial)
XCTAssertEqual(1, tutorial?.sections.count)
XCTAssertEqual([
"org.swift.docc.\(Tutorial.self).DuplicateSectionTitle",
], problems.map { $0.diagnostic.identifier })
}
}
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()
Tutorial.analyze(node, completedContext: context, engine: engine)
XCTAssertEqual(engine.problems.count, 1)
XCTAssertEqual(engine.problems.map { $0.diagnostic.identifier }, ["org.swift.docc.UnreferencedTutorial"])
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()
Tutorial.analyze(node, completedContext: context, engine: engine)
XCTAssertEqual(engine.problems.count, 1)
XCTAssertEqual(engine.problems.map { $0.diagnostic.identifier }, ["org.swift.docc.UnreferencedTutorial"])
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()
Tutorial.analyze(node, completedContext: context, engine: engine)
XCTAssertEqual(engine.problems.count, 1)
XCTAssertEqual(engine.problems.map { $0.diagnostic.identifier }, ["org.swift.docc.UnreferencedTutorial"])
XCTAssertTrue(engine.problems.allSatisfy { $0.diagnostic.severity == .warning })
let problem = try XCTUnwrap(engine.problems.first)
XCTAssertNil(problem.diagnostic.source)
}
/// Verify that a `Tutorial` 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 tutorialNode = node(withTitle: "tutorial-article", ofKind: .tutorial)
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: tutorialNode)
let engine = DiagnosticEngine()
Tutorial.analyze(tutorialNode, 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: tutorialNode.reference).isEmpty)
}
for kind in otherKinds {
let parentNode = node(withTitle: "technology-x", ofKind: kind)
context.topicGraph.addEdge(from: parentNode, to: tutorialNode)
let engine = DiagnosticEngine()
Tutorial.analyze(tutorialNode, 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.UnreferencedTutorial")
context.topicGraph.removeEdges(from: parentNode)
context.topicGraph.nodes.removeValue(forKey: parentNode.reference)
XCTAssert(context.parents(of: tutorialNode.reference).isEmpty)
}
}
}
|