Skip to content

Commit c790386

Browse files
committed
XPath for CHILD_LOOKUP differences was affected by NodeFilter
closes #29
1 parent ca50b31 commit c790386

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

RELEASE_NOTES.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Release Notes
22

3-
## XMLUnit.NET 2.7.1 - /not released, yet/
3+
## XMLUnit.NET 2.7.2 - /not released, yet/
4+
5+
* the XPath values for comparisons resulting in `CHILD_LOOKUP`
6+
differences could be wrong when `NodeFilter`s were present.
7+
Issue [#29](https://github.com/xmlunit/xmlunit.net/issues/29)
48

59
## XMLUnit.NET 2.7.1 - /Released 2019-06-21/
610

src/main/net-core/Diff/DOMDifferenceEngine.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,14 @@ private ComparisonState CompareNodeLists(IEnumerable<XmlNode> allControlChildren
500500
}
501501

502502
return chain
503-
.AndThen(UnmatchedControlNodes(controlList, controlContext, seen, testContext))
504-
.AndThen(UnmatchedTestNodes(testList, testContext, seen, controlContext));
503+
.AndThen(UnmatchedControlNodes(controlListForXpath, controlList, controlContext,
504+
seen, testContext))
505+
.AndThen(UnmatchedTestNodes(testListForXpath, testList, testContext, seen,
506+
controlContext));
505507
}
506508

507-
private Func<ComparisonState> UnmatchedControlNodes(IList<XmlNode> controlList,
509+
private Func<ComparisonState> UnmatchedControlNodes(IList<XmlNode> controlListForXpath,
510+
IList<XmlNode> controlList,
508511
XPathContext controlContext,
509512
ICollection<XmlNode> seen,
510513
XPathContext testContext) {
@@ -513,7 +516,8 @@ private Func<ComparisonState> UnmatchedControlNodes(IList<XmlNode> controlList,
513516
int controlSize = controlList.Count;
514517
for (int i = 0; i < controlSize; i++) {
515518
if (!seen.Contains(controlList[i])) {
516-
controlContext.NavigateToChild(i);
519+
controlContext
520+
.NavigateToChild(controlListForXpath.IndexOf(controlList[i]));
517521
try {
518522
chain = chain
519523
.AndThen(new Comparison(ComparisonType.CHILD_LOOKUP,
@@ -532,7 +536,8 @@ private Func<ComparisonState> UnmatchedControlNodes(IList<XmlNode> controlList,
532536
};
533537
}
534538

535-
private Func<ComparisonState> UnmatchedTestNodes(IList<XmlNode> testList,
539+
private Func<ComparisonState> UnmatchedTestNodes(IList<XmlNode> testListForXpath,
540+
IList<XmlNode> testList,
536541
XPathContext testContext,
537542
ICollection<XmlNode> seen,
538543
XPathContext controlContext) {
@@ -541,7 +546,7 @@ private Func<ComparisonState> UnmatchedTestNodes(IList<XmlNode> testList,
541546
int testSize = testList.Count;
542547
for (int i = 0; i < testSize; i++) {
543548
if (!seen.Contains(testList[i])) {
544-
testContext.NavigateToChild(i);
549+
testContext.NavigateToChild(testListForXpath.IndexOf(testList[i]));
545550
try {
546551
chain = chain
547552
.AndThen(new Comparison(ComparisonType.CHILD_LOOKUP,

src/tests/net-core/Diff/DOMDifferenceEngineTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,42 @@ public void AttributesWithDifferentPrefixesAreSimilar() {
11351135
Assert.AreEqual(ComparisonType.NAMESPACE_PREFIX, diff.Differences.First().Comparison.Type);
11361136
}
11371137

1138+
[Test]
1139+
public void XPathKnowsAboutNodeFiltersForUnmatchedControlNodes() {
1140+
var diff = DiffBuilder.Compare("<Document><Section><Binding /><Binding /><Finding /><Finding /><Finding /><Finding /><Finding /><Finding /><Finding /></Section></Document>")
1141+
.WithTest("<Document><Section><Binding /><Binding /><Finding /><Finding /><Finding /><Finding /><Finding /><Finding /></Section></Document>")
1142+
.IgnoreWhitespace()
1143+
.WithNodeFilter(node => "Document,Section,Finding".Split(',').Contains(node.Name))
1144+
.WithNodeMatcher(new DefaultNodeMatcher(ElementSelectors.ByNameAndText))
1145+
.Build();
1146+
Assert.AreEqual(2, diff.Differences.Count());
1147+
Assert.AreEqual(ComparisonType.CHILD_NODELIST_LENGTH, diff.Differences.First().Comparison.Type);
1148+
Assert.AreEqual("/Document[1]/Section[1]", diff.Differences.First().Comparison.ControlDetails.XPath);
1149+
Assert.AreEqual("/Document[1]/Section[1]", diff.Differences.First().Comparison.TestDetails.XPath);
1150+
Assert.AreEqual(ComparisonType.CHILD_LOOKUP, diff.Differences.Last().Comparison.Type);
1151+
Assert.AreEqual("/Document[1]/Section[1]/Finding[7]", diff.Differences.Last().Comparison.ControlDetails.XPath);
1152+
Assert.IsNull(diff.Differences.Last().Comparison.TestDetails.XPath);
1153+
Assert.AreEqual("/Document[1]/Section[1]", diff.Differences.Last().Comparison.TestDetails.ParentXPath);
1154+
}
1155+
1156+
[Test]
1157+
public void XPathKnowsAboutNodeFiltersForUnmatchedTestNodes() {
1158+
var diff = DiffBuilder.Compare("<Document><Section><Binding /><Binding /><Finding /><Finding /><Finding /><Finding /><Finding /><Finding /></Section></Document>")
1159+
.WithTest("<Document><Section><Binding /><Binding /><Finding /><Finding /><Finding /><Finding /><Finding /><Finding /><Finding /></Section></Document>")
1160+
.IgnoreWhitespace()
1161+
.WithNodeFilter(node => "Document,Section,Finding".Split(',').Contains(node.Name))
1162+
.WithNodeMatcher(new DefaultNodeMatcher(ElementSelectors.ByNameAndText))
1163+
.Build();
1164+
Assert.AreEqual(2, diff.Differences.Count());
1165+
Assert.AreEqual(ComparisonType.CHILD_NODELIST_LENGTH, diff.Differences.First().Comparison.Type);
1166+
Assert.AreEqual("/Document[1]/Section[1]", diff.Differences.First().Comparison.ControlDetails.XPath);
1167+
Assert.AreEqual("/Document[1]/Section[1]", diff.Differences.First().Comparison.TestDetails.XPath);
1168+
Assert.AreEqual(ComparisonType.CHILD_LOOKUP, diff.Differences.Last().Comparison.Type);
1169+
Assert.AreEqual("/Document[1]/Section[1]/Finding[7]", diff.Differences.Last().Comparison.TestDetails.XPath);
1170+
Assert.IsNull(diff.Differences.Last().Comparison.ControlDetails.XPath);
1171+
Assert.AreEqual("/Document[1]/Section[1]", diff.Differences.Last().Comparison.ControlDetails.ParentXPath);
1172+
}
1173+
11381174
private XmlDocument DocumentForString(string s) {
11391175
return Org.XmlUnit.Util.Convert.ToDocument(InputBuilder.FromString(s).Build());
11401176
}

0 commit comments

Comments
 (0)