KT-1151 Code completion for not imported extension functions - test for completion extension function without params

This commit is contained in:
Nikolay Krasko
2012-02-27 20:30:01 +04:00
parent e6b79debb2
commit 0a669b9c01
13 changed files with 143 additions and 22 deletions
@@ -131,6 +131,7 @@ public class JetCompletionContributor extends CompletionContributor {
}
}
// Iterate through the function with attempt to resolve found functions
for (String functionFQN : functionFQNs) {
// System.out.println(functionFQN);
@@ -0,0 +1,13 @@
package first
import java.util.ArrayList
fun firstFun() {
val a = ArrayList<Int>()
a.toLinke<caret>
}
// RUNTIME: 1
// TIME: 2
// EXIST: toLinkedList
// NUMBER: 1
@@ -0,0 +1,10 @@
package first
fun firstFun() {
val a = ""
a.hello<caret>
}
// EXIST: helloFun
// EXIST: helloFunPreventAutoInsert
// NUMBER: 2
@@ -0,0 +1,7 @@
package second
fun String.helloFun() {
}
fun String.helloFunPreventAutoInsert() {
}
@@ -0,0 +1,6 @@
package first
fun firstFun() {
val a = ""
a.hello<caret>
}
@@ -0,0 +1,4 @@
package second
fun String.helloFun() {
}
@@ -0,0 +1,8 @@
package first
import second.helloFun
fun firstFun() {
val a = ""
a.helloFun()
}
@@ -68,17 +68,17 @@ public class ExpectedCompletionUtils {
@Nullable
public Integer getExpectedNumber(String fileText) {
final String[] numberStrings = findListWithPrefix(numberLinePrefix, fileText);
if (numberStrings.length > 0) {
return Integer.parseInt(numberStrings[0]);
}
return null;
return getPrefixedInt(fileText, numberLinePrefix);
}
@Nullable
public Integer getExecutionTime(String fileText) {
final String[] numberStrings = findListWithPrefix(executionTimePrefix, fileText);
return getPrefixedInt(fileText, executionTimePrefix);
}
@Nullable
public static Integer getPrefixedInt(String fileText, String prefix) {
final String[] numberStrings = findListWithPrefix(prefix, fileText);
if (numberStrings.length > 0) {
return Integer.parseInt(numberStrings[0]);
}
@@ -45,6 +45,10 @@ public class JetBasicCompletionTest extends JetCompletionTestBase {
doTest();
}
public void testExtensionFromStandardLibrary() {
doTest();
}
public void testFromImports() {
doTest();
}
@@ -101,6 +105,8 @@ public class JetBasicCompletionTest extends JetCompletionTestBase {
doTest();
}
@Override
protected String getTestDataPath() {
return new File(PluginTestCaseBase.getTestDataPathBase(), "/completion/basic").getPath() +
@@ -22,7 +22,12 @@ import com.intellij.codeInsight.completion.LightCompletionTestCase;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import org.apache.commons.lang.SystemUtils;
import org.jetbrains.jet.plugin.JetWithJdkAndRuntimeLightProjectDescriptor;
import org.jetbrains.jet.plugin.PluginTestCaseBase;
/**
@@ -47,32 +52,71 @@ public abstract class JetCompletionTestBase extends LightCompletionTestCase {
final String fileText = getFile().getText();
Integer completionTime = completionUtils.getExecutionTime(fileText);
complete(completionTime == null ? 1 : completionTime);
boolean withKotlinRuntime = ExpectedCompletionUtils.getPrefixedInt(fileText, "// RUNTIME:") != null;
final String[] expected = completionUtils.itemsShouldExist(fileText);
final String[] unexpected = completionUtils.itemsShouldAbsent(fileText);
Integer itemsNumber = completionUtils.getExpectedNumber(fileText);
try {
if (withKotlinRuntime) {
configureWithKotlinRuntime();
}
assertTrue("Should be some assertions about completion", expected.length != 0 || unexpected.length != 0 || itemsNumber != null);
Integer completionTime = completionUtils.getExecutionTime(fileText);
assertContainsItems(expected);
assertNotContainItems(unexpected);
complete(completionTime == null ? 1 : completionTime);
if (itemsNumber != null) {
assertEquals(itemsNumber.intValue(), myItems.length);
final String[] expected = completionUtils.itemsShouldExist(fileText);
final String[] unexpected = completionUtils.itemsShouldAbsent(fileText);
Integer itemsNumber = completionUtils.getExpectedNumber(fileText);
assertTrue("Should be some assertions about completion", expected.length != 0 || unexpected.length != 0 || itemsNumber != null);
assertContainsItems(expected);
assertNotContainItems(unexpected);
if (itemsNumber != null) {
assertEquals(itemsNumber.intValue(), myItems.length);
}
}
finally {
if (withKotlinRuntime) {
unConfigureKotlinRuntime();
}
}
} catch (Exception e) {
throw new AssertionError(e);
}
}
protected static void configureWithKotlinRuntime() {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(getModule());
final ModifiableRootModel rootModel = rootManager.getModifiableModel();
rootModel.setSdk(getFullJavaJDK());
JetWithJdkAndRuntimeLightProjectDescriptor.INSTANCE.configureModule(getModule(), rootModel, null);
rootModel.commit();
}
protected void unConfigureKotlinRuntime() {
final ModuleRootManager rootManager = ModuleRootManager.getInstance(getModule());
final ModifiableRootModel rootModel = rootManager.getModifiableModel();
rootModel.setSdk(getProjectJDK());
JetWithJdkAndRuntimeLightProjectDescriptor.unConfigureModule(rootModel);
rootModel.commit();
}
@Override
protected Sdk getProjectJDK() {
return PluginTestCaseBase.jdkFromIdeaHome();
}
protected static Sdk getFullJavaJDK() {
return JavaSdk.getInstance().createJdk("JDK", SystemUtils.getJavaHome().getAbsolutePath());
}
@Override
protected void complete(final int time) {
new CodeCompletionHandlerBase(type, false, false, true).invokeCompletion(getProject(), getEditor(), time, false);
@@ -27,6 +27,10 @@ public class JetMultifileBasicCompletionTest extends JetCompletionMultiTestBase
doFileTest(2);
}
public void testNotImportedExtensionFunction() throws Exception {
doFileTest(2);
}
public void testExtensionFunction() throws Exception {
// TODO: fix and uncomment
// doFileTest();
@@ -26,6 +26,10 @@ import java.io.File;
*/
public class CompletionMultifileHandlerTest extends CompletionTestCase {
public void testExtensionFunctions() throws Exception {
doTest();
}
public void testTopLevelFunctionImport() throws Exception {
doTest();
}
@@ -21,14 +21,14 @@ import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.module.StdModuleTypes;
import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.LightProjectDescriptor;
import org.apache.commons.lang.SystemUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.codegen.ForTestCompileStdlib;
/**
@@ -52,11 +52,25 @@ public class JetWithJdkAndRuntimeLightProjectDescriptor implements LightProjectD
}
@Override
public void configureModule(Module module, ModifiableRootModel model, ContentEntry contentEntry) {
public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @Nullable ContentEntry contentEntry) {
Library.ModifiableModel modifiableModel = model.getModuleLibraryTable().createLibrary("ktl").getModifiableModel();
VirtualFile cd = JarFileSystem.getInstance().findFileByPath(ForTestCompileStdlib.stdlibJarForTests() + "!/");
assert cd != null;
modifiableModel.addRoot(cd, OrderRootType.CLASSES);
modifiableModel.commit();
}
public static void unConfigureModule(@NotNull ModifiableRootModel model) {
for (OrderEntry orderEntry : model.getOrderEntries()) {
if (orderEntry instanceof LibraryOrderEntry) {
LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry;
Library library = libraryOrderEntry.getLibrary();
if (library != null && library.getName().equals("ktl")) {
model.getModuleLibraryTable().removeLibrary(library);
break;
}
}
}
}
}