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
|
/*
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
class AvailabilityRenderOrderTests: XCTestCase {
let availabilitySGFURL = Bundle.module.url(
forResource: "Availability.symbols", withExtension: "json", subdirectory: "Test Resources")!
func testSortingAtRenderTime() throws {
let (bundleURL, bundle, context) = try testBundleAndContext(copying: "TestBundle", excludingPaths: [], codeListings: [:]) { url in
try? FileManager.default.copyItem(at: self.availabilitySGFURL, to: url.appendingPathComponent("Availability.symbols.json"))
}
defer {
try? FileManager.default.removeItem(at: bundleURL)
}
let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: "/documentation/Availability/MyStruct", sourceLanguage: .swift))
var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference, source: nil)
let renderNode = translator.visit(node.semantic as! Symbol) as! RenderNode
// Verify that all the symbol's availabilities were sorted into the order
// they need to appear for rendering (they are not in the symbol graph fixture).
// Additionally verify all the platforms have their correctly spelled name including spaces.
XCTAssertEqual(renderNode.metadata.platforms?.map({ "\($0.name ?? "") \($0.introduced ?? "")" }), [
"iOS 12.0", "iOS App Extension 12.0",
"iPadOS 12.0",
"Mac Catalyst 2.0", "Mac Catalyst App Extension 1.0",
"macOS 10.12", "macOS App Extension 10.12",
"tvOS 12.0", "tvOS App Extension 12.0",
"visionOS 12.0",
"watchOS 6.0", "watchOS App Extension 6.0",
"Swift 4.2"
])
// Test roundtrip to verify availability items are correctly
// initialized from the display name that's used for render JSON
// instead of the platform key which is used in the symbol graph.
let roundtripData = try JSONEncoder().encode(renderNode)
XCTAssertNoThrow(try RenderNode.decode(fromJSON: roundtripData))
let roundtripNode = try RenderNode.decode(fromJSON: roundtripData)
XCTAssertEqual(roundtripNode.metadata.platforms?.map({ "\($0.name ?? "") \($0.introduced ?? "")" }), [
"iOS 12.0", "iOS App Extension 12.0",
"iPadOS 12.0",
"Mac Catalyst 2.0", "Mac Catalyst App Extension 1.0",
"macOS 10.12", "macOS App Extension 10.12",
"tvOS 12.0", "tvOS App Extension 12.0",
"visionOS 12.0",
"watchOS 6.0", "watchOS App Extension 6.0",
"Swift 4.2"
])
}
}
|