Fixed KotlinConfidenceTests

This commit is contained in:
Alexander Podkhalyuzin
2018-10-22 15:04:23 +03:00
parent d66619b1c2
commit 16d3bf7716
@@ -23,22 +23,25 @@ import com.intellij.codeInsight.completion.LightCompletionTestCase;
import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupManager; import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl; import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.projectRoots.JavaSdk; import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiFile;
import com.intellij.util.ArrayUtil; import com.intellij.util.ArrayUtil;
import org.apache.commons.lang.SystemUtils; import org.apache.commons.lang.SystemUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.completion.test.CompletionTestUtilKt; import org.jetbrains.kotlin.idea.completion.test.CompletionTestUtilKt;
import org.jetbrains.kotlin.idea.test.RunnableWithException;
import org.jetbrains.kotlin.idea.test.TestUtilsKt; import org.jetbrains.kotlin.idea.test.TestUtilsKt;
import org.jetbrains.kotlin.test.InTextDirectivesUtils; import org.jetbrains.kotlin.test.InTextDirectivesUtils;
import java.io.File; import java.io.File;
import java.lang.reflect.Method;
import java.util.List; import java.util.List;
public class KotlinConfidenceTest extends LightCompletionTestCase { public class KotlinConfidenceTest extends LightCompletionTestCase {
private static final String TYPE_DIRECTIVE_PREFIX = "// TYPE:"; private static final String TYPE_DIRECTIVE_PREFIX = "// TYPE:";
private final ThreadLocal<Boolean> skipComplete = ThreadLocal.withInitial(() -> false);
public void testCompleteOnDotOutOfRanges() { public void testCompleteOnDotOutOfRanges() {
doTest(); doTest();
@@ -92,19 +95,46 @@ public class KotlinConfidenceTest extends LightCompletionTestCase {
CodeInsightSettings.getInstance().SELECT_AUTOPOPUP_SUGGESTIONS_BY_CHARS = true; CodeInsightSettings.getInstance().SELECT_AUTOPOPUP_SUGGESTIONS_BY_CHARS = true;
try { try {
configureByFile(getBeforeFileName()); skipComplete.set(true);
String text = getEditor().getDocument().getText(); try {
String typeText = getTypeText(text); configureByFile(getBeforeFileName());
List<String> expectedElements = InTextDirectivesUtils.findLinesWithPrefixesRemoved(text, "// ELEMENT:"); } finally {
boolean noLookup = InTextDirectivesUtils.isDirectiveDefined(text, "// NO_LOOKUP"); skipComplete.set(false);
}
String text = getEditor().getDocument().getText();
boolean noLookup = InTextDirectivesUtils.isDirectiveDefined(text, "// NO_LOOKUP");
if (!noLookup) complete(); //This will cause NPE in case of absence of autopopup completion
List<String> expectedElements = InTextDirectivesUtils.findLinesWithPrefixesRemoved(text, "// ELEMENT:");
assertFalse("Can't both expect lookup elements and no lookup", !expectedElements.isEmpty() && noLookup); assertFalse("Can't both expect lookup elements and no lookup", !expectedElements.isEmpty() && noLookup);
if (noLookup) { if (noLookup) {
assertNull("Expected no lookup", getLookup()); try {
Method m = null; //Looking for shouldSkipAutoPopup method (note, we can't use name because of scrambling in Ultimate)
for (Method method : CodeCompletionHandlerBase.class.getDeclaredMethods()) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (method.getReturnType().equals(Boolean.TYPE) &&
parameterTypes.length == 2 &&
parameterTypes[0].equals(Editor.class) &&
parameterTypes[1].equals(PsiFile.class)) {
assertNull("Only one method with such signature should exist", m);
m = method;
}
}
assertNotNull(m);
m.setAccessible(true);
Object o = m.invoke(null, getEditor(), getFile());
assert o instanceof Boolean;
assertTrue("Should skip autopopup completion", ((Boolean) o).booleanValue());
} catch (Throwable e) {
throw new AssertionError(e);
}
return; return;
} }
else if (!expectedElements.isEmpty()) {
String typeText = getTypeText(text);
if (!expectedElements.isEmpty()) {
assertContainsItems(ArrayUtil.toStringArray(expectedElements)); assertContainsItems(ArrayUtil.toStringArray(expectedElements));
return; return;
} }
@@ -117,8 +147,7 @@ public class KotlinConfidenceTest extends LightCompletionTestCase {
} }
} }
protected static String getTypeText(String text) { private static String getTypeText(String text) {
String[] directives = InTextDirectivesUtils.findArrayWithPrefixes(text, TYPE_DIRECTIVE_PREFIX); String[] directives = InTextDirectivesUtils.findArrayWithPrefixes(text, TYPE_DIRECTIVE_PREFIX);
if (directives.length == 0) return null; if (directives.length == 0) return null;
assertEquals("One directive with \"" + TYPE_DIRECTIVE_PREFIX +"\" expected", 1, directives.length); assertEquals("One directive with \"" + TYPE_DIRECTIVE_PREFIX +"\" expected", 1, directives.length);
@@ -126,11 +155,11 @@ public class KotlinConfidenceTest extends LightCompletionTestCase {
return StringUtil.unquoteString(directives[0]); return StringUtil.unquoteString(directives[0]);
} }
protected String getBeforeFileName() { private String getBeforeFileName() {
return getTestName(false) + ".kt"; return getTestName(false) + ".kt";
} }
protected String getAfterFileName() { private String getAfterFileName() {
return getTestName(false) + ".kt.after"; return getTestName(false) + ".kt.after";
} }
@@ -147,6 +176,7 @@ public class KotlinConfidenceTest extends LightCompletionTestCase {
@Override @Override
protected void complete() { protected void complete() {
if (skipComplete.get()) return;
new CodeCompletionHandlerBase(CompletionType.BASIC, false, true, true).invokeCompletion( new CodeCompletionHandlerBase(CompletionType.BASIC, false, true, true).invokeCompletion(
getProject(), getEditor(), 0, false, false); getProject(), getEditor(), 0, false, false);