File: PlistSymbolTests.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 (189 lines) | stat: -rw-r--r-- 7,593 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
/*
 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 Foundation
import XCTest
@testable import SwiftDocC
import SwiftDocCTestUtilities

class PlistSymbolTests: XCTestCase {
    private let plistSymbolURL = Bundle.module.url(
        forResource: "plist-symbol", withExtension: "json",
        subdirectory: "Rendering Fixtures")!
    
    func testDecodePlistSymbol() throws {
        let data = try Data(contentsOf: plistSymbolURL)
        let symbol = try RenderNode.decode(fromJSON: data)
        
        //
        // Plist Details
        //
        
        guard let section = symbol.primaryContentSections.first(where: { section -> Bool in
            return section.kind == .plistDetails
        }) as? PlistDetailsRenderSection else {
            XCTFail("Plist details section not decoded")
            return
        }
        
        XCTAssertEqual(section.details.rawKey, "com.apple.developer.networking.wifi")
        XCTAssertEqual(section.details.displayName, "WiFi access")
        XCTAssertEqual(section.details.titleStyle, .useDisplayName)
        guard section.details.value.count == 2 else {
            XCTFail("Invalid number of value types found")
            return
        }
        
        XCTAssertEqual(section.details.value[0].baseType, "string")
        XCTAssertEqual(section.details.value[0].arrayMode, true)
        XCTAssertEqual(section.details.value[1].baseType, "number")
        XCTAssertNil(section.details.value[1].arrayMode)
        
        XCTAssertEqual(section.details.platforms, ["iOS", "macOS"])
        
        /// Plist Properties
        guard let properties = symbol.primaryContentSections.first(where: { section -> Bool in
            return section.kind == .properties
        }) as? PropertiesRenderSection else {
            XCTFail("Plist properties section not decoded")
            return
        }
        
        XCTAssertEqual(properties.items.count, 1)
        guard properties.items.count == 1 else { return }
        
        XCTAssertEqual(properties.title, "Properties")
        
        guard let attributes = properties.items[0].attributes else {
            XCTFail("The property doesn't have the expected attributes")
            return
        }

        XCTAssertEqual(attributes.count, 1)
        guard attributes.count == 1 else { return }
        
        if case RenderAttribute.default(let value) = attributes[0] {
            XCTAssertEqual(value, "AABBCC")
        } else {
            XCTFail("Unexpected attribute")
        }
        
        /// Plist Attributes
        guard let attributesSection = symbol.primaryContentSections.first(where: { section -> Bool in
            return section.kind == .attributes
        }) as? AttributesRenderSection else {
            XCTFail("Plist attributes section not decoded")
            return
        }
        
        XCTAssertEqual(attributesSection.attributes?.count, 1)
        guard attributesSection.attributes?.count == 1 else { return }
        
        XCTAssertEqual(attributesSection.title, "Attributes")
        
        XCTAssertEqual(attributesSection.attributes?[0].title, "Default value")
        
        if case .default(let value)? = attributesSection.attributes?[0] {
            XCTAssertEqual(value, "AABBCC")
        } else {
            XCTFail("Unexpected attribute")
        }
        
        /// Plist Possible Values
        guard let values = symbol.primaryContentSections.first(where: { section -> Bool in
            return section.kind == .possibleValues
        }) as? PossibleValuesRenderSection else {
            XCTFail("Plist possible values section not decoded")
            return
        }
        
        XCTAssertEqual(values.title, "Possible Values")
        XCTAssertEqual(values.values.map { $0.name }, ["ppc", "i386", "arm"])
        XCTAssertEqual(values.values.map { $0.content?.firstParagraph.first }, [nil, .text("Any i386 type of processor"), nil])
        
        // Test render reference to plist symbol
        guard let reference = symbol.references["doc://org.swift.docc.example/plist/dataaccess"] as? TopicRenderReference else {
            XCTFail("Did not find doc://org.swift.docc.example/plist/dataaccess reference")
            return
        }
        XCTAssertEqual(reference.propertyListKeyNames?.titleStyle, .useDisplayName)
        XCTAssertEqual(reference.propertyListKeyNames?.rawKey, "com.apple.enabledataaccess")
        XCTAssertEqual(reference.propertyListKeyNames?.displayName, "Enable Data Access")
    
        // Test navigator information
        XCTAssertEqual(symbol.navigatorPageType(), .propertyListKey)
        
        AssertRoundtrip(for: symbol)
    }
    
    func testDecodeDetailsSectionNoIdeTitle() throws {
        let modifiedJSON = try String(contentsOf: plistSymbolURL)
            .replacingOccurrences(of: "\"ideTitle\": \"WiFi access\",", with: "")
        
        let tempFolderURL = try createTempFolder(content: [
            TextFile(name: "missingIdeTitle.json", utf8Content: modifiedJSON),
        ])
        let symbol = try RenderNode.decode(fromJSON: try Data(contentsOf: tempFolderURL.appendingPathComponent("missingIdeTitle.json")))
        
        //
        // Plist Details
        //
        
        guard let section = symbol.primaryContentSections.first(where: { section -> Bool in
            return section.kind == .plistDetails
        }) as? PlistDetailsRenderSection else {
            XCTFail("Plist details section not decoded")
            return
        }
        
        XCTAssertEqual(section.details.rawKey, "com.apple.developer.networking.wifi")
        XCTAssertNil(section.details.displayName)
        
        AssertRoundtrip(for: symbol)
    }
        
    func testDecodePossibleValuesNoTitle() throws {
        let modifiedJSON = try String(contentsOf: plistSymbolURL)
            .replacingOccurrences(of: "\"title\": \"Possible Values\",", with: "")
        
        let tempFolderURL = try createTempFolder(content: [
            TextFile(name: "missingPossibleValuesTitle.json", utf8Content: modifiedJSON),
        ])
        let symbol = try RenderNode.decode(fromJSON: try Data(contentsOf: tempFolderURL.appendingPathComponent("missingPossibleValuesTitle.json")))
        
        //
        // Plist Details
        //
        
        guard let section = symbol.primaryContentSections.first(where: { section -> Bool in
            return section.kind == .possibleValues
        }) as? PossibleValuesRenderSection else {
            XCTFail("Plist details section not decoded")
            return
        }
        
        XCTAssertEqual(section.values.count, 3)
        XCTAssertNil(section.title)
        
        AssertRoundtrip(for: symbol)
    }
}


/// Ensures a given render node can be encoded and decode back without throwing.
public func AssertRoundtrip(for renderNode: RenderNode, file: StaticString = #file, line: UInt = #line) {
    // Ensure roundtripping, so we encode the render node and decode it back.
    do {
        let roundtripData = try JSONEncoder().encode(renderNode)
        XCTAssertNoThrow(try RenderNode.decode(fromJSON: roundtripData), file: (file), line: line)
    } catch {
        XCTFail("Encoding process failed with error: \(error.localizedDescription)", file: (file), line: line)
    }
}