|
| 1 | +package org.javacs.kt |
| 2 | + |
| 3 | +import com.google.gson.Gson |
| 4 | +import org.eclipse.lsp4j.ExecuteCommandParams |
| 5 | +import org.eclipse.lsp4j.Position |
| 6 | +import org.eclipse.lsp4j.Range |
| 7 | +import org.eclipse.lsp4j.TextDocumentIdentifier |
| 8 | +import org.eclipse.lsp4j.TextDocumentPositionParams |
| 9 | +import org.junit.Test |
| 10 | +import org.hamcrest.Matchers.equalTo |
| 11 | +import org.hamcrest.Matchers.hasSize |
| 12 | +import org.junit.Assert.assertThat |
| 13 | + |
| 14 | +// TODO: what should the title be? just the signature? or the name of the member? should we separate between methods and variables? |
| 15 | + |
| 16 | +class OverrideMemberTest : SingleFileTestFixture("overridemember", "OverrideMembers.kt") { |
| 17 | + |
| 18 | + val root = testResourcesRoot().resolve(workspaceRoot) |
| 19 | + val fileUri = root.resolve(file).toUri().toString() |
| 20 | + |
| 21 | + @Test |
| 22 | + fun `should show all overrides for class`() { |
| 23 | + val result = languageServer.getProtocolExtensionService().overrideMember(TextDocumentPositionParams(TextDocumentIdentifier(fileUri), position(9, 8))).get() |
| 24 | + |
| 25 | + assertThat(result, hasSize(2)) |
| 26 | + |
| 27 | + val firstCodeAction = result[0] |
| 28 | + assertThat(firstCodeAction.title, equalTo("text")) |
| 29 | + |
| 30 | + val firstTextEdit = firstCodeAction.edit.changes |
| 31 | + assertThat(firstTextEdit.containsKey(fileUri), equalTo(true)) |
| 32 | + assertThat(firstTextEdit[fileUri], hasSize(1)) |
| 33 | + |
| 34 | + val memberToImplementEdit = firstTextEdit[fileUri]?.get(0) |
| 35 | + assertThat(memberToImplementEdit?.range, equalTo(range(9, 32, 9, 32))) |
| 36 | + assertThat(memberToImplementEdit?.newText, equalTo(System.lineSeparator() + System.lineSeparator() + " override val text: String = ???")) |
| 37 | + |
| 38 | + |
| 39 | + val secondCodeAction = result[1] |
| 40 | + assertThat(secondCodeAction.title, equalTo("print")) |
| 41 | + |
| 42 | + val secondTextEdit = secondCodeAction.edit.changes |
| 43 | + assertThat(secondTextEdit.containsKey(fileUri), equalTo(true)) |
| 44 | + assertThat(secondTextEdit[fileUri], hasSize(1)) |
| 45 | + |
| 46 | + val functionToImplementEdit = secondTextEdit[fileUri]?.get(0) |
| 47 | + assertThat(functionToImplementEdit?.range, equalTo(range(9, 32, 9, 32))) |
| 48 | + assertThat(functionToImplementEdit?.newText, equalTo(System.lineSeparator() + System.lineSeparator() + " override fun print() {}")) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `should show one override for class where other alternatives are already implemented`() { |
| 53 | + val result = languageServer.getProtocolExtensionService().overrideMember(TextDocumentPositionParams(TextDocumentIdentifier(fileUri), position(11, 8))).get() |
| 54 | + |
| 55 | + assertThat(result, hasSize(1)) |
| 56 | + |
| 57 | + val codeAction = result[0] |
| 58 | + assertThat(codeAction.title, equalTo("print")) |
| 59 | + |
| 60 | + val textEdit = codeAction.edit.changes |
| 61 | + assertThat(textEdit.containsKey(fileUri), equalTo(true)) |
| 62 | + assertThat(textEdit[fileUri], hasSize(1)) |
| 63 | + |
| 64 | + val functionToImplementEdit = textEdit[fileUri]?.get(0) |
| 65 | + assertThat(functionToImplementEdit?.range, equalTo(range(12, 57, 12, 57))) |
| 66 | + assertThat(functionToImplementEdit?.newText, equalTo(System.lineSeparator() + System.lineSeparator() + " override fun print() {}")) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `should show NO overrides for class where all other alternatives are already implemented`() { |
| 71 | + val result = languageServer.getProtocolExtensionService().overrideMember(TextDocumentPositionParams(TextDocumentIdentifier(fileUri), position(15, 8))).get() |
| 72 | + |
| 73 | + assertThat(result, hasSize(0)) |
| 74 | + } |
| 75 | + |
| 76 | + // TODO: test for kotlin sdk and jdk classes to verify that it works |
| 77 | +} |
0 commit comments