public void TestReplace() { var s = TestHelper.ParseString("(node {attr (node) attr2 -2} [(node) -1 (node)])"); var n = s as Node; var matches = s.Find("/+/node"); Assert.AreEqual(3, matches.Count); var replaceWith = new NumberLiteral(0, 0); var s2 = s.Replace("/+/node", replaceWith); Assert.AreEqual(s, s2); TestHelper.AssertNumberLiteral(n.Attributes["attr"] as NumberLiteral, 0, 0); Assert.AreNotEqual(replaceWith, n.Attributes["attr"]); TestHelper.AssertNumberLiteral(n.Attributes["attr2"] as NumberLiteral, -2, 0); TestHelper.AssertNumberLiteral(n.Children[0] as NumberLiteral, 0, 0); Assert.AreNotEqual(replaceWith, n.Children[0]); TestHelper.AssertNumberLiteral(n.Children[1] as NumberLiteral, -1, 0); TestHelper.AssertNumberLiteral(n.Children[2] as NumberLiteral, 0, 0); Assert.AreNotEqual(replaceWith, n.Children[2]); var s3 = s2.Replace("^number[<=0]", new NullLiteral()); Assert.AreEqual(s, s3); Assert.AreEqual(typeof(NullLiteral), n.Attributes["attr"].GetType()); Assert.AreEqual(typeof(NullLiteral), n.Attributes["attr2"].GetType()); Assert.AreEqual(typeof(NullLiteral), n.Children[0].GetType()); Assert.AreEqual(typeof(NullLiteral), n.Children[1].GetType()); Assert.AreEqual(typeof(NullLiteral), n.Children[2].GetType()); replaceWith = new NumberLiteral(1337, 0); var s4 = s3.Replace("*", replaceWith); Assert.AreEqual(replaceWith, s4); }
public void TestAbsoluteAndRelativePaths() { var s = TestHelper.ParseString("(node {attr (node 1)} [3.7 (subnode 4) (node 2)])"); var n = s as Node; var matches = s.Find("/node"); Assert.AreEqual(1, matches.Count); Assert.AreEqual(s, matches[0].Value); TestHelper.AssertNodeAndFirstNumberLiteralChild(n, "node", 1, 3, 3, 7); matches = s.Find("node"); Assert.AreEqual(3, matches.Count); Assert.AreEqual(s, matches[0].Value); Assert.AreEqual(n.Children[2] as Node, matches[1].Value); Assert.AreEqual(n.Attributes["attr"] as Node, matches[2].Value); Assert.AreEqual("/node/node#2", matches[1].Path); Assert.AreEqual(matches[0], matches[1].Parent); TestHelper.AssertNodeAndFirstNumberLiteralChild(matches[1].Value as Node, "node", 0, 1, 2, 0); Assert.AreEqual("/node/@attr", matches[2].Path); Assert.AreEqual(matches[0], matches[2].Parent); TestHelper.AssertNodeAndFirstNumberLiteralChild(matches[2].Value as Node, "node", 0, 1, 1, 0); matches = s.Find("/node/@attr"); Assert.AreEqual(1, matches.Count); Assert.AreEqual(n.Attributes["attr"] as Node, matches[0].Value); matches = s.Find("node/^number"); Assert.AreEqual(3, matches.Count); Assert.AreEqual("/node/#0", matches[0].Path); TestHelper.AssertNumberLiteral(matches[0].Value as NumberLiteral, 3, 7); Assert.AreEqual("/node/node#2/#0", matches[1].Path); TestHelper.AssertNumberLiteral(matches[1].Value as NumberLiteral, 2, 0); Assert.AreEqual("/node/@attr/#0", matches[2].Path); TestHelper.AssertNumberLiteral(matches[2].Value as NumberLiteral, 1, 0); matches = s.Find("node/#1"); Assert.AreEqual(1, matches.Count); Assert.AreEqual("/node/subnode#1", matches[0].Path); TestHelper.AssertNodeAndFirstNumberLiteralChild(matches[0].Value as Node, "subnode", 0, 1, 4, 0); matches = s.Find("^node"); Assert.AreEqual(4, matches.Count); matches = s.Find("[has_child(subnode)]"); Assert.AreEqual(1, matches.Count); Assert.AreEqual(s, matches[0].Value); matches = s.Find("[has_attr(attr)]"); Assert.AreEqual(1, matches.Count); Assert.AreEqual(s, matches[0].Value); }