File: SemanticAnalyzerTests.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (136 lines) | stat: -rw-r--r-- 6,022 bytes parent folder | download
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
/*
 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
import Markdown
@testable import SwiftDocC
@testable import SwiftDocCUtilities
import SwiftDocCTestUtilities

class SemanticAnalyzerTests: XCTestCase {
    let bundleFolderHierarchy = 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**.

                ![alt](test.png)
                """),
            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() {}
                ```

                ![alt](test.png)
                """),
            ]),
        InfoPlist(displayName: "TestBundle", identifier: "com.test.example"),
    ])
    
    func testDontCrashOnInvalidContent() throws {
        let workspace = DocumentationWorkspace()
        let context = try! DocumentationContext(dataProvider: workspace)
        let bundleURL = try bundleFolderHierarchy.write(inside: createTemporaryDirectory())
        let dataProvider = try LocalFileSystemDataProvider(rootURL: bundleURL)
        try workspace.registerProvider(dataProvider)
        let bundle = context.bundle(identifier: "com.test.example")!
        
        XCTAssertThrowsError(try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/Oops", sourceLanguage: .swift)))
    }
    
    func testWarningsAboutDirectiveSupport() throws {
        func problemsConvertingTestContent(withFileExtension fileExtension: String) throws -> (unsupportedTopLevelChildProblems: [Problem], missingTopLevelChildProblems: [Problem]) {
            let workspace = DocumentationWorkspace()
            let context = try! DocumentationContext(dataProvider: workspace)
            
            let folderHierarchy = 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 bundleURL = try folderHierarchy.write(inside: createTemporaryDirectory())
            let dataProvider = try LocalFileSystemDataProvider(rootURL: bundleURL)
            try workspace.registerProvider(dataProvider)
            
            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 workspace = DocumentationWorkspace()
        let context = try! DocumentationContext(dataProvider: workspace)
        let bundleURL = try bundleFolderHierarchy.write(inside: createTemporaryDirectory())
        let dataProvider = try LocalFileSystemDataProvider(rootURL: bundleURL)
        try workspace.registerProvider(dataProvider)
        let bundle = context.bundle(identifier: "com.test.example")!
        
        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)
    }
}