Skip to content

Commit

Permalink
Add option to normalize line-endings
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Aug 14, 2024
1 parent 59c33ef commit 2cf8993
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# visual-assert

Assertion methods that generate an html file with the differences highlighting the additions and deletions.
Useful for comparing large strings.
Useful for comparing large strings or files.
Available on Java and .NET platforms.

- From Java include the `visual-assert` dependency as indicated in the
Expand Down Expand Up @@ -82,7 +82,7 @@ You can obtain an aggregated view of all failed assertions by using one or both
following methods:

- If a framework has been specified using `setFramework()` (java only) you can see the aggregated differences
from your development environment in the same sway as with the native `assertEquals()`.
from your development environment in the same way as with the native `assertEquals()`.
- If you specify the name of an html file as argument to `assertAll(String htmlFile)`
the aggregated differences can be viewed by opening this file.

Expand All @@ -92,15 +92,16 @@ The behaviour of the `VisualAssert` and `SoftVisualAssert` instances can be cust
These methods follow a fluent style, so as, they can be concatenated in a single statement.

- `setReportSubdir(String reportSubdir)`: Sets the folder where generated files with the differences are stored (default is `target`).
- `setNormalizeEol(boolean normalizeEol)`: If set to true, the compared strings are normalized to linux line-endings by removing all CR characters.
- `setSoftDifferences(boolean useSoftDifferences)`: 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.
- `setBrightColors(boolean useBrightColors)`: By default differences are highlighted with pale red and green colors,
if set to true the colors are brighter to easily locate small differences.
- `setUseLocalAbsolutePath(boolean useLocalAbsolutePath)`: If set to true, the link with the differences file will include an file url with the absolute path to the file,
- `setUseLocalAbsolutePath(boolean useLocalAbsolutePath)`: If set to true, the link with the differences file will include a file url with the absolute path to the file,
useful when running tests from a development environment that allows links in the assertion messages (e.g. MS Visual Studio).
- `setShowExpectedAndActual(boolean showExpectedAndActual)`: If set to true, the assert message will include the whole content of the exepcted and actual strings that are compared.

The `SoftVisualAssert` instances have an additional method:
The `SoftVisualAssert` instances have an additional method to customize:
- `setCallStackLength(int length)`:
Sets the number of call stack items that are shown for each failed assertion (default 1)

