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.
*/
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<LineMarkerInfo> 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("<lineMarker") && markers.isEmpty()) {
throw new AssertionError("Some line markers are expected, but nothing is present at all");
if (FileUtil.loadFile(File(path)).contains("<lineMarker") && markers.isEmpty()) {
throw AssertionError("Some line markers are expected, but nothing is present at all")
}
}
catch (AssertionError error) {
catch (error: AssertionError) {
try {
String actualTextWithTestData = TagsTestDataUtil.insertInfoTags(markers, true, myFixture.getFile().getText());
KotlinTestUtils.assertEqualsToFile(new File(path), actualTextWithTestData);
val actualTextWithTestData = TagsTestDataUtil.insertInfoTags(markers, true, myFixture.file.text)
KotlinTestUtils.assertEqualsToFile(File(path), actualTextWithTestData)
}
catch (FileComparisonFailure failure) {
throw new FileComparisonFailure(error.getMessage() + "\n" + failure.getMessage(),
failure.getExpected(),
failure.getActual(),
failure.getFilePath());
catch (failure: FileComparisonFailure) {
throw FileComparisonFailure(error.message + "\n" + failure.message,
failure.expected,
failure.actual,
failure.filePath)
}
}
assertNavigationElements(markers);
assertNavigationElements(markers)
}
catch (Exception exc) {
throw new RuntimeException(exc);
catch (exc: Exception) {
throw RuntimeException(exc)
}
}
private void assertNavigationElements(List<LineMarkerInfo> markers) {
List<String> navigationDataComments = KotlinTestUtils.getLastCommentsInFile(
(KtFile) myFixture.getFile(), KotlinTestUtils.CommentType.BLOCK_COMMENT, false);
if (navigationDataComments.isEmpty()) return;
private fun assertNavigationElements(markers: List<LineMarkerInfo<*>>) {
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<LineMarkerInfo>() {
@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<NavigatablePsiElement> navigateElements =
((SuperDeclarationMarkerNavigationHandler) handler).getNavigationElements();
handler.navigate(null, element)
val navigateElements = handler.getNavigationElements()
Collections.sort(navigateElements, new Comparator<NavigatablePsiElement>() {
@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
}
}
}