Refactor, extract some 'goto' related testing utils

This commit is contained in:
Pavel V. Talanov
2017-06-08 14:49:05 +03:00
parent 4356795324
commit b12de14482
9 changed files with 161 additions and 162 deletions
@@ -34,8 +34,8 @@ import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescrip
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.ReferenceUtils
import org.jetbrains.kotlin.test.TagsTestDataUtil
import org.jetbrains.kotlin.test.util.renderAsGotoImplementation
import org.junit.Assert
import java.io.File
@@ -114,7 +114,7 @@ abstract class AbstractLineMarkersTest : KotlinLightCodeInsightFixtureTestCase()
val handler = navigateMarker.navigationHandler
if (handler is TestableLineMarkerNavigator) {
val navigateElements = handler.getTargetsPopupDescriptor(navigateMarker.element)?.targets?.sortedBy { ReferenceUtils.renderAsGotoImplementation(it) }
val navigateElements = handler.getTargetsPopupDescriptor(navigateMarker.element)?.targets?.sortedBy { it.renderAsGotoImplementation() }
val actualNavigationData = NavigationTestUtils.getNavigateElementsText(myFixture.project, navigateElements)
UsefulTestCase.assertSameLines(getExpectedNavigationText(navigationComment), actualNavigationData)
@@ -16,39 +16,23 @@
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.ide.util.gotoByName.FilteringGotoByModel;
import com.intellij.ide.util.gotoByName.GotoClassModel2;
import com.intellij.ide.util.gotoByName.GotoSymbolModel2;
import com.intellij.lang.Language;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.testFramework.UsefulTestCase;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase;
import org.jetbrains.kotlin.test.InTextDirectivesUtils;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.ReferenceUtils;
import org.junit.Assert;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.jetbrains.kotlin.idea.navigation.GotoCheck.checkGotoDirectives;
public abstract class AbstractKotlinGotoTest extends KotlinLightCodeInsightFixtureTestCase {
protected void doSymbolTest(String path) {
myFixture.configureByFile(path);
assertGotoSymbol(new GotoSymbolModel2(getProject()), myFixture.getEditor());
checkGotoDirectives(new GotoSymbolModel2(getProject()), myFixture.getEditor());
}
protected void doClassTest(String path) {
myFixture.configureByFile(path);
assertGotoSymbol(new GotoClassModel2(getProject()), myFixture.getEditor());
checkGotoDirectives(new GotoClassModel2(getProject()), myFixture.getEditor());
}
private String dirPath = null;
@@ -75,51 +59,4 @@ public abstract class AbstractKotlinGotoTest extends KotlinLightCodeInsightFixtu
protected String fileName() {
return getTestName(true) + ".kt";
}
private static void assertGotoSymbol(@NotNull FilteringGotoByModel<Language> model, @NotNull Editor editor) {
String documentText = editor.getDocument().getText();
List<String> searchTextList = InTextDirectivesUtils.findListWithPrefixes(documentText, "// SEARCH_TEXT:");
Assert.assertFalse("There's no search text in test data file given. Use '// SEARCH_TEXT:' directive",
searchTextList.isEmpty());
List<String> expectedReferences = CollectionsKt.map(
InTextDirectivesUtils.findLinesWithPrefixesRemoved(documentText, "// REF:"),
new Function1<String, String>() {
@Override
public String invoke(String input) {
return input.trim();
}
}
);
boolean enableCheckbox = InTextDirectivesUtils.isDirectiveDefined(documentText, "// CHECK_BOX");
String searchText = searchTextList.get(0);
List<Object> elementsByName = new ArrayList<Object>();
String[] names = model.getNames(enableCheckbox);
for (String name : names) {
if (name != null && name.startsWith(searchText)) {
elementsByName.addAll(Arrays.asList(model.getElementsByName(name, enableCheckbox, name + "*")));
}
}
List<String> renderedElements = Lists.transform(elementsByName, new Function<Object, String>() {
@Override
public String apply(@Nullable Object element) {
Assert.assertNotNull(element);
Assert.assertTrue(element instanceof PsiElement);
return ReferenceUtils.renderAsGotoImplementation((PsiElement) element);
}
});
boolean inexactMatching = InTextDirectivesUtils.isDirectiveDefined(documentText, "// ALLOW_MORE_RESULTS");
if (inexactMatching) {
UsefulTestCase.assertContainsElements(renderedElements, expectedReferences);
}
else {
UsefulTestCase.assertOrderedEquals(Ordering.natural().sortedCopy(renderedElements), expectedReferences);
}
}
}
@@ -0,0 +1,81 @@
/*
* Copyright 2010-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 org.jetbrains.kotlin.idea.navigation
import com.intellij.ide.util.gotoByName.FilteringGotoByModel
import com.intellij.lang.Language
import com.intellij.openapi.editor.Editor
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<Language>, 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())
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()
}
val inexactMatching = InTextDirectivesUtils.isDirectiveDefined(documentText, "// ALLOW_MORE_RESULTS")
val renderedSymbols = foundSymbols.map { (it as PsiElement).renderAsGotoImplementation() }
if (checkNavigation && (expectedReferences.size != 1 || inexactMatching)) {
error("Cannot check navigation targets when multiple references are expected")
}
if (inexactMatching) {
UsefulTestCase.assertContainsElements(renderedSymbols, expectedReferences)
}
else {
UsefulTestCase.assertOrderedEquals(renderedSymbols.sorted(), expectedReferences)
}
if (!checkNavigation) return
assertNavigationElementMatches(foundSymbols.single() as PsiElement, documentText)
}
@JvmStatic
fun assertNavigationElementMatches(resolved: PsiElement, textWithDirectives: String) {
val expectedBinaryFile = InTextDirectivesUtils.findStringWithPrefixes(textWithDirectives, "// BINARY:")
val expectedSourceFile = InTextDirectivesUtils.findStringWithPrefixes(textWithDirectives, "// SRC:")
assertEquals(expectedBinaryFile, getFileWithDir(resolved))
val srcElement = resolved.navigationElement
Assert.assertNotEquals(srcElement, resolved)
assertEquals(expectedSourceFile, getFileWithDir(srcElement))
}
// TODO: move somewhere
fun getFileWithDir(resolved: PsiElement): String {
val targetFile = resolved.containingFile
val targetDir = targetFile.parent
return targetDir!!.name + "/" + targetFile.name
}
}
@@ -36,7 +36,7 @@ import junit.framework.TestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.test.InTextDirectivesUtils;
import org.jetbrains.kotlin.test.ReferenceUtils;
import org.jetbrains.kotlin.test.util.ReferenceUtils;
import org.junit.Assert;
import java.util.*;
@@ -16,9 +16,7 @@
package org.jetbrains.kotlin.idea.references;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiPolyVariantReference;
import com.intellij.testFramework.LightProjectDescriptor;
import org.jetbrains.annotations.NotNull;
@@ -28,20 +26,18 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor;
import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies;
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde;
import org.jetbrains.kotlin.idea.navigation.GotoCheck;
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase;
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase;
import org.jetbrains.kotlin.idea.test.ProjectDescriptorWithStdlibSources;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.test.InTextDirectivesUtils;
import org.jetbrains.kotlin.test.ReferenceUtils;
import org.junit.Assert;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static org.jetbrains.kotlin.test.ReferenceUtils.getFileWithDir;
import static org.jetbrains.kotlin.test.ReferenceUtils.renderAsGotoImplementation;
import static org.jetbrains.kotlin.test.util.ReferenceUtils.renderAsGotoImplementation;
public class BuiltInsReferenceResolverTest extends KotlinLightCodeInsightFixtureTestCase {
public void testAny() throws Exception {
@@ -122,16 +118,11 @@ public class BuiltInsReferenceResolverTest extends KotlinLightCodeInsightFixture
assertEquals(1, reference.multiResolve(false).length);
String text = myFixture.getFile().getText();
String expectedBinaryFile = InTextDirectivesUtils.findStringWithPrefixes(text, "// BINARY:");
String expectedSourceFile = InTextDirectivesUtils.findStringWithPrefixes(text, "// SRC:");
String expectedTarget = InTextDirectivesUtils.findStringWithPrefixes(text, "// TARGET:");
assertEquals(expectedBinaryFile, getFileWithDir(resolved));
String expectedTarget = InTextDirectivesUtils.findStringWithPrefixes(text, "// TARGET:");
assertEquals(expectedTarget, renderAsGotoImplementation(resolved));
PsiElement srcElement = resolved.getNavigationElement();
Assert.assertNotEquals(srcElement, resolved);
assertEquals(expectedSourceFile, getFileWithDir(srcElement));
assertEquals(expectedTarget, renderAsGotoImplementation(srcElement));
GotoCheck.assertNavigationElementMatches(resolved, text);
}
@Override
@@ -28,7 +28,7 @@ import org.jetbrains.kotlin.idea.test.KotlinLightPlatformCodeInsightFixtureTestC
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.test.ReferenceUtils
import org.jetbrains.kotlin.test.util.renderAsGotoImplementation
import org.junit.Assert
import kotlin.test.assertTrue
@@ -82,7 +82,7 @@ abstract class AbstractReferenceResolveTest : KotlinLightPlatformCodeInsightFixt
val actualResolvedTo = Lists.newArrayList<String>()
for (result in results) {
actualResolvedTo.add(ReferenceUtils.renderAsGotoImplementation(result.element!!))
actualResolvedTo.add(result.element!!.renderAsGotoImplementation())
}
UsefulTestCase.assertOrderedEquals("Not matching for reference #$index", actualResolvedTo.sorted(), expectedReferences.sorted())
@@ -138,7 +138,7 @@ abstract class AbstractReferenceResolveTest : KotlinLightPlatformCodeInsightFixt
val resolvedTo = psiReference.resolve()
if (resolvedTo != null) {
checkResolvedTo(resolvedTo)
val resolvedToElementStr = replacePlaceholders(ReferenceUtils.renderAsGotoImplementation(resolvedTo))
val resolvedToElementStr = replacePlaceholders(resolvedTo.renderAsGotoImplementation())
assertEquals("Found reference to '$resolvedToElementStr', but '$expectedString' was expected", expectedString, resolvedToElementStr)
}
else {
@@ -25,12 +25,13 @@ import com.intellij.testFramework.PlatformTestUtil
import org.jetbrains.kotlin.checkers.AbstractPsiCheckerTest
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime
import org.jetbrains.kotlin.idea.core.script.KotlinScriptConfigurationManager
import org.jetbrains.kotlin.idea.navigation.GotoCheck
import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor
import org.jetbrains.kotlin.script.ScriptTemplatesProvider
import org.jetbrains.kotlin.test.InTextDirectivesUtils
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.MockLibraryUtil
import org.jetbrains.kotlin.test.ReferenceUtils
import org.jetbrains.kotlin.test.util.renderAsGotoImplementation
import org.jetbrains.kotlin.utils.PathUtil
import org.junit.Assert
import java.io.File
@@ -56,12 +57,12 @@ abstract class AbstractScriptConfigurationNavigationTest : AbstractScriptConfigu
val resolved = reference.resolve()!!.navigationElement!!
val expectedReference = InTextDirectivesUtils.findStringWithPrefixes(myFixture.file.text, "// REF:")
val actualReference = ReferenceUtils.renderAsGotoImplementation(resolved)
val actualReference = resolved.renderAsGotoImplementation()
Assert.assertEquals(expectedReference, actualReference)
val expectedFile = InTextDirectivesUtils.findStringWithPrefixes(myFixture.file.text, "// FILE:")
val actualFile = ReferenceUtils.getFileWithDir(resolved)
val actualFile = GotoCheck.getFileWithDir(resolved)
Assert.assertEquals(expectedFile, actualFile)
}
@@ -1,73 +0,0 @@
/*
* Copyright 2010-2015 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 org.jetbrains.kotlin.test;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.NavigationItem;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.psi.KtClass;
import org.jetbrains.kotlin.psi.KtObjectDeclaration;
import org.jetbrains.kotlin.psi.KtStringTemplateExpression;
import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt;
import org.junit.Assert;
public final class ReferenceUtils {
private ReferenceUtils() {
}
public static String renderAsGotoImplementation(@NotNull PsiElement element) {
PsiElement navigationElement = element.getNavigationElement();
if (navigationElement instanceof KtObjectDeclaration && ((KtObjectDeclaration) navigationElement).isCompanion()) {
//default presenter return null for companion object
KtClass containingClass = PsiTreeUtil.getParentOfType(navigationElement, KtClass.class);
assert containingClass != null;
return "companion object of " + renderAsGotoImplementation(containingClass);
}
if (navigationElement instanceof KtStringTemplateExpression) {
return KtPsiUtilKt.getPlainContent((KtStringTemplateExpression) navigationElement);
}
Assert.assertTrue(navigationElement instanceof NavigationItem);
ItemPresentation presentation = ((NavigationItem) navigationElement).getPresentation();
if (presentation == null) {
String elementText = element.getText();
return elementText != null ? elementText : navigationElement.getText();
}
String presentableText = presentation.getPresentableText();
String locationString = presentation.getLocationString();
if (locationString == null && element.getParent() instanceof PsiAnonymousClass) {
locationString = "<anonymous>";
}
return locationString == null || navigationElement instanceof PsiPackage
// for PsiPackage, presentableText is FQ name of current package
? presentableText
: locationString + "." + presentableText;
}
@NotNull
public static String getFileWithDir(@NotNull PsiElement resolved) {
PsiFile targetFile = resolved.getContainingFile();
PsiDirectory targetDir = targetFile.getParent();
return targetDir.getName() + "/" + targetFile.getName();
}
}
@@ -0,0 +1,62 @@
/*
* Copyright 2010-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.
*/
@file:JvmName("ReferenceUtils")
package org.jetbrains.kotlin.test.util
import com.intellij.navigation.NavigationItem
import com.intellij.psi.PsiAnonymousClass
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiPackage
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtObjectDeclaration
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.psiUtil.plainContent
import org.junit.Assert
fun PsiElement.renderAsGotoImplementation(): String {
val navigationElement = navigationElement
if (navigationElement is KtObjectDeclaration && navigationElement.isCompanion()) {
//default presenter return null for companion object
val containingClass = PsiTreeUtil.getParentOfType(navigationElement, KtClass::class.java)!!
return "companion object of " + containingClass.renderAsGotoImplementation()
}
if (navigationElement is KtStringTemplateExpression) {
return navigationElement.plainContent
}
Assert.assertTrue(navigationElement is NavigationItem)
val presentation = (navigationElement as NavigationItem).presentation
if (presentation == null) {
val elementText = text
return elementText ?: navigationElement.text
}
val presentableText = presentation.presentableText
var locationString = presentation.locationString
if (locationString == null && parent is PsiAnonymousClass) {
locationString = "<anonymous>"
}
return if (locationString == null || navigationElement is PsiPackage)
presentableText!!
else
locationString + "." + presentableText// for PsiPackage, presentableText is FQ name of current package
}