Skip to content

Commit

Permalink
Improve default html display of diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Oct 19, 2024
1 parent d07837c commit 52df03d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 16 deletions.
29 changes: 25 additions & 4 deletions java/src/main/java/giis/visualassert/AbstractVisualAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public T setNormalizeEol(boolean normalizeEol) {
}

/**
* By default (hard), differences in whitespaces are rendered as whitespace html
* entities and therefore, always visible in the html ouput; if set to true
* (soft), some whitespace differences may be hidden from the html output
* By default (hard), html differences are displayed inside a pre tag;
* if set to true (soft), some whitespace differences and indentation
* may be hidden from the html output
* @param useSoftDifferences sets soft differences
* @return this object to allow fluent style
*/
Expand Down Expand Up @@ -262,8 +262,29 @@ public String getHtmlDiffs(String expected, String actual) {
return diffs;
}

// customized method to display spaces as whtiespace entities
// customized method to display spaces as whitespace entities
protected String diffPrettyHtmlHard(LinkedList<DiffMatchPatch.Diff> diffs) {
StringBuilder html = new StringBuilder();
for (DiffMatchPatch.Diff aDiff : diffs) {
String text = aDiff.text.replace("&", "&amp;")
.replace("<", "&lt;").replace(">", "&gt;")
.replace("\r", " ").replace("\n", "&para;\n"); // don't use br because everything will be insde pre
switch (aDiff.operation) {
case INSERT:
html.append("<ins style=\"background:#e6ffe6;\">").append(text).append("</ins>");
break;
case DELETE:
html.append("<del style=\"background:#ffe6e6;\">").append(text).append("</del>");
break;
case EQUAL:
html.append("<span>").append(text).append("</span>");
break;
}
}
return "<pre>\n" + html.toString() + "\n</pre>";
}

