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); }
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); }
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); }
internal XPathContext Map(XmlNode n) { XPathContext ctx = (XPathContext)xpathContext.Clone(); ctx.NavigateToChild(childIndex[n]); return(ctx); }
public void singleAttribute() { XPathContext ctx = new XPathContext(); ctx.SetChildren(Linqy.Singleton(new Element("foo"))); ctx.NavigateToChild(0); ctx.AddAttribute(new XmlQualifiedName("bar")); ctx.NavigateToAttribute(new XmlQualifiedName("bar")); Assert.AreEqual("/foo[1]/@bar", ctx.XPath); Assert.AreEqual("/foo[1]", ctx.ParentXPath); }
/// <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 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); }
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(); }