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
|
/*
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 Foundation
import XCTest
@testable import SwiftDocC
class RenderSectionTests: XCTestCase {
func testDecoderAcceptsHeroKey() throws {
let json = """
{
"backgroundImage" : "intro_school.jpg",
"chapter" : "Getting Started",
"content" : [
{
"inlineContent" : [
{
"text" : "Here is some introduction text that will explain all the things and such you will be following this tutorial. You are going to learn a lot by building an app.",
"type" : "text"
}
],
"type" : "paragraph"
}
],
"estimatedTimeInMinutes" : 20,
"kind" : "hero",
"projectFiles" : "project.zip",
"title" : "Basic Augmented Reality App",
"video" : "video.mov",
"xcodeRequirement" : "Xcode 10 beta"
}
""".data(using: .utf8)!
// We should be able to correctly decode a renderJSON intro section that is described as a hero
let renderSection = try JSONDecoder().decode(CodableRenderSection.self, from: json)
XCTAssertEqual(renderSection.section.kind, .hero)
}
func testDecoderAcceptsIntroKey() throws {
let json = """
{
"backgroundImage" : "intro_school.jpg",
"chapter" : "Getting Started",
"content" : [
{
"inlineContent" : [
{
"text" : "Here is some introduction text that will explain all the things and such you will be following this tutorial. You are going to learn a lot by building an app.",
"type" : "text"
}
],
"type" : "paragraph"
}
],
"estimatedTimeInMinutes" : 20,
"kind" : "intro",
"projectFiles" : "project.zip",
"title" : "Basic Augmented Reality App",
"video" : "video.mov",
"xcodeRequirement" : "Xcode 10 beta"
}
""".data(using: .utf8)!
// We should be able to correctly decode a renderJSON intro section that is described as an intro
let renderSection = try JSONDecoder().decode(CodableRenderSection.self, from: json)
XCTAssertEqual(renderSection.section.kind, .hero)
}
func testDecoderAcceptsIntroKeyAndOutputsHeroKey() throws {
// The input JSON uses the "intro" kind key.
let inputJSON = """
{
"backgroundImage" : "intro_school.jpg",
"chapter" : "Getting Started",
"content" : [
{
"inlineContent" : [
{
"text" : "Here is some introduction text that will explain all the things and such you will be following this tutorial. You are going to learn a lot by building an app.",
"type" : "text"
}
],
"type" : "paragraph"
}
],
"estimatedTimeInMinutes" : 20,
"kind" : "intro",
"projectFiles" : "project.zip",
"title" : "Basic Augmented Reality App",
"video" : "video.mov",
"xcodeRequirement" : "Xcode 10 beta"
}
""".data(using: .utf8)!
// The expected output JSON uses the "hero" kind key.
let expectedOutputJSON = """
{
"backgroundImage" : "intro_school.jpg",
"chapter" : "Getting Started",
"content" : [
{
"inlineContent" : [
{
"text" : "Here is some introduction text that will explain all the things and such you will be following this tutorial. You are going to learn a lot by building an app.",
"type" : "text"
}
],
"type" : "paragraph"
}
],
"estimatedTimeInMinutes" : 20,
"kind" : "hero",
"projectFiles" : "project.zip",
"title" : "Basic Augmented Reality App",
"video" : "video.mov",
"xcodeRequirement" : "Xcode 10 beta"
}
"""
let renderSection = try JSONDecoder().decode(CodableRenderSection.self, from: inputJSON)
XCTAssertEqual(renderSection.section.kind, .hero)
// We should always output renderJSON that uses "hero" to describe an intro section to
// maintain compatibility
let encodedJSONData = try RenderJSONEncoder.makeEncoder(prettyPrint: true).encode(renderSection)
let encodedJSONString = String(data: encodedJSONData, encoding: .utf8)
XCTAssertEqual(encodedJSONString, expectedOutputJSON)
}
}
|