Skip to content

Commit

Permalink
new builder methods that are aware of XML's idea of whitespace
Browse files Browse the repository at this point in the history
see #39
  • Loading branch information
bodewig committed Apr 10, 2023
1 parent 893c09b commit f5fe30b
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 1 deletion.
106 changes: 105 additions & 1 deletion src/main/net-core/Builder/DiffBuilder.cs
Expand Up @@ -79,6 +79,12 @@ public class DiffBuilder : IDifferenceEngineConfigurer<DiffBuilder> {

private bool ignoreECW;

private bool ignoreXmlWhitespace;

private bool normalizeXmlWhitespace;

private bool ignoreXmlECW;

private bool ignoreComments;

private string ignoreCommentVersion = null;
Expand Down Expand Up @@ -121,19 +127,53 @@ public class DiffBuilder : IDifferenceEngineConfigurer<DiffBuilder> {
/// If you only want to remove text nodes consisting solely of
/// whitespace (AKA element content whitespace) but leave all
/// other text nodes alone you should use
/// ignoreElementContentWhitespace instead.
/// <see cref="IgnoreElementContentWhitespace"/> instead.
/// </para>
/// <para>
/// Unlike <see cref="IgnoreXmlWhitespace"/> this uses Unicode's idea
/// of whitespace rather than the more restricted subset considered
/// whitespace by XML.
/// </para>
/// </remarks>
public DiffBuilder IgnoreWhitespace() {
ignoreWhitespace = true;
return this;
}

/// <summary>
/// Ignore XML whitespace by removing all empty text nodes and trimming the non-empty ones.
/// </summary>
/// <remarks>
/// <para>
/// If you only want to remove text nodes consisting solely of
/// whitespace (AKA element content whitespace) but leave all
/// other text nodes alone you should use
/// <see cref="IgnoreXmlElementContentWhitespace"/> instead.
/// </para>
/// <para>
/// Unlike <see cref="IgnoreWhitespace"/> this uses XML's idea
/// of whitespace rather than the more extensive set considered
/// whitespace by Unicode.
/// </para>
/// <para>
/// since XMLUnit 2.10.0
/// </para>
/// </remarks>
public DiffBuilder IgnoreXmlWhitespace() {
ignoreXmlWhitespace = true;
return this;
}

/// <summary>
/// Ignore element content whitespace by removing all text
/// nodes solely consisting of whitespace.
/// </summary>
/// <remarks>
/// <para>
/// Unlike <see cref="IgnoreXmlElementContentWhitespace"/> this uses Unicode's idea
/// of whitespace rather than the more restricted subset considered
/// whitespace by XML.
/// </para>
/// <para>
/// since XMLUnit 2.6.0
/// </para>
Expand All @@ -143,6 +183,25 @@ public class DiffBuilder : IDifferenceEngineConfigurer<DiffBuilder> {
return this;
}

/// <summary>
/// Ignore element content whitespace by removing all text
/// nodes solely consisting of XML whitespace.
/// </summary>
/// <remarks>
/// <para>
/// Unlike <see cref="IgnoreElementContentWhitespace"/> this uses XML's idea
/// of whitespace rather than the more extensive set considered
/// whitespace by Unicode.
/// </para>
/// <para>
/// since XMLUnit 2.10.0
/// </para>
/// </remarks>
public DiffBuilder IgnoreXmlElementContentWhitespace() {
ignoreXmlECW = true;
return this;
}

/// <summary>
/// Normalize Text-Elements by removing all empty text nodes and normalizing the non-empty ones.
/// </summary>
Expand All @@ -152,12 +211,48 @@ public class DiffBuilder : IDifferenceEngineConfigurer<DiffBuilder> {
/// characters are replaced by space characters and
/// consecutive whitespace characters are collapsed.
/// </para>
/// <para>
/// This method is similiar to <see cref="IgnoreWhitespace"/>
/// but in addition "normalizes" whitespace.
/// </para>
/// <para>
/// Unlike <see cref="NormalizeXmlWhitespace"/> this uses Unicode's idea
/// of whitespace rather than the more restricted subset considered
/// whitespace by XML.
/// </para>
/// </remarks>
public DiffBuilder NormalizeWhitespace() {
normalizeWhitespace = true;
return this;
}

/// <summary>
/// Normalize Text-Elements by removing all empty text nodes and normalizing the non-empty ones.
/// </summary>
/// <remarks>
/// <para>
/// "normalized" in this context means all XML whitespace
/// characters are replaced by space characters and
/// consecutive XML whitespace characters are collapsed.
/// </para>
/// <para>
/// This method is similiar to <see cref="IgnoreXmlWhitespace"/>
/// but in addition "normalizes" XML whitespace.
/// </para>
/// <para>
/// Unlike <see cref="NormalizeWhitespace"/> this uses XML's idea
/// of whitespace rather than the more extensive set considered
/// whitespace by Unicode.
/// </para>
/// <para>
/// since XMLUnit 2.10.0
/// </para>
/// </remarks>
public DiffBuilder NormalizeXmlWhitespace() {
normalizeXmlWhitespace = true;
return this;
}

/// <summary>
/// Will remove all comment-Tags "&lt;!-- Comment --&gt;" from
/// test- and control-XML before comparing.
Expand Down Expand Up @@ -405,9 +500,15 @@ public class DiffBuilder : IDifferenceEngineConfigurer<DiffBuilder> {
if (ignoreWhitespace) {
newSource = new WhitespaceStrippedSource(newSource);
}
if (ignoreXmlWhitespace) {
newSource = new XmlWhitespaceStrippedSource(newSource);
}
if (normalizeWhitespace) {
newSource = new WhitespaceNormalizedSource(newSource);
}
if (normalizeXmlWhitespace) {
newSource = new XmlWhitespaceNormalizedSource(newSource);
}
if (ignoreComments) {
newSource = ignoreCommentVersion == null
? new CommentLessSource(newSource)
Expand All @@ -416,6 +517,9 @@ public class DiffBuilder : IDifferenceEngineConfigurer<DiffBuilder> {
if (ignoreECW) {
newSource = new ElementContentWhitespaceStrippedSource(newSource);
}
if (ignoreXmlECW) {
newSource = new XmlElementContentWhitespaceStrippedSource(newSource);
}
return newSource;
}

Expand Down
96 changes: 96 additions & 0 deletions src/tests/net-core/Builder/DiffBuilderTest.cs
Expand Up @@ -56,6 +56,38 @@ public class DiffBuilderTest {
Assert.IsFalse(myDiff.HasDifferences(), "XML similar " + myDiff.ToString());
}

[Test]
public void IgnoreWhitespaceAndIgnoreXmlWhitespaceWorkAsExpected() {
// prepare testData
string controlXml = "<a><b>Test Value</b></a>";
string testXml1 = "<a>\n <b>\n Test Value\n </b>\n</a>";
string testXml2 = "<a>\n\u00a0<b>\n Test Value\n </b>\n</a>";

// run test
var plainDiff1 = DiffBuilder.Compare(Input.FromString(controlXml).Build())
.WithTest(Input.FromString(testXml1).Build())
.IgnoreWhitespace()
.Build();
var xmlDiff1 = DiffBuilder.Compare(Input.FromString(controlXml).Build())
.WithTest(Input.FromString(testXml1).Build())
.IgnoreXmlWhitespace()
.Build();
var plainDiff2 = DiffBuilder.Compare(Input.FromString(controlXml).Build())
.WithTest(Input.FromString(testXml2).Build())
.IgnoreWhitespace()
.Build();
var xmlDiff2 = DiffBuilder.Compare(Input.FromString(controlXml).Build())
.WithTest(Input.FromString(testXml2).Build())
.IgnoreXmlWhitespace()
.Build();

// validate result
Assert.IsFalse(plainDiff1.HasDifferences(), "XML similar " + plainDiff1.ToString());
Assert.IsFalse(xmlDiff1.HasDifferences(), "XML similar " + xmlDiff1.ToString());
Assert.IsFalse(plainDiff2.HasDifferences(), "XML similar " + plainDiff2.ToString());
Assert.IsTrue(xmlDiff2.HasDifferences(), "XML similar " + xmlDiff2.ToString());
}

[Test]
public void TestDiff_withoutNormalizeWhitespaces_shouldFail() {
// prepare testData
Expand Down Expand Up @@ -87,6 +119,38 @@ public class DiffBuilderTest {
Assert.IsFalse(myDiff.HasDifferences(), "XML similar " + myDiff.ToString());
}

[Test]
public void NormalizeWhitespaceAndNormalizeXmlWhitespaceWorkAsExpected() {
// prepare testData
string controlXml = "<a><b>Test Value</b></a>";
string testXml1 = "<a>\n <b>\n Test\nValue\n </b>\n</a>";
string testXml2 = "<a>\n <b>\n Test\u00a0Value\n </b>\n</a>";

// run test
var plainDiff1 = DiffBuilder.Compare(Input.FromString(controlXml).Build())
.WithTest(Input.FromString(testXml1).Build())
.NormalizeWhitespace()
.Build();
var xmlDiff1 = DiffBuilder.Compare(Input.FromString(controlXml).Build())
.WithTest(Input.FromString(testXml1).Build())
.NormalizeXmlWhitespace()
.Build();
var plainDiff2 = DiffBuilder.Compare(Input.FromString(controlXml).Build())
.WithTest(Input.FromString(testXml2).Build())
.NormalizeWhitespace()
.Build();
var xmlDiff2 = DiffBuilder.Compare(Input.FromString(controlXml).Build())
.WithTest(Input.FromString(testXml2).Build())
.NormalizeXmlWhitespace()
.Build();

// validate result
Assert.IsFalse(plainDiff1.HasDifferences(), "XML similar " + plainDiff1.ToString());
Assert.IsFalse(xmlDiff1.HasDifferences(), "XML similar " + xmlDiff1.ToString());
Assert.IsFalse(plainDiff2.HasDifferences(), "XML similar " + plainDiff2.ToString());
Assert.IsTrue(xmlDiff2.HasDifferences(), "XML similar " + xmlDiff2.ToString());
}

[Test]
public void TestDiff_withNormalizeAndIgnoreWhitespaces_shouldSucceed() {
// prepare testData
Expand Down Expand Up @@ -486,6 +550,38 @@ public class DiffBuilderTest {
Assert.IsFalse(myDiff.HasDifferences(), "XML similar " + myDiff.ToString());
}

[Test]
public void IgnoreElementContentWhitespacesAndIgnoreXmlElementContentWhitespacesWorkAsExpected() {
// prepare testData
string controlXml = "<a><b>Test Value</b></a>";
string testXml1 = "<a>\n <b>Test Value</b>\n</a>";
string testXml2 = "<a>\n <b>Test Value</b>\u00a0</a>";

// run test
var plainDiff1 = DiffBuilder.Compare(Input.FromString(controlXml).Build())
.WithTest(Input.FromString(testXml1).Build())
.IgnoreElementContentWhitespace()
.Build();
var plainDiff2 = DiffBuilder.Compare(Input.FromString(controlXml).Build())
.WithTest(Input.FromString(testXml2).Build())
.IgnoreElementContentWhitespace()
.Build();
var xmlDiff1 = DiffBuilder.Compare(Input.FromString(controlXml).Build())
.WithTest(Input.FromString(testXml1).Build())
.IgnoreXmlElementContentWhitespace()
.Build();
var xmlDiff2 = DiffBuilder.Compare(Input.FromString(controlXml).Build())
.WithTest(Input.FromString(testXml2).Build())
.IgnoreXmlElementContentWhitespace()
.Build();

// validate result
Assert.IsFalse(plainDiff1.HasDifferences(), "XML similar " + plainDiff1.ToString());
Assert.IsFalse(plainDiff2.HasDifferences(), "XML similar " + plainDiff2.ToString());
Assert.IsFalse(xmlDiff1.HasDifferences(), "XML similar " + xmlDiff1.ToString());
Assert.IsTrue(xmlDiff2.HasDifferences(), "XML similar " + xmlDiff2.ToString());
}

internal class DummyComparisonFormatter : IComparisonFormatter {
public string GetDescription(Comparison difference) {
return "foo";
Expand Down

0 comments on commit f5fe30b

Please sign in to comment.