Skip to content

Commit a6f85d8

Browse files
committed
Add test cases that check code actions invoked from a range
1 parent 449d2a9 commit a6f85d8

File tree

1 file changed

+69
-11
lines changed

1 file changed

+69
-11
lines changed

Tests/SourceKitLSPTests/CodeActionTests.swift

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,8 @@ final class CodeActionTests: XCTestCase {
689689
try await assertCodeActions(
690690
"""
691691
let x = 1️⃣12️⃣63️⃣
692-
"""
692+
""",
693+
ranges: [("1️⃣", "2️⃣"), ("1️⃣", "3️⃣")]
693694
) { uri, positions in
694695
[
695696
CodeAction(
@@ -728,6 +729,7 @@ final class CodeActionTests: XCTestCase {
728729
"""
729730
let x = 1️⃣#"Hello 2️⃣world"#3️⃣
730731
""",
732+
ranges: [("1️⃣", "3️⃣")],
731733
exhaustive: false
732734
) { uri, positions in
733735
[
@@ -749,6 +751,7 @@ final class CodeActionTests: XCTestCase {
749751
##"""
750752
let x = 1️⃣#"Hello 2️⃣\#(name)"#3️⃣
751753
"""##,
754+
ranges: [("1️⃣", "3️⃣")],
752755
exhaustive: false
753756
) { uri, positions in
754757
[
@@ -787,9 +790,10 @@ final class CodeActionTests: XCTestCase {
787790
func testMigrateIfLetSyntax() async throws {
788791
try await assertCodeActions(
789792
##"""
790-
1️⃣if 2️⃣let 3️⃣foo = 4️⃣foo {}9️⃣
793+
1️⃣if 2️⃣let 3️⃣foo = 4️⃣foo {}5️⃣
791794
"""##,
792-
markers: ["1️⃣", "2️⃣", "3️⃣", "4️⃣"]
795+
markers: ["1️⃣", "2️⃣", "3️⃣", "4️⃣"],
796+
ranges: [("1️⃣", "4️⃣"), ("1️⃣", "5️⃣")]
793797
) { uri, positions in
794798
[
795799
CodeAction(
@@ -800,7 +804,7 @@ final class CodeActionTests: XCTestCase {
800804
changes: [
801805
uri: [
802806
TextEdit(
803-
range: positions["1️⃣"]..<positions["9️⃣"],
807+
range: positions["1️⃣"]..<positions["5️⃣"],
804808
newText: "if let foo {}"
805809
)
806810
]
@@ -827,9 +831,10 @@ final class CodeActionTests: XCTestCase {
827831
func testOpaqueParameterToGeneric() async throws {
828832
try await assertCodeActions(
829833
##"""
830-
1️⃣func 2️⃣someFunction(_ 3️⃣input: some4️⃣ Value) {}9️⃣
834+
1️⃣func 2️⃣someFunction(_ 3️⃣input: some4️⃣ Value) {}5️⃣
831835
"""##,
832836
markers: ["1️⃣", "2️⃣", "3️⃣", "4️⃣"],
837+
ranges: [("1️⃣", "2️⃣"), ("1️⃣", "5️⃣")],
833838
exhaustive: false
834839
) { uri, positions in
835840
[
@@ -841,7 +846,7 @@ final class CodeActionTests: XCTestCase {
841846
changes: [
842847
uri: [
843848
TextEdit(
844-
range: positions["1️⃣"]..<positions["9️⃣"],
849+
range: positions["1️⃣"]..<positions["5️⃣"],
845850
newText: "func someFunction<T1: Value>(_ input: T1) {}"
846851
)
847852
]
@@ -875,6 +880,7 @@ final class CodeActionTests: XCTestCase {
875880
}5️⃣
876881
877882
"""##,
883+
ranges: [("1️⃣", "5️⃣")],
878884
exhaustive: false
879885
) { uri, positions in
880886
[
@@ -903,9 +909,54 @@ final class CodeActionTests: XCTestCase {
903909
}
904910
}
905911

912+
func testAddDocumentationRefactorSingleParameter() async throws {
913+
try await assertCodeActions(
914+
"""
915+
1️⃣func 2️⃣refactor(3️⃣syntax: 4️⃣Decl5️⃣Syntax)6️⃣ { }7️⃣
916+
""",
917+
ranges: [("1️⃣", "2️⃣"), ("1️⃣", "6️⃣"), ("1️⃣", "7️⃣")],
918+
exhaustive: false
919+
) { uri, positions in
920+
[
921+
CodeAction(
922+
title: "Add documentation",
923+
kind: .refactorInline,
924+
diagnostics: nil,
925+
edit: WorkspaceEdit(
926+
changes: [
927+
uri: [
928+
TextEdit(
929+
range: Range(positions["1️⃣"]),
930+
newText: """
931+
/// A description
932+
/// - Parameter syntax:
933+
\("")
934+
"""
935+
)
936+
]
937+
]
938+
),
939+
command: nil
940+
)
941+
]
942+
}
943+
}
944+
945+
/// Retrieves the code action at a set of markers and asserts that it matches a list of expected code actions.
946+
///
947+
/// - Parameters:
948+
/// - markedText: The source file input to get the code actions for.
949+
/// - markers: The list of markers to retrieve code actions at. If `nil` code actions will be retrieved for all
950+
/// markers in `markedText`
951+
/// - ranges: If specified, code actions are also requested for selection ranges between these markers.
952+
/// - exhaustive: Whether `expected` is expected to be a subset of the returned code actions or whether it is
953+
/// expected to exhaustively match all code actions.
954+
/// - expected: A closure that returns the list of expected code actions, given the URI of the test document and the
955+
/// marker positions within.
906956
private func assertCodeActions(
907957
_ markedText: String,
908958
markers: [String]? = nil,
959+
ranges: [(String, String)] = [],
909960
exhaustive: Bool = true,
910961
expected: (_ uri: DocumentURI, _ positions: DocumentPositions) -> [CodeAction],
911962
file: StaticString = #filePath,
@@ -915,31 +966,38 @@ final class CodeActionTests: XCTestCase {
915966
let uri = DocumentURI.for(.swift)
916967
let positions = testClient.openDocument(markedText, uri: uri)
917968

918-
for marker in markers ?? extractMarkers(markedText).markers.map(\.key) {
969+
var ranges = ranges
970+
if let markers {
971+
ranges += markers.map { ($0, $0) }
972+
} else {
973+
ranges += extractMarkers(markedText).markers.map(\.key).map { ($0, $0) }
974+
}
975+
976+
for (startMarker, endMarker) in ranges {
919977
let result = try await testClient.send(
920978
CodeActionRequest(
921-
range: Range(positions[marker]),
979+
range: positions[startMarker]..<positions[endMarker],
922980
context: .init(),
923981
textDocument: TextDocumentIdentifier(uri)
924982
)
925983
)
926984
guard case .codeActions(let codeActions) = result else {
927-
XCTFail("Expected code actions at marker \(marker)", file: file, line: line)
985+
XCTFail("Expected code actions at range \(startMarker)-\(endMarker)", file: file, line: line)
928986
return
929987
}
930988
if exhaustive {
931989
XCTAssertEqual(
932990
codeActions,
933991
expected(uri, positions),
934-
"Found unexpected code actions at \(marker)",
992+
"Found unexpected code actions at range \(startMarker)-\(endMarker)",
935993
file: file,
936994
line: line
937995
)
938996
} else {
939997
XCTAssert(
940998
codeActions.contains(expected(uri, positions)),
941999
"""
942-
Code actions did not contain expected at \(marker):
1000+
Code actions did not contain expected at range \(startMarker)-\(endMarker):
9431001
\(codeActions)
9441002
""",
9451003
file: file,

0 commit comments

Comments
 (0)