Skip to content

Commit d9d6ae3

Browse files
committed
port fix for xmlunit/#156 to XMLUnit.Net
1 parent d60d352 commit d9d6ae3

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

RELEASE_NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
[xmlunit/#154](https://github.com/xmlunit/xmlunit/pull/154) by
88
[@NathanAtClarity](https://github.com/NathanAtClarity).
99

10+
* the XPath values of a comparison should not be affected by any
11+
`NodeFilter` being in effect.
12+
Issue similar to [xmlunit/#156](https://github.com/xmlunit/xmlunit/issues/156)
13+
1014
## XMLUnit.NET 2.7.0 - /Released 2019-04-13/
1115

1216
This release is identical to 2.7.0-beta-01 with only the version

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ internal ComparisonState CompareNodes(XmlNode control,
6363
XPathContext controlContext,
6464
XmlNode test,
6565
XPathContext testContext) {
66+
IEnumerable<XmlNode> allControlChildren =
67+
control.ChildNodes.Cast<XmlNode>();
6668
IEnumerable<XmlNode> controlChildren =
67-
control.ChildNodes.Cast<XmlNode>().Where(n => NodeFilter(n));
69+
allControlChildren.Where(n => NodeFilter(n));
70+
IEnumerable<XmlNode> allTestChildren =
71+
test.ChildNodes.Cast<XmlNode>();
6872
IEnumerable<XmlNode> testChildren =
69-
test.ChildNodes.Cast<XmlNode>().Where(n => NodeFilter(n));
73+
allTestChildren.Where(n => NodeFilter(n));
7074

7175
return Compare(new Comparison(ComparisonType.NODE_TYPE,
7276
control, GetXPath(controlContext),
@@ -94,8 +98,10 @@ internal ComparisonState CompareNodes(XmlNode control,
9498
// and finally recurse into children
9599
.AndIfTrueThen(control.NodeType != XmlNodeType.Attribute,
96100
CompareChildren(controlContext,
101+
allControlChildren,
97102
controlChildren,
98103
testContext,
104+
allTestChildren,
99105
testChildren));
100106
}
101107

@@ -163,19 +169,21 @@ private ComparisonState NodeTypeSpecificComparison(XmlNode control,
163169
}
164170

165171
private Func<ComparisonState> CompareChildren(XPathContext controlContext,
172+
IEnumerable<XmlNode> allControlChildren,
166173
IEnumerable<XmlNode> controlChildren,
167174
XPathContext testContext,
175+
IEnumerable<XmlNode> allTestChildren,
168176
IEnumerable<XmlNode> testChildren) {
169177

170178
return () => {
171179
controlContext
172-
.SetChildren(controlChildren.Select<XmlNode, XPathContext.INodeInfo>
180+
.SetChildren(allControlChildren.Select<XmlNode, XPathContext.INodeInfo>
173181
(ElementSelectors.TO_NODE_INFO));
174182
testContext
175-
.SetChildren(testChildren.Select<XmlNode, XPathContext.INodeInfo>
183+
.SetChildren(allTestChildren.Select<XmlNode, XPathContext.INodeInfo>
176184
(ElementSelectors.TO_NODE_INFO));
177-
return CompareNodeLists(controlChildren, controlContext,
178-
testChildren, testContext);
185+
return CompareNodeLists(allControlChildren, controlChildren, controlContext,
186+
allTestChildren, testChildren, testContext);
179187
};
180188
}
181189

@@ -449,15 +457,19 @@ private ComparisonState CompareProcessingInstructions(XmlProcessingInstruction c
449457
/// Also performs CHILD_LOOKUP comparisons for each node that
450458
/// couldn't be matched to one of the "other" list.
451459
/// </remarks>
452-
private ComparisonState CompareNodeLists(IEnumerable<XmlNode> controlSeq,
460+
private ComparisonState CompareNodeLists(IEnumerable<XmlNode> allControlChildren,
461+
IEnumerable<XmlNode> controlSeq,
453462
XPathContext controlContext,
463+
IEnumerable<XmlNode> allTestChildren,
454464
IEnumerable<XmlNode> testSeq,
455465
XPathContext testContext) {
456466

457467
ComparisonState chain = new OngoingComparisonState(this);
458468

459469
IEnumerable<KeyValuePair<XmlNode, XmlNode>> matches =
460470
NodeMatcher.Match(controlSeq, testSeq);
471+
IList<XmlNode> controlListForXpath = new List<XmlNode>(allControlChildren);
472+
IList<XmlNode> testListForXpath = new List<XmlNode>(allTestChildren);
461473
IList<XmlNode> controlList = new List<XmlNode>(controlSeq);
462474
IList<XmlNode> testList = new List<XmlNode>(testSeq);
463475
ICollection<XmlNode> seen = new HashSet<XmlNode>();
@@ -466,10 +478,12 @@ private ComparisonState CompareNodeLists(IEnumerable<XmlNode> controlSeq,
466478
seen.Add(control);
467479
XmlNode test = pair.Value;
468480
seen.Add(test);
481+
int controlIndexForXpath = controlListForXpath.IndexOf(control);
482+
int testIndexForXpath = testListForXpath.IndexOf(test);
469483
int controlIndex = controlList.IndexOf(control);
470484
int testIndex = testList.IndexOf(test);
471-
controlContext.NavigateToChild(controlIndex);
472-
testContext.NavigateToChild(testIndex);
485+
controlContext.NavigateToChild(controlIndexForXpath);
486+
testContext.NavigateToChild(testIndexForXpath);
473487
try {
474488
chain =
475489
chain.AndThen(new Comparison(ComparisonType.CHILD_NODELIST_SEQUENCE,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,9 @@ public void SourceforgeForumThread92c980ec5b() {
776776
d.ComparisonController = ComparisonControllers.StopWhenDifferent;
777777
d.DifferenceEvaluator = ev;
778778
d.NodeMatcher = new DefaultNodeMatcher(ElementSelectors.ByNameAndAllAttributes);
779+
d.NodeFilter = n =>
780+
n.NodeType != XmlNodeType.DocumentType &&
781+
!("parent" == n.Name && "0" == n.Attributes["id"].Value);
779782
Assert.AreEqual(WrapAndStop(ComparisonResult.DIFFERENT),
780783
d.CompareNodes(gp1, new XPathContext(gp1),
781784
gp2, new XPathContext(gp2)));

0 commit comments

Comments
 (0)