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
|
/*
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
import SymbolKit
import SwiftDocC
// MARK: - Symbol Graph objects
extension XCTestCase {
package func makeSymbolGraph(
moduleName: String,
platform: SymbolGraph.Platform = .init(),
symbols: [SymbolGraph.Symbol] = [],
relationships: [SymbolGraph.Relationship] = []
) -> SymbolGraph {
return SymbolGraph(
metadata: makeMetadata(),
module: makeModule(moduleName: moduleName, platform: platform),
symbols: symbols,
relationships: relationships
)
}
package func makeMetadata(major: Int = 0, minor: Int = 6, patch: Int = 0) -> SymbolGraph.Metadata {
SymbolGraph.Metadata(
formatVersion: SymbolGraph.SemanticVersion(major: major, minor: minor, patch: patch),
generator: "unit-test"
)
}
package func makeModule(moduleName: String, platform: SymbolGraph.Platform = .init()) -> SymbolGraph.Module {
SymbolGraph.Module(name: moduleName, platform: platform)
}
// MARK: Line List
package func makeLineList(
docComment: String,
moduleName: String?,
startOffset: SymbolGraph.LineList.SourceRange.Position = defaultSymbolPosition,
url: URL = defaultSymbolURL
) -> SymbolGraph.LineList {
SymbolGraph.LineList(
// Create a `LineList/Line` for each line of the doc comment and calculate a realistic range for each line.
docComment.components(separatedBy: .newlines)
.enumerated()
.map { lineOffset, line in
SymbolGraph.LineList.Line(
text: line,
range: SymbolGraph.LineList.SourceRange(
start: .init(line: startOffset.line + lineOffset, character: startOffset.character),
end: .init(line: startOffset.line + lineOffset, character: startOffset.character + line.count)
)
)
},
// We want to include the file:// scheme here
uri: url.absoluteString,
moduleName: moduleName
)
}
package func makeMixins(_ mixins: [any Mixin]) -> [String: any Mixin] {
[String: any Mixin](
mixins.map { (type(of: $0).mixinKey, $0) },
uniquingKeysWith: { old, _ in old /* Keep the first encountered value */ }
)
}
// MARK: Symbol
package func makeSymbol(
id: String,
language: SourceLanguage = .swift,
kind kindID: SymbolGraph.Symbol.KindIdentifier,
pathComponents: [String],
docComment: String? = nil,
moduleName: String? = nil,
accessLevel: SymbolGraph.Symbol.AccessControl = .init(rawValue: "public"), // Defined internally in SwiftDocC
location: (position: SymbolGraph.LineList.SourceRange.Position, url: URL)? = (defaultSymbolPosition, defaultSymbolURL),
signature: SymbolGraph.Symbol.FunctionSignature? = nil,
availability: [SymbolGraph.Symbol.Availability.AvailabilityItem]? = nil,
otherMixins: [any Mixin] = []
) -> SymbolGraph.Symbol {
precondition(!pathComponents.isEmpty, "Need at least one path component to name the symbol")
var mixins = otherMixins // Earlier mixins are prioritized if there are duplicates
if let location {
mixins.append(SymbolGraph.Symbol.Location(uri: location.url.absoluteString /* we want to include the file:// scheme */, position: location.position))
}
if let signature {
mixins.append(signature)
}
if let availability {
mixins.append(SymbolGraph.Symbol.Availability(availability: availability))
}
return SymbolGraph.Symbol(
identifier: SymbolGraph.Symbol.Identifier(precise: id, interfaceLanguage: language.id),
names: makeSymbolNames(name: pathComponents.last!),
pathComponents: pathComponents,
docComment: docComment.map {
makeLineList(
docComment: $0,
moduleName: moduleName,
startOffset: location?.position ?? defaultSymbolPosition,
url: location?.url ?? defaultSymbolURL
)
},
accessLevel: accessLevel,
kind: makeSymbolKind(kindID),
mixins: makeMixins(mixins)
)
}
package func makeAvailabilityItem(
domainName: String,
introduced: SymbolGraph.SemanticVersion? = nil,
deprecated: SymbolGraph.SemanticVersion? = nil,
obsoleted: SymbolGraph.SemanticVersion? = nil,
unconditionallyUnavailable: Bool = false
) -> SymbolGraph.Symbol.Availability.AvailabilityItem {
return SymbolGraph.Symbol.Availability.AvailabilityItem(domain: .init(rawValue: domainName), introducedVersion: introduced, deprecatedVersion: deprecated, obsoletedVersion: obsoleted, message: nil, renamed: nil, isUnconditionallyDeprecated: false, isUnconditionallyUnavailable: unconditionallyUnavailable, willEventuallyBeDeprecated: false)
}
package func makeSymbolNames(name: String) -> SymbolGraph.Symbol.Names {
SymbolGraph.Symbol.Names(
title: name,
navigator: [.init(kind: .identifier, spelling: name, preciseIdentifier: nil)],
subHeading: [.init(kind: .identifier, spelling: name, preciseIdentifier: nil)],
prose: nil
)
}
package func makeSymbolKind(_ kindID: SymbolGraph.Symbol.KindIdentifier) -> SymbolGraph.Symbol.Kind {
var documentationNodeKind: DocumentationNode.Kind {
switch kindID {
case .associatedtype: .associatedType
case .class: .class
case .deinit: .deinitializer
case .enum: .enumeration
case .case: .enumerationCase
case .func: .function
case .operator: .operator
case .`init`: .initializer
case .ivar: .instanceVariable
case .macro: .macro
case .method: .instanceMethod
case .namespace: .namespace
case .property: .instanceProperty
case .protocol: .protocol
case .snippet: .snippet
case .struct: .structure
case .subscript: .instanceSubscript
case .typeMethod: .typeMethod
case .typeProperty: .typeProperty
case .typeSubscript: .typeSubscript
case .typealias: .typeAlias
case .union: .union
case .var: .globalVariable
case .module: .module
case .extension: .extension
case .dictionary: .dictionary
case .dictionaryKey: .dictionaryKey
case .httpRequest: .httpRequest
case .httpParameter: .httpParameter
case .httpResponse: .httpResponse
case .httpBody: .httpBody
default: .unknown
}
}
return SymbolGraph.Symbol.Kind(parsedIdentifier: kindID, displayName: documentationNodeKind.name)
}
}
// MARK: Constants
private let defaultSymbolPosition = SymbolGraph.LineList.SourceRange.Position(line: 11, character: 17) // an arbitrary non-zero start position
private let defaultSymbolURL = URL(fileURLWithPath: "/Users/username/path/to/SomeFile.swift")
// MARK: - JSON strings
extension XCTestCase {
public func makeSymbolGraphString(moduleName: String, symbols: String = "", relationships: String = "", platform: String = "") -> String {
return """
{
"metadata": {
"formatVersion": {
"major": 0,
"minor": 6,
"patch": 0
},
"generator": "unit-test"
},
"module": {
"name": "\(moduleName)",
"platform": { \(platform) }
},
"relationships" : [
\(relationships)
],
"symbols" : [
\(symbols)
]
}
"""
}
}
|