From f9bca06e3378900441a0dd6d66122ffb9d0b6a5f Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 27 Apr 2017 13:45:42 +0200 Subject: [PATCH] AbstractLineMarkersTest: J2K --- .../codeInsight/AbstractLineMarkersTest.kt | 224 ++++++++---------- 1 file changed, 103 insertions(+), 121 deletions(-) diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractLineMarkersTest.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractLineMarkersTest.kt index 07f36ac175e..0593cc930d0 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractLineMarkersTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractLineMarkersTest.kt @@ -14,179 +14,161 @@ * 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.impl.DaemonCodeAnalyzerImpl; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Condition; -import com.intellij.openapi.util.io.FileUtil; -import com.intellij.psi.NavigatablePsiElement; -import com.intellij.psi.PsiDocumentManager; -import com.intellij.psi.PsiElement; -import com.intellij.rt.execution.junit.FileComparisonFailure; -import com.intellij.testFramework.ExpectedHighlightingData; -import com.intellij.testFramework.LightProjectDescriptor; -import com.intellij.testFramework.PlatformTestUtil; -import com.intellij.util.containers.ContainerUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil; -import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase; -import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor; -import org.jetbrains.kotlin.idea.test.PluginTestCaseBase; -import org.jetbrains.kotlin.idea.highlighter.markers.SuperDeclarationMarkerNavigationHandler; -import org.jetbrains.kotlin.idea.navigation.NavigationTestUtils; -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.junit.Assert; +import com.intellij.codeInsight.daemon.LineMarkerInfo +import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl +import com.intellij.openapi.util.io.FileUtil +import com.intellij.psi.PsiDocumentManager +import com.intellij.rt.execution.junit.FileComparisonFailure +import com.intellij.testFramework.ExpectedHighlightingData +import com.intellij.testFramework.LightProjectDescriptor +import com.intellij.testFramework.PlatformTestUtil +import com.intellij.testFramework.UsefulTestCase +import junit.framework.TestCase +import org.jetbrains.kotlin.idea.highlighter.markers.SuperDeclarationMarkerNavigationHandler +import org.jetbrains.kotlin.idea.navigation.NavigationTestUtils +import org.jetbrains.kotlin.idea.test.ConfigLibraryUtil +import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase +import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor +import org.jetbrains.kotlin.idea.test.PluginTestCaseBase +import org.jetbrains.kotlin.psi.KtDeclaration +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.junit.Assert +import java.io.File +import java.util.* -import java.io.File; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; +abstract class AbstractLineMarkersTest : KotlinLightCodeInsightFixtureTestCase() { -public abstract class AbstractLineMarkersTest extends KotlinLightCodeInsightFixtureTestCase { - - 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"; + override fun getBasePath(): String { + return PluginTestCaseBase.TEST_DATA_PROJECT_RELATIVE + "/codeInsight/lineMarker" } - @NotNull - @Override - protected LightProjectDescriptor getProjectDescriptor() { - return KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE; + override fun getProjectDescriptor(): LightProjectDescriptor { + return KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE } - public void doTest(String path) { + fun doTest(path: String) { try { - String fileText = FileUtil.loadFile(new File(path)); - ConfigLibraryUtil.configureLibrariesByDirective(myFixture.getModule(), PlatformTestUtil.getCommunityPath(), fileText); + val fileText = FileUtil.loadFile(File(path)) + ConfigLibraryUtil.configureLibrariesByDirective(myFixture.module, PlatformTestUtil.getCommunityPath(), fileText) - myFixture.configureByFile(path); - Project project = myFixture.getProject(); - Document document = myFixture.getEditor().getDocument(); + myFixture.configureByFile(path) + val project = myFixture.project + val document = myFixture.editor.document - ExpectedHighlightingData data = new ExpectedHighlightingData( - document, false, false, false, myFixture.getFile()); - data.init(); + val data = ExpectedHighlightingData( + document, false, false, false, myFixture.file) + data.init() - PsiDocumentManager.getInstance(project).commitAllDocuments(); + PsiDocumentManager.getInstance(project).commitAllDocuments() - myFixture.doHighlighting(); + myFixture.doHighlighting() - List markers = DaemonCodeAnalyzerImpl.getLineMarkers(document, project); + val markers = DaemonCodeAnalyzerImpl.getLineMarkers(document, project) try { - data.checkLineMarkers(markers, document.getText()); + data.checkLineMarkers(markers, document.text) // 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. - if (FileUtil.loadFile(new File(path)).contains(" markers) { - List navigationDataComments = KotlinTestUtils.getLastCommentsInFile( - (KtFile) myFixture.getFile(), KotlinTestUtils.CommentType.BLOCK_COMMENT, false); - if (navigationDataComments.isEmpty()) return; + private fun assertNavigationElements(markers: List>) { + val navigationDataComments = KotlinTestUtils.getLastCommentsInFile( + myFixture.file as KtFile, KotlinTestUtils.CommentType.BLOCK_COMMENT, false) + if (navigationDataComments.isEmpty()) return - for (String navigationComment : navigationDataComments) { - final String description = getLineMarkerDescription(navigationComment); - LineMarkerInfo navigateMarker = ContainerUtil.find(markers, new Condition() { - @Override - public boolean value(LineMarkerInfo marker) { - String tooltip = marker.getLineMarkerTooltip(); - return tooltip != null && tooltip.startsWith(description); - } - }); + for (navigationComment in navigationDataComments) { + val description = getLineMarkerDescription(navigationComment) + val navigateMarker = markers.find { it.lineMarkerTooltip?.startsWith(description) == true }!! - assertNotNull( + TestCase.assertNotNull( String.format("Can't find marker for navigation check with description \"%s\"", description), - navigateMarker); + navigateMarker) - GutterIconNavigationHandler handler = navigateMarker.getNavigationHandler(); - if (handler instanceof SuperDeclarationMarkerNavigationHandler) { - PsiElement element = navigateMarker.getElement(); + val handler = navigateMarker.navigationHandler + if (handler is SuperDeclarationMarkerNavigationHandler) { + val element = navigateMarker.element as KtDeclaration - //noinspection unchecked - handler.navigate(null, element); - List navigateElements = - ((SuperDeclarationMarkerNavigationHandler) handler).getNavigationElements(); + handler.navigate(null, element) + val navigateElements = handler.getNavigationElements() - Collections.sort(navigateElements, new Comparator() { - @Override - public int compare(@NotNull NavigatablePsiElement first, @NotNull NavigatablePsiElement second) { - String elementFirstStr = ReferenceUtils.renderAsGotoImplementation(first); - String elementSecondStr = ReferenceUtils.renderAsGotoImplementation(second); + Collections.sort(navigateElements) { first, second -> + val elementFirstStr = ReferenceUtils.renderAsGotoImplementation(first) + val elementSecondStr = ReferenceUtils.renderAsGotoImplementation(second) - return elementFirstStr.compareTo(elementSecondStr); - } - }); + elementFirstStr.compareTo(elementSecondStr) + } - String actualNavigationData = - NavigationTestUtils.getNavigateElementsText(myFixture.getProject(), navigateElements); + val actualNavigationData = NavigationTestUtils.getNavigateElementsText(myFixture.project, navigateElements) - assertSameLines(getExpectedNavigationText(navigationComment), actualNavigationData); + UsefulTestCase.assertSameLines(getExpectedNavigationText(navigationComment), actualNavigationData) } 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) { - int firstLineEnd = navigationComment.indexOf("\n"); - assertTrue("The first line in block comment must contain description of marker for navigation check", firstLineEnd != -1); + companion object { - 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), - navigationMarkerText.startsWith(LINE_MARKER_PREFIX)); + private fun getLineMarkerDescription(navigationComment: String): String { + 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) { - int firstLineEnd = navigationComment.indexOf("\n"); + navigationMarkerText = navigationMarkerText.substring(LINE_MARKER_PREFIX.length) - String expectedNavigationText = navigationComment.substring(firstLineEnd + 1); + return navigationMarkerText.trim { it <= ' ' } + } - assertTrue( - String.format("Marker %s is expected before navigation data", TARGETS_PREFIX), - expectedNavigationText.startsWith(TARGETS_PREFIX)); + private fun getExpectedNavigationText(navigationComment: String): String { + val firstLineEnd = navigationComment.indexOf("\n") - 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 + } } }