Files
sourcekit-lsp/Tests/SourceKitLSPTests/ImplementationTests.swift
Alex Hoppen ee1fc93b7c Remove dependency on ISDBTestSupport
We weren’t using it anymore anyway.
2024-10-17 11:09:39 -07:00

310 lines
7.6 KiB
Swift
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import LanguageServerProtocol
import SKTestSupport
import TSCBasic
import XCTest
final class ImplementationTests: XCTestCase {
// MARK: - Utilities
private func testImplementation(
_ markedText: String,
expectedLocations expectedLocationMarkers: [String] = ["2"],
testName: String = #function,
line: UInt = #line
) async throws {
let project = try await IndexedSingleSwiftFileTestProject(markedText, testName: testName)
let response = try await project.testClient.send(
ImplementationRequest(
textDocument: TextDocumentIdentifier(project.fileURI),
position: project.positions["1"]
)
)
let expectedLocations = expectedLocationMarkers.map {
Location(uri: project.fileURI, range: Range(project.positions[$0]))
}
XCTAssertEqual(response?.locations, expectedLocations, line: line)
}
// MARK: - Tests
func testProtocolInheritance() async throws {
try await testImplementation(
"""
protocol 1⃣Protocol {}
struct Struct: 2⃣Protocol {}
"""
)
}
func testProtocolStaticVar() async throws {
try await testImplementation(
"""
protocol Protocol {
static var 1⃣staticVar: Int { get }
}
struct Struct: Protocol {
static var 2⃣staticVar: Int { 123 }
}
"""
)
}
func testProtocolStaticFunc() async throws {
try await testImplementation(
"""
protocol Protocol {
static func 1⃣staticFunction()
}
struct Struct: Protocol {
static func 2⃣staticFunction() {}
}
"""
)
}
func testProtocolVar() async throws {
try await testImplementation(
"""
protocol Protocol {
var 1⃣variable: Int { get }
}
struct Struct: Protocol {
var 2⃣variable: Int { 123 }
}
"""
)
}
func testProtocolFunc() async throws {
try await testImplementation(
"""
protocol Protocol {
func 1⃣function()
}
struct Struct: Protocol {
func 2⃣function() {}
}
"""
)
}
func testClassInheritance() async throws {
try await testImplementation(
"""
class 1⃣Class {}
class Subclass: 2⃣Class {}
"""
)
}
func testClassClassVar() async throws {
try await testImplementation(
"""
class Class {
class var 1⃣classVar: Int { 123 }
}
class Subclass: Class {
override class var 2⃣classVar: Int { 123 }
}
"""
)
}
func testClassClassFunc() async throws {
try await testImplementation(
"""
class Class {
class func 1⃣classFunction() {}
}
class Subclass: Class {
override class func 2⃣classFunction() {}
}
"""
)
}
func testClassVar() async throws {
try await testImplementation(
"""
class Class {
var 1⃣variable: Int { 123 }
}
class Subclass: Class {
override var 2⃣variable: Int { 123 }
}
"""
)
}
func testClassFunc() async throws {
try await testImplementation(
"""
class Class {
func 1⃣function() {}
}
class Subclass: Class {
override func 2⃣function() {}
}
"""
)
}
func testClassHierarchy() async throws {
try await testImplementation(
"""
class 1⃣MyClass {}
class SubA: 2⃣MyClass {}
class SubASub: SubA {}
class SubB: 3⃣MyClass {}
class SubC: 4⃣MyClass {}
""",
expectedLocations: ["2", "3", "4"]
)
}
func testSubclassHierarchy() async throws {
try await testImplementation(
"""
class MyClass {}
class 1⃣Subclass: MyClass {}
class SubSubClassA: 2⃣Subclass {}
class SubSubClassB: 3⃣Subclass {}
""",
expectedLocations: ["2", "3"]
)
}
func testProtocolHierarchy() async throws {
try await testImplementation(
"""
protocol 1⃣MyProtocol {}
protocol OtherProtocol: 2⃣MyProtocol {}
class ClassA: 3⃣MyProtocol {}
class ClassB: OtherProtocol, 4⃣MyProtocol {}
""",
expectedLocations: ["2", "3", "4"]
)
}
func testProtocolConformanceInExtension() async throws {
try await testImplementation(
"""
protocol 1⃣MyProtocol {}
class MyClass {}
extension MyClass: 2⃣MyProtocol {}
"""
)
}
func testStandaloneClass() async throws {
try await testImplementation(
"""
class 1⃣MyClass {}
""",
expectedLocations: []
)
}
func testStandaloneClassFunc() async throws {
try await testImplementation(
"""
class MyClass {
func 1⃣myFunc() {}
}
""",
expectedLocations: []
)
}
func testOverrideClassVar() async throws {
try await testImplementation(
"""
class MyClass {
var 1⃣member: String { "puszysta" }
}
class SubclassA: MyClass {
override var 2⃣member: String { "glazurowana" }
}
class SubclassB: MyClass {
override var 3⃣member: String { "piaskowana" }
}
""",
expectedLocations: ["2", "3"]
)
}
func testOverrideProtocolFunc() async throws {
// TODO: We should not be reporting locations 4, 5 and 7 because they don't actually contain myFunc.
// We should, however, be reporting location 6. (https://github.com/swiftlang/sourcekit-lsp/issues/1600)
try await testImplementation(
"""
protocol MyProto {
func 1⃣myFunc()
}
class ClassA: MyProto {
func 2⃣myFunc() {}
}
class ClassB: MyProto {
func 3⃣myFunc() {}
}
class 4⃣ClassBSubA: ClassB {}
class 5⃣ClassBSubB: ClassB {}
class RetroactiveConformanceClassWithMyFuncInClassDecl {
func 6⃣myFunc() { }
}
extension 7⃣RetroactiveConformanceClassWithMyFuncInClassDecl: MyProto {}
class RetroactiveConformanceClassWithMyFuncInExtension {}
extension RetroactiveConformanceClassWithMyFuncInExtension: MyProto {
func 8⃣myFunc() { }
}
""",
expectedLocations: ["2", "3", "4", "5", "7", "8"]
)
}
func testCrossFile() async throws {
let project = try await SwiftPMTestProject(
files: [
"a.swift": """
protocol 1⃣MyProto {}
""",
"b.swift": """
struct MyStruct: 2⃣MyProto {}
""",
],
enableBackgroundIndexing: true
)
let (aUri, aPositions) = try project.openDocument("a.swift")
let response = try await project.testClient.send(
ImplementationRequest(
textDocument: TextDocumentIdentifier(aUri),
position: aPositions["1"]
)
)
XCTAssertEqual(
response?.locations,
[Location(uri: try project.uri(for: "b.swift"), range: Range(try project.position(of: "2", in: "b.swift")))]
)
}
}