Skip to content

Commit

Permalink
add unit tests for toCompletionParams
Browse files Browse the repository at this point in the history
  • Loading branch information
ghentschke authored and mickaelistria committed Aug 23, 2023
1 parent 17a349e commit c4d9f08
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit c4d9f08

Please sign in to comment.