diff --git a/java/src/main/java/giis/visualassert/SoftVisualAssert.java b/java/src/main/java/giis/visualassert/SoftVisualAssert.java index dfb6dd3..68e9511 100644 --- a/java/src/main/java/giis/visualassert/SoftVisualAssert.java +++ b/java/src/main/java/giis/visualassert/SoftVisualAssert.java @@ -4,6 +4,7 @@ import java.util.List; import giis.visualassert.portable.CallStack; +import giis.visualassert.portable.JavaCs; /** * Records all VisualAssert assertion message that fail and the diff files instead of throwing and exception; @@ -13,6 +14,7 @@ public class SoftVisualAssert extends AbstractVisualAssert { private List assertionMessages; + private List userMessages; // additional message set in the assert //For each failure message, stores expected and actual to generate aggregated diffs private List failureExpected; private List failureActual; @@ -39,10 +41,10 @@ public void assertEquals(String expected, String actual, String message, String expected = normalize(expected); actual = normalize(actual); if (!stringsAreEqual(expected, actual)) - throwAssertionError(getAssertionMessage(expected, actual, message, fileName), expected, actual); + throwAssertionError(getAssertionMessage(expected, actual, message, fileName), message, expected, actual); } - protected void throwAssertionError(String message, String expected, String actual) { + protected void throwAssertionError(String message, String userMessage, String expected, String actual) { // instead of throwing the exception, stores the message if (callStackLength>0) { //firstly adds the call stack traces when appropriate @@ -50,6 +52,7 @@ protected void throwAssertionError(String message, String expected, String actua message += (message.endsWith("\n") ? "" : "\n") + getStackTraceMessage(stack); } assertionMessages.add(message); + userMessages.add(userMessage); failureExpected.add(expected); failureActual.add(actual); } @@ -132,21 +135,28 @@ private String composeOutputInfo(List expectedOrActual) { StringBuilder sb=new StringBuilder(); sb.append("Aggregated failures:"); for (int i = 0; i(); + userMessages=new ArrayList(); failureExpected=new ArrayList(); failureActual=new ArrayList(); } @@ -169,7 +179,7 @@ public void fail() { public void fail(String message) { String messageInfo = "Fail assertion raised." + ("".equals(message) ? "" : "\n"+message); // sets the message info as the actual value to show in the comparison file - throwAssertionError(getMessagePrefix() + " " + messageInfo, "", messageInfo); + throwAssertionError(getMessagePrefix() + " " + messageInfo, message, "", messageInfo); } diff --git a/java/src/test/java/giis/visualassert/TestSoftAggregateDiffs.java b/java/src/test/java/giis/visualassert/TestSoftAggregateDiffs.java index ee8b361..03ef5c3 100644 --- a/java/src/test/java/giis/visualassert/TestSoftAggregateDiffs.java +++ b/java/src/test/java/giis/visualassert/TestSoftAggregateDiffs.java @@ -55,31 +55,31 @@ public void testJunit5FailureWithFileAndFramework() { assertAggregateFile(tempReportPath); } private void assertAggregateFile(String tempReportPath) { - assertEquals(FileUtil.fileRead(FileUtil.getPath(SRC_TEST_RESOURCES, AGGREGATE_HTML)), + assertEquals(FileUtil.fileRead(FileUtil.getPath(SRC_TEST_RESOURCES, AGGREGATE_HTML)).replace("\r", ""), FileUtil.fileRead(FileUtil.getPath(tempReportPath, AGGREGATE_HTML))); } private String getExpectedDiffsJUnit5(SoftVisualAssert va) { return " ==> expected: but was: "; } @@ -108,22 +108,22 @@ public void testJunit3FailureWithFileAndFramework() { private String getExpectedDiffsJUnit34(SoftVisualAssert va) { return " expected:<...----------------" + "\nab [zz cd" - + "\n" + va.getAggregateFailureHeader(1) + + "\n" + va.getAggregateFailureHeader(1, "msg4") + "\n" - + "\n" + va.getAggregateFailureHeader(2) + + "\n" + va.getAggregateFailureHeader(2, "msgen") + "\n" - + "\n" + va.getAggregateFailureHeader(3) + + "\n" + va.getAggregateFailureHeader(3, "msgan") + "\nthis is notnull" - + "\n" + va.getAggregateFailureHeader(4) + + "\n" + va.getAggregateFailureHeader(4, "msg5") + "\nxy] vw> but was:<...----------------" + "\nab [cd" - + "\n" + va.getAggregateFailureHeader(1) + + "\n" + va.getAggregateFailureHeader(1, "msg4") + "\nFail assertion raised.\nmsg4" - + "\n" + va.getAggregateFailureHeader(2) + + "\n" + va.getAggregateFailureHeader(2, "msgen") + "\nthis is notnull" - + "\n" + va.getAggregateFailureHeader(3) + + "\n" + va.getAggregateFailureHeader(3, "msgan") + "\n" - + "\n" + va.getAggregateFailureHeader(4) + + "\n" + va.getAggregateFailureHeader(4, "msg5") + "\nxy zz] vw>"; } diff --git a/java/src/test/resources/Aggregate.html b/java/src/test/resources/Aggregate.html index aada1fc..8b805ad 100644 --- a/java/src/test/resources/Aggregate.html +++ b/java/src/test/resources/Aggregate.html @@ -8,22 +8,26 @@ ¶ ---------------------------¶ -------- Failure 2 --------¶ +-------- msg4¶ ---------------------------¶ Fail assertion raised.¶ msg4¶ ---------------------------¶ -------- Failure 3 --------¶ +-------- msgen¶ ---------------------------¶ this is notnull¶ ---------------------------¶ -------- Failure 4 --------¶ +-------- msgan¶ ---------------------------¶ this is notnull¶ ---------------------------¶ -------- Failure 5 --------¶ +-------- msg5¶ ---------------------------¶ xy zz vw \ No newline at end of file diff --git a/net/TestVisualAssert/Giis.Visualassert/TestSoftAggregateDiffs.cs b/net/TestVisualAssert/Giis.Visualassert/TestSoftAggregateDiffs.cs index f8679f4..5828544 100644 --- a/net/TestVisualAssert/Giis.Visualassert/TestSoftAggregateDiffs.cs +++ b/net/TestVisualAssert/Giis.Visualassert/TestSoftAggregateDiffs.cs @@ -54,14 +54,14 @@ public virtual void TestJunit5FailureWithFile() private void AssertAggregateFile(string tempReportPath) { - NUnit.Framework.Legacy.ClassicAssert.AreEqual(FileUtil.FileRead(FileUtil.GetPath(SrcTestResources, AggregateHtml)), FileUtil.FileRead(FileUtil.GetPath(tempReportPath, AggregateHtml))); + NUnit.Framework.Legacy.ClassicAssert.AreEqual(FileUtil.FileRead(FileUtil.GetPath(SrcTestResources, AggregateHtml)).Replace("\r", string.Empty), FileUtil.FileRead(FileUtil.GetPath(tempReportPath, AggregateHtml))); } private string GetExpectedDiffsJUnit5(SoftVisualAssert va) { - return " ==> expected: but was: "; + return " ==> expected: but was: "; } //Full test with other frameworks (3 and 4), expected diffs are slightly different @@ -85,8 +85,9 @@ private string GetExpectedDiffsJUnit5(SoftVisualAssert va) private string GetExpectedDiffsJUnit34(SoftVisualAssert va) { - return " expected:<...----------------" + "\nab [zz cd" + "\n" + va.GetAggregateFailureHeader(1) + "\n" + "\n" + va.GetAggregateFailureHeader(2) + "\n" + "\n" + va.GetAggregateFailureHeader(3) + "\nthis is notnull" + "\n" + va.GetAggregateFailureHeader(4) + "\nxy] vw> but was:<...----------------" - + "\nab [cd" + "\n" + va.GetAggregateFailureHeader(1) + "\nFail assertion raised.\nmsg4" + "\n" + va.GetAggregateFailureHeader(2) + "\nthis is notnull" + "\n" + va.GetAggregateFailureHeader(3) + "\n" + "\n" + va.GetAggregateFailureHeader(4) + "\nxy zz] vw>"; + return " expected:<...----------------" + "\nab [zz cd" + "\n" + va.GetAggregateFailureHeader(1, "msg4") + "\n" + "\n" + va.GetAggregateFailureHeader(2, "msgen") + "\n" + "\n" + va.GetAggregateFailureHeader(3, "msgan") + "\nthis is notnull" + "\n" + va.GetAggregateFailureHeader(4, + "msg5") + "\nxy] vw> but was:<...----------------" + "\nab [cd" + "\n" + va.GetAggregateFailureHeader(1, "msg4") + "\nFail assertion raised.\nmsg4" + "\n" + va.GetAggregateFailureHeader(2, "msgen") + "\nthis is notnull" + "\n" + va.GetAggregateFailureHeader(3, "msgan") + "\n" + "\n" + + va.GetAggregateFailureHeader(4, "msg5") + "\nxy zz] vw>"; } } } diff --git a/net/VisualAssert/Giis.Visualassert/SoftVisualAssert.cs b/net/VisualAssert/Giis.Visualassert/SoftVisualAssert.cs index 7858ca9..aeab8b4 100644 --- a/net/VisualAssert/Giis.Visualassert/SoftVisualAssert.cs +++ b/net/VisualAssert/Giis.Visualassert/SoftVisualAssert.cs @@ -17,6 +17,8 @@ public class SoftVisualAssert : AbstractVisualAssert assertionMessages; + private IList userMessages; + private IList failureExpected; private IList failureActual; @@ -26,6 +28,7 @@ public class SoftVisualAssert : AbstractVisualAssert 0) @@ -60,6 +63,7 @@ protected internal virtual void ThrowAssertionError(string message, string expec message += (message.EndsWith("\n") ? string.Empty : "\n") + GetStackTraceMessage(stack); } assertionMessages.Add(message); + userMessages.Add(userMessage); failureExpected.Add(expected); failureActual.Add(actual); } @@ -154,20 +158,30 @@ private string ComposeOutputInfo(IList expectedOrActual) sb.Append("Aggregated failures:"); for (int i = 0; i < expectedOrActual.Count; i++) { - sb.Append("\n").Append(GetAggregateFailureHeader(i)).Append("\n").Append(expectedOrActual[i] == null ? string.Empty : expectedOrActual[i]); + sb.Append("\n").Append(GetAggregateFailureHeader(i, userMessages[i])).Append("\n").Append(expectedOrActual[i] == null ? string.Empty : expectedOrActual[i]); } return sb.ToString(); } - public virtual string GetAggregateFailureHeader(int failureNumber) + public virtual string GetAggregateFailureHeader(int failureNumber, string userMessage) + { + return "\n---------------------------" + "\n-------- Failure " + (failureNumber + 1) + " --------" + GetUserMessage(userMessage) + "\n---------------------------"; + } + + private string GetUserMessage(string userMessage) { - return "\n---------------------------" + "\n-------- Failure " + (failureNumber + 1) + " --------" + "\n---------------------------"; + if (JavaCs.IsEmpty(userMessage)) + { + return string.Empty; + } + return "\n-------- " + userMessage; } /// Resets the current failure messages that are stored public virtual void AssertClear() { assertionMessages = new List(); + userMessages = new List(); failureExpected = new List(); failureActual = new List(); } @@ -189,7 +203,7 @@ public virtual void Fail(string message) { string messageInfo = "Fail assertion raised." + (string.Empty.Equals(message) ? string.Empty : "\n" + message); // sets the message info as the actual value to show in the comparison file - ThrowAssertionError(GetMessagePrefix() + " " + messageInfo, string.Empty, messageInfo); + ThrowAssertionError(GetMessagePrefix() + " " + messageInfo, message, string.Empty, messageInfo); } } }