コード例 #1
0
    public void MaxDepthNavigator()
    {
        var source = new TestSource5();
        var nav    = new NavigableNavigator(source, maxDepth: 1);

        // go to /root
        Assert.IsTrue(nav.MoveToFirstChild());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(-1, (nav.UnderlyingObject as TestContent).Id);

        // go to /root/1
        Assert.IsTrue(nav.MoveToFirstChild());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(1, (nav.UnderlyingObject as TestContent).Id);
        Assert.IsTrue(nav.MoveToId("1"));
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(1, (nav.UnderlyingObject as TestContent).Id);

        // go to /root/1/prop1
        Assert.IsTrue(nav.MoveToFirstChild());

        // go to /root/1/prop2
        Assert.IsTrue(nav.MoveToNext());

        // can't go to /root/1/3
        Assert.IsFalse(nav.MoveToNext());
        Assert.IsFalse(nav.MoveToId("3"));

        //// Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        //// Assert.AreEqual(3, (nav.UnderlyingObject as TestContent).Id);
    }
コード例 #2
0
    public void RootedNavigator1()
    {
        var source = new TestSource5();
        var nav    = new NavigableNavigator(source, 1);

        // go to (/root) /1
        Assert.IsTrue(nav.MoveToFirstChild());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(1, (nav.UnderlyingObject as TestContent).Id);

        // go to (/root) /1/prop1
        Assert.IsTrue(nav.MoveToFirstChild());

        // go to (/root) /1/prop2
        Assert.IsTrue(nav.MoveToNext());

        // go to (/root) /1/3
        Assert.IsTrue(nav.MoveToNext());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(3, (nav.UnderlyingObject as TestContent).Id);

        // go to (/root) /1
        Assert.IsTrue(nav.MoveToParent());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(1, (nav.UnderlyingObject as TestContent).Id);

        // go to (/root) ie root
        Assert.IsTrue(nav.MoveToParent());
        Assert.AreEqual(NavigableNavigator.StatePosition.Root, nav.InternalState.Position);
        Assert.IsFalse(nav.MoveToParent());

        // can't go there
        Assert.IsFalse(nav.MoveToId("2"));
    }
コード例 #3
0
    public void SelectByIdWithVariable(int id)
    {
        var source = new TestSource5();
        var nav    = new NavigableNavigator(source);

        var iter = nav.Select("//* [@id=$id]", new XPathVariable("id", id.ToString(CultureInfo.InvariantCulture)));

        Assert.IsTrue(iter.MoveNext());
        var current = iter.Current as NavigableNavigator;

        Assert.AreEqual(NavigableNavigator.StatePosition.Element, current.InternalState.Position);
        Assert.AreEqual(id, (current.UnderlyingObject as TestContent).Id);
    }
コード例 #4
0
    public void SelectById(int id)
    {
        var source = new TestSource5();
        var nav    = new NavigableNavigator(source);

        var iter = nav.Select(string.Format("//* [@id={0}]", id));

        Assert.IsTrue(iter.MoveNext());
        var current = iter.Current as NavigableNavigator;

        Assert.AreEqual(NavigableNavigator.StatePosition.Element, current.InternalState.Position);
        Assert.AreEqual(id, (current.UnderlyingObject as TestContent).Id);
    }
コード例 #5
0
    public void CloneIsSafe()
    {
        var source = new TestSource5();
        var nav    = new NavigableNavigator(source);

        Assert.AreEqual(NavigableNavigator.StatePosition.Root, nav.InternalState.Position);
        Assert.IsTrue(nav.MoveToFirstChild());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual("root", nav.Name); // at -1
        Assert.IsTrue(nav.MoveToFirstChild());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(1, (nav.UnderlyingObject as TestContent).Id);
        Assert.IsTrue(nav.MoveToFirstChild());
        Assert.AreEqual(NavigableNavigator.StatePosition.PropertyElement, nav.InternalState.Position);
        Assert.AreEqual("prop1", nav.Name);
        Assert.IsTrue(nav.MoveToNext());
        Assert.AreEqual(NavigableNavigator.StatePosition.PropertyElement, nav.InternalState.Position);
        Assert.AreEqual("prop2", nav.Name);
        Assert.IsTrue(nav.MoveToNext());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(3, (nav.UnderlyingObject as TestContent).Id);

        // at that point nav is at /root/1/3
        var clone = nav.Clone() as NavigableNavigator;

        // move nav to /root/1/5 and ensure that clone stays at /root/1/3
        Assert.IsTrue(nav.MoveToNext());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(5, (nav.UnderlyingObject as TestContent).Id);
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, clone.InternalState.Position);
        Assert.AreEqual(3, (clone.UnderlyingObject as TestContent).Id);

        // move nav to /root/2
        Assert.IsTrue(nav.MoveToParent());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(1, (nav.UnderlyingObject as TestContent).Id);
        Assert.IsTrue(nav.MoveToNext());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(2, (nav.UnderlyingObject as TestContent).Id);
        Assert.IsTrue(nav.MoveToFirstChild());
        Assert.AreEqual(NavigableNavigator.StatePosition.PropertyElement, nav.InternalState.Position);
        Assert.AreEqual("prop1", nav.Name);
        Assert.AreEqual("p21", nav.Value);

        // move clone to .. /root/1
        Assert.IsTrue(clone.MoveToParent());

        // clone has not been corrupted by nav
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, clone.InternalState.Position);
        Assert.AreEqual(1, (clone.UnderlyingObject as TestContent).Id);
    }
