File: RenderNodeSerializationTests.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 (338 lines) | stat: -rw-r--r-- 17,972 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
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
/*
 This source file is part of the Swift.org open source project

 Copyright (c) 2021-2023 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 RenderNodeSerializationTests: XCTestCase {
    func testRoundTrip() throws {
        let inputIdentifier = ResolvedTopicReference(bundleIdentifier: "com.example.docc", path: "/example", sourceLanguage: .swift)
        var inputNode = RenderNode(identifier: inputIdentifier, kind: .tutorial)
        
        let introSection = IntroRenderSection(title: "Basic Augmented Reality App")
        
        inputNode.sections.append(introSection)
        
        let inlines: [RenderInlineContent] = [
            .text("Let's get started building the "),
            .emphasis(inlineContent: [
                .text("Augmented "),
                .strong(inlineContent: [.text("Reality")]),
            ]),
            .text(" app. First, create a new Xcode project. For more information, download "),
            .reference(identifier: .init(forExternalLink: "https://www.example.com/page"), isActive: true, overridingTitle: nil, overridingTitleInlineContent: nil),
            .text(" and then see "),
            .reference(identifier: .init("GettingStartedInXcode.md"), isActive: true, overridingTitle: nil, overridingTitleInlineContent: nil),
            .text(". Then run "),
            .codeVoice(code: "swift package generate-xcodeproj"),
            .text(".")
        ]
        
        let blockContent: [RenderBlockContent] = [
            .paragraph(.init(inlineContent: inlines)),
            .aside(.init(style: .init(rawValue: "Experiment"), content: [
                .paragraph(.init(inlineContent: [
                    .text("Try running the project in the Simulator using the "),
                    .strong(inlineContent: [.text("Project > Run")]),
                    .text(" menu item, or the following code:"),
                ])),
                .codeListing(.init(syntax: "swift", code: ["xcrun xcodebuild -h", "xcrun xcodebuild build -configuration Debug"], metadata: nil)),
            ]))
        ]
        
        let steps: [RenderBlockContent] = [
            .paragraph(.init(inlineContent: [.text("After you download Xcode, create a project.")])),
            .step(.init(content: [.paragraph(.init(inlineContent: [.text("Lorem ipsum")]))], caption: [.paragraph(.init(inlineContent: [.text("Caption")]))], media: .init("screenshot2.png"), code: nil, runtimePreview: nil)),
            .step(.init(content: [.paragraph(.init(inlineContent: [.text("Lorem ipsum")]))], caption: [], media: nil, code: .init("helloworld.swift"), runtimePreview: .init("screenshot2.png"))),
            .step(.init(content: [.paragraph(.init(inlineContent: [.text("Lorem ipsum")]))], caption: [], media: .init("screenshot3.png"), code: nil, runtimePreview: nil)),
            .aside(.init(style: .init(rawValue: "Note"), content: [.paragraph(.init(inlineContent: [.text("Lorem ipsum dolor emit.")]))])),
            .step(.init(content: [.paragraph(.init(inlineContent: [.text("Lorem ipsum")]))], caption: [], media: .init("screenshot4.png"), code: nil, runtimePreview: nil)),
        ]
        
        var contentAndMedia = ContentAndMediaSection(layout: .horizontal, title: "", media: RenderReferenceIdentifier("screenshot1.png"), mediaPosition: .leading)
        contentAndMedia.content = blockContent
        let tutorialSection = TutorialSectionsRenderSection.Section(title: "Create a new AR project", contentSection: [.contentAndMedia(content: contentAndMedia)], stepsSection: steps, anchor: "")
        var tutorialSection2 = tutorialSection
        var contentAndMedia2 = contentAndMedia
        contentAndMedia2.title = "Initiate ARKit plane detection"
        tutorialSection2.contentSection[0] = .contentAndMedia(content: contentAndMedia2)
        let tutorialSectionsSection = TutorialSectionsRenderSection(sections: [tutorialSection, tutorialSection2])
        
        inputNode.sections.append(tutorialSectionsSection)
        
        let assessment1 = TutorialAssessmentsRenderSection.Assessment(title: [.paragraph(.init(inlineContent: [.text("Lorem ipsum dolor sit amet?")]))],
                                                                     content: nil,
                                                                     choices: [
            .init(content: [.codeListing(.init(syntax: "swift", code: ["override func viewDidLoad() {", "super.viewDidLoad()", "}"], metadata: nil))], isCorrect: true, justification: [.paragraph(.init(inlineContent: [.text("It's correct because...")]))], reaction: "That's right!"),
            .init(content: [.codeListing(.init(syntax: "swift", code: ["sceneView.delegate = self"], metadata: nil))], isCorrect: false, justification: [.paragraph(.init(inlineContent: [.text("It's incorrect because...")]))], reaction: "Not quite."),
            .init(content: [.paragraph(.init(inlineContent: [.text("None of the above.")]))], isCorrect: false, justification: [.paragraph(.init(inlineContent: [.text("It's incorrect because...")]))], reaction: nil),
        ])
        
        let assessment2 = TutorialAssessmentsRenderSection.Assessment(title: [.paragraph(.init(inlineContent: [.text("Duis aute irure dolor in reprehenderit?")]))],
                                                                     content: [.paragraph(.init(inlineContent: [.text("What is the airspeed velocity of an unladen swallow?")]))],
                                                                     choices: [
            .init(content: [.codeListing(.init(syntax: "swift", code: ["super.viewWillAppear()"], metadata: nil))], isCorrect: true, justification: [.paragraph(.init(inlineContent: [.text("It's correct because...")]))], reaction: "Correct."),
            .init(content: [.codeListing(.init(syntax: "swift", code: ["sceneView.delegate = self"], metadata: nil))], isCorrect: true, justification: [.paragraph(.init(inlineContent: [.text("It's correct because...")]))], reaction: "Yep."),
            .init(content: [.paragraph(.init(inlineContent: [.text("None of the above.")]))], isCorrect: false, justification: [.paragraph(.init(inlineContent: [.text("It's incorrect because...")]))], reaction: "Close!"),
        ])
        
        let assessments = TutorialAssessmentsRenderSection(assessments: [assessment1, assessment2], anchor: "Check-Your-Understanding")
        
        inputNode.sections.append(assessments)
        
        checkRoundTrip(inputNode)
    }
    
    func testBundleRoundTrip() throws {
        let (bundle, context) = try testBundleAndContext(named: "TestBundle")
        let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/tutorials/Test-Bundle/TestTutorial", sourceLanguage: .swift))
        
        guard let tutorialDirective = node.markup as? BlockDirective else {
            XCTFail("Unexpected document structure, tutorial not found as first child.")
            return
        }
        
        var problems = [Problem]()
        guard let tutorial = Tutorial(from: tutorialDirective, source: nil, for: bundle, in: context, problems: &problems) else {
            XCTFail("Couldn't create tutorial from markup: \(problems)")
            return
        }
        
        XCTAssertEqual(problems.count, 1, "Found problems \(problems.map { DiagnosticConsoleWriter.formattedDescription(for: $0.diagnostic) }) analyzing tutorial markup")
        
        var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
        
        let renderNode = translator.visit(tutorial) as! RenderNode
        checkRoundTrip(renderNode)
    }
    
    func testTutorialArticleRoundTrip() throws {
        let (bundle, context) = try testBundleAndContext(named: "TestBundle")
        let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/tutorials/Test-Bundle/TestTutorialArticle", sourceLanguage: .swift))
        
        guard let articleDirective = node.markup as? BlockDirective else {
            XCTFail("Unexpected document structure, article not found as first child.")
            return
        }
        
        var problems = [Problem]()
        guard let article = TutorialArticle(from: articleDirective, source: nil, for: bundle, in: context, problems: &problems) else {
            XCTFail("Couldn't create article from markup: \(problems)")
            return
        }
        
        XCTAssertEqual(problems.count, 0, "Found problems \(problems.map { DiagnosticConsoleWriter.formattedDescription(for: $0.diagnostic) }) analyzing article markup")
        
        var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
        
        let renderNode = translator.visit(article) as! RenderNode
        checkRoundTrip(renderNode)
    }
    
    func testAssetReferenceDictionary() throws {
        typealias JSONDictionary = [String: Any]
        
        let (bundle, context) = try testBundleAndContext(named: "TestBundle")
        let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/tutorials/Test-Bundle/TestTutorial", sourceLanguage: .swift))
        
        guard let tutorialDirective = node.markup as? BlockDirective else {
            XCTFail("Unexpected document structure, tutorial not found as first child.")
            return
        }
        
        var problems = [Problem]()
        guard let tutorial = Tutorial(from: tutorialDirective, source: nil, for: bundle, in: context, problems: &problems) else {
            XCTFail("Couldn't create tutorial from markup: \(problems)")
            return
        }
        
        XCTAssertEqual(problems.count, 1, "Found problems \(problems.map { DiagnosticConsoleWriter.formattedDescription(for: $0.diagnostic) }) analyzing tutorial markup")
        
        var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
        
        let renderNode = translator.visit(tutorial) as! RenderNode
        let data = try encode(renderNode: renderNode)
        
        // Ensure references are correct
        XCTAssertNotNil(renderNode.projectFiles())
        XCTAssertEqual(renderNode.projectFiles()?.url.lastPathComponent, "project.zip")
        
        XCTAssertEqual(renderNode.navigatorChildren(for: nil).count, 0)
        XCTAssertEqual(renderNode.downloadReferences().count, 1)
        
        // Check the output of the dictionary
        let dictionary = try JSONSerialization.jsonObject(with: data, options: []) as! JSONDictionary
        let references = dictionary["references"] as! [String: JSONDictionary]
        for asset in references.values {
            let type = asset["type"] as! String
            if type == "image" {
                let variants = asset["variants"] as! [JSONDictionary]
                for variant in variants {
                    XCTAssertNotNil(variant["traits"])
                    XCTAssertNotNil(variant["url"])
                }
            } else if type == "video" {
                let variants = asset["variants"] as! [JSONDictionary]
                for variant in variants {
                    // Ensure video has no size.
                    XCTAssertNil(variant["size"])
                    XCTAssertNotNil(variant["traits"])
                    XCTAssertNotNil(variant["url"])
                }
            }
        }
    }

    func testDiffAvailability() throws {
        let (bundle, context) = try testBundleAndContext(named: "TestBundle")
        let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/tutorials/Test-Bundle/TestTutorialArticle", sourceLanguage: .swift))
        
        guard let articleDirective = node.markup as? BlockDirective else {
            XCTFail("Unexpected document structure, article not found as first child.")
            return
        }
        
        var problems = [Problem]()
        guard let article = TutorialArticle(from: articleDirective, source: nil, for: bundle, in: context, problems: &problems) else {
            XCTFail("Couldn't create article from markup: \(problems)")
            return
        }

        var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)

        var renderNode = translator.visit(article) as! RenderNode

        renderNode.diffAvailability = DiffAvailability(
            beta: nil,
            minor: DiffAvailability.Info(change: "modified", platform: "Xcode", versions: ["11.3", "11.4"]),
            major: DiffAvailability.Info(change: "added", platform: "Xcode", versions: ["11.0", "11.4"]),
            sdk: DiffAvailability.Info(change: "deprecated", platform: "Xcode", versions: ["12A123", "12A124"])
        )

        checkRoundTrip(renderNode)
    }

    func testKindTutorialSerialization() throws {
        struct Wrapper: Codable {
            let kind: RenderNode.Kind
        }
        func decodeKind(jsonString: String) throws -> RenderNode.Kind {
            return try JSONDecoder().decode(Wrapper.self, from: "{ \"kind\" : \(jsonString) }".data(using: .utf8)!).kind
        }
        
        // Both values can be decoded
        XCTAssertEqual(try decodeKind(jsonString: "\"tutorial\""), .tutorial)
        XCTAssertEqual(try decodeKind(jsonString: "\"project\""), .tutorial)
        
        // A `tutorial` kind is still encoded as "project" for compatibility.
        let decoded = try decodeKind(jsonString: "\"tutorial\"")
        let encoded = try String(data: JSONEncoder().encode(Wrapper(kind: decoded)), encoding: .utf8)
        XCTAssertEqual(encoded, "{\"kind\":\"project\"}")
    }
    
    func testRenderMetadataSerialization() throws {
        func decodeMetadata(jsonString: String) throws -> RenderMetadata {
            return try JSONDecoder().decode(RenderMetadata.self, from: jsonString.data(using: .utf8)!)
        }
        
        // Both values can be decoded, and are decoded as "project".
        XCTAssertEqual(try decodeMetadata(jsonString: "{}").role, nil)
        XCTAssertEqual(try decodeMetadata(jsonString: "{ \"role\" : \"tutorial\" }").role, "project")
        XCTAssertEqual(try decodeMetadata(jsonString: "{ \"role\" : \"project\" }").role, "project")
        
        // A `tutorial` role is still encoded as "project" for compatibility.
        let decoded = try decodeMetadata(jsonString: "{ \"role\" : \"tutorial\" }")
        let encoded = try String(data: JSONEncoder().encode(decoded), encoding: .utf8)
        XCTAssertEqual(encoded, "{\"role\":\"project\"}")
    }
    
    func testRenderHierarchyChapterSerialization() throws {
        func decodeMetadata(jsonString: String) throws -> RenderHierarchyChapter {
            return try JSONDecoder().decode(RenderHierarchyChapter.self, from: jsonString.data(using: .utf8)!)
        }
        
        // Both keys can be decoded, and are decoded as "tutorials".
        XCTAssertEqual(try decodeMetadata(jsonString: """
        {
          "reference" : "chapter-identifier",
          "tutorials" : [
            {
              "reference" : "tutorial-identifier",
              "sections" : []
            }
          ]
        }
        """).tutorials.first?.reference.identifier, "tutorial-identifier")
        XCTAssertEqual(try decodeMetadata(jsonString: """
        {
          "reference" : "chapter-identifier",
          "projects" : [
            {
              "reference" : "tutorial-identifier",
              "sections" : []
            }
          ]
        }
        """).tutorials.first?.reference.identifier, "tutorial-identifier")
        
        // The `tutorials` property is still encoded as "projects" for compatibility.
        let decoded = try decodeMetadata(jsonString: """
        {
          "reference" : "chapter-identifier",
          "tutorials" : []
        }
        """)
        let encoded = try String(data: JSONEncoder().encode(decoded), encoding: .utf8)!
        XCTAssertTrue(encoded.contains("\"projects\":[]"))
        XCTAssertFalse(encoded.contains("\"tutorials\":[]"))
    }
    
    // MARK: - Utility functions

    func checkRoundTrip(_ inputNode: RenderNode, file: StaticString = #file, line: UInt = #line) {
        // Make sure we're not using a shared encoder
        let testEncoder = JSONEncoder()
        let testDecoder = JSONDecoder()
        
        let data: Data
        do {
            data = try inputNode.encodeToJSON(with: testEncoder)
        } catch {
            XCTFail("Failed to encode node to JSON: \(error.localizedDescription)", file: (file), line: line)
            return
        }

        let newNode: RenderNode
        do {
            newNode = try RenderNode.decode(fromJSON: data, with: testDecoder)
        } catch {
            let json = String(data: data, encoding: .utf8) ?? ""
            XCTFail("Failed to decode node from JSON: \(error.localizedDescription)\n\(json)", file: (file), line: line)
            return
        }

        do {
            let newData = try newNode.encodeToJSON(with: testEncoder)
            XCTAssertEqual(data.count, newData.count, file: (file), line: line)
        } catch {
            XCTFail("Failed to encode new node back to JSON: \(error.localizedDescription)", file: (file), line: line)
        }
    }

    func encode(renderNode: RenderNode) throws -> Data {
        let encoder = JSONEncoder()
        if #available(OSX 10.13, *) {
            encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
        }
        
        return try encoder.encode(renderNode)
    }
}