summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2009-11-03 19:55:15 +0000
committerarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2009-11-03 19:55:15 +0000
commitaf04c0f754fba9a3cac75b0b0f504a895500e83c (patch)
tree942d5c5b8ff864292794eb6d9a34058ef5bbbf63 /tests
parent050a633009d91f74e58a36f28b4de515b48c4341 (diff)
tests: Added tests for all axes except namespace
git-svn-id: http://pugixml.googlecode.com/svn/trunk@201 99668b35-9821-0410-8761-19e4c4f06640
Diffstat (limited to 'tests')
-rw-r--r--tests/test.hpp4
-rw-r--r--tests/test_xpath_paths.cpp185
2 files changed, 187 insertions, 2 deletions
diff --git a/tests/test.hpp b/tests/test.hpp
index a4e80df..3071a47 100644
--- a/tests/test.hpp
+++ b/tests/test.hpp
@@ -146,7 +146,7 @@ struct xpath_node_set_tester
// check document order
pugi::xpath_node node = result.begin()[last];
unsigned int order = node.attribute() ? node.attribute().document_order() : node.node().document_order();
-
+
check(order == expected);
// continue to the next element
@@ -221,7 +221,7 @@ struct dummy_fixture {};
#define CHECK_XPATH_NUMBER(node, query, expected) CHECK_TEXT(test_xpath_number(node, query, expected), STR(query) " does not evaluate to " STR(expected) " in context " STR(node))
#define CHECK_XPATH_NUMBER_NAN(node, query) CHECK_TEXT(test_xpath_number_nan(node, query), STR(query) " does not evaluate to NaN in context " STR(node))
#define CHECK_XPATH_FAIL(query) CHECK_TEXT(test_xpath_fail_compile(query), STR(query) " should not compile")
-#define CHECK_XPATH_NODESET(node, query) xpath_node_set_tester(node, query, STR(query) " does not evaluate to expected set in context " STR(node))
+#define CHECK_XPATH_NODESET(node, query) xpath_node_set_tester(node, query, CHECK_JOIN2(STR(query) " does not evaluate to expected set in context " STR(node), " at "__FILE__ ":", __LINE__))
#endif
#endif
diff --git a/tests/test_xpath_paths.cpp b/tests/test_xpath_paths.cpp
new file mode 100644
index 0000000..31704df
--- /dev/null
+++ b/tests/test_xpath_paths.cpp
@@ -0,0 +1,185 @@
+#ifndef PUGIXML_NO_XPATH
+
+#include "common.hpp"
+
+TEST_XML(xpath_paths_axes_child, "<node attr='value'><child attr='value'><subchild/></child><another/><last/></node>")
+{
+ doc.precompute_document_order();
+
+ xml_node c;
+ xml_node n = doc.child("node");
+
+ CHECK_XPATH_NODESET(c, "child:: node()");
+
+ CHECK_XPATH_NODESET(n, "child:: node()") % 4 % 7 % 8; // child, another, last
+ CHECK_XPATH_NODESET(n, "another/child:: node()");
+}
+
+TEST_XML(xpath_paths_axes_descendant, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
+{
+ doc.precompute_document_order();
+
+ xml_node c;
+ xml_node n = doc.child("node");
+
+ CHECK_XPATH_NODESET(c, "descendant:: node()");
+
+ CHECK_XPATH_NODESET(n, "descendant:: node()") % 4 % 6 % 7 % 8 % 9; // child, subchild, another, subchild, last
+ CHECK_XPATH_NODESET(doc, "descendant:: node()") % 2 % 4 % 6 % 7 % 8 % 9; // node, child, subchild, another, subchild, last
+ CHECK_XPATH_NODESET(n, "another/descendant:: node()") % 8; // subchild
+ CHECK_XPATH_NODESET(n, "last/descendant:: node()");
+}
+
+TEST_XML(xpath_paths_axes_parent, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
+{
+ doc.precompute_document_order();
+
+ xml_node c;
+ xml_node n = doc.child("node");
+
+ CHECK_XPATH_NODESET(c, "parent:: node()");
+
+ CHECK_XPATH_NODESET(n.child("child"), "parent:: node()") % 2; // node
+ CHECK_XPATH_NODESET(n, "child/subchild/parent:: node()") % 4; // child
+ CHECK_XPATH_NODESET(n, "@attr/parent:: node()") % 2; // node
+ CHECK_XPATH_NODESET(n, "parent:: node()") % 1; // root
+ CHECK_XPATH_NODESET(doc, "parent:: node()");
+}
+
+TEST_XML(xpath_paths_axes_ancestor, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
+{
+ doc.precompute_document_order();
+
+ xml_node c;
+ xml_node n = doc.child("node");
+
+ CHECK_XPATH_NODESET(c, "ancestor:: node()");
+
+ CHECK_XPATH_NODESET(n.child("child"), "ancestor:: node()") % 2 % 1; // node, root
+ CHECK_XPATH_NODESET(n, "child/subchild/ancestor:: node()") % 4 % 2 % 1; // child, node, root
+ CHECK_XPATH_NODESET(n, "child/@attr/ancestor:: node()") % 4 % 2 % 1; // child, node, root
+ CHECK_XPATH_NODESET(n, "ancestor:: node()") % 1; // root
+ CHECK_XPATH_NODESET(doc, "ancestor:: node()");
+}
+
+TEST_XML(xpath_paths_axes_following_sibling, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
+{
+ doc.precompute_document_order();
+
+ xml_node c;
+ xml_node n = doc.child("node");
+
+ CHECK_XPATH_NODESET(c, "following-sibling:: node()");
+
+ CHECK_XPATH_NODESET(n.child("child"), "following-sibling:: node()") % 8 % 10; // another, last
+ CHECK_XPATH_NODESET(n.child("last"), "following-sibling:: node()");
+ CHECK_XPATH_NODESET(n, "@attr1/following-sibling:: node()"); // attributes are not siblings
+}
+
+TEST_XML(xpath_paths_axes_preceding_sibling, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
+{
+ doc.precompute_document_order();
+
+ xml_node c;
+ xml_node n = doc.child("node");
+
+ CHECK_XPATH_NODESET(c, "preceding-sibling:: node()");
+
+ CHECK_XPATH_NODESET(n.child("child"), "preceding-sibling:: node()");
+ CHECK_XPATH_NODESET(n.child("last"), "preceding-sibling:: node()") % 8 % 5; // another, child
+ CHECK_XPATH_NODESET(n, "@attr2/following-sibling:: node()"); // attributes are not siblings
+}
+
+TEST_XML(xpath_paths_axes_following, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
+{
+ doc.precompute_document_order();
+
+ xml_node c;
+ xml_node n = doc.child("node");
+
+ CHECK_XPATH_NODESET(c, "following:: node()");
+
+ CHECK_XPATH_NODESET(n, "following:: node()"); // no descendants
+ CHECK_XPATH_NODESET(n.child("child"), "following:: node()") % 8 % 9 % 10; // another, subchild, last
+ CHECK_XPATH_NODESET(n.child("child").child("subchild"), "following:: node()") % 8 % 9 % 10; // another, subchild, last
+ CHECK_XPATH_NODESET(n.child("last"), "following:: node()");
+}
+
+TEST_XML(xpath_paths_axes_preceding, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
+{
+ doc.precompute_document_order();
+
+ xml_node c;
+ xml_node n = doc.child("node");
+
+ CHECK_XPATH_NODESET(c, "preceding:: node()");
+
+ CHECK_XPATH_NODESET(n.child("child"), "preceding:: node()"); // no ancestors
+ CHECK_XPATH_NODESET(n.child("last"), "preceding:: node()") % 9 % 8 % 7 % 5; // subchild, another, subchild, child
+ CHECK_XPATH_NODESET(n.child("another").child("subchild"), "preceding:: node()") % 7 % 5; // subchild, child
+ CHECK_XPATH_NODESET(n, "preceding:: node()");
+}
+
+TEST_XML(xpath_paths_axes_attribute, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another xmlns:foo='bar'><subchild/></another><last/></node>")
+{
+ doc.precompute_document_order();
+
+ xml_node c;
+ xml_node n = doc.child("node");
+
+ CHECK_XPATH_NODESET(c, "attribute:: node()");
+
+ CHECK_XPATH_NODESET(n.child("child"), "attribute:: node()") % 6; // child/@attr
+ CHECK_XPATH_NODESET(n.child("last"), "attribute:: node()");
+ CHECK_XPATH_NODESET(n, "attribute:: node()") % 3 % 4; // node/@attr1 node/@attr2
+ CHECK_XPATH_NODESET(doc, "descendant-or-self:: node()/attribute:: node()") % 3 % 4 % 6; // all attributes
+ CHECK_XPATH_NODESET(n.child("another"), "attribute:: node()"); // namespace nodes are not attributes
+}
+
+TEST_XML(xpath_paths_axes_self, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
+{
+ doc.precompute_document_order();
+
+ xml_node c;
+ xml_node n = doc.child("node");
+
+ CHECK_XPATH_NODESET(c, "self:: node()");
+
+ CHECK_XPATH_NODESET(n.child("child"), "self:: node()") % 4; // child
+ CHECK_XPATH_NODESET(n, "self:: node()") % 2; // node
+ CHECK_XPATH_NODESET(n, "child/@attr/self:: node()") % 5; // @attr
+ CHECK_XPATH_NODESET(doc, "self:: node()") % 1; // root
+}
+
+TEST_XML(xpath_paths_axes_descendant_or_self, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
+{
+ doc.precompute_document_order();
+
+ xml_node c;
+ xml_node n = doc.child("node");
+
+ CHECK_XPATH_NODESET(c, "descendant-or-self:: node()");
+
+ CHECK_XPATH_NODESET(n, "descendant-or-self:: node()") % 2 % 4 % 6 % 7 % 8 % 9; // node, child, subchild, another, subchild, last
+ CHECK_XPATH_NODESET(doc, "descendant-or-self:: node()") % 1 % 2 % 4 % 6 % 7 % 8 % 9; // root, node, child, subchild, another, subchild, last
+ CHECK_XPATH_NODESET(n, "another/descendant-or-self:: node()") % 7 % 8; // another, subchild
+ CHECK_XPATH_NODESET(n, "last/descendant-or-self:: node()") % 9; // last
+}
+
+TEST_XML(xpath_paths_axes_ancestor_or_self, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>")
+{
+ doc.precompute_document_order();
+
+ xml_node c;
+ xml_node n = doc.child("node");
+
+ CHECK_XPATH_NODESET(c, "ancestor-or-self:: node()");
+
+ CHECK_XPATH_NODESET(n.child("child"), "ancestor-or-self:: node()") % 4 % 2 % 1; // child, node, root
+ CHECK_XPATH_NODESET(n, "child/subchild/ancestor-or-self:: node()") % 6 % 4 % 2 % 1; // subchild, child, node, root
+ CHECK_XPATH_NODESET(n, "child/@attr/ancestor-or-self:: node()") % 5 % 4 % 2 % 1; // @attr, child, node, root
+ CHECK_XPATH_NODESET(n, "ancestor-or-self:: node()") % 2 % 1; // root, node
+ CHECK_XPATH_NODESET(doc, "ancestor-or-self:: node()") % 1; // root
+}
+
+#endif