From bf22e0af6b33edae7b77e7544ab2dc1332fd7601 Mon Sep 17 00:00:00 2001 From: Yaroslav Pankratyev Date: Thu, 14 Jun 2018 15:23:58 +0700 Subject: [PATCH] UastCallMatcherTest minor refactoring --- .../codeInspection/UastCallMatcherTest.java | 62 ++++++++----------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/UastCallMatcherTest.java b/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/UastCallMatcherTest.java index 0b291e94f463..3ae4c56f4c73 100644 --- a/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/UastCallMatcherTest.java +++ b/jvm/jvm-analysis-java-tests/testSrc/com/intellij/codeInspection/UastCallMatcherTest.java @@ -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 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 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 expressions) { - return (int)expressions.stream().filter(e -> matcher.testCallExpression(e)).count(); + private static int matchCallExpression(UastCallMatcher matcher, Set expressions) { + return (int)expressions.stream().filter(matcher::testCallExpression).count(); } }