From c4d9f082d74f0bb08bad3ad6b698500d8437a0e8 Mon Sep 17 00:00:00 2001 From: Gesa HENTSCHKE Date: Wed, 23 Aug 2023 12:37:10 +0200 Subject: [PATCH] add unit tests for toCompletionParams --- .../lsp4e/test/edit/LSPEclipseUtilsTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/LSPEclipseUtilsTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/LSPEclipseUtilsTest.java index db70843e6..062d72cb2 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/LSPEclipseUtilsTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/LSPEclipseUtilsTest.java @@ -49,6 +49,7 @@ import org.eclipse.lsp4e.test.utils.NoErrorLoggedRule; import org.eclipse.lsp4e.test.utils.TestUtils; import org.eclipse.lsp4e.ui.UI; +import org.eclipse.lsp4j.CompletionTriggerKind; import org.eclipse.lsp4j.CreateFile; import org.eclipse.lsp4j.Location; import org.eclipse.lsp4j.Position; @@ -464,6 +465,56 @@ public void testGetOpenEditorExternalFile() throws Exception { Assert.assertNotEquals(Collections.emptySet(), LSPEclipseUtils.findOpenEditorsFor(file.toURI())); } + @Test + public void testToCompletionParams_EmptyDocument() throws Exception { + IProject p = TestUtils.createProject(getClass().getSimpleName() + System.currentTimeMillis()); + // Given an empty file/document + var file = TestUtils.createFile(p, "dummy"+new Random().nextInt(), ""); + var triggerChars = new char[] {':', '>'}; + // When toCompletionParams get called with offset == 0 and document.getLength() == 0: + var param = LSPEclipseUtils.toCompletionParams(file.getLocationURI(), 0, LSPEclipseUtils.getDocument(file), triggerChars); + // Then no context has been added to param: + Assert.assertNull(param.getContext()); + } + + @Test + public void testToCompletionParams_ZeroOffset() throws Exception { + IProject p = TestUtils.createProject(getClass().getSimpleName() + System.currentTimeMillis()); + // Given a non empty file/document containing a non trigger character at position 3: + var file = TestUtils.createFile(p, "dummy"+new Random().nextInt(), "std"); + var triggerChars = new char[] {':', '>'}; + // When toCompletionParams get called with offset == 0 and document.getLength() > 0: + var param = LSPEclipseUtils.toCompletionParams(file.getLocationURI(), 0, LSPEclipseUtils.getDocument(file), triggerChars); + // Then the trigger kind is Invoked: + Assert.assertEquals(param.getContext().getTriggerKind(), CompletionTriggerKind.Invoked); + } + + @Test + public void testToCompletionParams_MatchingTriggerCharacter() throws Exception { + IProject p = TestUtils.createProject(getClass().getSimpleName() + System.currentTimeMillis()); + // Given a non empty file/document containing a trigger character at position 4: + var file = TestUtils.createFile(p, "dummy"+new Random().nextInt(), "std:"); + var triggerChars = new char[] {':', '>'}; + // When toCompletionParams get called with offset > 0 and document.getLength() > 0: + var param = LSPEclipseUtils.toCompletionParams(file.getLocationURI(), 4, LSPEclipseUtils.getDocument(file), triggerChars); + // Then the context has been added with a colon as trigger character: + Assert.assertEquals(param.getContext().getTriggerCharacter(), ":"); + // And the trigger kind is TriggerCharacter: + Assert.assertEquals(param.getContext().getTriggerKind(), CompletionTriggerKind.TriggerCharacter); + } + + @Test + public void testToCompletionParams_NonMatchingTriggerCharacter() throws Exception { + IProject p = TestUtils.createProject(getClass().getSimpleName() + System.currentTimeMillis()); + // Given a non empty file/document containing a non trigger character at position 3: + var file = TestUtils.createFile(p, "dummy"+new Random().nextInt(), "std"); + var triggerChars = new char[] {':', '>'}; + // When toCompletionParams get called with offset > 0 and document.getLength() > 0: + var param = LSPEclipseUtils.toCompletionParams(file.getLocationURI(), 3, LSPEclipseUtils.getDocument(file), triggerChars); + // Then the trigger kind is Invoked: + Assert.assertEquals(param.getContext().getTriggerKind(), CompletionTriggerKind.Invoked); + } + @Test public void parseRange_shouldReturnRange_UriWithStartLineNo() { Range actual = LSPEclipseUtils.parseRange("file:///a/b#L35");