Expand Down
17 changes: 17 additions & 0 deletions java/src/main/java/giis/visualassert/AbstractVisualAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public abstract class AbstractVisualAssert<T extends AbstractVisualAssert<T>> {
private boolean useLocalAbsolutePath = false;
private boolean showExpectedAndActual = false;
private boolean softDifferences = false;
protected boolean normalizeEol = false;
private boolean brightColors = false;
private String reportSubdir = JavaCs.DEFAULT_REPORT_SUBDIR;
protected FrameworkAssert platformAssert = new FrameworkAssert(Framework.NONE); // no platform by default
Expand Down Expand Up @@ -43,6 +44,18 @@ public T setReportSubdir(String reportSubdir) {
return (T) this;
}

/**
* If set to true, the compared strings are normalized to linux
* line-endings by removing all CR characters.
* @param normalizeEol sets the end of line normalization
* @return this object to allow fluent style
*/
@SuppressWarnings("unchecked")
public T setNormalizeEol(boolean normalizeEol) {
this.normalizeEol = normalizeEol;
return (T) this;
}

/**
* By default (hard), differences in whitespaces are rendered as whitespace html
* entities and therefore, always visible in the html ouput; if set to true
Expand Down Expand Up @@ -146,6 +159,10 @@ public void assertEquals(String expected, String actual, String message) {
protected abstract String getAssertionMessage(String expected, String actual, String message, String fileName);

// Common methods to obtain messages and diffs

protected String normalize(String value) {
return value != null && this.normalizeEol ? value.replace("\r", "") : value;
}

// Always use this to check for equality with null handling
protected boolean stringsAreEqual(String expected, String actual) {
Expand Down
2 changes: 2 additions & 0 deletions java/src/main/java/giis/visualassert/SoftVisualAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public SoftVisualAssert setCallStackLength(int length) {

@Override
public void assertEquals(String expected, String actual, String message, String fileName) {
expected = normalize(expected);
actual = normalize(actual);
if (!stringsAreEqual(expected, actual))
throwAssertionError(getAssertionMessage(expected, actual, message, fileName), expected, actual);
}
Expand Down
2 changes: 2 additions & 0 deletions java/src/main/java/giis/visualassert/VisualAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class VisualAssert extends AbstractVisualAssert<VisualAssert> {

@Override
public void assertEquals(String expected, String actual, String message, String fileName) {
expected = normalize(expected);
actual = normalize(actual);
if (!stringsAreEqual(expected, actual))
platformAssert.failNotEquals(expected, actual, getAssertionMessage(expected, actual, message, fileName));
}
Expand Down
11 changes: 11 additions & 0 deletions java/src/test/java/giis/visualassert/TestSoftVisualAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,15 @@ public static String expectedMessage(boolean aggregateDiffs) {
+ " at giis.visualassert.TestSoftVisualAssert.doFailSoftAssert(TestSoftVisualAssert.java:21)";
}

@Test
public void testNormalizeLineEndings() {
String linux = "line1\nline2\nline3\n";
String windows = "line1\r\nline2\r\nline3\r\n";
SoftVisualAssert sva = new SoftVisualAssert().setNormalizeEol(true);
sva.assertEquals(windows, linux, "Should not fail with normalize eol");
sva.assertEquals(linux, windows, "Should not fail with normalize eol");
sva.assertEquals(null, null, "Should not fail with nulls");
sva.assertAll("sva-normalize-eol.html");
}

}
19 changes: 19 additions & 0 deletions java/src/test/java/giis/visualassert/TestVisualAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ private void doAssertNulls(VisualAssert va, String expected, String actual, Stri
assertEquals(expectedMessage, e.getMessage().replace("\\", "/"));
}
}

@Test
public void testNormalizeLineEndings() {
String linux = "line1\nline2\nline3\n";
String windows = "line1\r\nline2\r\nline3\r\n";
VisualAssert va = new VisualAssert();
boolean success = false;
try {
va.assertEquals(windows, linux, "Should fail without normalize eol", "va-normalize-eol.html");
} catch (AssertionError e) {
success = true;
}
assertTrue(success);

va = new VisualAssert().setNormalizeEol(true);
va.assertEquals(windows, linux, "Should not fail with normalize eol", "va-normalize-eol.html");
va.assertEquals(linux, windows, "Should not fail with normalize eol", "va-normalize-eol.html");
va.assertEquals(null, null, "Should not fail with nulls", "va-normalize-eol.html");
}

@Test
public void testAutogeneratedFileSequence() {
Expand Down
12 changes: 12 additions & 0 deletions net/TestVisualAssert/Giis.Visualassert/TestSoftVisualAssert.cs

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

22 changes: 22 additions & 0 deletions net/TestVisualAssert/Giis.Visualassert/TestVisualAssert.cs

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

19 changes: 19 additions & 0 deletions net/VisualAssert/Giis.Visualassert/AbstractVisualAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public abstract class AbstractVisualAssert<T>

private bool softDifferences = false;

protected internal bool normalizeEol = false;

private bool brightColors = false;

private string reportSubdir = JavaCs.DefaultReportSubdir;
Expand Down Expand Up @@ -53,6 +55,18 @@ public virtual T SetReportSubdir(string reportSubdir)
return (T)this;
}

/// <summary>
/// If set to true, the compared strings are normalized to linux
/// line-endings by removing all CR characters.
/// </summary>
/// <param name="normalizeEol">sets the end of line normalization</param>
/// <returns>this object to allow fluent style</returns>
public virtual T SetNormalizeEol(bool normalizeEol)
{
this.normalizeEol = normalizeEol;
return (T)this;
}

/// <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
Expand Down Expand Up @@ -160,6 +174,11 @@ public virtual void AssertEquals(string expected, string actual, string message)
protected internal abstract string GetAssertionMessage(string expected, string actual, string message, string fileName);

// Common methods to obtain messages and diffs
protected internal virtual string Normalize(string value)
{
return value != null && this.normalizeEol ? value.Replace("\r", string.Empty) : value;
}

// Always use this to check for equality with null handling
protected internal virtual bool StringsAreEqual(string expected, string actual)
{
Expand Down
19 changes: 19 additions & 0 deletions net/VisualAssert/Giis.Visualassert/FailedTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/////////////////////////////////////////////////////////////////////////////////////////////
/////// THIS FILE HAS BEEN AUTOMATICALLY CONVERTED FROM THE JAVA SOURCES. DO NOT EDIT ///////
/////////////////////////////////////////////////////////////////////////////////////////////
using System;


namespace Giis.Visualassert
{
[System.Serializable]
public class FailedTest : Exception
{
private const long serialVersionUID = 5657525735601303767L;

public FailedTest()
: base("Previous statement in this test should fail")
{
}
}
}
2 changes: 2 additions & 0 deletions net/VisualAssert/Giis.Visualassert/SoftVisualAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public virtual Giis.Visualassert.SoftVisualAssert SetCallStackLength(int length)

public override void AssertEquals(string expected, string actual, string message, string fileName)
{
expected = Normalize(expected);
actual = Normalize(actual);
if (!StringsAreEqual(expected, actual))
{
ThrowAssertionError(GetAssertionMessage(expected, actual, message, fileName), expected, actual);
Expand Down
2 changes: 2 additions & 0 deletions net/VisualAssert/Giis.Visualassert/VisualAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class VisualAssert : AbstractVisualAssert<VisualAssert>
{
public override void AssertEquals(string expected, string actual, string message, string fileName)
{
expected = Normalize(expected);
actual = Normalize(actual);
if (!StringsAreEqual(expected, actual))
{
platformAssert.FailNotEquals(expected, actual, GetAssertionMessage(expected, actual, message, fileName));
Expand Down

0 comments on commit 2cf8993

Please sign in to comment.