From b4571fd548b482553a9745b70f0fbdbddae3f7e6 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 19 Feb 2019 12:00:20 +0300 Subject: [PATCH] Minor: cleanup tests in org.jetbrains.kotlin.idea.navigation --- .../idea/navigation/AbstractGotoActionTest.kt | 3 +- .../AbstractKotlinGotoImplementationTest.kt | 5 +- .../navigation/AbstractKotlinGotoTest.java | 1 + .../kotlin/idea/navigation/GotoCheck.kt | 25 ++++++---- .../GotoWithMultipleLibrariesTest.kt | 22 ++++----- ...KotlinGotoImplementationMultifileTest.java | 33 ++++++------- .../idea/navigation/NavigationTestUtils.java | 48 +++++++------------ 7 files changed, 67 insertions(+), 70 deletions(-) diff --git a/idea/tests/org/jetbrains/kotlin/idea/navigation/AbstractGotoActionTest.kt b/idea/tests/org/jetbrains/kotlin/idea/navigation/AbstractGotoActionTest.kt index 7f1e903cb3b..1b5707cf37a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/navigation/AbstractGotoActionTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/navigation/AbstractGotoActionTest.kt @@ -34,8 +34,7 @@ abstract class AbstractGotoActionTest : KotlinLightCodeInsightFixtureTestCase() val afterText = StringBuilder(text).insert(editor.caretModel.offset, "").toString() Assert.assertEquals(parts[1], afterText) - } - else { + } else { val fileOffset = currentEditor.caretModel.offset val lineNumber = currentEditor.document.getLineNumber(fileOffset) val lineStart = currentEditor.document.getLineStartOffset(lineNumber) diff --git a/idea/tests/org/jetbrains/kotlin/idea/navigation/AbstractKotlinGotoImplementationTest.kt b/idea/tests/org/jetbrains/kotlin/idea/navigation/AbstractKotlinGotoImplementationTest.kt index f5b63f788e0..d80975d5f78 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/navigation/AbstractKotlinGotoImplementationTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/navigation/AbstractKotlinGotoImplementationTest.kt @@ -26,7 +26,10 @@ abstract class AbstractKotlinGotoImplementationTest : LightCodeInsightTestCase() protected fun doTest(path: String) { configureByFile(path) - val gotoData = NavigationTestUtils.invokeGotoImplementations(LightPlatformCodeInsightTestCase.getEditor(), LightPlatformCodeInsightTestCase.getFile()) + val gotoData = NavigationTestUtils.invokeGotoImplementations( + LightPlatformCodeInsightTestCase.getEditor(), + LightPlatformCodeInsightTestCase.getFile() + ) NavigationTestUtils.assertGotoDataMatching(LightPlatformCodeInsightTestCase.getEditor(), gotoData) } } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/navigation/AbstractKotlinGotoTest.java b/idea/tests/org/jetbrains/kotlin/idea/navigation/AbstractKotlinGotoTest.java index bc0bcc0cc3a..8a6adacfe62 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/navigation/AbstractKotlinGotoTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/navigation/AbstractKotlinGotoTest.java @@ -38,6 +38,7 @@ public abstract class AbstractKotlinGotoTest extends KotlinLightCodeInsightFixtu dirPath = null; } + @NotNull @Override protected String getTestDataPath() { return dirPath; diff --git a/idea/tests/org/jetbrains/kotlin/idea/navigation/GotoCheck.kt b/idea/tests/org/jetbrains/kotlin/idea/navigation/GotoCheck.kt index cc5bd2f786d..8b9f9b99d2e 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/navigation/GotoCheck.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/navigation/GotoCheck.kt @@ -12,25 +12,33 @@ import com.intellij.psi.PsiElement import com.intellij.testFramework.UsefulTestCase import org.jetbrains.kotlin.test.InTextDirectivesUtils import org.jetbrains.kotlin.test.util.renderAsGotoImplementation -import org.jetbrains.kotlin.utils.sure import org.junit.Assert import kotlin.test.assertEquals object GotoCheck { - @JvmStatic @JvmOverloads - fun checkGotoDirectives(model: FilteringGotoByModel, editor: Editor, nonProjectSymbols: Boolean = false, checkNavigation: Boolean = false) { + @JvmStatic + @JvmOverloads + fun checkGotoDirectives( + model: FilteringGotoByModel, + editor: Editor, + nonProjectSymbols: Boolean = false, + checkNavigation: Boolean = false + ) { val documentText = editor.document.text val searchTextList = InTextDirectivesUtils.findListWithPrefixes(documentText, "// SEARCH_TEXT:") - Assert.assertFalse("There's no search text in test data file given. Use '// SEARCH_TEXT:' directive", - searchTextList.isEmpty()) + Assert.assertFalse( + "There's no search text in test data file given. Use '// SEARCH_TEXT:' directive", + searchTextList.isEmpty() + ) - val expectedReferences = InTextDirectivesUtils.findLinesWithPrefixesRemoved(documentText, "// REF:").map { input -> input.trim { it <= ' ' } } + val expectedReferences = + InTextDirectivesUtils.findLinesWithPrefixesRemoved(documentText, "// REF:").map { input -> input.trim { it <= ' ' } } val includeNonProjectSymbols = nonProjectSymbols || InTextDirectivesUtils.isDirectiveDefined(documentText, "// CHECK_BOX") val searchText = searchTextList.first() val foundSymbols = model.getNames(includeNonProjectSymbols).filter { it?.startsWith(searchText) ?: false }.flatMap { - model.getElementsByName(it, includeNonProjectSymbols, it + "*").toList() + model.getElementsByName(it, includeNonProjectSymbols, "$it*").toList() } val inexactMatching = InTextDirectivesUtils.isDirectiveDefined(documentText, "// ALLOW_MORE_RESULTS") @@ -42,8 +50,7 @@ object GotoCheck { if (inexactMatching) { UsefulTestCase.assertContainsElements(renderedSymbols, expectedReferences) - } - else { + } else { UsefulTestCase.assertOrderedEquals(renderedSymbols.sorted(), expectedReferences) } if (!checkNavigation) return diff --git a/idea/tests/org/jetbrains/kotlin/idea/navigation/GotoWithMultipleLibrariesTest.kt b/idea/tests/org/jetbrains/kotlin/idea/navigation/GotoWithMultipleLibrariesTest.kt index 9a4e6fc3ab2..46a59ab58a1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/navigation/GotoWithMultipleLibrariesTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/navigation/GotoWithMultipleLibrariesTest.kt @@ -18,34 +18,34 @@ import org.jetbrains.kotlin.test.util.projectLibrary class GotoWithMultipleLibrariesTest : AbstractMultiModuleTest() { override fun getTestDataPath() = "${PluginTestCaseBase.getTestDataPathBase()}/multiModuleReferenceResolve/sameJarInDifferentLibraries/" - fun testOneHasSourceAndOneDoesnot() { + fun testOneHasSourceAndOneDoesNot() { doTestSameJarSharedByLibrariesWithAndWithoutSourceAttached( - withSource = 1, - noSource = 1 + withSource = 1, + noSource = 1 ) } - fun testOneHasSourceAndManyDont() { + fun testOneHasSourceAndManyDoNot() { doTestSameJarSharedByLibrariesWithAndWithoutSourceAttached( - withSource = 1, - noSource = 3 + withSource = 1, + noSource = 3 ) } fun testSeveralWithSource() { doTestSameJarSharedByLibrariesWithAndWithoutSourceAttached( - withSource = 2, - noSource = 2 + withSource = 2, + noSource = 2 ) } - fun doTestSameJarSharedByLibrariesWithAndWithoutSourceAttached(withSource: Int, noSource: Int) { + private fun doTestSameJarSharedByLibrariesWithAndWithoutSourceAttached(withSource: Int, noSource: Int) { val srcPath = testDataPath + "src" val sharedJar = MockLibraryUtil.compileJvmLibraryToJar(testDataPath + "libSrc", "sharedJar", addSources = true) val jarRoot = sharedJar.jarRoot - var i: Int = 0 + var i = 0 repeat(noSource) { module("m${++i}", srcPath).addDependency(projectLibrary("libA", jarRoot)) } @@ -58,5 +58,5 @@ class GotoWithMultipleLibrariesTest : AbstractMultiModuleTest() { } } - protected fun module(name: String, srcPath: String) = createModuleFromTestData(srcPath, name, StdModuleTypes.JAVA, true)!! + private fun module(name: String, srcPath: String) = createModuleFromTestData(srcPath, name, StdModuleTypes.JAVA, true)!! } diff --git a/idea/tests/org/jetbrains/kotlin/idea/navigation/KotlinGotoImplementationMultifileTest.java b/idea/tests/org/jetbrains/kotlin/idea/navigation/KotlinGotoImplementationMultifileTest.java index 975b0426aac..d818f55d62d 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/navigation/KotlinGotoImplementationMultifileTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/navigation/KotlinGotoImplementationMultifileTest.java @@ -15,56 +15,56 @@ import org.jetbrains.kotlin.idea.test.PluginTestCaseBase; import java.io.File; public class KotlinGotoImplementationMultifileTest extends KotlinLightCodeInsightFixtureTestCase { - public void testImplementFunInJava() throws Exception { + public void testImplementFunInJava() { doKotlinJavaTest(); } - public void testImplementKotlinClassInJava() throws Exception { + public void testImplementKotlinClassInJava() { doKotlinJavaTest(); } - public void testImplementKotlinAbstractClassInJava() throws Exception { + public void testImplementKotlinAbstractClassInJava() { doKotlinJavaTest(); } - public void testImplementKotlinTraitInJava() throws Exception { + public void testImplementKotlinTraitInJava() { doKotlinJavaTest(); } - public void testImplementJavaClassInKotlin() throws Exception { + public void testImplementJavaClassInKotlin() { doKotlinJavaTest(); } - public void testImplementJavaAbstractClassInKotlin() throws Exception { + public void testImplementJavaAbstractClassInKotlin() { doKotlinJavaTest(); } - public void testImplementJavaInterfaceInKotlin() throws Exception { + public void testImplementJavaInterfaceInKotlin() { doKotlinJavaTest(); } - public void testImplementMethodInKotlin() throws Exception { + public void testImplementMethodInKotlin() { doKotlinJavaTest(); } - public void testImplementVarInJava() throws Exception { + public void testImplementVarInJava() { doKotlinJavaTest(); } - public void testImplementJavaInnerInterface() throws Exception { + public void testImplementJavaInnerInterface() { doJavaKotlinTest(); } - public void testImplementJavaInnerInterfaceFromUsage() throws Exception { + public void testImplementJavaInnerInterfaceFromUsage() { doJavaKotlinTest(); } - private void doKotlinJavaTest() throws Exception { - doMultifileTest(getTestName(false) + ".kt", getTestName(false) + ".java"); + private void doKotlinJavaTest() { + doMultiFileTest(getTestName(false) + ".kt", getTestName(false) + ".java"); } - private void doJavaKotlinTest() throws Exception { - doMultifileTest(getTestName(false) + ".java", getTestName(false) + ".kt"); + private void doJavaKotlinTest() { + doMultiFileTest(getTestName(false) + ".java", getTestName(false) + ".kt"); } @NotNull @@ -73,12 +73,13 @@ public class KotlinGotoImplementationMultifileTest extends KotlinLightCodeInsigh return KotlinLightProjectDescriptor.INSTANCE; } - private void doMultifileTest(String ... fileNames) throws Exception { + private void doMultiFileTest(String... fileNames) { myFixture.configureByFiles(fileNames); GotoTargetHandler.GotoData gotoData = NavigationTestUtils.invokeGotoImplementations(getEditor(), getFile()); NavigationTestUtils.assertGotoDataMatching(getEditor(), gotoData); } + @NotNull @Override protected String getTestDataPath() { return new File(PluginTestCaseBase.getTestDataPathBase(), diff --git a/idea/tests/org/jetbrains/kotlin/idea/navigation/NavigationTestUtils.java b/idea/tests/org/jetbrains/kotlin/idea/navigation/NavigationTestUtils.java index c454c4e05c2..64b63e1b09c 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/navigation/NavigationTestUtils.java +++ b/idea/tests/org/jetbrains/kotlin/idea/navigation/NavigationTestUtils.java @@ -5,8 +5,6 @@ package org.jetbrains.kotlin.idea.navigation; -import com.google.common.base.Function; -import com.google.common.collect.Lists; import com.google.common.collect.Ordering; import com.intellij.codeInsight.navigation.GotoTargetHandler; import com.intellij.openapi.editor.Document; @@ -17,20 +15,20 @@ import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiFileSystemItem; import com.intellij.testFramework.UsefulTestCase; import com.intellij.testFramework.fixtures.CodeInsightTestUtil; import com.intellij.util.PathUtil; import com.intellij.util.containers.MultiMap; import junit.framework.TestCase; import kotlin.collections.ArraysKt; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.asJava.LightClassUtilsKt; import org.jetbrains.kotlin.test.InTextDirectivesUtils; import org.jetbrains.kotlin.test.util.ReferenceUtils; import org.junit.Assert; import java.util.*; +import java.util.stream.Collectors; public final class NavigationTestUtils { private NavigationTestUtils() { @@ -62,18 +60,16 @@ public final class NavigationTestUtils { if (gotoData != null) { List targets; if (InTextDirectivesUtils.isDirectiveDefined(documentText, "// DISTINCT_REF")) { - targets = ArraysKt.distinctBy(gotoData.targets, element -> LightClassUtilsKt.getUnwrapped(element)); - } else { + targets = ArraysKt.distinctBy(gotoData.targets, LightClassUtilsKt::getUnwrapped); + } + else { targets = Arrays.asList(gotoData.targets); } // Transform given reference result to strings - List psiElements = Lists.transform(targets, new Function() { - @Override - public String apply(@Nullable PsiElement element) { - Assert.assertNotNull(element); - return ReferenceUtils.renderAsGotoImplementation(element, renderModule); - } - }); + List psiElements = targets.stream().map(element -> { + Assert.assertNotNull(element); + return ReferenceUtils.renderAsGotoImplementation(element, renderModule); + }).collect(Collectors.toList()); // Compare UsefulTestCase.assertOrderedEquals(Ordering.natural().sortedCopy(psiElements), expectedReferences); @@ -84,10 +80,10 @@ public final class NavigationTestUtils { } public static String getNavigateElementsText(Project project, Collection navigableElements) { - MultiMap> filesToNumbersAndOffsets = new MultiMap>(); + MultiMap> filesToNumbersAndOffsets = new MultiMap<>(); int refNumber = 1; for (PsiElement navigationElement : navigableElements) { - Pair numberAndOffset = new Pair(refNumber++, navigationElement.getTextOffset()); + Pair numberAndOffset = new Pair<>(refNumber++, navigationElement.getTextOffset()); filesToNumbersAndOffsets.putValue(navigationElement.getContainingFile(), numberAndOffset); } @@ -95,25 +91,15 @@ public final class NavigationTestUtils { return ""; } - List files = new ArrayList(filesToNumbersAndOffsets.keySet()); - Collections.sort(files, new Comparator() { - @Override - public int compare(@NotNull PsiFile f1, @NotNull PsiFile f2) { - return f1.getName().compareTo(f2.getName()); - } - }); + List files = new ArrayList<>(filesToNumbersAndOffsets.keySet()); + files.sort(Comparator.comparing(PsiFileSystemItem::getName)); StringBuilder result = new StringBuilder(); for (PsiFile file : files) { - List> numbersAndOffsets = new ArrayList>(filesToNumbersAndOffsets.get(file)); + List> numbersAndOffsets = new ArrayList<>(filesToNumbersAndOffsets.get(file)); - Collections.sort(numbersAndOffsets, Collections.reverseOrder(new Comparator>() { - @Override - public int compare(Pair t1, Pair t2) { - int offsets = t1.second.compareTo(t2.second); - return offsets == 0 ? t1.first.compareTo(t2.first) : offsets; - } - })); + numbersAndOffsets.sort(Collections.reverseOrder( + Comparator.comparingInt((Pair t) -> t.second).thenComparingInt(t -> t.first))); Document document = PsiDocumentManager.getInstance(project).getDocument(file); TestCase.assertNotNull(document); @@ -132,7 +118,7 @@ public final class NavigationTestUtils { Document annotated = EditorFactory.getInstance().createDocument(resultForFile); String filePart = annotated.getText().substring(annotated.getLineStartOffset(minLine), - annotated.getLineEndOffset(maxLine)); + annotated.getLineEndOffset(maxLine)); result.append(" ").append(file.getName()).append("\n"); result.append(filePart).append("\n"); }