protected String diffPrettyHtmlHardOld(LinkedList<DiffMatchPatch.Diff> diffs) {
StringBuilder html = new StringBuilder();
for (DiffMatchPatch.Diff aDiff : diffs) {
String text = aDiff.text.replace("&", "&amp;")
Expand Down
14 changes: 9 additions & 5 deletions java/src/test/java/giis/visualassert/TestVisualAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ public class TestVisualAssert {
static String expectedMessageShort = "Strings are different. First diff at line 1 column 5." + "\nThis is the additional message."
+ "\n- Visual diffs at: " + diffFile;
static String htmlDiffs = ""
+ "<span>abc </span><del style=\"background:#ffe6e6;\">def</del>"
+ "<ins style=\"background:#e6ffe6;\">DEF</ins><span> ghi&para;"
+ "<br></span><ins style=\"background:#e6ffe6;\">other&nbsp;line&para;"
+ "<br></ins><span>mno pqr s</span><del style=\"background:#ffe6e6;\">&nbsp;</del><span>tu</span>";
+ "<pre>\n"
+ "<span>abc </span><del style=\"background:#ffe6e6;\">def</del><ins style=\"background:#e6ffe6;\">DEF</ins><span> ghi&para;\n"
+ "</span><ins style=\"background:#e6ffe6;\">other line&para;\n"
+ "</ins><span>mno pqr s</span><del style=\"background:#ffe6e6;\"> </del><span>tu</span>\n"
+ "</pre>";

@Test
public void testNoFail() {
Expand Down Expand Up @@ -103,7 +104,10 @@ public void testFailAllConditionsTrue() {
+ "\n- Expected: <" + expected + ">."
+ "\n- Actual: <" + actualFail + ">.";
assertEquals(expectedMessageLong.replace("\r", ""), e.getMessage().replace("\r", ""));
assertEquals(htmlDiffs.replace("&nbsp;", " ").replace("e6ffe6", "00ff00").replace("ffe6e6", "ff4000"),
assertEquals(htmlDiffs
.replace("<pre>\n", "").replace("\n</pre>", "")
.replace("\n", "<br>")
.replace("e6ffe6", "00ff00").replace("ffe6e6", "ff4000"),
FileUtil.fileRead(FileUtil.getPath(tempReportPath, diffFileName)));
success = true;
}
Expand Down
30 changes: 29 additions & 1 deletion java/src/test/resources/Aggregate.html
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
<span>Aggregated failures:&para;<br>&para;<br>---------------------------&para;<br>-------- Failure 1 --------&para;<br>---------------------------&para;<br>ab </span><del style="background:#ffe6e6;">zz&nbsp;</del><span>cd&para;<br></span><ins style="background:#e6ffe6;"></ins><span>&para;<br>---------------------------&para;<br>-------- Failure 2 --------&para;<br>---------------------------&para;<br></span><ins style="background:#e6ffe6;">Fail&nbsp;assertion&nbsp;raised.&para;<br>msg4</ins><span>&para;<br></span><del style="background:#ffe6e6;"></del><span>&para;<br>---------------------------&para;<br>-------- Failure 3 --------&para;<br>---------------------------&para;<br></span><ins style="background:#e6ffe6;">this&nbsp;is&nbsp;notnull</ins><span>&para;<br></span><del style="background:#ffe6e6;"></del><span>&para;<br>---------------------------&para;<br>-------- Failure 4 --------&para;<br>---------------------------&para;<br></span><del style="background:#ffe6e6;">this&nbsp;is&nbsp;notnull</del><span>&para;<br></span><ins style="background:#e6ffe6;"></ins><span>&para;<br>---------------------------&para;<br>-------- Failure 5 --------&para;<br>---------------------------&para;<br></span><del style="background:#ffe6e6;"></del><span>xy </span><ins style="background:#e6ffe6;">zz&nbsp;</ins><span>vw</span>
<pre>
<span>Aggregated failures:&para;
&para;
---------------------------&para;
-------- Failure 1 --------&para;
---------------------------&para;
ab </span><del style="background:#ffe6e6;">zz </del><span>cd&para;
</span><ins style="background:#e6ffe6;"></ins><span>&para;
---------------------------&para;
-------- Failure 2 --------&para;
---------------------------&para;
</span><ins style="background:#e6ffe6;">Fail assertion raised.&para;
msg4</ins><span>&para;
</span><del style="background:#ffe6e6;"></del><span>&para;
---------------------------&para;
-------- Failure 3 --------&para;
---------------------------&para;
</span><ins style="background:#e6ffe6;">this is notnull</ins><span>&para;
</span><del style="background:#ffe6e6;"></del><span>&para;
---------------------------&para;
-------- Failure 4 --------&para;
---------------------------&para;
</span><del style="background:#ffe6e6;">this is notnull</del><span>&para;
</span><ins style="background:#e6ffe6;"></ins><span>&para;
---------------------------&para;
-------- Failure 5 --------&para;
---------------------------&para;
</span><del style="background:#ffe6e6;"></del><span>xy </span><ins style="background:#e6ffe6;">zz </ins><span>vw</span>
</pre>
5 changes: 3 additions & 2 deletions net/TestVisualAssert/Giis.Visualassert/TestVisualAssert.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 35 additions & 4 deletions net/VisualAssert/Giis.Visualassert/AbstractVisualAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public virtual T SetNormalizeEol(bool normalizeEol)
}

/// <summary>
/// By default (hard), differences in whitespaces are rendered as whitespace html
/// entities and therefore, always visible in the html ouput; if set to true
/// (soft), some whitespace differences may be hidden from the html output
/// By default (hard), html differences are displayed inside a pre tag;
/// if set to true (soft), some whitespace differences and indentation
/// may be hidden from the html output
/// </summary>
/// <param name="useSoftDifferences">sets soft differences</param>
/// <returns>this object to allow fluent style</returns>
Expand Down Expand Up @@ -303,8 +303,39 @@ public virtual string GetHtmlDiffs(string expected, string actual)
return diffs;
}

// customized method to display spaces as whtiespace entities
// customized method to display spaces as whitespace entities
protected internal virtual string DiffPrettyHtmlHard(List<DiffMatchPatch.Diff> diffs)
{
StringBuilder html = new StringBuilder();
foreach (DiffMatchPatch.Diff aDiff in diffs)
{
string text = aDiff.text.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\r", " ").Replace("\n", "&para;\n");
switch (aDiff.operation)
{
case DiffMatchPatch.Operation.INSERT:
{
// don't use br because everything will be insde pre
html.Append("<ins style=\"background:#e6ffe6;\">").Append(text).Append("</ins>");
break;
}

case DiffMatchPatch.Operation.DELETE:
{
html.Append("<del style=\"background:#ffe6e6;\">").Append(text).Append("</del>");
break;
}

case DiffMatchPatch.Operation.EQUAL:
{
html.Append("<span>").Append(text).Append("</span>");
break;
}
}
}
return "<pre>\n" + html.ToString() + "\n</pre>";
}

protected internal virtual string DiffPrettyHtmlHardOld(List<DiffMatchPatch.Diff> diffs)
{
StringBuilder html = new StringBuilder();
foreach (DiffMatchPatch.Diff aDiff in diffs)
Expand Down

0 comments on commit 52df03d

Please sign in to comment.