Minor: cleanup tests in org.jetbrains.kotlin.idea.navigation

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