public void AppendChildren() { List <Element> l = new List <Element>(); l.Add(new Element("foo")); l.Add(new Element("foo")); XPathContext ctx = new XPathContext(); ctx.SetChildren(l); l = new List <Element>(); l.Add(new Element("bar")); l.Add(new Element("foo")); ctx.AppendChildren(l); ctx.NavigateToChild(0); Assert.AreEqual("/foo[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(1); Assert.AreEqual("/foo[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(2); Assert.AreEqual("/bar[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(3); Assert.AreEqual("/foo[3]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); }
public void ElementsAndNs() { List <Element> l = new List <Element>(); l.Add(new Element("foo", "urn:foo:foo")); l.Add(new Element("foo")); l.Add(new Element("foo", "urn:foo:bar")); Dictionary <string, string> m = new Dictionary <string, string>(); m["bar"] = "urn:foo:bar"; XPathContext ctx = new XPathContext(m); ctx.SetChildren(l); ctx.NavigateToChild(0); Assert.AreEqual("/foo[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(1); Assert.AreEqual("/foo[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(2); Assert.AreEqual("/bar:foo[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); }
private Func <ComparisonState> UnmatchedTestNodes(IList <XmlNode> testListForXpath, IList <XmlNode> testList, XPathContext testContext, ICollection <XmlNode> seen, XPathContext controlContext) { return(() => { ComparisonState chain = new OngoingComparisonState(this); int testSize = testList.Count; for (int i = 0; i < testSize; i++) { if (!seen.Contains(testList[i])) { testContext.NavigateToChild(testListForXpath.IndexOf(testList[i])); try { chain = chain .AndThen(new Comparison(ComparisonType.CHILD_LOOKUP, null, null, null, GetXPath(controlContext), testList[i], GetXPath(testContext), testList[i].GetQName(), GetParentXPath(testContext))); } finally { testContext.NavigateToParent(); } } } return chain; }); }
public void ShouldCreateCopyOnClone() { List <Element> l = new List <Element>(); l.Add(new Element("foo")); l.Add(new Element("foo")); l.Add(new Element("bar")); XPathContext ctx = new XPathContext(); ctx.SetChildren(l); ctx.NavigateToChild(1); Assert.AreEqual("/foo[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); XPathContext clone = (XPathContext)ctx.Clone(); Assert.AreEqual("/foo[2]", clone.XPath); Assert.AreEqual("/", clone.ParentXPath); Assert.AreNotSame(ctx, clone); clone.NavigateToParent(); clone.NavigateToChild(2); Assert.AreEqual("/bar[1]", clone.XPath); Assert.AreEqual("/", clone.ParentXPath); Assert.AreEqual("/foo[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); }
/// <summary> /// Compares xsi:type attribute values /// </summary> private ComparisonState CompareXsiType(XmlAttribute control, XPathContext controlContext, XmlAttribute test, XPathContext testContext) { bool mustChangeControlContext = control != null; bool mustChangeTestContext = test != null; if (!mustChangeControlContext && !mustChangeTestContext) { return(new OngoingComparisonState(this)); } bool attributePresentOnBothSides = mustChangeControlContext && mustChangeTestContext; try { XmlQualifiedName controlAttrName = null; if (mustChangeControlContext) { controlAttrName = control.GetQName(); controlContext.AddAttribute(controlAttrName); controlContext.NavigateToAttribute(controlAttrName); } XmlQualifiedName testAttrName = null; if (mustChangeTestContext) { testAttrName = test.GetQName(); testContext.AddAttribute(testAttrName); testContext.NavigateToAttribute(testAttrName); } return(Compare(new Comparison(ComparisonType.ATTR_NAME_LOOKUP, control, GetXPath(controlContext), controlAttrName, GetParentXPath(controlContext), test, GetXPath(testContext), testAttrName, GetParentXPath(testContext))) .AndIfTrueThen(attributePresentOnBothSides, () => CompareAttributeExplicitness(control, controlContext, test, testContext)) .AndIfTrueThen(attributePresentOnBothSides, new Comparison(ComparisonType.ATTR_VALUE, control, GetXPath(controlContext), ValueAsQName(control), GetParentXPath(controlContext), test, GetXPath(testContext), ValueAsQName(test), GetParentXPath(testContext)))); } finally { if (mustChangeControlContext) { controlContext.NavigateToParent(); } if (mustChangeTestContext) { testContext.NavigateToParent(); } } }
/// <summary> /// Matches nodes of two node lists and invokes compareNode on /// each pair. /// </summary> /// <remarks> /// Also performs CHILD_LOOKUP comparisons for each node that /// couldn't be matched to one of the "other" list. /// </remarks> private ComparisonState CompareNodeLists(IEnumerable <XmlNode> allControlChildren, IEnumerable <XmlNode> controlSeq, XPathContext controlContext, IEnumerable <XmlNode> allTestChildren, IEnumerable <XmlNode> testSeq, XPathContext testContext) { ComparisonState chain = new OngoingComparisonState(this); IEnumerable <KeyValuePair <XmlNode, XmlNode> > matches = NodeMatcher.Match(controlSeq, testSeq); IList <XmlNode> controlListForXpath = new List <XmlNode>(allControlChildren); IList <XmlNode> testListForXpath = new List <XmlNode>(allTestChildren); IList <XmlNode> controlList = new List <XmlNode>(controlSeq); IList <XmlNode> testList = new List <XmlNode>(testSeq); ICollection <XmlNode> seen = new HashSet <XmlNode>(); foreach (KeyValuePair <XmlNode, XmlNode> pair in matches) { XmlNode control = pair.Key; seen.Add(control); XmlNode test = pair.Value; seen.Add(test); int controlIndexForXpath = controlListForXpath.IndexOf(control); int testIndexForXpath = testListForXpath.IndexOf(test); int controlIndex = controlList.IndexOf(control); int testIndex = testList.IndexOf(test); controlContext.NavigateToChild(controlIndexForXpath); testContext.NavigateToChild(testIndexForXpath); try { chain = chain.AndThen(new Comparison(ComparisonType.CHILD_NODELIST_SEQUENCE, control, GetXPath(controlContext), controlIndex, GetParentXPath(controlContext), test, GetXPath(testContext), testIndex, GetParentXPath(testContext))) .AndThen(() => CompareNodes(control, controlContext, test, testContext)); } finally { testContext.NavigateToParent(); controlContext.NavigateToParent(); } } return(chain .AndThen(UnmatchedControlNodes(controlListForXpath, controlList, controlContext, seen, testContext)) .AndThen(UnmatchedTestNodes(testListForXpath, testList, testContext, seen, controlContext))); }
public void AttributesAndNs() { Dictionary <string, string> m = new Dictionary <string, string>(); m["bar"] = "urn:foo:bar"; XPathContext ctx = new XPathContext(m); ctx.SetChildren(Linqy.Singleton(new Element("foo", "urn:foo:bar"))); ctx.NavigateToChild(0); List <XmlQualifiedName> l = new List <XmlQualifiedName>(); l.Add(new XmlQualifiedName("baz")); l.Add(new XmlQualifiedName("baz", "urn:foo:bar")); ctx.AddAttributes(l); ctx.NavigateToAttribute(new XmlQualifiedName("baz")); Assert.AreEqual("/bar:foo[1]/@baz", ctx.XPath); Assert.AreEqual("/bar:foo[1]", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToAttribute(new XmlQualifiedName("baz", "urn:foo:bar")); Assert.AreEqual("/bar:foo[1]/@bar:baz", ctx.XPath); Assert.AreEqual("/bar:foo[1]", ctx.ParentXPath); ctx.NavigateToParent(); }
public void Mixed() { List <XPathContext.INodeInfo> l = new List <XPathContext.INodeInfo>(); l.Add(new Text()); l.Add(new Comment()); l.Add(new CDATA()); l.Add(new PI()); l.Add(new CDATA()); l.Add(new Comment()); l.Add(new PI()); l.Add(new Text()); XPathContext ctx = new XPathContext(); ctx.SetChildren(l); ctx.NavigateToChild(0); Assert.AreEqual("/text()[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(1); Assert.AreEqual("/comment()[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(2); Assert.AreEqual("/text()[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(3); Assert.AreEqual("/processing-instruction()[1]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(4); Assert.AreEqual("/text()[3]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(5); Assert.AreEqual("/comment()[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(6); Assert.AreEqual("/processing-instruction()[2]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); ctx.NavigateToParent(); ctx.NavigateToChild(7); Assert.AreEqual("/text()[4]", ctx.XPath); Assert.AreEqual("/", ctx.ParentXPath); }
private Func <ComparisonState> NormalAttributeComparer(XmlElement control, XPathContext controlContext, Attributes controlAttributes, XmlElement test, XPathContext testContext, Attributes testAttributes) { return(() => { ComparisonState chain = new OngoingComparisonState(this); ICollection <XmlAttribute> foundTestAttributes = new HashSet <XmlAttribute>(); foreach (XmlAttribute controlAttr in controlAttributes.RemainingAttributes) { XmlQualifiedName controlAttrName = controlAttr.GetQName(); XmlAttribute testAttr = FindMatchingAttr(testAttributes.RemainingAttributes, controlAttr); XmlQualifiedName testAttrName = testAttr != null ? testAttr.GetQName() : null; controlContext.NavigateToAttribute(controlAttrName); try { chain = chain.AndThen(new Comparison(ComparisonType.ATTR_NAME_LOOKUP, control, GetXPath(controlContext), controlAttrName, GetParentXPath(controlContext), test, GetXPath(testContext), testAttrName, GetParentXPath(testContext))); if (testAttr != null) { testContext.NavigateToAttribute(testAttrName); try { chain = chain.AndThen(() => CompareNodes(controlAttr, controlContext, testAttr, testContext)); foundTestAttributes.Add(testAttr); } finally { testContext.NavigateToParent(); } } } finally { controlContext.NavigateToParent(); } } return chain.AndThen(() => { ComparisonState secondChain = new OngoingComparisonState(this); foreach (XmlAttribute testAttr in testAttributes.RemainingAttributes) { if (!foundTestAttributes.Contains(testAttr)) { XmlQualifiedName testAttrName = testAttr.GetQName(); testContext.NavigateToAttribute(testAttrName); try { secondChain = secondChain .AndThen(new Comparison(ComparisonType.ATTR_NAME_LOOKUP, control, GetXPath(controlContext), null, GetParentXPath(controlContext), test, GetXPath(testContext), testAttrName, GetParentXPath(testContext))); } finally { testContext.NavigateToParent(); } } } return secondChain; }); }); }