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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2022-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 XCTest
import Foundation
@testable import SymbolKit
class UnifiedGraphTests: XCTestCase {
/// Verify that ``UnifiedSymbolGraph`` sorts relationships correctly in the basic case.
func testUnifyRelations() throws {
let collector = GraphCollector()
collector.mergeSymbolGraph(swiftSymbolGraph(), at: .init(fileURLWithPath: "swift/DemoKit.symbols.json"))
collector.mergeSymbolGraph(objcSymbolGraph(), at: .init(fileURLWithPath: "objc/DemoKit.symbols.json"))
let (unifiedGraphs, _) = collector.finishLoading()
let demoGraph = try XCTUnwrap(unifiedGraphs["DemoKit"])
if let swiftRelations = demoGraph.relationshipsByLanguage.first(where: { $0.key.interfaceLanguage == "swift" })?.value {
compareRelationships(swiftRelations, swiftSymbolGraph().relationships)
} else {
XCTFail("Unified graph did not have swift relationships")
}
if let objcRelations = demoGraph.relationshipsByLanguage.first(where: { $0.key.interfaceLanguage == "objc" })?.value {
compareRelationships(objcRelations, objcSymbolGraph().relationships)
} else {
XCTFail("Unified graph did not have objc relationships")
}
}
func testOrphanRelationships() throws {
var swiftSyms = swiftSymbolGraph()
swiftSyms.relationships.append(.init(
source: "unknownIdentifier",
target: "unknownProtocol",
kind: .conformsTo,
targetFallback: nil))
let collector = GraphCollector()
collector.mergeSymbolGraph(swiftSyms, at: .init(fileURLWithPath: "swift/DemoKit.symbols.json"))
collector.mergeSymbolGraph(objcSymbolGraph(), at: .init(fileURLWithPath: "objc/DemoKit.symbols.json"))
let (unifiedGraphs, _) = collector.finishLoading()
let demoGraph = try XCTUnwrap(unifiedGraphs["DemoKit"])
XCTAssertEqual(demoGraph.orphanRelationships.count, 1)
XCTAssertEqual(demoGraph.orphanRelationships, [
.init(
source: "unknownIdentifier",
target: "unknownProtocol",
kind: .conformsTo,
targetFallback: nil)
])
}
func testCollectOrphanRelationships() throws {
var swiftSyms = swiftSymbolGraph()
swiftSyms.relationships.append(.init(
source: "unknownIdentifier",
target: "unknownProtocol",
kind: .conformsTo,
targetFallback: nil))
var objcSyms = objcSymbolGraph()
objcSyms.symbols["unknownProtocol"] = .init(
identifier: .init(precise: "unknownProtocol", interfaceLanguage: "objc"),
names: .init(title: "unknownProtocol", navigator: nil, subHeading: nil, prose: nil),
pathComponents: ["unknownProtocol"],
docComment: nil,
accessLevel: .init(rawValue: "public"),
kind: .init(parsedIdentifier: .protocol, displayName: "Protocol"),
mixins: [:])
let collector = GraphCollector()
collector.mergeSymbolGraph(swiftSyms, at: .init(fileURLWithPath: "swift/DemoKit.symbols.json"))
collector.mergeSymbolGraph(objcSyms, at: .init(fileURLWithPath: "objc/DemoKit.symbols.json"))
let (unifiedGraphs, _) = collector.finishLoading()
let demoGraph = try XCTUnwrap(unifiedGraphs["DemoKit"])
XCTAssert(demoGraph.orphanRelationships.isEmpty)
// Since the only matching symbol in this relationship was `unknownProtocol` in the objc
// graph, the relation is only sorted among the objc relationships, even though it appeared
// in a "Swift" symbol graph. This is because even though in practice all the symbols in a
// single graph have the same source language, the symbol graph itself does not define a
// source language as a whole. In practice this is unlikely to be a problem, but it could be
// a surprising behavior for new symbol graph implementors.
let objcRelations = try XCTUnwrap(demoGraph.relationshipsByLanguage.first(where: { $0.key.interfaceLanguage == "objc" })?.value)
XCTAssert(objcRelations.contains(where: { $0.target == "unknownProtocol" }))
}
func testCollectExtensionGraph() throws {
let baseSyms = swiftSymbolGraph()
var extensionSyms = makeSymbolGraph(
symbols: [
.init(
identifier: .init(precise: "s:SomeStruct", interfaceLanguage: "swift"),
names: .init(title: "SomeStruct", navigator: nil, subHeading: nil, prose: nil),
pathComponents: ["PlayingCard", "SomeStruct"],
docComment: nil,
accessLevel: .init(rawValue: "public"),
kind: .init(parsedIdentifier: .struct, displayName: "Structure"),
mixins: [:]
)
],
relations: [
.init(
source: "s:SomeStruct",
target: "c:objc(cs)PlayingCard",
kind: .memberOf,
targetFallback: "DemoKit.PlayingCard")
]
)
extensionSyms.module.name = "OtherKit"
let collector = GraphCollector()
collector.mergeSymbolGraph(baseSyms, at: .init(fileURLWithPath: "DemoKit.symbols.json"))
collector.mergeSymbolGraph(extensionSyms, at: .init(fileURLWithPath: "OtherKit@DemoKit.symbols.json"))
let (unifiedGraphs, _) = collector.finishLoading()
XCTAssertFalse(unifiedGraphs.keys.contains("OtherKit"))
let demoGraph = try XCTUnwrap(unifiedGraphs["DemoKit"])
let extensionSym = try XCTUnwrap(demoGraph.symbols["s:SomeStruct"])
let extensionSymModule = try XCTUnwrap(extensionSym.modules[.init(forSymbolGraph: extensionSyms)!])
XCTAssertEqual(extensionSymModule.name, "OtherKit")
}
}
/// Compare the given lists of relationships and assert that they contain the same relationships.
private func compareRelationships(_ left: [SymbolGraph.Relationship], _ right: [SymbolGraph.Relationship]) {
func compareRelations(_ l: SymbolGraph.Relationship, _ r: SymbolGraph.Relationship) -> Bool {
if l.source < r.source {
return true
} else if l.source == r.source && l.target < r.target {
return true
} else if l.source == r.source && l.target == r.target && l.kind.rawValue < r.kind.rawValue {
return true
} else {
return false
}
}
let leftSorted = left.sorted(by: compareRelations(_:_:))
let rightSorted = right.sorted(by: compareRelations(_:_:))
for (l, r) in zip(leftSorted, rightSorted) {
XCTAssertEqual(l, r)
}
}
private func swiftSymbolGraph() -> SymbolGraph {
let symbols: [SymbolGraph.Symbol] = [
.init(
identifier: .init(precise: "c:objc(cs)PlayingCard", interfaceLanguage: "swift"),
names: .init(title: "PlayingCard", navigator: nil, subHeading: nil, prose: nil),
pathComponents: ["PlayingCard"],
docComment: nil,
accessLevel: .init(rawValue: "open"),
kind: .init(parsedIdentifier: .class, displayName: "Class"),
mixins: [:]
),
.init(
identifier: .init(precise: "c:objc(pl)ColorDetecting", interfaceLanguage: "swift"),
names: .init(title: "ColorDetecting", navigator: nil, subHeading: nil, prose: nil),
pathComponents: ["ColorDetecting"],
docComment: nil,
accessLevel: .init(rawValue: "public"),
kind: .init(parsedIdentifier: .protocol, displayName: "Protocol"),
mixins: [:]
)
]
let relations: [SymbolGraph.Relationship] = [
.init(
source: "c:objc(cs)PlayingCard",
target: "c:objc(pl)ColorDetecting",
kind: .conformsTo,
targetFallback: nil
),
.init(
source: "c:objc(cs)PlayingCard",
target: "c:objc(pl)NSObject",
kind: .inheritsFrom,
targetFallback: "ObjectiveC.NSObject"
),
.init(
source: "c:objc(cs)PlayingCard",
target: "s:SH",
kind: .conformsTo,
targetFallback: "Swift.Hashable"
)
]
return makeSymbolGraph(symbols: symbols, relations: relations)
}
private func objcSymbolGraph() -> SymbolGraph {
let symbols: [SymbolGraph.Symbol] = [
.init(
identifier: .init(precise: "c:objc(cs)PlayingCard", interfaceLanguage: "objc"),
names: .init(title: "PlayingCard", navigator: nil, subHeading: nil, prose: nil),
pathComponents: ["PlayingCard"],
docComment: nil,
accessLevel: .init(rawValue: "public"),
kind: .init(parsedIdentifier: .class, displayName: "Class"),
mixins: [:]
),
.init(
identifier: .init(precise: "c:objc(pl)ColorDetecting", interfaceLanguage: "objc"),
names: .init(title: "ColorDetecting", navigator: nil, subHeading: nil, prose: nil),
pathComponents: ["ColorDetecting"],
docComment: nil,
accessLevel: .init(rawValue: "public"),
kind: .init(parsedIdentifier: .protocol, displayName: "Protocol"),
mixins: [:]
)
]
let relations: [SymbolGraph.Relationship] = [
.init(
source: "c:objc(cs)PlayingCard",
target: "c:objc(pl)ColorDetecting",
kind: .conformsTo,
targetFallback: nil
),
.init(
source: "c:objc(cs)PlayingCard",
target: "c:objc(pl)NSObject",
kind: .inheritsFrom,
targetFallback: "NSObject"
)
]
return makeSymbolGraph(symbols: symbols, relations: relations)
}
private func makeSymbolGraph(symbols: [SymbolGraph.Symbol], relations: [SymbolGraph.Relationship]) -> SymbolGraph {
let metadata = SymbolGraph.Metadata(
formatVersion: .init(major: 1, minor: 0, patch: 0),
generator: "unit-test"
)
let module = SymbolGraph.Module(
name: "DemoKit",
platform: .init(
architecture: "x86_64",
vendor: "apple",
operatingSystem: .init(name: "macosx"),
environment: nil
)
)
return SymbolGraph(
metadata: metadata,
module: module,
symbols: symbols,
relationships: relations
)
}
|