IntelliJ Platform SDK DevGuide

Edit page

3. Completion Test

In this test we will check if code completion, implemented in the Reference Contributor section of the Custom Language Support Tutorial works as we expect.

3.1. Define test data

Create a file DefaultTestData.simple.

# You are reading the ".properties" entry. ! The exclamation mark can also mark text as comments. website = http://en.wikipedia.org/ language = English # The backslash below tells the application to continue reading # the value onto the next line. message = Welcome to \ Wikipedia! # Add spaces to the key key\ with\ spaces = This is the value that could be looked up with the key "key with spaces". # Unicode tab : \u0009

Create one more file CompleteTestData.java.

public class Test { public static void main(String[] args) { System.out.println("simple:<caret>"); } }

3.2. Define a test

package com.simpleplugin; import com.intellij.application.options.CodeStyle; import com.intellij.codeInsight.completion.CompletionType; import com.intellij.codeInsight.generation.actions.CommentByLineCommentAction; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.psi.PsiElement; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; import com.intellij.usageView.UsageInfo; import com.intellij.util.containers.ContainerUtil; import com.simpleplugin.psi.SimpleProperty; import java.util.Arrays; import java.util.Collection; import java.util.List; public class SimpleCodeInsightTest extends LightCodeInsightFixtureTestCase { @Override protected void setUp() throws Exception { super.setUp(); } @Override protected String getTestDataPath() { return "testData"; } public void testCompletion() { myFixture.configureByFiles("CompleteTestData.java", "DefaultTestData.simple"); myFixture.complete(CompletionType.BASIC, 1); List<String> strings = myFixture.getLookupElementStrings(); assertTrue(strings.containsAll(Arrays.asList("key with spaces", "language", "message", "tab", "website"))); assertEquals(5, strings.size()); } public void testAnnotator() { myFixture.configureByFiles("AnnotatorTestData.java", "DefaultTestData.simple"); myFixture.checkHighlighting(false, false, true, true); } public void testFormatter() { myFixture.configureByFiles("FormatterTestData.simple"); CodeStyle.getLanguageSettings(myFixture.getFile()).SPACE_AROUND_ASSIGNMENT_OPERATORS = true; CodeStyle.getLanguageSettings(myFixture.getFile()).KEEP_BLANK_LINES_IN_CODE = 2; WriteCommandAction.writeCommandAction(getProject()).run(() -> { CodeStyleManager.getInstance(getProject()).reformatText(myFixture.getFile(), ContainerUtil.newArrayList(myFixture.getFile().getTextRange())); }); myFixture.checkResultByFile("DefaultTestData.simple"); } public void testRename() { myFixture.configureByFiles("RenameTestData.java", "RenameTestData.simple"); myFixture.renameElementAtCaret("websiteUrl"); myFixture.checkResultByFile("RenameTestData.simple", "RenameTestDataAfter.simple", false); } public void testFolding() { myFixture.configureByFiles("DefaultTestData.simple"); myFixture.testFolding(getTestDataPath() + "/FoldingTestData.java"); } public void testFindUsages() { Collection<UsageInfo> usageInfos = myFixture.testFindUsages("FindUsagesTestData.simple", "FindUsagesTestData.java"); assertEquals(1, usageInfos.size()); } public void testCommenter() { myFixture.configureByText(SimpleFileType.INSTANCE, "<caret>website = http://en.wikipedia.org/"); CommentByLineCommentAction commentAction = new CommentByLineCommentAction(); commentAction.actionPerformedImpl(getProject(), myFixture.getEditor()); myFixture.checkResult("#website = http://en.wikipedia.org/"); commentAction.actionPerformedImpl(getProject(), myFixture.getEditor()); myFixture.checkResult("website = http://en.wikipedia.org/"); } public void testReference() { myFixture.configureByFiles("ReferenceTestData.java", "DefaultTestData.simple"); PsiElement element = myFixture.getFile().findElementAt(myFixture.getCaretOffset()).getParent(); assertEquals("http://en.wikipedia.org/", ((SimpleProperty) element.getReferences()[0].resolve()).getValue()); } }

3.3. Run the test

Run the test and make sure it’s green.

Last modified: 9 October 2017