From dfe3062340bcac6c3dcecab1907cd6b7cba0fd13 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 17 Jun 2014 16:12:49 +0400 Subject: [PATCH] Auto insert acual line markers for failed tests --- .../OverrideImplementLineMarkerTest.java | 23 ++- .../highlighter/AbstractHighlightingTest.java | 89 +--------- .../jet/testing/HighlightTestDataUtil.java | 167 ++++++++++++++++++ 3 files changed, 194 insertions(+), 85 deletions(-) create mode 100644 idea/tests/org/jetbrains/jet/testing/HighlightTestDataUtil.java diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/OverrideImplementLineMarkerTest.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/OverrideImplementLineMarkerTest.java index 00f2ffb8fdf..6875e7d7c73 100644 --- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/OverrideImplementLineMarkerTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/OverrideImplementLineMarkerTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * Copyright 2010-2014 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. @@ -21,10 +21,14 @@ import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl; import com.intellij.openapi.editor.Document; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiDocumentManager; +import com.intellij.rt.execution.junit.FileComparisonFailure; import com.intellij.testFramework.ExpectedHighlightingData; +import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.plugin.JetLightCodeInsightFixtureTestCase; import org.jetbrains.jet.plugin.PluginTestCaseBase; +import org.jetbrains.jet.testing.HighlightTestDataUtil; +import java.io.File; import java.util.List; public class OverrideImplementLineMarkerTest extends JetLightCodeInsightFixtureTestCase { @@ -87,7 +91,22 @@ public class OverrideImplementLineMarkerTest extends JetLightCodeInsightFixtureT myFixture.doHighlighting(); List markers = DaemonCodeAnalyzerImpl.getLineMarkers(document, project); - data.checkLineMarkers(markers, document.getText()); + + try { + data.checkLineMarkers(markers, document.getText()); + } + catch (AssertionError error) { + try { + String actualTextWithTestData = HighlightTestDataUtil.insertInfoTags(markers, false, myFixture.getFile().getText()); + JetTestUtils.assertEqualsToFile(new File(getTestDataPath(), fileName()), actualTextWithTestData); + } + catch (FileComparisonFailure failure) { + throw new FileComparisonFailure(error.getMessage() + "\n" + failure.getMessage(), + failure.getExpected(), + failure.getActual(), + failure.getFilePath()); + } + } } catch (Exception exc) { throw new RuntimeException(exc); diff --git a/idea/tests/org/jetbrains/jet/plugin/highlighter/AbstractHighlightingTest.java b/idea/tests/org/jetbrains/jet/plugin/highlighter/AbstractHighlightingTest.java index 9c4d362ec34..b276da1063a 100644 --- a/idea/tests/org/jetbrains/jet/plugin/highlighter/AbstractHighlightingTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/highlighter/AbstractHighlightingTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013 JetBrains s.r.o. + * Copyright 2010-2014 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. @@ -16,11 +16,8 @@ package org.jetbrains.jet.plugin.highlighter; -import com.google.common.collect.Lists; -import com.google.common.collect.Ordering; import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl; import com.intellij.codeInsight.daemon.impl.HighlightInfo; -import com.intellij.lang.annotation.HighlightSeverity; import com.intellij.openapi.util.io.FileUtil; import com.intellij.rt.execution.junit.FileComparisonFailure; import com.intellij.testFramework.LightProjectDescriptor; @@ -28,6 +25,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.InTextDirectivesUtils; import org.jetbrains.jet.plugin.JetLightCodeInsightFixtureTestCase; import org.jetbrains.jet.plugin.JetLightProjectDescriptor; +import org.jetbrains.jet.testing.HighlightTestDataUtil; import java.io.File; import java.util.List; @@ -51,8 +49,11 @@ public abstract class AbstractHighlightingTest extends JetLightCodeInsightFixtur myFixture.checkHighlighting(checkWarnings, checkInfos, checkWeakWarnings); } catch (FileComparisonFailure e) { - System.out.println(getFileWithTextAttributesKeys()); + List highlights = + DaemonCodeAnalyzerImpl.getHighlights(myFixture.getDocument(myFixture.getFile()), null, myFixture.getProject()); + String text = myFixture.getFile().getText(); + System.out.println(HighlightTestDataUtil.insertInfoTags(highlights, text)); throw e; } } @@ -62,82 +63,4 @@ public abstract class AbstractHighlightingTest extends JetLightCodeInsightFixtur protected String getTestDataPath() { return ""; } - - private String getFileWithTextAttributesKeys() { - List highlights = - DaemonCodeAnalyzerImpl.getHighlights(myFixture.getDocument(myFixture.getFile()), null, myFixture.getProject()); - - List pointsForHighlightInfos = Lists.newArrayList(); - for (HighlightInfo highlight : highlights) { - pointsForHighlightInfos.add(new HighlightTagPoint(highlight.startOffset, true, highlight)); - pointsForHighlightInfos.add(new HighlightTagPoint(highlight.endOffset, false, highlight)); - } - - StringBuilder builder = new StringBuilder(myFixture.getFile().getText()); - - List sortedTagPoints = Ordering.natural().reverse().sortedCopy(pointsForHighlightInfos); - - // Insert tags into text starting from the end for preventing invalidating of highlight offsets - for (HighlightTagPoint point : sortedTagPoints) { - String tagName; - if (point.highlightInfo.getSeverity().equals(HighlightSeverity.INFORMATION)) { - tagName = "info"; - } - else { - tagName = point.highlightInfo.getSeverity().toString().toLowerCase(); - } - - String tagText; - if (point.isStart) { - if (point.highlightInfo.getDescription() != null) { - tagText = String.format("<%s textAttributesKey=\"%s\" descr=%s>", - tagName, - point.highlightInfo.forcedTextAttributesKey, - point.highlightInfo.getDescription()); - } - else { - tagText = String.format("<%s textAttributesKey=\"%s\">", tagName, point.highlightInfo.forcedTextAttributesKey); - } - } - else { - tagText = String.format("", tagName); - } - - builder.insert(point.offset, tagText); - } - - return builder.toString(); - } - - private static class HighlightTagPoint implements Comparable { - private final int offset; - private final boolean isStart; - private final HighlightInfo highlightInfo; - - private HighlightTagPoint(int offset, boolean start, HighlightInfo info) { - this.offset = offset; - isStart = start; - highlightInfo = info; - } - - @Override - public int compareTo(@NotNull HighlightTagPoint other) { - if (offset != other.offset) { - return ((Integer) offset).compareTo(other.offset); - } - - if (isStart != other.isStart) { - // All "starts" should go after "ends" for same offset - return isStart ? -1 : 1; - } - - if (highlightInfo.getSeverity() != other.highlightInfo.getSeverity()) { - // Invert order for end tags - return highlightInfo.getSeverity().compareTo(other.highlightInfo.getSeverity()) * (isStart ? -1 : 1); - } - - // The order isn't important for highlighting with same offset and severity - return 0; - } - } } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/testing/HighlightTestDataUtil.java b/idea/tests/org/jetbrains/jet/testing/HighlightTestDataUtil.java new file mode 100644 index 00000000000..33a2e346e60 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/testing/HighlightTestDataUtil.java @@ -0,0 +1,167 @@ +/* + * Copyright 2010-2014 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.jet.testing; + +import com.google.common.collect.Lists; +import com.google.common.collect.Ordering; +import com.intellij.codeInsight.daemon.LineMarkerInfo; +import com.intellij.codeInsight.daemon.impl.HighlightInfo; +import com.intellij.lang.annotation.HighlightSeverity; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class HighlightTestDataUtil { + public static String insertInfoTags(List lineMarkers, boolean withDescription, String text) { + List lineMarkerPoints = Lists.newArrayList(); + for (LineMarkerInfo markerInfo : lineMarkers) { + lineMarkerPoints.add(new LineMarkerTagPoint(markerInfo.startOffset, true, markerInfo, withDescription)); + lineMarkerPoints.add(new LineMarkerTagPoint(markerInfo.endOffset, false, markerInfo, withDescription)); + } + + return insertTagsInText(lineMarkerPoints, text); + } + + public static String insertInfoTags(List highlights, String text) { + List highlightPoints = Lists.newArrayList(); + for (HighlightInfo highlight : highlights) { + highlightPoints.add(new HighlightTagPoint(highlight.startOffset, true, highlight)); + highlightPoints.add(new HighlightTagPoint(highlight.endOffset, false, highlight)); + } + + return insertTagsInText(highlightPoints, text); + } + + private static String insertTagsInText(List tags, String text) { + StringBuilder builder = new StringBuilder(text); + + List sortedTagPoints = Ordering.natural().reverse().sortedCopy(tags); + + // Insert tags into text starting from the end for preventing invalidating previous tags offsets + for (HighlightTag point : sortedTagPoints) { + String tagText; + if (point.isStart) { + String attributesString = point.getAttributesString(); + if (attributesString.isEmpty()) { + tagText = String.format("<%s>", point.getName()); + } + else { + tagText = String.format("<%s %s>", point.getName(), attributesString); + } + } + else { + tagText = String.format("", point.getName()); + } + + builder.insert(point.offset, tagText); + } + + return builder.toString(); + } + + private static abstract class HighlightTag implements Comparable> { + protected final int offset; + protected final boolean isStart; + protected final Data data; + + private HighlightTag(int offset, boolean start, Data data) { + this.offset = offset; + isStart = start; + this.data = data; + } + + @Override + public int compareTo(@NotNull HighlightTag other) { + if (offset != other.offset) { + return ((Integer) offset).compareTo(other.offset); + } + + if (isStart != other.isStart) { + // All "starts" should go after "ends" for same offset + return isStart ? -1 : 1; + } + + String thisTag = this.getName(); + String otherTag = other.getName(); + + // Invert order for end tags + return thisTag.compareTo(otherTag) * (isStart ? -1 : 1); + } + + @NotNull + abstract String getName(); + + @NotNull + abstract String getAttributesString(); + } + + private static class HighlightTagPoint extends HighlightTag { + private final HighlightInfo highlightInfo; + + private HighlightTagPoint(int offset, boolean start, HighlightInfo info) { + super(offset, start, info); + highlightInfo = info; + } + + @NotNull + @Override + public String getName() { + return highlightInfo.getSeverity().equals(HighlightSeverity.INFORMATION) + ? "info" + : highlightInfo.getSeverity().toString().toLowerCase(); + } + + @NotNull + @Override + String getAttributesString() { + if (isStart) { + if (highlightInfo.getDescription() != null) { + return String.format("textAttributesKey=\"%s\" descr=%s", + highlightInfo.forcedTextAttributesKey, + highlightInfo.getDescription()); + } + else { + return String.format("textAttributesKey=\"%s\"", getName()); + } + } + else { + return ""; + } + } + } + + private static class LineMarkerTagPoint extends HighlightTag { + private final boolean withDescription; + + public LineMarkerTagPoint(int offset, boolean start, LineMarkerInfo info, boolean withDescription) { + super(offset, start, info); + this.withDescription = withDescription; + } + + @NotNull + @Override + String getName() { + return "lineMarker"; + } + + @NotNull + @Override + String getAttributesString() { + return withDescription ? String.format("descr=\"%s\"", data.getLineMarkerTooltip()) : "descr=\"*\""; + } + } +}