UastCallMatcherTest minor refactoring

This commit is contained in:
Yaroslav Pankratyev
2018-06-14 19:05:06 +07:00
parent 388dda7ec5
commit bf22e0af6b
@@ -1,21 +1,22 @@
package com.intellij.codeInspection;
import com.intellij.jvm.analysis.JvmAnalysisTestsUtil;
import com.intellij.psi.PsiElement;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.PsiElementProcessor;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.TestDataPath;
import com.intellij.testFramework.builders.JavaModuleFixtureBuilder;
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
import com.intellij.util.PathUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.uast.UCallExpression;
import org.jetbrains.uast.UastContextKt;
import java.util.*;
import java.util.Locale;
import java.util.Set;
import static com.intellij.codeInspection.JvmAnalysisTestsUastUtil.getUElementsOfTypeFromFile;
import static com.intellij.codeInspection.UastCallMatcher.builder;
@TestDataPath("$CONTENT_ROOT/testData/codeInspection/uastCallMatcher")
public class UastCallMatcherTest extends JavaCodeInsightFixtureTestCase {
@Override
protected String getBasePath() {
@@ -29,91 +30,80 @@ public class UastCallMatcherTest extends JavaCodeInsightFixtureTestCase {
public void testSimpleMatcher() {
PsiFile file = myFixture.configureByFile("MyClass.java");
Set<UCallExpression> expressions = new HashSet<>();
PsiTreeUtil.processElements(file, new PsiElementProcessor() {
@Override
public boolean execute(@NotNull PsiElement element) {
UCallExpression callExpression = UastContextKt.toUElement(element, UCallExpression.class);
if (callExpression != null && callExpression.getMethodName() != null) { // skip constructor calls
expressions.add(callExpression);
}
return true;
}
});
Set<UCallExpression> expressions = getUElementsOfTypeFromFile(file, UCallExpression.class, ce -> ce.getMethodName() != null);
assertSize(5, expressions);
assertEquals(0, match(
assertEquals(0, matchCallExpression(
builder().withReceiverType("java.util.ArrayList").build(),
expressions)
);
assertEquals(0, match(
assertEquals(0, matchCallExpression(
builder().withReceiverType("java.util.ArrayList").withMethodName("size").build(),
expressions)
);
assertEquals(0, match(
assertEquals(0, matchCallExpression(
builder().withMethodName("size").build(),
expressions)
);
assertEquals(0, match(
assertEquals(0, matchCallExpression(
builder().withReceiverType("java.util.ArrayList").withMethodName("addAll").withArgumentsCount(1).build(),
expressions)
);
assertEquals(0, match(
assertEquals(0, matchCallExpression(
builder().withReceiverType("java.util.ArrayList").withMethodName("addAll").withArgumentTypes("java.util.Collection").build(),
expressions)
);
assertEquals(4, match(
assertEquals(4, matchCallExpression(
builder().withReceiverType("java.lang.String").build(),
expressions
));
assertEquals(2, match(
assertEquals(2, matchCallExpression(
builder().withMethodName("toUpperCase").build(),
expressions
));
assertEquals(2, match(
assertEquals(2, matchCallExpression(
builder().withReceiverType("java.lang.String").withMethodName("toUpperCase").build(),
expressions
));
assertEquals(3, match(
assertEquals(3, matchCallExpression(
builder().withReturnType("java.lang.String").build(),
expressions
));
assertEquals(2, match(
assertEquals(2, matchCallExpression(
builder().withReturnType("java.lang.String").withMethodName("toUpperCase").build(),
expressions
));
assertEquals(1, match(
assertEquals(1, matchCallExpression(
builder().withReturnType("java.lang.String").withMethodName("toUpperCase").withArgumentsCount(1).build(),
expressions
));
assertEquals(1, match(
assertEquals(1, matchCallExpression(
builder().withReturnType("java.lang.String").withMethodName("toUpperCase").withArgumentTypes("java.util.Locale").build(),
expressions
));
assertEquals(2, match(
assertEquals(2, matchCallExpression(
builder().withArgumentsCount(0).build(),
expressions
));
assertEquals(3, match(
assertEquals(3, matchCallExpression(
builder().withArgumentsCount(1).build(),
expressions
));
assertEquals(1, match(
assertEquals(1, matchCallExpression(
builder().withArgumentTypes("java.util.Locale").build(),
expressions
));
assertEquals(1, match(
assertEquals(1, matchCallExpression(
builder().withArgumentTypes("java.util.Collection").withMatchArgumentTypeInheritors(true).build(),
expressions
));
}
private static int match(UastCallMatcher matcher, Set<UCallExpression> expressions) {
return (int)expressions.stream().filter(e -> matcher.testCallExpression(e)).count();
private static int matchCallExpression(UastCallMatcher matcher, Set<UCallExpression> expressions) {
return (int)expressions.stream().filter(matcher::testCallExpression).count();
}
}