Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inputs created from XmlDocument with "PreserveWhitespace = true" generate unexpected differences. #38

Closed
jlocans opened this issue Mar 9, 2023 · 7 comments
Milestone

Comments

@jlocans
Copy link

jlocans commented Mar 9, 2023

Hello!

First of all, thanks for the great library!

Maybe this is intentional, but I just spent way too much time figuring out why any 3 of the whitespace ignore rules were not working and were in fact returning twice the differences than w/o any additional rules.

Turns out, if you create XmlDocument with PreserveWhitespace set to true and use that as input source, then the whitespace rules don't work.

I've modified one of the test cases:

[Test]
public void TestDiff_withIgnoreWhitespaces_shouldSucceed() {
    // prepare testData
    string controlXml = "<a><b>Test Value</b></a>";
    string testXml = "<a>\n <b>\n  Test Value\n </b>\n</a>";
    var controlDoc = new XmlDocument
    {
        PreserveWhitespace = true
    };
    controlDoc.LoadXml(controlXml);
    var testDoc = new XmlDocument
    {
        PreserveWhitespace = true
    };
    testDoc.LoadXml(testXml);

    // run test
    var myDiff = DiffBuilder.Compare(Input.FromDocument(controlDoc).Build())
        .WithTest(Input.FromDocument(testDoc).Build())
        .IgnoreWhitespace()
        .Build();

    // validate result
    Assert.IsFalse(myDiff.HasDifferences(), "XML similar " + myDiff.ToString());
}

I guess I would have expected that any of the whitespace ignore rules would take precedence over PreserveWhitespace setting.

@bodewig
Copy link
Member

bodewig commented Mar 14, 2023

To be honest I would have expected the same as you. :-)

It doesn't work without PreserveWhitespace either. The problem is the parsed document contains XmlWhitespace nodes that the code in Nodes doesn't handle at all. Patch upcoming.

@bodewig
Copy link
Member

bodewig commented Mar 14, 2023

I'll see whether this works for older frameworks - I certainly haven't got access to everything XMLUnit.NET wants to support anymore - during CI. Will cut a new release the coming days.

Many thanks for finding the bug :-)

@jlocans
Copy link
Author

jlocans commented Mar 15, 2023

Glad I could help.

It doesn't work without PreserveWhitespace either

IgnoreElementContentWhitespace does seem to work though. In my test case, if document was created with preserving whitespace, then a simple compare w/o any filtering would return 8 SIMILAR differences. Adding IgnoreElementContentWhitespace would strangely generate 22 differences, including DIFFERENT differences.

However creating document w/o PreserveWhitespace in combination with IgnoreElementContentWhitespace returns 0 differences.

@bodewig
Copy link
Member

bodewig commented Mar 15, 2023

you are correct, when I wrote what you quote my failing unit test actually contained PreserveWhitespace = true, I had just forgotten about it.

Anyway, this is fixed now and 5d5edaa#diff-ee535be5744414e990a2353b3b7b3cc4d2ade8bb30e046c28405572e69f3a048R31 passes (it failed before my change).

@bodewig bodewig added this to the 2.9.2 milestone Mar 16, 2023
@bodewig
Copy link
Member

bodewig commented Mar 16, 2023

I've just published nuget packages for 2.9.2

@jlocans
Copy link
Author

jlocans commented Mar 17, 2023

Thanks for the quick fix!

@bodewig
Copy link
Member

bodewig commented Mar 17, 2023

you're welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants