File: SymbolDisambiguationTests.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 (357 lines) | stat: -rw-r--r-- 20,133 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 This source file is part of the Swift.org open source project

 Copyright (c) 2022 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 SymbolKit
@testable import SwiftDocC

class SymbolDisambiguationTests: XCTestCase {
    
    func testPathCollisionWithDifferentTypesInSameLanguage() throws {
        let references = try disambiguatedReferencesForSymbols(
            swift: [
                TestSymbolData(preciseID: "first", pathComponents: ["Something", "first"], kind: .property),
                TestSymbolData(preciseID: "second", pathComponents: ["Something", "First"], kind: .struct),
            ],
            objectiveC: []
        )
        
        XCTAssertEqual(references.count, 2)
        
        XCTAssertEqual(
            references[SymbolGraph.Symbol.Identifier(precise: "first", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/first-swift.property"
        )
        
        XCTAssertEqual(
            references[SymbolGraph.Symbol.Identifier(precise: "second", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/First-swift.struct"
        )
    }
    
    func testPathCollisionWithDifferentArgumentTypesInSameLanguage() throws {
        let references = try disambiguatedReferencesForSymbols(
            swift: [
                // The argument type isn't represented in the symbol name in the path components
                TestSymbolData(preciseID: "first", pathComponents: ["Something", "first(_:)"], kind: .method),
                TestSymbolData(preciseID: "second", pathComponents: ["Something", "first(_:)"], kind: .method),
            ],
            objectiveC: []
        )
        
        XCTAssertEqual(references.count, 2)
        
        XCTAssertEqual(
            references[SymbolGraph.Symbol.Identifier(precise: "first", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/first(_:)-\("first".stableHashString)"
        )
        
        XCTAssertEqual(
            references[SymbolGraph.Symbol.Identifier(precise: "second", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/first(_:)-\("second".stableHashString)"
        )
    }
    
    func testSameSymbolWithDifferentKindsInDifferentLanguages() throws {
        let references = try disambiguatedReferencesForSymbols(
            swift: [
                TestSymbolData(preciseID: "first", pathComponents: ["Something", "First"], kind: .enum),
            ],
            objectiveC: [
                TestSymbolData(preciseID: "first", pathComponents: ["Something", "First"], kind: .protocol),
            ]
        )
        
        XCTAssertEqual(references.count, 1)
        
        XCTAssertEqual(
            references[SymbolGraph.Symbol.Identifier(precise: "first", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/First" // Same symbol doesn't require disambiguation
        )
    }
    
    func testDifferentSymbolsWithDifferentKindsInDifferentLanguages() throws {
        let references = try disambiguatedReferencesForSymbols(
            swift: [
                TestSymbolData(preciseID: "first", pathComponents: ["Something", "First"], kind: .struct),
            ],
            objectiveC: [
                TestSymbolData(preciseID: "second", pathComponents: ["Something", "First"], kind: .protocol),
            ]
        )
        
        XCTAssertEqual(references.count, 2)
        
        XCTAssertEqual(references[SymbolGraph.Symbol.Identifier(precise: "first", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/First-swift.struct"
        )
        
        XCTAssertEqual(references[SymbolGraph.Symbol.Identifier(precise: "second", interfaceLanguage: "objective-c")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/First-c.protocol"
        )
    }
    
    func testSameSymbolWithDifferentNamesInDifferentLanguages() throws {
        let references = try disambiguatedReferencesForSymbols(
            swift: [
                TestSymbolData(preciseID: "first", pathComponents: ["Something", "first(one:two:)"], kind: .method),
            ],
            objectiveC: [
                TestSymbolData(preciseID: "first", pathComponents: ["Something", "firstWithOne:two:"], kind: .method),
            ]
        )
        
        XCTAssertEqual(references.count, 1)
        
        XCTAssertEqual(
            references[SymbolGraph.Symbol.Identifier(precise: "first", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/first(one:two:)"
        )
    }
    
    func testOneVariantOfMultiLanguageSymbolCollidesWithDifferentTypeSymbol() throws {
        let references = try disambiguatedReferencesForSymbols(
            swift: [
                TestSymbolData(preciseID: "instance-method", pathComponents: ["Something", "first(one:two:)"], kind: .method),
                TestSymbolData(preciseID: "type-method", pathComponents: ["Something", "first(one:two:)"], kind: .typeMethod),
            ],
            objectiveC: [
                TestSymbolData(preciseID: "type-method", pathComponents: ["Something", "firstWithOne:two:"], kind: .typeMethod),
            ]
        )
        
        XCTAssertEqual(references.count, 2)
        
        XCTAssertEqual(
            references[SymbolGraph.Symbol.Identifier(precise: "instance-method", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/first(one:two:)-swift.method"
        )
        XCTAssertEqual(
            references[SymbolGraph.Symbol.Identifier(precise: "type-method", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/first(one:two:)-swift.type.method"
        )
    }
    
    func testStructAndEnumAndTypeAliasCollisionOfSameSymbol() throws {
        let references = try disambiguatedReferencesForSymbols(
            swift: [
                TestSymbolData(preciseID: "first", pathComponents: ["Something", "First"], kind: .struct),
            ],
            objectiveC: [
                TestSymbolData(preciseID: "first", pathComponents: ["Something", "First"], kind: .enum),
                TestSymbolData(preciseID: "second", pathComponents: ["Something", "First"], kind: .typealias),
            ]
        )
        
        XCTAssertEqual(references.count, 2)
        
        XCTAssertEqual(references[SymbolGraph.Symbol.Identifier(precise: "first", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/First-swift.struct"
        )
        
        XCTAssertEqual(references[SymbolGraph.Symbol.Identifier(precise: "second", interfaceLanguage: "objective-c")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/First-c.typealias"
        )
    }
    
    func testTripleCollisionWithBothSameTypeAndDifferentType() throws {
        let references = try disambiguatedReferencesForSymbols(
            swift: [
                TestSymbolData(preciseID: "first", pathComponents: ["Something", "first(_:_:)"], kind: .method),
                TestSymbolData(preciseID: "second", pathComponents: ["Something", "first(_:_:)"], kind: .typeMethod),
                TestSymbolData(preciseID: "third", pathComponents: ["Something", "first(_:_:)"], kind: .typeMethod),
            ],
            objectiveC: []
        )
        
        XCTAssertEqual(references.count, 3)
        
        // The first collision can be disambiguated with its kind information
        XCTAssertEqual(
            references[SymbolGraph.Symbol.Identifier(precise: "first", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/first(_:_:)-swift.method"
        )
        XCTAssertEqual(
            references[SymbolGraph.Symbol.Identifier(precise: "second", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/first(_:_:)-\("second".stableHashString)"
        )
        XCTAssertEqual(
            references[SymbolGraph.Symbol.Identifier(precise: "third", interfaceLanguage: "swift")]?.path,
            "/documentation/SymbolDisambiguationTests/Something/first(_:_:)-\("third".stableHashString)"
        )
    }
    
    func testMixedLanguageFramework() throws {
        let (bundle, context) = try testBundleAndContext(named: "MixedLanguageFramework")
        
        var loader = SymbolGraphLoader(bundle: bundle, dataProvider: context.dataProvider)
        try loader.loadAll()
        
        let references = context.linkResolver.localResolver.referencesForSymbols(in: loader.unifiedGraphs, bundle: bundle, context: context).mapValues(\.path)
        XCTAssertEqual(Set(references.keys), [
            SymbolGraph.Symbol.Identifier(precise: "c:@CM@TestFramework@objc(cs)MixedLanguageClassConformingToProtocol(im)mixedLanguageMethod", interfaceLanguage: "swift"),
            .init(precise: "c:@E@Foo", interfaceLanguage: "swift"),
            .init(precise: "c:@E@Foo@first", interfaceLanguage: "swift"),
            .init(precise: "c:@E@Foo@fourth", interfaceLanguage: "swift"),
            .init(precise: "c:@E@Foo@second", interfaceLanguage: "swift"),
            .init(precise: "c:@E@Foo@third", interfaceLanguage: "swift"),
            .init(precise: "c:@M@TestFramework@objc(cs)MixedLanguageClassConformingToProtocol", interfaceLanguage: "swift"),
            .init(precise: "c:@M@TestFramework@objc(cs)MixedLanguageClassConformingToProtocol(im)init", interfaceLanguage: "swift"),
            .init(precise: "c:@M@TestFramework@objc(pl)MixedLanguageProtocol", interfaceLanguage: "swift"),
            .init(precise: "c:@M@TestFramework@objc(pl)MixedLanguageProtocol(im)mixedLanguageMethod", interfaceLanguage: "swift"),
            .init(precise: "c:@MixedLanguageFrameworkVersionNumber", interfaceLanguage: "occ"),
            .init(precise: "c:@MixedLanguageFrameworkVersionString", interfaceLanguage: "occ"),
            .init(precise: "c:MixedLanguageFramework.h@T@Foo", interfaceLanguage: "occ"),
            .init(precise: "c:objc(cs)Bar", interfaceLanguage: "swift"),
            .init(precise: "c:objc(cs)Bar(cm)myStringFunction:error:", interfaceLanguage: "swift"),
            .init(precise: "s:22MixedLanguageFramework15SwiftOnlyClassV", interfaceLanguage: "swift"),
            .init(precise: "s:22MixedLanguageFramework15SwiftOnlyStructV", interfaceLanguage: "swift"),
            .init(precise: "s:22MixedLanguageFramework15SwiftOnlyStructV4tadayyF", interfaceLanguage: "swift"),
            .init(precise: "s:So3FooV8rawValueABSu_tcfc", interfaceLanguage: "swift"),
        ])
        
        XCTAssertEqual(references[.init(precise: "c:@CM@TestFramework@objc(cs)MixedLanguageClassConformingToProtocol(im)mixedLanguageMethod", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/MixedLanguageClassConformingToProtocol/mixedLanguageMethod()")
        XCTAssertEqual(references[.init(precise: "c:@E@Foo", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/Foo-swift.struct")
        XCTAssertEqual(references[.init(precise: "c:@E@Foo@first", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/Foo-swift.struct/first")
        XCTAssertEqual(references[.init(precise: "c:@E@Foo@fourth", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/Foo-swift.struct/fourth")
        XCTAssertEqual(references[.init(precise: "c:@E@Foo@second", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/Foo-swift.struct/second")
        XCTAssertEqual(references[.init(precise: "c:@E@Foo@third", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/Foo-swift.struct/third")
        XCTAssertEqual(references[.init(precise: "c:@M@TestFramework@objc(cs)MixedLanguageClassConformingToProtocol", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/MixedLanguageClassConformingToProtocol")
        XCTAssertEqual(references[.init(precise: "c:@M@TestFramework@objc(cs)MixedLanguageClassConformingToProtocol(im)init", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/MixedLanguageClassConformingToProtocol/init()")
        XCTAssertEqual(references[.init(precise: "c:@M@TestFramework@objc(pl)MixedLanguageProtocol", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/MixedLanguageProtocol")
        XCTAssertEqual(references[.init(precise: "c:@M@TestFramework@objc(pl)MixedLanguageProtocol(im)mixedLanguageMethod", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/MixedLanguageProtocol/mixedLanguageMethod()")
        XCTAssertEqual(references[.init(precise: "c:@MixedLanguageFrameworkVersionNumber", interfaceLanguage: "occ")],
                       "/documentation/MixedLanguageFramework/_MixedLanguageFrameworkVersionNumber")
        XCTAssertEqual(references[.init(precise: "c:@MixedLanguageFrameworkVersionString", interfaceLanguage: "occ")],
                       "/documentation/MixedLanguageFramework/_MixedLanguageFrameworkVersionString")
        XCTAssertEqual(references[.init(precise: "c:MixedLanguageFramework.h@T@Foo", interfaceLanguage: "occ")],
                       "/documentation/MixedLanguageFramework/Foo-c.typealias")
        XCTAssertEqual(references[.init(precise: "c:objc(cs)Bar", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/Bar")
        XCTAssertEqual(references[.init(precise: "c:objc(cs)Bar(cm)myStringFunction:error:", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/Bar/myStringFunction(_:)")
        XCTAssertEqual(references[.init(precise: "s:22MixedLanguageFramework15SwiftOnlyClassV", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/SwiftOnlyClass")
        XCTAssertEqual(references[.init(precise: "s:22MixedLanguageFramework15SwiftOnlyStructV", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/SwiftOnlyStruct")
        XCTAssertEqual(references[.init(precise: "s:22MixedLanguageFramework15SwiftOnlyStructV4tadayyF", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/SwiftOnlyStruct/tada()")
        XCTAssertEqual(references[.init(precise: "s:So3FooV8rawValueABSu_tcfc", interfaceLanguage: "swift")],
                       "/documentation/MixedLanguageFramework/Foo-swift.struct/init(rawValue:)")
    }
    
    // MARK: - Test Helpers
    
    private struct TestSymbolData {
        let preciseID: String
        let pathComponents: [String]
        let kind: SymbolGraph.Symbol.KindIdentifier
    }
    
    private func disambiguatedReferencesForSymbols(swift swiftSymbols: [TestSymbolData], objectiveC objectiveCSymbols: [TestSymbolData]) throws -> [SymbolGraph.Symbol.Identifier : ResolvedTopicReference] {
        let graph = SymbolGraph(
            metadata: SymbolGraph.Metadata(
                formatVersion: SymbolGraph.SemanticVersion(major: 1, minor: 1, patch: 1),
                generator: "unit-test"
            ),
            module: SymbolGraph.Module(
                name: "SymbolDisambiguationTests",
                platform: SymbolGraph.Platform(architecture: nil, vendor: nil, operatingSystem: nil)
            ),
            symbols: swiftSymbols.map {
                SymbolGraph.Symbol(
                    identifier: SymbolGraph.Symbol.Identifier(precise: $0.preciseID, interfaceLanguage: "swift"),
                    names: SymbolGraph.Symbol.Names(title: "Title", navigator: nil, subHeading: nil, prose: nil), // names doesn't matter for path disambiguation
                    pathComponents: $0.pathComponents,
                    docComment: nil,
                    accessLevel: SymbolGraph.Symbol.AccessControl(rawValue: "public"),
                    kind: SymbolGraph.Symbol.Kind(parsedIdentifier: $0.kind, displayName: "Kind Display Name"), // kind display names doesn't matter for path disambiguation
                    mixins: [:]
                )
            },
            relationships: []
        )
        
        let unified = try XCTUnwrap(UnifiedSymbolGraph(fromSingleGraph: graph, at: URL(fileURLWithPath: "fake-path-for-swift-symbol-graph")))
        
        let graph2 = SymbolGraph(
            metadata: SymbolGraph.Metadata(
                formatVersion: SymbolGraph.SemanticVersion(major: 1, minor: 1, patch: 1),
                generator: "unit-test"
            ),
            module: SymbolGraph.Module(
                name: "SymbolDisambiguationTests",
                platform: SymbolGraph.Platform(architecture: nil, vendor: nil, operatingSystem: nil)
            ),
            symbols: objectiveCSymbols.map {
                SymbolGraph.Symbol(
                    identifier: SymbolGraph.Symbol.Identifier(precise: $0.preciseID, interfaceLanguage: "objective-c"),
                    names: SymbolGraph.Symbol.Names(title: "Title", navigator: nil, subHeading: nil, prose: nil), // names doesn't matter for path disambiguation
                    pathComponents: $0.pathComponents,
                    docComment: nil,
                    accessLevel: SymbolGraph.Symbol.AccessControl(rawValue: "public"),
                    kind: SymbolGraph.Symbol.Kind(parsedIdentifier: $0.kind, displayName: "Kind Display Name"), // kind display names doesn't matter for path disambiguation
                    mixins: [:]
                )
            },
            relationships: []
        )
        
        let swiftSymbolGraphURL = URL(fileURLWithPath: "fake-path-for-swift-symbol-graph")
        let objcSymbolGraphURL = URL(fileURLWithPath: "fake-path-for-swift-objc-graph")
        
        unified.mergeGraph(graph: graph2, at: objcSymbolGraphURL)
        let uniqueSymbolCount = Set(swiftSymbols.map(\.preciseID) + objectiveCSymbols.map(\.preciseID)).count
        XCTAssertEqual(unified.symbols.count, uniqueSymbolCount)
        
        let bundle = DocumentationBundle(
            info: DocumentationBundle.Info(
                displayName: "SymbolDisambiguationTests",
                identifier: "com.test.SymbolDisambiguationTests"),
            symbolGraphURLs: [swiftSymbolGraphURL, objcSymbolGraphURL],
            markupURLs: [],
            miscResourceURLs: []
        )
        
        class TestProvider: DocumentationContextDataProvider {
            var delegate: DocumentationContextDataProviderDelegate? = nil
            var bundles: [SwiftDocC.BundleIdentifier : SwiftDocC.DocumentationBundle] = [:]
            var symbolGraphData: [URL: Data] = [:]
            
            func contentsOfURL(_ url: URL, in bundle: SwiftDocC.DocumentationBundle) throws -> Data {
                guard let data = symbolGraphData[url] else {
                    fatalError("Only symbol graph content will be loaded from the bundle in this test")
                }
                return data
            }
        }
        
        let provider = TestProvider()
        provider.symbolGraphData = [
            swiftSymbolGraphURL: try JSONEncoder().encode(graph),
            objcSymbolGraphURL: try JSONEncoder().encode(graph2),
        ]
        provider.bundles[bundle.identifier] = bundle
        
        let context = try DocumentationContext(dataProvider: provider)
        
        return context.linkResolver.localResolver.referencesForSymbols(in: ["SymbolDisambiguationTests": unified], bundle: bundle, context: context)
    }
}