KT-1051 Java interoperability completion - tests
This commit is contained in:
@@ -78,9 +78,11 @@ public class JetShortNamesCache extends PsiShortNamesCache {
|
||||
List<PsiClass> result = new ArrayList<PsiClass>();
|
||||
|
||||
for (String fqName : JetFullClassNameIndex.getInstance().getAllKeys(project)) {
|
||||
final PsiClass psiClass = javaElementFinder.findClass(fqName, scope);
|
||||
if (psiClass != null) {
|
||||
result.add(psiClass);
|
||||
if (fqnToShortName(fqName).equals(name)) {
|
||||
final PsiClass psiClass = javaElementFinder.findClass(fqName, scope);
|
||||
if (psiClass != null) {
|
||||
result.add(psiClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
public class Testing {
|
||||
public static void test() {
|
||||
ClassFr<caret>
|
||||
}
|
||||
}
|
||||
|
||||
// EXIST: ClassFromJet
|
||||
@@ -0,0 +1,4 @@
|
||||
package jettesting
|
||||
|
||||
class ClassFromJet {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
public class Testing {
|
||||
public static void test() {
|
||||
testing.<caret>
|
||||
}
|
||||
}
|
||||
|
||||
// EXIST: jet
|
||||
@@ -0,0 +1,4 @@
|
||||
package testing.jet
|
||||
|
||||
fun somefun() {
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package org.jetbrains.jet.completion;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Extract a number of statements about completion from the given text. Those statements
|
||||
* should be asserted during test execution.
|
||||
*
|
||||
* @author Nikolay Krasko
|
||||
*/
|
||||
public class ExpectedCompletionUtils {
|
||||
|
||||
public static final String EXIST_LINE_PREFIX = "// EXIST:";
|
||||
public static final String ABSENT_LINE_PREFIX = "// ABSENT:";
|
||||
public static final String NUMBER_LINE_PREFIX = "// NUMBER:";
|
||||
|
||||
private final String existLinePrefix;
|
||||
private final String absentLinePrefix;
|
||||
private final String numberLinePrefix;
|
||||
|
||||
public ExpectedCompletionUtils() {
|
||||
this(EXIST_LINE_PREFIX, ABSENT_LINE_PREFIX, NUMBER_LINE_PREFIX);
|
||||
}
|
||||
|
||||
public ExpectedCompletionUtils(String existLinePrefix, String absentLinePrefix, String numberLinePrefix) {
|
||||
|
||||
this.existLinePrefix = existLinePrefix;
|
||||
this.absentLinePrefix = absentLinePrefix;
|
||||
this.numberLinePrefix = numberLinePrefix;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String[] itemsShouldExist(String fileText) {
|
||||
return findListWithPrefix(existLinePrefix, fileText);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String[] itemsShouldAbsent(String fileText) {
|
||||
return findListWithPrefix(absentLinePrefix, fileText);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Integer getExpectedNumber(String fileText) {
|
||||
final String[] numberStrings = findListWithPrefix(numberLinePrefix, fileText);
|
||||
if (numberStrings.length > 0) {
|
||||
return Integer.parseInt(numberStrings[0]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String[] findListWithPrefix(String prefix, String fileText) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
|
||||
for (String line : findLinesWithPrefixRemoved(prefix, fileText)) {
|
||||
String[] completions = line.split(",");
|
||||
|
||||
for (String completion : completions) {
|
||||
result.add(completion.trim());
|
||||
}
|
||||
}
|
||||
|
||||
return result.toArray(new String[result.size()]);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<String> findLinesWithPrefixRemoved(String prefix, String fileText) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
|
||||
for (String line : fileNonEmptyLines(fileText)) {
|
||||
if (line.startsWith(prefix)) {
|
||||
result.add(line.substring(prefix.length()).trim());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<String> fileNonEmptyLines(String fileText) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new StringReader(fileText));
|
||||
try {
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (!line.isEmpty()) {
|
||||
result.add(line.trim());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
} catch(IOException e) {
|
||||
assert false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package org.jetbrains.jet.completion;
|
||||
|
||||
import com.intellij.codeInsight.completion.CompletionTestCase;
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.util.containers.HashSet;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Nikolay Krasko
|
||||
*/
|
||||
public abstract class JetCompletionMultiTestBase extends CompletionTestCase {
|
||||
|
||||
abstract String[] getFileNameList();
|
||||
|
||||
/**
|
||||
* @param completionLevel {@see CompletionParameters.getInvocationCount()} javadoc
|
||||
* @throws Exception
|
||||
*/
|
||||
protected void doFileTest(int completionLevel) throws Exception {
|
||||
configureByFiles(null, getFileNameList());
|
||||
complete(completionLevel);
|
||||
|
||||
final String fileText = getFile().getText();
|
||||
final ExpectedCompletionUtils completionUtils = new ExpectedCompletionUtils();
|
||||
|
||||
assertContainsItems(completionUtils.itemsShouldExist(fileText));
|
||||
assertNotContainItems(completionUtils.itemsShouldAbsent(fileText));
|
||||
|
||||
Integer itemsNumber = completionUtils.getExpectedNumber(fileText);
|
||||
if (itemsNumber != null) {
|
||||
assertEquals(itemsNumber.intValue(), myItems.length);
|
||||
}
|
||||
}
|
||||
|
||||
protected void doFileTest() throws Exception {
|
||||
doFileTest(1);
|
||||
}
|
||||
|
||||
// Copied from com.intellij.codeInsight.completion.LightCompletionTestCase
|
||||
protected void assertContainsItems(final String... expected) {
|
||||
final Set<String> actual = getLookupStrings();
|
||||
for (String s : expected) {
|
||||
assertTrue("Expected '" + s + "' not found in " + actual,
|
||||
actual.contains(s));
|
||||
}
|
||||
}
|
||||
|
||||
// Copied from com.intellij.codeInsight.completion.LightCompletionTestCase
|
||||
protected void assertNotContainItems(final String... unexpected) {
|
||||
final Set<String> actual = getLookupStrings();
|
||||
for (String s : unexpected) {
|
||||
assertFalse("Unexpected '" + s + "' presented in " + actual,
|
||||
actual.contains(s));
|
||||
}
|
||||
}
|
||||
|
||||
// Copied from com.intellij.codeInsight.completion.LightCompletionTestCase
|
||||
private Set<String> getLookupStrings() {
|
||||
final Set<String> actual = new HashSet<String>();
|
||||
if (myItems != null) {
|
||||
for (LookupElement lookupElement : myItems) {
|
||||
actual.add(lookupElement.getLookupString());
|
||||
}
|
||||
}
|
||||
return actual;
|
||||
}
|
||||
}
|
||||
@@ -8,15 +8,9 @@ import com.intellij.codeInsight.lookup.LookupManager;
|
||||
import com.intellij.codeInsight.lookup.impl.LookupImpl;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Nikolay.Krasko
|
||||
@@ -58,10 +52,13 @@ public abstract class JetCompletionTestBase extends LightCompletionTestCase {
|
||||
|
||||
configureByFile(testName + ".kt");
|
||||
|
||||
assertContainsItems(itemsShouldExist(getFile().getText()));
|
||||
assertNotContainItems(itemsShouldAbsent(getFile().getText()));
|
||||
final String fileText = getFile().getText();
|
||||
final ExpectedCompletionUtils completionUtils = new ExpectedCompletionUtils();
|
||||
|
||||
assertContainsItems(completionUtils.itemsShouldExist(fileText));
|
||||
assertNotContainItems(completionUtils.itemsShouldAbsent(fileText));
|
||||
|
||||
Integer itemsNumber = getExpectedNumber(getFile().getText());
|
||||
Integer itemsNumber = completionUtils.getExpectedNumber(fileText);
|
||||
if (itemsNumber != null) {
|
||||
assertEquals(itemsNumber.intValue(), myItems.length);
|
||||
}
|
||||
@@ -80,73 +77,4 @@ public abstract class JetCompletionTestBase extends LightCompletionTestCase {
|
||||
myItems = lookup == null ? null : lookup.getItems().toArray(LookupElement.EMPTY_ARRAY);
|
||||
myPrefix = lookup == null ? null : lookup.itemPattern(lookup.getItems().get(0));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String[] itemsShouldExist(String fileText) {
|
||||
return findListWithPrefix("// EXIST:", fileText);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String[] itemsShouldAbsent(String fileText) {
|
||||
return findListWithPrefix("// ABSENT:", fileText);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Integer getExpectedNumber(String fileText) {
|
||||
final String[] numberStrings = findListWithPrefix("// NUMBER:", fileText);
|
||||
if (numberStrings.length > 0) {
|
||||
return Integer.parseInt(numberStrings[0]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String[] findListWithPrefix(String prefix, String fileText) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
|
||||
for (String line : findLinesWithPrefixRemoved(prefix, fileText)) {
|
||||
String[] completions = line.split(",");
|
||||
|
||||
for (String completion : completions) {
|
||||
result.add(completion.trim());
|
||||
}
|
||||
}
|
||||
|
||||
return result.toArray(new String[result.size()]);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<String> findLinesWithPrefixRemoved(String prefix, String fileText) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
|
||||
for (String line : fileNonEmptyLines(fileText)) {
|
||||
if (line.startsWith(prefix)) {
|
||||
result.add(line.substring(prefix.length()).trim());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<String> fileNonEmptyLines(String fileText) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
|
||||
BufferedReader reader = new BufferedReader(new StringReader(fileText));
|
||||
|
||||
try {
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (!line.isEmpty()) {
|
||||
result.add(line.trim());
|
||||
}
|
||||
}
|
||||
} catch(IOException e) {
|
||||
assert false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.jetbrains.jet.completion;
|
||||
|
||||
import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||
|
||||
/**
|
||||
* @author Nikolay Krasko
|
||||
*/
|
||||
public class JetInJavaCompletionTest extends JetCompletionMultiTestBase {
|
||||
|
||||
public void testJetClassInJava() throws Exception {
|
||||
doFileTest();
|
||||
}
|
||||
|
||||
public void testJetSubpackage() throws Exception {
|
||||
doFileTest();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return PluginTestCaseBase.getTestDataPathBase() + "/completion/injava/";
|
||||
}
|
||||
|
||||
@Override
|
||||
String[] getFileNameList() {
|
||||
String fileName = getTestName(false);
|
||||
return new String[]{fileName + ".java", fileName + ".kt"};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user