AbstractLineMarkersTest: J2K

This commit is contained in:
Dmitry Jemerov
2017-04-27 13:45:42 +02:00
parent 18a065865b
commit f9bca06e33
@@ -14,179 +14,161 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.idea.codeInsight; package org.jetbrains.kotlin.idea.codeInsight
import com.intellij.codeInsight.daemon.GutterIconNavigationHandler; import com.intellij.codeInsight.daemon.LineMarkerInfo
import com.intellij.codeInsight.daemon.LineMarkerInfo; import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl
import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl; import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.editor.Document; import com.intellij.psi.PsiDocumentManager
import com.intellij.openapi.project.Project; import com.intellij.rt.execution.junit.FileComparisonFailure
import com.intellij.openapi.util.Condition; import com.intellij.testFramework.ExpectedHighlightingData
import com.intellij.openapi.util.io.FileUtil; import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.psi.NavigatablePsiElement; import com.intellij.testFramework.PlatformTestUtil
import com.intellij.psi.PsiDocumentManager; import com.intellij.testFramework.UsefulTestCase
import com.intellij.psi.PsiElement; import junit.framework.TestCase
import com.intellij.rt.execution.junit.FileComparisonFailure; import org.jetbrains.kotlin.idea.highlighter.markers.SuperDeclarationMarkerNavigationHandler
import com.intellij.testFramework.ExpectedHighlightingData; import org.jetbrains.kotlin.idea.navigation.NavigationTestUtils
import com.intellij.testFramework.LightProjectDescriptor; import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil
import com.intellij.testFramework.PlatformTestUtil; import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import com.intellij.util.containers.ContainerUtil; import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil; import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase; import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor; import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase; import org.jetbrains.kotlin.test.ReferenceUtils
import org.jetbrains.kotlin.idea.highlighter.markers.SuperDeclarationMarkerNavigationHandler; import org.jetbrains.kotlin.test.TagsTestDataUtil
import org.jetbrains.kotlin.idea.navigation.NavigationTestUtils; import org.junit.Assert
import org.jetbrains.kotlin.psi.KtFile; import java.io.File
import org.jetbrains.kotlin.test.KotlinTestUtils; import java.util.*
import org.jetbrains.kotlin.test.ReferenceUtils;
import org.jetbrains.kotlin.test.TagsTestDataUtil;
import org.junit.Assert;
import java.io.File; abstract class AbstractLineMarkersTest : KotlinLightCodeInsightFixtureTestCase() {
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public abstract class AbstractLineMarkersTest extends KotlinLightCodeInsightFixtureTestCase { override fun getBasePath(): String {
return PluginTestCaseBase.TEST_DATA_PROJECT_RELATIVE + "/codeInsight/lineMarker"
private static final String LINE_MARKER_PREFIX = "LINEMARKER:";
private static final String TARGETS_PREFIX = "TARGETS";
@Override
protected String getBasePath() {
return PluginTestCaseBase.TEST_DATA_PROJECT_RELATIVE + "/codeInsight/lineMarker";
} }
@NotNull override fun getProjectDescriptor(): LightProjectDescriptor {
@Override return KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
protected LightProjectDescriptor getProjectDescriptor() {
return KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE;
} }
public void doTest(String path) { fun doTest(path: String) {
try { try {
String fileText = FileUtil.loadFile(new File(path)); val fileText = FileUtil.loadFile(File(path))
ConfigLibraryUtil.configureLibrariesByDirective(myFixture.getModule(), PlatformTestUtil.getCommunityPath(), fileText); ConfigLibraryUtil.configureLibrariesByDirective(myFixture.module, PlatformTestUtil.getCommunityPath(), fileText)
myFixture.configureByFile(path); myFixture.configureByFile(path)
Project project = myFixture.getProject(); val project = myFixture.project
Document document = myFixture.getEditor().getDocument(); val document = myFixture.editor.document
ExpectedHighlightingData data = new ExpectedHighlightingData( val data = ExpectedHighlightingData(
document, false, false, false, myFixture.getFile()); document, false, false, false, myFixture.file)
data.init(); data.init()
PsiDocumentManager.getInstance(project).commitAllDocuments(); PsiDocumentManager.getInstance(project).commitAllDocuments()
myFixture.doHighlighting(); myFixture.doHighlighting()
List<LineMarkerInfo> markers = DaemonCodeAnalyzerImpl.getLineMarkers(document, project); val markers = DaemonCodeAnalyzerImpl.getLineMarkers(document, project)
try { try {
data.checkLineMarkers(markers, document.getText()); data.checkLineMarkers(markers, document.text)
// This is a workaround for sad bug in ExpectedHighlightingData: // This is a workaround for sad bug in ExpectedHighlightingData:
// the latter doesn't throw assertion error when some line markers are expected, but none are present. // the latter doesn't throw assertion error when some line markers are expected, but none are present.
if (FileUtil.loadFile(new File(path)).contains("<lineMarker") && markers.isEmpty()) { if (FileUtil.loadFile(File(path)).contains("<lineMarker") && markers.isEmpty()) {
throw new AssertionError("Some line markers are expected, but nothing is present at all"); throw AssertionError("Some line markers are expected, but nothing is present at all")
} }
} }
catch (AssertionError error) { catch (error: AssertionError) {
try { try {
String actualTextWithTestData = TagsTestDataUtil.insertInfoTags(markers, true, myFixture.getFile().getText()); val actualTextWithTestData = TagsTestDataUtil.insertInfoTags(markers, true, myFixture.file.text)
KotlinTestUtils.assertEqualsToFile(new File(path), actualTextWithTestData); KotlinTestUtils.assertEqualsToFile(File(path), actualTextWithTestData)
} }
catch (FileComparisonFailure failure) { catch (failure: FileComparisonFailure) {
throw new FileComparisonFailure(error.getMessage() + "\n" + failure.getMessage(), throw FileComparisonFailure(error.message + "\n" + failure.message,
failure.getExpected(), failure.expected,
failure.getActual(), failure.actual,
failure.getFilePath()); failure.filePath)
} }
} }
assertNavigationElements(markers); assertNavigationElements(markers)
} }
catch (Exception exc) { catch (exc: Exception) {
throw new RuntimeException(exc); throw RuntimeException(exc)
} }
} }
private void assertNavigationElements(List<LineMarkerInfo> markers) { private fun assertNavigationElements(markers: List<LineMarkerInfo<*>>) {
List<String> navigationDataComments = KotlinTestUtils.getLastCommentsInFile( val navigationDataComments = KotlinTestUtils.getLastCommentsInFile(
(KtFile) myFixture.getFile(), KotlinTestUtils.CommentType.BLOCK_COMMENT, false); myFixture.file as KtFile, KotlinTestUtils.CommentType.BLOCK_COMMENT, false)
if (navigationDataComments.isEmpty()) return; if (navigationDataComments.isEmpty()) return
for (String navigationComment : navigationDataComments) { for (navigationComment in navigationDataComments) {
final String description = getLineMarkerDescription(navigationComment); val description = getLineMarkerDescription(navigationComment)
LineMarkerInfo navigateMarker = ContainerUtil.find(markers, new Condition<LineMarkerInfo>() { val navigateMarker = markers.find { it.lineMarkerTooltip?.startsWith(description) == true }!!
@Override
public boolean value(LineMarkerInfo marker) {
String tooltip = marker.getLineMarkerTooltip();
return tooltip != null && tooltip.startsWith(description);
}
});
assertNotNull( TestCase.assertNotNull(
String.format("Can't find marker for navigation check with description \"%s\"", description), String.format("Can't find marker for navigation check with description \"%s\"", description),
navigateMarker); navigateMarker)
GutterIconNavigationHandler handler = navigateMarker.getNavigationHandler(); val handler = navigateMarker.navigationHandler
if (handler instanceof SuperDeclarationMarkerNavigationHandler) { if (handler is SuperDeclarationMarkerNavigationHandler) {
PsiElement element = navigateMarker.getElement(); val element = navigateMarker.element as KtDeclaration
//noinspection unchecked handler.navigate(null, element)
handler.navigate(null, element); val navigateElements = handler.getNavigationElements()
List<NavigatablePsiElement> navigateElements =
((SuperDeclarationMarkerNavigationHandler) handler).getNavigationElements();
Collections.sort(navigateElements, new Comparator<NavigatablePsiElement>() { Collections.sort(navigateElements) { first, second ->
@Override val elementFirstStr = ReferenceUtils.renderAsGotoImplementation(first)
public int compare(@NotNull NavigatablePsiElement first, @NotNull NavigatablePsiElement second) { val elementSecondStr = ReferenceUtils.renderAsGotoImplementation(second)
String elementFirstStr = ReferenceUtils.renderAsGotoImplementation(first);
String elementSecondStr = ReferenceUtils.renderAsGotoImplementation(second);
return elementFirstStr.compareTo(elementSecondStr); elementFirstStr.compareTo(elementSecondStr)
} }
});
String actualNavigationData = val actualNavigationData = NavigationTestUtils.getNavigateElementsText(myFixture.project, navigateElements)
NavigationTestUtils.getNavigateElementsText(myFixture.getProject(), navigateElements);
assertSameLines(getExpectedNavigationText(navigationComment), actualNavigationData); UsefulTestCase.assertSameLines(getExpectedNavigationText(navigationComment), actualNavigationData)
} }
else { else {
Assert.fail("Only SuperDeclarationMarkerNavigationHandler are supported in navigate check"); Assert.fail("Only SuperDeclarationMarkerNavigationHandler are supported in navigate check")
} }
} }
} }
private static String getLineMarkerDescription(String navigationComment) { companion object {
int firstLineEnd = navigationComment.indexOf("\n");
assertTrue("The first line in block comment must contain description of marker for navigation check", firstLineEnd != -1);
String navigationMarkerText = navigationComment.substring(0, firstLineEnd); private val LINE_MARKER_PREFIX = "LINEMARKER:"
private val TARGETS_PREFIX = "TARGETS"
assertTrue(String.format("Add %s directive in first line of comment", LINE_MARKER_PREFIX), private fun getLineMarkerDescription(navigationComment: String): String {
navigationMarkerText.startsWith(LINE_MARKER_PREFIX)); val firstLineEnd = navigationComment.indexOf("\n")
TestCase.assertTrue("The first line in block comment must contain description of marker for navigation check", firstLineEnd != -1)
navigationMarkerText = navigationMarkerText.substring(LINE_MARKER_PREFIX.length()); var navigationMarkerText = navigationComment.substring(0, firstLineEnd)
return navigationMarkerText.trim(); TestCase.assertTrue(String.format("Add %s directive in first line of comment", LINE_MARKER_PREFIX),
} navigationMarkerText.startsWith(LINE_MARKER_PREFIX))
private static String getExpectedNavigationText(String navigationComment) { navigationMarkerText = navigationMarkerText.substring(LINE_MARKER_PREFIX.length)
int firstLineEnd = navigationComment.indexOf("\n");
String expectedNavigationText = navigationComment.substring(firstLineEnd + 1); return navigationMarkerText.trim { it <= ' ' }
}
assertTrue( private fun getExpectedNavigationText(navigationComment: String): String {
String.format("Marker %s is expected before navigation data", TARGETS_PREFIX), val firstLineEnd = navigationComment.indexOf("\n")
expectedNavigationText.startsWith(TARGETS_PREFIX));
expectedNavigationText = expectedNavigationText.substring(expectedNavigationText.indexOf("\n") + 1); var expectedNavigationText = navigationComment.substring(firstLineEnd + 1)
return expectedNavigationText; TestCase.assertTrue(
String.format("Marker %s is expected before navigation data", TARGETS_PREFIX),
expectedNavigationText.startsWith(TARGETS_PREFIX))
expectedNavigationText = expectedNavigationText.substring(expectedNavigationText.indexOf("\n") + 1)
return expectedNavigationText
}
} }
} }