Skip to content

Commit

Permalink
Add assert message to soft assert aggregate diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Oct 20, 2024
1 parent aac8f15 commit 78993ca
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 34 deletions.
20 changes: 15 additions & 5 deletions java/src/main/java/giis/visualassert/SoftVisualAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +14,7 @@
public class SoftVisualAssert extends AbstractVisualAssert<SoftVisualAssert> {

private List<String> assertionMessages;
private List<String> userMessages; // additional message set in the assert
//For each failure message, stores expected and actual to generate aggregated diffs
private List<String> failureExpected;
private List<String> failureActual;
Expand All @@ -39,17 +41,18 @@ 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
CallStack stack=new CallStack();
message += (message.endsWith("\n") ? "" : "\n") + getStackTraceMessage(stack);
}
assertionMessages.add(message);
userMessages.add(userMessage);
failureExpected.add(expected);
failureActual.add(actual);
}
Expand Down Expand Up @@ -132,21 +135,28 @@ private String composeOutputInfo(List<String> expectedOrActual) {
StringBuilder sb=new StringBuilder();
sb.append("Aggregated failures:");
for (int i = 0; i<expectedOrActual.size(); i++) {
sb.append("\n").append(getAggregateFailureHeader(i))
sb.append("\n").append(getAggregateFailureHeader(i, userMessages.get(i)))
.append("\n").append(expectedOrActual.get(i) == null ? "" : expectedOrActual.get(i));
}
return sb.toString();
}
public String getAggregateFailureHeader(int failureNumber) {
public String getAggregateFailureHeader(int failureNumber, String userMessage) {
return "\n---------------------------"
+ "\n-------- Failure " + (failureNumber+1) + " --------"
+ getUserMessage(userMessage)
+ "\n---------------------------";
}
private String getUserMessage(String userMessage) {
if (JavaCs.isEmpty(userMessage))
return "";
return "\n-------- " + userMessage;
}
/**
* Resets the current failure messages that are stored
*/
public void assertClear() {
assertionMessages=new ArrayList<String>();
userMessages=new ArrayList<String>();
failureExpected=new ArrayList<String>();
failureActual=new ArrayList<String>();
}
Expand All @@ -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);
}


Expand Down
36 changes: 18 additions & 18 deletions java/src/test/java/giis/visualassert/TestSoftAggregateDiffs.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,25 @@ private void assertAggregateFile(String tempReportPath) {

private String getExpectedDiffsJUnit5(SoftVisualAssert va) {
return " ==> expected: <Aggregated failures:"
+ "\n" + va.getAggregateFailureHeader(0)
+ "\n" + va.getAggregateFailureHeader(0, "")
+ "\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: <Aggregated failures:"
+ "\n" + va.getAggregateFailureHeader(0)
+ "\n" + va.getAggregateFailureHeader(0, "")
+ "\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>";
}

Expand Down Expand Up @@ -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>";
}

Expand Down
4 changes: 4 additions & 0 deletions java/src/test/resources/Aggregate.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@
</span><ins style="background:#e6ffe6;"></ins><span>&para;
---------------------------&para;
-------- Failure 2 --------&para;
-------- msg4&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;
-------- msgen&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;
-------- msgan&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;
-------- msg5&para;
---------------------------&para;
</span><del style="background:#ffe6e6;"></del><span>xy </span><ins style="background:#e6ffe6;">zz </ins><span>vw</span>
</pre>
11 changes: 6 additions & 5 deletions net/TestVisualAssert/Giis.Visualassert/TestSoftAggregateDiffs.cs

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

26 changes: 20 additions & 6 deletions net/VisualAssert/Giis.Visualassert/SoftVisualAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class SoftVisualAssert : AbstractVisualAssert<Giis.Visualassert.SoftVisua
{
private IList<string> assertionMessages;

private IList<string> userMessages;

private IList<string> failureExpected;

private IList<string> failureActual;
Expand All @@ -26,6 +28,7 @@ public class SoftVisualAssert : AbstractVisualAssert<Giis.Visualassert.SoftVisua
public SoftVisualAssert()
: base()
{
// additional message set in the assert
//For each failure message, stores expected and actual to generate aggregated diffs
//by default shows the single line where assert failed
AssertClear();
Expand All @@ -46,11 +49,11 @@ public override void AssertEquals(string expected, string actual, string message
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 internal virtual void ThrowAssertionError(string message, string expected, string actual)
protected internal virtual void ThrowAssertionError(string message, string userMessage, string expected, string actual)
{
// instead of throwing the exception, stores the message
if (callStackLength > 0)
Expand All @@ -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);
}
Expand Down Expand Up @@ -154,20 +158,30 @@ private string ComposeOutputInfo(IList<string> 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;
}

/// <summary>Resets the current failure messages that are stored</summary>
public virtual void AssertClear()
{
assertionMessages = new List<string>();
userMessages = new List<string>();
failureExpected = new List<string>();
failureActual = new List<string>();
}
Expand All @@ -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);
}
}
}

0 comments on commit 78993ca

Please sign in to comment.