Auto insert acual line markers for failed tests

This commit is contained in:
Nikolay Krasko
2014-06-17 16:12:49 +04:00
parent 2d54139018
commit dfe3062340
3 changed files with 194 additions and 85 deletions
@@ -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<LineMarkerInfo> 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);
@@ -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<HighlightInfo> 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<HighlightInfo> highlights =
DaemonCodeAnalyzerImpl.getHighlights(myFixture.getDocument(myFixture.getFile()), null, myFixture.getProject());
List<HighlightTagPoint> 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<HighlightTagPoint> 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("</%s>", tagName);
}
builder.insert(point.offset, tagText);
}
return builder.toString();
}
private static class HighlightTagPoint implements Comparable<HighlightTagPoint> {
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;
}
}
}
@@ -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<LineMarkerInfo> lineMarkers, boolean withDescription, String text) {
List<LineMarkerTagPoint> 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<HighlightInfo> highlights, String text) {
List<HighlightTagPoint> 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<? extends HighlightTag> tags, String text) {
StringBuilder builder = new StringBuilder(text);
List<? extends HighlightTag> 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("</%s>", point.getName());
}
builder.insert(point.offset, tagText);
}
return builder.toString();
}
private static abstract class HighlightTag<Data> implements Comparable<HighlightTag<?>> {
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<HighlightInfo> {
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<LineMarkerInfo> {
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=\"*\"";
}
}
}