|
| 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 | +} |
0 commit comments