added a test for completion

This commit is contained in:
Dmitry Batkovich
2017-03-09 17:02:55 +03:00
parent 7bb56d7bac
commit 6d4e63d001
11 changed files with 306 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
import java.util.*;
class Foo {
void m() {
List l0 = new ArrayList();
List l1 = new AbstractList() {
@Override
public int size() {
return 0;
}
@Override
public Object get(int index) {
return null;
}
};
List l2 = new AbstractList() {
@Override
public int size() {
return 0;
}
@Override
public Object get(int index) {
return null;
}
};
List l3 = new AbstractList() {
@Override
public int size() {
return 0;
}
@Override
public Object get(int index) {
return null;
}
};
<caret>
}
}

View File

@@ -0,0 +1,18 @@
import java.util.*;
class Foo {
void m() {
List l1 = new ArrayList();
List l2 = new ArrayList();
List l3 = new LinkedList();
List l4 = new LinkedList();
List l5 = new LinkedList();
<caret>
}
}

View File

@@ -0,0 +1,23 @@
class Foo {
void m() {
new C();
new C();
new B();
new B(0);
new B(0, 0);
<caret>
}
}
abstract class A {}
class B extends A {
B() {}
B(int i) {}
B(int i, int j) {}
}
class C extends A {
}

View File

@@ -0,0 +1,23 @@
class Foo {
void m() {
new C();
new C();
new B();
new B(0);
new B(0, 0);
<caret>
}
}
interface A {}
class B implements A {
B() {}
B(int i) {}
B(int i, int j) {}
}
class C implements A {
}

View File

@@ -0,0 +1,12 @@
class Bar {
void m(Foo foo) {
foo.asd();
foo.asd(null, null, null);
foo.asd(null, null, null);
<caret>
}
}

View File

@@ -0,0 +1,7 @@
public class Foo {
public void asd() {}
public void asd(String i, String j, String k) {}
}

View File

@@ -0,0 +1,21 @@
class Bar {
void m(Foo foo) {
foo.asd();
foo.asd();
foo.asd();
System.out.println(foo.qwe);
System.out.println(foo.qwe);
System.out.println(foo.qwe);
System.out.println(foo.qwe);
System.out.println(foo.qwe);
foo.zxc();
foo.zxc();
foo.zxc();
<caret>
}
}

View File

@@ -0,0 +1,9 @@
public class Foo {
public void asd() {}
public String qwe = "AEDI JilletnI";
public void zxc() {}
}

View File

@@ -0,0 +1,21 @@
class Bar {
void m(Foo foo) {
foo.asd();
foo.asd();
foo.asd();
foo.qwe();
foo.qwe();
foo.qwe();
foo.qwe();
foo.qwe();
foo.zxc();
foo.zxc();
foo.zxc();
<caret>
}
}

View File

@@ -0,0 +1,9 @@
public class Foo {
public void asd() {}
public void qwe() {}
public void zxc() {}
}

View File

@@ -0,0 +1,120 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.compiler;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.completion.CompletionType;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMember;
import com.intellij.psi.PsiMethod;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.function.Predicate;
public class CompilerReferenceDataInCompletionTest extends CompilerReferencesTestBase {
@Override
public void setUp() throws Exception {
super.setUp();
installCompiler();
}
@Override
protected String getTestDataPath() {
return JavaTestUtil.getJavaTestDataPath() + "/compiler/completionOrdering/";
}
public void testSimpleMethods() {
doTestMemberCompletionOrdering(new String[] {"Bar.java", "Foo.java"}, "qwe(0)", "asd(0)", "zxc(0)");
}
public void testSimpleMembers() {
doTestMemberCompletionOrdering(new String[] {"Bar.java", "Foo.java"}, "qwe", "asd(0)", "zxc(0)");
}
public void testOverloads() {
doTestMemberCompletionOrdering(new String[] {"Bar.java", "Foo.java"}, "asd(3)", "asd(0)");
}
public void testConstructor() {
doTestConstructorCompletionOrdering(new String[] {"Foo.java"}, "List l = new ", "LinkedList", "ArrayList");
}
public void testConstructorSumOccurrences() {
doTestConstructorCompletionOrdering(new String[] {"Foo.java"}, "A a = new ", "B", "C");
}
public void testConstructorSumOccurrences2() {
doTestConstructorCompletionOrdering(new String[] {"Foo.java"}, "A a = new ", "B", "C");
}
public void testAnonymous() {
doTestConstructorCompletionOrdering(new String[] {"Foo.java"}, "List l = new ", "AbstractList", "ArrayList");
}
private void doTestConstructorCompletionOrdering(@NotNull String[] files,
@NotNull String phraseToComplete,
String... expectedOrder) {
doTestCompletion(files, phraseToComplete, expectedOrder, m -> m instanceof PsiClass && ArrayUtil.contains(m.getName(), expectedOrder));
}
private void doTestMemberCompletionOrdering(@NotNull String[] files, String... expectedOrder) {
doTestCompletion(files, "foo.", expectedOrder, m -> "Foo".equals(m.getContainingClass().getName()));
}
private void doTestCompletion(@NotNull String[] files,
@NotNull String phraseToComplete,
@NotNull String[] expectedOrder,
@NotNull Predicate<PsiMember> resultFilter) {
myFixture.configureByFiles(ContainerUtil.map2Array(files, String.class, f -> getName() + "/" + f));
myFixture.type(phraseToComplete);
final int offset = myFixture.getCaretOffset();
final LookupElement[] completionVariantsBeforeCompilation = myFixture.complete(CompletionType.BASIC);
WriteCommandAction.runWriteCommandAction(myFixture.getProject(), () -> myFixture.getDocument(myFixture.getFile()).deleteString(offset - phraseToComplete.length(), offset));
rebuildProject();
myFixture.type(phraseToComplete);
final LookupElement[] completionVariantsAfterCompilation = myFixture.complete(CompletionType.BASIC);
assertFalse("Seems the test doesn't test anything: compiler indices doesn't affect on sorting",
Arrays.toString(completionVariantsBeforeCompilation).equals(Arrays.toString(completionVariantsAfterCompilation)));
final String[] orderedMethods = Arrays.stream(completionVariantsAfterCompilation)
.map(l -> l.getObject())
.filter(o -> o instanceof PsiMember)
.map(o -> ((PsiMember)o))
.filter(resultFilter)
.map(CompilerReferenceDataInCompletionTest::getPresentation)
.toArray(String[]::new);
assertOrderedEquals(orderedMethods, expectedOrder);
}
private static String getPresentation(PsiMember member) {
if (member instanceof PsiMethod) {
return member.getName() + "(" + ((PsiMethod)member).getParameterList().getParametersCount() + ")";
}
else if (member instanceof PsiField || member instanceof PsiClass) {
return member.getName();
}
fail("Unexpected member = " + member + " type = " + member.getClass());
return null;
}
}