|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import Foundation |
| 12 | +import XCTest |
| 13 | +import SymbolKit |
| 14 | +@testable import SwiftDocC |
| 15 | +import SwiftDocCTestUtilities |
| 16 | + |
| 17 | +class SymbolAvailabilityTests: XCTestCase { |
| 18 | + |
| 19 | + private func symbolAvailability( |
| 20 | + defaultAvailability: [DefaultAvailability.ModuleAvailability] = [], |
| 21 | + symbolGraphOperatingSystemPlatformName: String, |
| 22 | + symbols: [SymbolGraph.Symbol], |
| 23 | + symbolName: String |
| 24 | + ) throws -> [SymbolGraph.Symbol.Availability.AvailabilityItem] { |
| 25 | + let catalog = Folder( |
| 26 | + name: "unit-test.docc", |
| 27 | + content: [ |
| 28 | + InfoPlist(defaultAvailability: [ |
| 29 | + "ModuleName": defaultAvailability |
| 30 | + ]), |
| 31 | + JSONFile(name: "ModuleName.symbols.json", content: makeSymbolGraph( |
| 32 | + moduleName: "ModuleName", |
| 33 | + platform: SymbolGraph.Platform(architecture: nil, vendor: nil, operatingSystem: SymbolGraph.OperatingSystem(name: symbolGraphOperatingSystemPlatformName), environment: nil), |
| 34 | + symbols: symbols, |
| 35 | + relationships: [] |
| 36 | + )), |
| 37 | + ] |
| 38 | + ) |
| 39 | + let (_, context) = try loadBundle(catalog: catalog) |
| 40 | + let reference = try XCTUnwrap(context.soleRootModuleReference).appendingPath(symbolName) |
| 41 | + let symbol = try XCTUnwrap(context.entity(with: reference).semantic as? Symbol) |
| 42 | + return try XCTUnwrap(symbol.availability?.availability) |
| 43 | + } |
| 44 | + |
| 45 | + private func renderNodeAvailability( |
| 46 | + defaultAvailability: [DefaultAvailability.ModuleAvailability] = [], |
| 47 | + symbolGraphOperatingSystemPlatformName: String, |
| 48 | + symbols: [SymbolGraph.Symbol], |
| 49 | + symbolName: String |
| 50 | + ) throws -> [AvailabilityRenderItem] { |
| 51 | + let catalog = Folder( |
| 52 | + name: "unit-test.docc", |
| 53 | + content: [ |
| 54 | + InfoPlist(defaultAvailability: [ |
| 55 | + "ModuleName": defaultAvailability |
| 56 | + ]), |
| 57 | + JSONFile(name: "ModuleName.symbols.json", content: makeSymbolGraph( |
| 58 | + moduleName: "ModuleName", |
| 59 | + platform: SymbolGraph.Platform(architecture: nil, vendor: nil, operatingSystem: SymbolGraph.OperatingSystem(name: symbolGraphOperatingSystemPlatformName), environment: nil), |
| 60 | + symbols: symbols, |
| 61 | + relationships: [] |
| 62 | + )), |
| 63 | + ] |
| 64 | + ) |
| 65 | + let (bundle, context) = try loadBundle(catalog: catalog) |
| 66 | + let reference = try XCTUnwrap(context.soleRootModuleReference).appendingPath(symbolName) |
| 67 | + let node = try context.entity(with: ResolvedTopicReference(bundleIdentifier: bundle.identifier, path: reference.path, sourceLanguage: .swift)) |
| 68 | + var translator = RenderNodeTranslator(context: context, bundle: bundle, identifier: node.reference) |
| 69 | + return try XCTUnwrap((translator.visit(node.semantic as! Symbol) as! RenderNode).metadata.platformsVariants.defaultValue) |
| 70 | + } |
| 71 | + |
| 72 | + func testSymbolGraphSymbolWithoutDeprecatedVersionAndIntroducedVersion() throws { |
| 73 | + |
| 74 | + let availability = try renderNodeAvailability( |
| 75 | + defaultAvailability: [], |
| 76 | + symbolGraphOperatingSystemPlatformName: "ios", |
| 77 | + symbols: [ |
| 78 | + makeSymbol( |
| 79 | + id: "platform-1-symbol", |
| 80 | + kind: .class, |
| 81 | + pathComponents: ["SymbolName"], |
| 82 | + availability: [makeAvailabilityItem(domainName: "iOS", deprecated: SymbolGraph.SemanticVersion(string: "1.2.3"))] |
| 83 | + ) |
| 84 | + ], |
| 85 | + symbolName: "SymbolName" |
| 86 | + ) |
| 87 | + |
| 88 | + XCTAssertEqual(availability.map { "\($0.name ?? "<nil>") \($0.introduced ?? "<nil>") - \($0.deprecated ?? "<nil>")" }, [ |
| 89 | + // The availability items wihout an introduced version should still emit the deprecated version if available. |
| 90 | + "iOS <nil> - 1.2.3", |
| 91 | + "iPadOS <nil> - 1.2.3", |
| 92 | + "Mac Catalyst <nil> - 1.2.3", |
| 93 | + ]) |
| 94 | + } |
| 95 | + |
| 96 | + func testSymbolGraphSymbolWithObsoleteVersion() throws { |
| 97 | + |
| 98 | + let availability = try renderNodeAvailability( |
| 99 | + defaultAvailability: [], |
| 100 | + symbolGraphOperatingSystemPlatformName: "ios", |
| 101 | + symbols: [ |
| 102 | + makeSymbol( |
| 103 | + id: "platform-1-symbol", |
| 104 | + kind: .class, |
| 105 | + pathComponents: ["SymbolName"], |
| 106 | + availability: [makeAvailabilityItem(domainName: "iOS", obsoleted: SymbolGraph.SemanticVersion(string: "1.2.3"))] |
| 107 | + ) |
| 108 | + ], symbolName: "SymbolName" |
| 109 | + ) |
| 110 | + XCTAssertEqual(availability.map { "\($0.name ?? "<nil>") \($0.introduced ?? "<nil>") - \($0.deprecated ?? "<nil>")" }.sorted(), [ |
| 111 | + // The availability items that are obsolete are not rendered in the final documentation. |
| 112 | + ]) |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments