File: LineHighlighterTests.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 (293 lines) | stat: -rw-r--r-- 13,535 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
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
/*
 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
import SwiftDocCTestUtilities

class LineHighlighterTests: XCTestCase {
    static let bundleIdentifier = "org.swift.docc.LineHighlighterTests"
    static let defaultOverview = TextFile(name: "TechnologyX.tutorial", utf8Content: """
        @Technology(name: "TechnologyX") {
           @Intro(title: "Technology X") {

              You'll learn all about Technology X.

              @Image(source: arkit.png, alt: arkit)
           }

           @Volume(title: "Volume 1") {
              This volume contains Chapter 1.

              @Chapter(name: "Module 1") {
                 In this chapter, you'll follow Tutorial 1.
                 @TutorialReference(tutorial: Tutorial)
                 @Image(source: blah, alt: blah)
              }
           }
        }
        """)

    static func bundleFolder(overview: TextFile = defaultOverview,
                             tutorial: TextFile,
                             codeFiles: [TextFile]) -> Folder {
        return Folder(name: "TestNoSteps.docc", content: [
            InfoPlist(displayName: "Line Highlighter Tests", identifier: bundleIdentifier),
            Folder(name: "Symbols", content: []),
            Folder(name: "Resources", content: [
                overview,
                tutorial,
                ] + codeFiles),
            ])
    }
    
    func testBundleAndContext(bundleRoot: Folder, bundleIdentifier: BundleIdentifier) throws -> (DocumentationBundle, DocumentationContext) {
        let workspace = DocumentationWorkspace()
        let context = try! DocumentationContext(dataProvider: workspace)
        
        let bundleURL = try bundleRoot.write(inside: createTemporaryDirectory())
        
        let dataProvider = try LocalFileSystemDataProvider(rootURL: bundleURL)
        try workspace.registerProvider(dataProvider)
        let bundle = context.bundle(identifier: bundleIdentifier)!
        return (bundle, context)
    }
    
    func highlights(tutorialFile: TextFile, codeFiles: [TextFile]) throws -> [LineHighlighter.Result] {
        let bundleFolder = LineHighlighterTests.bundleFolder(tutorial: tutorialFile, codeFiles: codeFiles)
        let (bundle, context) = try testBundleAndContext(bundleRoot: bundleFolder, bundleIdentifier: LineHighlighterTests.bundleIdentifier)
        
        let tutorialReference = ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/tutorials/Line-Highlighter-Tests/Tutorial", fragment: nil, sourceLanguage: .swift)
        let tutorial = try context.entity(with: tutorialReference).semantic as! Tutorial
        let section = tutorial.sections.first!
        return LineHighlighter(context: context, tutorialSection: section, tutorialReference: tutorialReference).highlights
    }
    
    func testNoSteps() throws {
        let tutorialFile = TextFile(name: "Tutorial.tutorial", utf8Content: """
            @Tutorial(time: 20, projectFiles: nothing.zip) {
               @XcodeRequirement(title: "Xcode X.Y Beta Z", destination: "https://www.example.com/download")
               @Intro(title: "Test Intro")
               @Section(title: "Section with no steps") {
                  @ContentAndMedia {
                     This is an introduction.
                  }
               }
               @Assessments
            }
            """)
        XCTAssertTrue(try highlights(tutorialFile: tutorialFile, codeFiles: []).isEmpty)
    }

    func testOneStep() throws {
        let tutorialFile = TextFile(name: "Tutorial.tutorial", utf8Content: """
            @Tutorial(title: "No Steps", time: 20, projectFiles: nothing.zip) {
               @XcodeRequirement(title: "Xcode X.Y Beta Z", destination: "https://www.example.com/download")
               @Intro(title: "Test Intro")
            @Section(title: "Section with one step") {
               @ContentAndMedia {
                  This is an introduction.
               }
               @Steps {
                  @Step {
                     This is the only step.
                     @Code(file: code1.swift, name: MyCode.swift)
                  }
               }
            }
            @Assessments
            """)
        let code1 = TextFile(name: "code1.swift", utf8Content: "func foo() {}")
        let results = try highlights(tutorialFile: tutorialFile, codeFiles: [code1])
        XCTAssertEqual(1, results.count)
        results.first.map { result in
            XCTAssertEqual(ResourceReference(bundleIdentifier: LineHighlighterTests.bundleIdentifier, path: code1.name), result.file)
            XCTAssertTrue(result.highlights.isEmpty)
        }
    }
    
    func testOneStepWithPrevious() throws {
        let tutorialFile = TextFile(name: "Tutorial.tutorial", utf8Content: """
            @Tutorial(title: "No Steps", time: 20, projectFiles: nothing.zip) {
               @XcodeRequirement(title: "Xcode X.Y Beta Z", destination: "https://www.example.com/download")
               @Intro(title: "Test Intro")
               @Section(title: "Section with one steps") {
                  @ContentAndMedia {
                  This is an introduction.
               }
               
               @Steps {
                  @Step {
                     This is the only step.
                     @Code(file: code1.swift, name: MyCode.swift, previousFile: code0.swift)
                  }
               }
            }
            @Assessments
            """)
        let code0 = TextFile(name: "code0.swift", utf8Content: "func foo() {}")
        let code1 = TextFile(name: "code1.swift", utf8Content: "func foo() {}\nfunc bar() {}")
        let results = try highlights(tutorialFile: tutorialFile, codeFiles: [code0, code1])
        XCTAssertEqual(1, results.count)
        results.first.map { result in
            XCTAssertEqual(ResourceReference(bundleIdentifier: LineHighlighterTests.bundleIdentifier, path: code1.name), result.file)
            XCTAssertEqual(1, result.highlights.count)
            result.highlights.first.map { highlight in
                XCTAssertEqual(2, highlight.line)
                XCTAssertNil(highlight.start)
                XCTAssertNil(highlight.length)
            }
        }
    }
    
    func testNameMismatch() throws {
        let tutorialFile = TextFile(name: "Tutorial.tutorial", utf8Content: """
        @Tutorial(title: "No Steps", time: 20, projectFiles: nothing.zip) {
          @XcodeRequirement(title: "Xcode X.Y Beta Z", destination: "https://www.example.com/download")
          @Intro(title: "Test Intro")
          @Section(title: "Section with two steps") {
            @ContentAndMedia {
              This is an introduction.
            }
            @Steps {
              @Step {
                This is the first step.
                @Code(file: code1.swift, name: somefile.swift)
              }
              @Step {
                This is the second step. This code will not produce diffs because it has a different presentation name.
                @Code(file: code2.swift, name: otherfile.swift)
              }
            }
          }
          @Assessments()
        }
        """)
        
        let code1 = TextFile(name: "code1.swift", utf8Content: "func foo() {}")
        let code2 = TextFile(name: "code2.swift", utf8Content: "func foo() {}\nfunc bar() {}")
        let results = try highlights(tutorialFile: tutorialFile, codeFiles: [code1, code2])
        XCTAssertEqual(2, results.count)
        
        XCTAssertEqual(ResourceReference(bundleIdentifier: LineHighlighterTests.bundleIdentifier, path: code1.name), results[0].file)
        XCTAssertTrue(results[0].highlights.isEmpty)
        
        XCTAssertEqual(ResourceReference(bundleIdentifier: LineHighlighterTests.bundleIdentifier, path: code2.name), results[1].file)
        XCTAssertTrue(results[1].highlights.isEmpty)
    }
    
    func testResetDiffAtStart() throws {
        let tutorialFile = TextFile(name: "Tutorial.tutorial", utf8Content: """
            @Tutorial(title: "No Steps", time: 20, projectFiles: nothing.zip) {
               @XcodeRequirement(title: "Xcode X.Y Beta Z", destination: "https://www.example.com/download")
               @Intro(title: "Test Intro")
            @Section(title: "Task with one step") {
               @ContentAndMedia {
                  This is an introduction.
               }
               @Steps {
                  @Step {
                     This is the only step.
                     @Code(file: code1.swift, name: MyCode.swift, previousFile: code0.swift, reset: true)
                  }
               }
            }
            @Assessments
            """)
        let code0 = TextFile(name: "code0.swift", utf8Content: "func foo() {}")
        let code1 = TextFile(name: "code1.swift", utf8Content: "func foo() {}\nfunc bar() {}")
        let results = try highlights(tutorialFile: tutorialFile, codeFiles: [code0, code1])
        XCTAssertEqual(1, results.count)
        results.first.map { result in
            XCTAssertEqual(ResourceReference(bundleIdentifier: LineHighlighterTests.bundleIdentifier, path: code1.name), result.file)
            XCTAssertTrue(result.highlights.isEmpty)
        }
    }
    
    func testResetDiff() throws {
        let tutorialFile = TextFile(name: "Tutorial.tutorial", utf8Content: """
            @Tutorial(title: "No Steps", time: 20, projectFiles: nothing.zip) {
               @XcodeRequirement(title: "Xcode X.Y Beta Z", destination: "https://www.example.com/download")
               @Intro(title: "Test Intro")
            @Section(title: "Task with two steps") {
               @ContentAndMedia {
                  This is an introduction.
               }
               @Steps {
                  @Step {
                     This is the first step.
                     @Code(file: code1.swift, name: somefile.swift)
                  }
                  @Step {
                     This is the second step. This will not produce diffs because `reset` is `true`.
                     @Code(file: code2.swift, name: somefile.swift, reset: true)
                  }
               }
            }
            @Assessments
            """)
        let code1 = TextFile(name: "code1.swift", utf8Content: "func foo() {}")
        let code2 = TextFile(name: "code2.swift", utf8Content: "func foo() {}\nfunc bar() {}")
        let results = try highlights(tutorialFile: tutorialFile, codeFiles: [code1, code2])
        
        XCTAssertEqual(2, results.count)
        
        XCTAssertEqual(ResourceReference(bundleIdentifier: LineHighlighterTests.bundleIdentifier, path: code1.name), results[0].file)
        XCTAssertTrue(results[0].highlights.isEmpty)
        
        XCTAssertEqual(ResourceReference(bundleIdentifier: LineHighlighterTests.bundleIdentifier, path: code2.name), results[1].file)
        XCTAssertTrue(results[1].highlights.isEmpty)
    }
    
    func testPreviousOverride() throws {
        let tutorialFile = TextFile(name: "Tutorial.tutorial", utf8Content: """
            @Tutorial(title: "No Steps", time: 20, projectFiles: nothing.zip) {
               @XcodeRequirement(title: "Xcode X.Y Beta Z", destination: "https://www.example.com/download")
               @Intro(title: "Test Intro")
            @Section(title: "Section with two steps") {
               @ContentAndMedia {
                  This is an introduction.
               }
               @Steps {
                  @Step {
                     This is the first step.
                     @Code(file: code0.swift, name: somefile.swift)
                  }
                  @Step {
                     This is the second step.
                     Normally, this diff would produce 2 highlighted lines because code0.swift has no lines and code2.swift has two.
                     However, we're overriding the previous file to use for diffing, code1.swift, which has one line, so we expect
                     to see a single line highlight for `func bar() {}` (line 2).
                     @Code(file: code2.swift, name: somefile.swift, previousFile: code1.swift)
                  }
               }
            }
            @Assessments
            """)
        let code0 = TextFile(name: "code0.swift", utf8Content: "")
        let code1 = TextFile(name: "code1.swift", utf8Content: "func foo() {}")
        let code2 = TextFile(name: "code2.swift", utf8Content: "func foo() {}\nfunc bar() {}")
        let results = try highlights(tutorialFile: tutorialFile, codeFiles: [code0, code1, code2])
        
        XCTAssertEqual(2, results.count)
        
        XCTAssertEqual(ResourceReference(bundleIdentifier: LineHighlighterTests.bundleIdentifier, path: code0.name), results[0].file)
        XCTAssertTrue(results[0].highlights.isEmpty)
        
        XCTAssertEqual(ResourceReference(bundleIdentifier: LineHighlighterTests.bundleIdentifier, path: code2.name), results[1].file)
        XCTAssertEqual(1, results[1].highlights.count)
        results[1].highlights.first.map { highlight in
            XCTAssertEqual(2, highlight.line)
            XCTAssertNil(highlight.start)
            XCTAssertNil(highlight.length)
        }
    }
}