Skip to content

Filter backticks in TestItem IDs #1211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions Sources/SourceKitLSP/Swift/SwiftTestingScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,24 @@ final class SyntacticSwiftTestingTestScanner: SyntaxVisitor {
}

override func visit(_ node: ActorDeclSyntax) -> SyntaxVisitorContinueKind {
return visitTypeOrExtensionDecl(node, typeNames: [node.name.text])
guard let identifier = node.name.identifier else {
return .skipChildren
}
return visitTypeOrExtensionDecl(node, typeNames: [identifier.name])
}

override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
return visitTypeOrExtensionDecl(node, typeNames: [node.name.text])
guard let identifier = node.name.identifier else {
return .skipChildren
}
return visitTypeOrExtensionDecl(node, typeNames: [identifier.name])
}

override func visit(_ node: EnumDeclSyntax) -> SyntaxVisitorContinueKind {
return visitTypeOrExtensionDecl(node, typeNames: [node.name.text])
guard let identifier = node.name.identifier else {
return .skipChildren
}
return visitTypeOrExtensionDecl(node, typeNames: [identifier.name])
}

override func visit(_ node: ExtensionDeclSyntax) -> SyntaxVisitorContinueKind {
Expand All @@ -289,24 +298,29 @@ final class SyntacticSwiftTestingTestScanner: SyntaxVisitor {
}

override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind {
return visitTypeOrExtensionDecl(node, typeNames: [node.name.text])
guard let identifier = node.name.identifier else {
return .skipChildren
}
return visitTypeOrExtensionDecl(node, typeNames: [identifier.name])
}

override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
let testAttribute = node.attributes
.compactMap { $0.as(AttributeSyntax.self) }
.first { $0.isNamed("Test", inModuleNamed: "Testing") }

guard let testAttribute else {
guard let testAttribute, let identifier = node.name.identifier else {
return .skipChildren
}
let attributeData = TestingAttributeData(attribute: testAttribute)
if attributeData.isHidden {
return .skipChildren
}

let name =
node.name.text + "(" + node.signature.parameterClause.parameters.map { "\($0.firstName.text):" }.joined() + ")"
let parameters = node.signature.parameterClause.parameters.map {
"\($0.firstName.identifier?.name ?? $0.firstName.text):"
}.joined()
let name = "\(identifier.name)(\(parameters))"

let range = snapshot.absolutePositionRange(
of: node.positionAfterSkippingLeadingTrivia..<node.endPositionBeforeTrailingTrivia
Expand Down Expand Up @@ -398,12 +412,12 @@ fileprivate extension TypeSyntax {
var components: [String]? {
switch self.as(TypeSyntaxEnum.self) {
case .identifierType(let identifierType):
return [identifierType.name.text]
return [identifierType.name.identifier?.name ?? identifierType.name.text]
case .memberType(let memberType):
guard let baseComponents = memberType.baseType.components else {
return nil
}
return baseComponents + [memberType.name.text]
return baseComponents + [memberType.name.identifier?.name ?? memberType.name.text]
default:
return nil
}
Expand Down
61 changes: 61 additions & 0 deletions Tests/SourceKitLSPTests/DocumentTestDiscoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,67 @@ final class DocumentTestDiscoveryTests: XCTestCase {
)
}

func testSwiftTestingTestWithBackticksInName() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)

let positions = testClient.openDocument(
"""
import Testing

1️⃣struct `MyTests` {
2️⃣@Test
func `oneIsTwo`(`foo`: Int) {
#expect(1 == 2)
}3️⃣
}4️⃣

5️⃣extension `MyTests` {
6️⃣@Test
func `twoIsThree`() {
#expect(2 == 3)
}7️⃣
}8️⃣
""",
uri: uri
)

let tests = try await testClient.send(DocumentTestsRequest(textDocument: TextDocumentIdentifier(uri)))
XCTAssertEqual(
tests,
[
TestItem(
id: "MyTests",
label: "MyTests",
disabled: false,
style: TestStyle.swiftTesting,
location: Location(uri: uri, range: positions["1️⃣"]..<positions["4️⃣"]),
children: [
TestItem(
id: "MyTests/oneIsTwo(foo:)",
label: "oneIsTwo(foo:)",
disabled: false,
style: TestStyle.swiftTesting,
location: Location(uri: uri, range: positions["2️⃣"]..<positions["3️⃣"]),
children: [],
tags: []
),
TestItem(
id: "MyTests/twoIsThree()",
label: "twoIsThree()",
disabled: false,
style: TestStyle.swiftTesting,
location: Location(uri: uri, range: positions["6️⃣"]..<positions["7️⃣"]),
children: [],
tags: []
),
],
tags: []
)
]
)
}

func testDisabledSwiftTestingTest() async throws {
let testClient = try await TestSourceKitLSPClient()
let uri = DocumentURI(for: .swift)
Expand Down