Skip to content

Commit 0d9b75f

Browse files
committed
Added ImplementAbstractFunctionsQuickFix tests
1 parent 653413c commit 0d9b75f

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

server/src/test/kotlin/org/javacs/kt/LanguageServerTestFixture.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ abstract class LanguageServerTestFixture(relativeWorkspaceRoot: String) : Langua
6868
fun textDocumentPosition(relativePath: String, line: Int, column: Int): TextDocumentPositionParams =
6969
textDocumentPosition(relativePath, position(line, column))
7070

71+
fun codeActionParams(relativePath: String, startLine: Int, startColumn: Int, endLine: Int, endColumn: Int, diagnostics: List<Diagnostic>, only: List<String>): CodeActionParams {
72+
val file = workspaceRoot.resolve(relativePath)
73+
val fileId = TextDocumentIdentifier(file.toUri().toString())
74+
val range = range(startLine, startColumn, endLine, endColumn)
75+
val context = CodeActionContext(diagnostics, only)
76+
77+
return CodeActionParams(fileId, range, context)
78+
}
79+
7180
fun hoverParams(relativePath: String, line: Int, column: Int): HoverParams =
7281
textDocumentPosition(relativePath, line, column).run { HoverParams(textDocument, position) }
7382

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.javacs.kt
2+
3+
import org.eclipse.lsp4j.CodeActionKind
4+
import org.eclipse.lsp4j.Diagnostic
5+
import org.eclipse.lsp4j.jsonrpc.messages.Either
6+
import org.hamcrest.Matchers
7+
import org.junit.Assert
8+
import org.junit.Test
9+
10+
class ImplementAbstractFunctionsQuickFixTest : SingleFileTestFixture("quickfixes", "SomeSubclass.kt") {
11+
@Test
12+
fun `gets workspace edit for all abstract methods when none are implemented`() {
13+
val diagnostic = Diagnostic(range(3, 1, 3, 19), "")
14+
diagnostic.code = Either.forLeft("ABSTRACT_CLASS_MEMBER_NOT_IMPLEMENTED")
15+
val codeActionParams = codeActionParams(file, 3, 1, 3, 19, listOf(diagnostic), listOf(CodeActionKind.QuickFix))
16+
17+
val codeActions = languageServer.textDocumentService.codeAction(codeActionParams).get()
18+
19+
Assert.assertThat(codeActions.size, Matchers.equalTo(1))
20+
Assert.assertThat(codeActions[0].right.kind, Matchers.equalTo(CodeActionKind.QuickFix))
21+
Assert.assertThat(codeActions[0].right.diagnostics.size, Matchers.equalTo(1))
22+
Assert.assertThat(codeActions[0].right.diagnostics[0], Matchers.equalTo(diagnostic))
23+
Assert.assertThat(codeActions[0].right.edit.changes.size, Matchers.equalTo(1))
24+
Assert.assertThat(codeActions[0].right.edit.changes[codeActionParams.textDocument.uri]?.size, Matchers.equalTo(2))
25+
Assert.assertThat(
26+
codeActions[0].right.edit.changes[codeActionParams.textDocument.uri]?.get(0)?.range,
27+
Matchers.equalTo(range(3, 55, 3, 55))
28+
)
29+
Assert.assertThat(
30+
codeActions[0].right.edit.changes[codeActionParams.textDocument.uri]?.get(0)?.newText,
31+
Matchers.equalTo(System.lineSeparator() + System.lineSeparator() + " override fun someSuperMethod(someParameter: String): Int { }")
32+
)
33+
Assert.assertThat(
34+
codeActions[0].right.edit.changes[codeActionParams.textDocument.uri]?.get(1)?.range,
35+
Matchers.equalTo(range(3, 55, 3, 55))
36+
)
37+
Assert.assertThat(
38+
codeActions[0].right.edit.changes[codeActionParams.textDocument.uri]?.get(1)?.newText,
39+
Matchers.equalTo(System.lineSeparator() + System.lineSeparator() + " override fun someInterfaceMethod() { }")
40+
)
41+
}
42+
43+
@Test
44+
fun `gets workspace edit for interface methods when super class abstract methods are implemented`() {
45+
val diagnostic = Diagnostic(range(6, 1, 6, 24), "")
46+
diagnostic.code = Either.forLeft("ABSTRACT_MEMBER_NOT_IMPLEMENTED")
47+
val codeActionParams = codeActionParams(file, 6, 1, 6, 24, listOf(diagnostic), listOf(CodeActionKind.QuickFix))
48+
49+
val codeActions = languageServer.textDocumentService.codeAction(codeActionParams).get()
50+
51+
Assert.assertThat(codeActions.size, Matchers.equalTo(1))
52+
Assert.assertThat(codeActions[0].right.kind, Matchers.equalTo(CodeActionKind.QuickFix))
53+
Assert.assertThat(codeActions[0].right.diagnostics.size, Matchers.equalTo(1))
54+
Assert.assertThat(codeActions[0].right.diagnostics[0], Matchers.equalTo(diagnostic))
55+
Assert.assertThat(codeActions[0].right.edit.changes.size, Matchers.equalTo(1))
56+
Assert.assertThat(codeActions[0].right.edit.changes[codeActionParams.textDocument.uri]?.size, Matchers.equalTo(1))
57+
Assert.assertThat(
58+
codeActions[0].right.edit.changes[codeActionParams.textDocument.uri]?.get(0)?.range,
59+
Matchers.equalTo(range(7, 74, 7, 74))
60+
)
61+
Assert.assertThat(
62+
codeActions[0].right.edit.changes[codeActionParams.textDocument.uri]?.get(0)?.newText,
63+
Matchers.equalTo(System.lineSeparator() + System.lineSeparator() + " override fun someInterfaceMethod() { }")
64+
)
65+
}
66+
67+
@Test
68+
fun `gets workspace edit for super class abstract methods when interface methods are implemented`() {
69+
val diagnostic = Diagnostic(range(10, 1, 10, 25), "")
70+
diagnostic.code = Either.forLeft("ABSTRACT_CLASS_MEMBER_NOT_IMPLEMENTED")
71+
val codeActionParams = codeActionParams(file, 10, 1, 10, 25, listOf(diagnostic), listOf(CodeActionKind.QuickFix))
72+
73+
val codeActions = languageServer.textDocumentService.codeAction(codeActionParams).get()
74+
75+
Assert.assertThat(codeActions.size, Matchers.equalTo(1))
76+
Assert.assertThat(codeActions[0].right.kind, Matchers.equalTo(CodeActionKind.QuickFix))
77+
Assert.assertThat(codeActions[0].right.diagnostics.size, Matchers.equalTo(1))
78+
Assert.assertThat(codeActions[0].right.diagnostics[0], Matchers.equalTo(diagnostic))
79+
Assert.assertThat(codeActions[0].right.edit.changes.size, Matchers.equalTo(1))
80+
Assert.assertThat(codeActions[0].right.edit.changes[codeActionParams.textDocument.uri]?.size, Matchers.equalTo(1))
81+
Assert.assertThat(
82+
codeActions[0].right.edit.changes[codeActionParams.textDocument.uri]?.get(0)?.range,
83+
Matchers.equalTo(range(11, 43, 11, 43))
84+
)
85+
Assert.assertThat(
86+
codeActions[0].right.edit.changes[codeActionParams.textDocument.uri]?.get(0)?.newText,
87+
Matchers.equalTo(System.lineSeparator() + System.lineSeparator() + " override fun someSuperMethod(someParameter: String): Int { }")
88+
)
89+
}
90+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package quickfixes
2+
3+
interface SomeInterface {
4+
fun someInterfaceMethod()
5+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package quickfixes
2+
3+
class SomeSubclass : SomeSuperClass(), SomeInterface {
4+
}
5+
6+
class SomeOtherSubclass : SomeSuperClass(), SomeInterface {
7+
override fun someSuperMethod(someParameter: String): Int { return 1 }
8+
}
9+
10+
class YetAnotherSubclass : SomeSuperClass(), SomeInterface {
11+
override fun someInterfaceMethod() { }
12+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package quickfixes
2+
3+
abstract class SomeSuperClass {
4+
5+
abstract fun someSuperMethod(someParameter: String): Int
6+
}

0 commit comments

Comments
 (0)