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
|
/*
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 XCTest
import Markdown
@testable import SwiftDocC
@testable import SwiftDocCUtilities
import SwiftDocCTestUtilities
class SemanticAnalyzerTests: XCTestCase {
private let catalogHierarchy = Folder(name: "SemanticAnalyzerTests.docc", content: [
Folder(name: "Symbols", content: []),
Folder(name: "Resources", content: [
TextFile(name: "Oops.md", utf8Content: """
> Oops! This is a random markdown file with no directives or anything.
`inlineCode`
- A
*******
```swift
func foo() {}
```
This *should not* [crash](test.html) despite not being **valid**.

"""),
TextFile(name: "MyArticle.md", utf8Content: """
# Check out my article
My article provides lots of detailed information.
This is my article's overview.
## This is a section.
```swift
func foo() {}
```

"""),
]),
InfoPlist(displayName: "TestBundle", identifier: "com.test.example"),
])
func testDoNotCrashOnInvalidContent() throws {
let (bundle, context) = try loadBundle(catalog: catalogHierarchy)
XCTAssertThrowsError(try context.entity(with: ResolvedTopicReference(bundleID: bundle.id, path: "/Oops", sourceLanguage: .swift)))
}
func testWarningsAboutDirectiveSupport() throws {
func problemsConvertingTestContent(withFileExtension fileExtension: String) throws -> (unsupportedTopLevelChildProblems: [Problem], missingTopLevelChildProblems: [Problem]) {
let catalogHierarchy = Folder(name: "SemanticAnalyzerTests.docc", content: [
TextFile(name: "FileWithDirective.\(fileExtension)", utf8Content: """
@Article
"""),
TextFile(name: "FileWithoutDirective.\(fileExtension)", utf8Content: """
# Article title
A paragraph of text
"""),
InfoPlist(displayName: "TestBundle", identifier: "com.test.example"),
])
let (_, context) = try loadBundle(catalog: catalogHierarchy)
return (
context.problems.filter({ $0.diagnostic.identifier == "org.swift.docc.unsupportedTopLevelChild" }),
context.problems.filter({ $0.diagnostic.identifier == "org.swift.docc.missingTopLevelChild" })
)
}
do {
let problems = try problemsConvertingTestContent(withFileExtension: "md")
XCTAssertEqual(problems.missingTopLevelChildProblems.count, 0)
XCTAssertEqual(problems.unsupportedTopLevelChildProblems.count, 1)
if let diagnostic = problems.unsupportedTopLevelChildProblems.first?.diagnostic {
XCTAssertEqual(diagnostic.summary, "Found unsupported 'Article' directive in '.md' file")
XCTAssertEqual(diagnostic.severity, .warning)
XCTAssertEqual(diagnostic.source?.lastPathComponent, "FileWithDirective.md")
}
}
do {
let problems = try problemsConvertingTestContent(withFileExtension: "tutorial")
XCTAssertEqual(problems.missingTopLevelChildProblems.count, 1)
XCTAssertEqual(problems.unsupportedTopLevelChildProblems.count, 0)
if let diagnostic = problems.missingTopLevelChildProblems.first?.diagnostic {
XCTAssertEqual(diagnostic.summary, "No valid content was found in this file")
XCTAssertEqual(diagnostic.explanation, "A '.tutorial' file should contain a top-level directive ('Tutorials', 'Tutorial', or 'Article') and valid child content. Only '.md' files support content without a top-level directive")
XCTAssertEqual(diagnostic.severity, .warning)
XCTAssertEqual(diagnostic.source?.lastPathComponent, "FileWithoutDirective.tutorial")
}
}
}
func testDoesNotWarnOnEmptyTutorials() throws {
let (bundle, context) = try loadBundle(catalog: catalogHierarchy)
let document = Document(parsing: "", options: .parseBlockDirectives)
var analyzer = SemanticAnalyzer(source: URL(string: "/empty.tutorial"), context: context, bundle: bundle)
let semantic = analyzer.visitDocument(document)
XCTAssertNil(semantic)
XCTAssert(analyzer.problems.isEmpty)
}
}
|