コード例 #6
0
    public void NewNavigatorIsAtRoot()
    {
        const string xml = @"<root><item1 /><item2 /></root>";
        var          doc = XmlHelper.CreateXPathDocument(xml);
        var          nav = doc.CreateNavigator();

        Assert.AreEqual(XPathNodeType.Root, nav.NodeType);
        Assert.AreEqual(string.Empty, nav.Name);

        var source = new TestSource5();

        nav = new NavigableNavigator(source);

        Assert.AreEqual(XPathNodeType.Root, nav.NodeType);
        Assert.AreEqual(string.Empty, nav.Name);
    }
コード例 #7
0
        public void RootedNavigator2()
        {
            var source = new TestSource5();
            var nav    = new NavigableNavigator(source, rootId: 3);

            // go to (/root/1) /3
            Assert.IsTrue(nav.MoveToFirstChild());
            Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
            Assert.AreEqual(3, (nav.UnderlyingObject as TestContent).Id);

            // go to (/root/1)
            Assert.IsTrue(nav.MoveToParent());
            Assert.AreEqual(NavigableNavigator.StatePosition.Root, nav.InternalState.Position);
            Assert.IsFalse(nav.MoveToParent());

            // can't go there
            Assert.IsFalse(nav.MoveToId("1"));
        }
コード例 #8
0
    public void MoveToId()
    {
        var source = new TestSource5();
        var nav    = new NavigableNavigator(source);

        // move to /root/1/3
        Assert.IsTrue(nav.MoveToId("3"));
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(3, (nav.UnderlyingObject as TestContent).Id);

        // move to /root/1
        Assert.IsTrue(nav.MoveToParent());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(1, (nav.UnderlyingObject as TestContent).Id);

        // move to /root
        Assert.IsTrue(nav.MoveToParent());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(-1, (nav.UnderlyingObject as TestContent).Id);

        // move up
        Assert.IsTrue(nav.MoveToParent());
        Assert.AreEqual(NavigableNavigator.StatePosition.Root, nav.InternalState.Position);
        Assert.IsFalse(nav.MoveToParent());

        // move to /root/1
        Assert.IsTrue(nav.MoveToId("1"));
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(1, (nav.UnderlyingObject as TestContent).Id);

        // move to /root
        Assert.IsTrue(nav.MoveToParent());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(-1, (nav.UnderlyingObject as TestContent).Id);

        // move up
        Assert.IsTrue(nav.MoveToParent());
        Assert.AreEqual(NavigableNavigator.StatePosition.Root, nav.InternalState.Position);
        Assert.IsFalse(nav.MoveToParent());

        // move to /root
        Assert.IsTrue(nav.MoveToId("-1"));
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(-1, (nav.UnderlyingObject as TestContent).Id);

        // move up
        Assert.IsTrue(nav.MoveToParent());
        Assert.AreEqual(NavigableNavigator.StatePosition.Root, nav.InternalState.Position);
        Assert.IsFalse(nav.MoveToParent());

        // move to /root
        Assert.IsTrue(nav.MoveToId("-1"));
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(-1, (nav.UnderlyingObject as TestContent).Id);

        // move down
        Assert.IsTrue(nav.MoveToFirstChild());
        Assert.AreEqual(NavigableNavigator.StatePosition.Element, nav.InternalState.Position);
        Assert.AreEqual(1, (nav.UnderlyingObject as TestContent).Id);

        // get lost
        Assert.IsFalse(nav.MoveToId("666"));
    }