diff options
author | arseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640> | 2010-08-29 15:27:51 +0000 |
---|---|---|
committer | arseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640> | 2010-08-29 15:27:51 +0000 |
commit | e08c065820d734d4f5768470ed6a4f123cbb56ef (patch) | |
tree | c28a4c58b36cd0cd48841617a6a6fc3f8ce350df /tests | |
parent | 7554e206885a3f3aa1a771f0d4ca1d4a921994ad (diff) |
tests: Added XPath tests for attribute context
git-svn-id: http://pugixml.googlecode.com/svn/trunk@664 99668b35-9821-0410-8761-19e4c4f06640
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test.cpp | 8 | ||||
-rw-r--r-- | tests/test.hpp | 10 | ||||
-rw-r--r-- | tests/test_xpath_api.cpp | 21 | ||||
-rw-r--r-- | tests/test_xpath_paths.cpp | 36 |
4 files changed, 63 insertions, 12 deletions
diff --git a/tests/test.cpp b/tests/test.cpp index 96a81e4..9435f1e 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -54,7 +54,7 @@ bool test_node(const pugi::xml_node& node, const pugi::char_t* contents, const p } #ifndef PUGIXML_NO_XPATH -bool test_xpath_string(const pugi::xml_node& node, const pugi::char_t* query, const pugi::char_t* expected) +bool test_xpath_string(const pugi::xpath_node& node, const pugi::char_t* query, const pugi::char_t* expected) { pugi::xpath_query q(query); @@ -70,14 +70,14 @@ bool test_xpath_string(const pugi::xml_node& node, const pugi::char_t* query, co return q.evaluate_string(&buffer[0], size, node) == size && test_string_equal(buffer.c_str(), expected); } -bool test_xpath_boolean(const pugi::xml_node& node, const pugi::char_t* query, bool expected) +bool test_xpath_boolean(const pugi::xpath_node& node, const pugi::char_t* query, bool expected) { pugi::xpath_query q(query); return q.evaluate_boolean(node) == expected; } -bool test_xpath_number(const pugi::xml_node& node, const pugi::char_t* query, double expected) +bool test_xpath_number(const pugi::xpath_node& node, const pugi::char_t* query, double expected) { pugi::xpath_query q(query); @@ -88,7 +88,7 @@ bool test_xpath_number(const pugi::xml_node& node, const pugi::char_t* query, do return absolute_error < tolerance || absolute_error < fabs(expected) * tolerance; } -bool test_xpath_number_nan(const pugi::xml_node& node, const pugi::char_t* query) +bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::char_t* query) { pugi::xpath_query q(query); diff --git a/tests/test.hpp b/tests/test.hpp index 3308b1e..4127697 100644 --- a/tests/test.hpp +++ b/tests/test.hpp @@ -37,10 +37,10 @@ template <typename Node> inline bool test_node_name_value(const Node& node, cons bool test_node(const pugi::xml_node& node, const pugi::char_t* contents, const pugi::char_t* indent, unsigned int flags); #ifndef PUGIXML_NO_XPATH -bool test_xpath_string(const pugi::xml_node& node, const pugi::char_t* query, const pugi::char_t* expected); -bool test_xpath_boolean(const pugi::xml_node& node, const pugi::char_t* query, bool expected); -bool test_xpath_number(const pugi::xml_node& node, const pugi::char_t* query, double expected); -bool test_xpath_number_nan(const pugi::xml_node& node, const pugi::char_t* query); +bool test_xpath_string(const pugi::xpath_node& node, const pugi::char_t* query, const pugi::char_t* expected); +bool test_xpath_boolean(const pugi::xpath_node& node, const pugi::char_t* query, bool expected); +bool test_xpath_number(const pugi::xpath_node& node, const pugi::char_t* query, double expected); +bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::char_t* query); bool test_xpath_fail_compile(const pugi::char_t* query); struct xpath_node_set_tester @@ -126,7 +126,7 @@ struct dummy_fixture {}; #define CHECK_XPATH_NUMBER(node, query, expected) CHECK_TEXT(test_xpath_number(node, query, expected), STRINGIZE(query) " does not evaluate to " STRINGIZE(expected) " in context " STRINGIZE(node)) #define CHECK_XPATH_NUMBER_NAN(node, query) CHECK_TEXT(test_xpath_number_nan(node, query), STRINGIZE(query) " does not evaluate to NaN in context " STRINGIZE(node)) #define CHECK_XPATH_FAIL(query) CHECK_TEXT(test_xpath_fail_compile(query), STRINGIZE(query) " should not compile") -#define CHECK_XPATH_NODESET(node, query) xpath_node_set_tester(node.select_nodes(query), CHECK_JOIN2(STRINGIZE(query) " does not evaluate to expected set in context " STRINGIZE(node), " at "__FILE__ ":", __LINE__)) +#define CHECK_XPATH_NODESET(node, query) xpath_node_set_tester(xpath_query(query).evaluate_node_set(node), CHECK_JOIN2(STRINGIZE(query) " does not evaluate to expected set in context " STRINGIZE(node), " at "__FILE__ ":", __LINE__)) #endif #define STR(text) PUGIXML_TEXT(text) diff --git a/tests/test_xpath_api.cpp b/tests/test_xpath_api.cpp index 8fc4d94..b91910a 100644 --- a/tests/test_xpath_api.cpp +++ b/tests/test_xpath_api.cpp @@ -147,6 +147,25 @@ TEST_XML(xpath_api_evaluate, "<node attr='3'/>") CHECK(ns.size() == 1 && ns[0].attribute() == doc.child(STR("node")).attribute(STR("attr"))); } +TEST_XML(xpath_api_evaluate_attr, "<node attr='3'/>") +{ + xpath_query q(STR(".")); + xpath_node n(doc.child(STR("node")).attribute(STR("attr")), doc.child(STR("node"))); + + CHECK(q.evaluate_boolean(n)); + CHECK(q.evaluate_number(n) == 3); + + char_t string[3]; + CHECK(q.evaluate_string(string, 3, n) == 2 && string[0] == '3' && string[1] == 0); + +#ifndef PUGIXML_NO_STL + CHECK(q.evaluate_string(n) == STR("3")); +#endif + + xpath_node_set ns = q.evaluate_node_set(n); + CHECK(ns.size() == 1 && ns[0] == n); +} + #ifdef PUGIXML_NO_EXCEPTIONS TEST_XML(xpath_api_evaluate_fail, "<node attr='3'/>") { @@ -288,6 +307,4 @@ TEST(xpath_api_exception_what) } #endif -// $$$ -// out of memory during parsing/execution (?) #endif diff --git a/tests/test_xpath_paths.cpp b/tests/test_xpath_paths.cpp index 2799f40..829cac7 100644 --- a/tests/test_xpath_paths.cpp +++ b/tests/test_xpath_paths.cpp @@ -6,6 +6,7 @@ TEST_XML(xpath_paths_axes_child, "<node attr='value'><child attr='value'><subchi { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.attribute(STR("attr")), n); CHECK_XPATH_NODESET(c, STR("child:: node()")); @@ -13,12 +14,14 @@ TEST_XML(xpath_paths_axes_child, "<node attr='value'><child attr='value'><subchi CHECK_XPATH_NODESET(n, STR("another/child:: node()")); CHECK_XPATH_NODESET(n, STR("@attr/child::node()")); + CHECK_XPATH_NODESET(na, STR("child::node()")); } TEST_XML(xpath_paths_axes_descendant, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>") { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.attribute(STR("attr")), n); CHECK_XPATH_NODESET(c, STR("descendant:: node()")); @@ -28,12 +31,14 @@ TEST_XML(xpath_paths_axes_descendant, "<node attr='value'><child attr='value'><s CHECK_XPATH_NODESET(n, STR("last/descendant:: node()")); CHECK_XPATH_NODESET(n, STR("@attr/descendant::node()")); + CHECK_XPATH_NODESET(na, STR("descendant::node()")); } TEST_XML(xpath_paths_axes_parent, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>") { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.attribute(STR("attr")), n); CHECK_XPATH_NODESET(c, STR("parent:: node()")); @@ -42,12 +47,15 @@ TEST_XML(xpath_paths_axes_parent, "<node attr='value'><child attr='value'><subch CHECK_XPATH_NODESET(n, STR("@attr/parent:: node()")) % 2; // node CHECK_XPATH_NODESET(n, STR("parent:: node()")) % 1; // root CHECK_XPATH_NODESET(doc, STR("parent:: node()")); + + CHECK_XPATH_NODESET(na, STR("parent:: node()")) % 2; // node } TEST_XML(xpath_paths_axes_ancestor, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>") { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.child(STR("child")).attribute(STR("attr")), n.child(STR("child"))); CHECK_XPATH_NODESET(c, STR("ancestor:: node()")); @@ -56,36 +64,45 @@ TEST_XML(xpath_paths_axes_ancestor, "<node attr='value'><child attr='value'><sub CHECK_XPATH_NODESET(n, STR("child/@attr/ancestor:: node()")) % 4 % 2 % 1; // child, node, root CHECK_XPATH_NODESET(n, STR("ancestor:: node()")) % 1; // root CHECK_XPATH_NODESET(doc, STR("ancestor:: node()")); + + CHECK_XPATH_NODESET(na, STR("ancestor:: node()")) % 4 % 2 % 1; // child, node, root } TEST_XML(xpath_paths_axes_following_sibling, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>") { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.attribute(STR("attr1")), n); CHECK_XPATH_NODESET(c, STR("following-sibling:: node()")); CHECK_XPATH_NODESET(n.child(STR("child")), STR("following-sibling:: node()")) % 8 % 10; // another, last CHECK_XPATH_NODESET(n.child(STR("last")), STR("following-sibling:: node()")); + CHECK_XPATH_NODESET(n, STR("@attr1/following-sibling:: node()")); // attributes are not siblings + CHECK_XPATH_NODESET(na, STR("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>") { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.attribute(STR("attr2")), n); CHECK_XPATH_NODESET(c, STR("preceding-sibling:: node()")); CHECK_XPATH_NODESET(n.child(STR("child")), STR("preceding-sibling:: node()")); CHECK_XPATH_NODESET(n.child(STR("last")), STR("preceding-sibling:: node()")) % 8 % 5; // another, child + CHECK_XPATH_NODESET(n, STR("@attr2/following-sibling:: node()")); // attributes are not siblings + CHECK_XPATH_NODESET(na, STR("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><almost/><last/></node>") { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.attribute(STR("attr1")), n); CHECK_XPATH_NODESET(c, STR("following:: node()")); @@ -96,12 +113,14 @@ TEST_XML(xpath_paths_axes_following, "<node attr1='value' attr2='value'><child a CHECK_XPATH_NODESET(n, STR("@attr1/following::node()")) % 5 % 7 % 8 % 9 % 10 % 11; // child, subchild, another, subchild, almost, last - because @/following CHECK_XPATH_NODESET(n, STR("child/@attr/following::node()")) % 7 % 8 % 9 % 10 % 11; // subchild, another, subchild, almost, last + CHECK_XPATH_NODESET(na, STR("following::node()")) % 5 % 7 % 8 % 9 % 10 % 11; // child, subchild, another, subchild, almost, last - because @/following } TEST_XML(xpath_paths_axes_preceding, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another><subchild id='1'/></another><almost/><last/></node>") { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.child(STR("child")).attribute(STR("attr")), n.child(STR("child"))); CHECK_XPATH_NODESET(c, STR("preceding:: node()")); @@ -112,12 +131,14 @@ TEST_XML(xpath_paths_axes_preceding, "<node attr1='value' attr2='value'><child a CHECK_XPATH_NODESET(n, STR("child/@attr/preceding::node()")); // no ancestors CHECK_XPATH_NODESET(n, STR("//subchild[@id]/@id/preceding::node()")) % 7 % 5; // subchild, child + CHECK_XPATH_NODESET(na, STR("preceding::node()")); // no ancestors } TEST_XML(xpath_paths_axes_attribute, "<node attr1='value' attr2='value'><child attr='value'><subchild/></child><another xmlns:foo='bar'><subchild/></another><last/></node>") { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.attribute(STR("attr1")), n); CHECK_XPATH_NODESET(c, STR("attribute:: node()")); @@ -128,20 +149,25 @@ TEST_XML(xpath_paths_axes_attribute, "<node attr1='value' attr2='value'><child a CHECK_XPATH_NODESET(n.child(STR("another")), STR("attribute:: node()")); // namespace nodes are not attributes CHECK_XPATH_NODESET(n, STR("@attr1/attribute::node()")); + CHECK_XPATH_NODESET(na, STR("attribute::node()")); } TEST_XML(xpath_paths_axes_namespace, "<node xmlns:foo='bar' attr='value'/>") { xml_node n = doc.child(STR("node")); + xpath_node na(n.attribute(STR("attr")), n); - CHECK_XPATH_NODESET(n, STR("namespace:: node()")); // namespace nodes are not supported + // namespace nodes are not supported + CHECK_XPATH_NODESET(n, STR("namespace:: node()")); CHECK_XPATH_NODESET(n, STR("@attr/attribute::node()")); + CHECK_XPATH_NODESET(na, STR("attribute::node()")); } TEST_XML(xpath_paths_axes_self, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>") { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.attribute(STR("attr")), n); CHECK_XPATH_NODESET(c, STR("self:: node()")); @@ -150,12 +176,14 @@ TEST_XML(xpath_paths_axes_self, "<node attr='value'><child attr='value'><subchil CHECK_XPATH_NODESET(n, STR("child/self:: node()")) % 4; // child CHECK_XPATH_NODESET(n, STR("child/@attr/self:: node()")) % 5; // @attr CHECK_XPATH_NODESET(doc, STR("self:: node()")) % 1; // root + CHECK_XPATH_NODESET(na, STR("self:: node()")) % 3; // @attr } TEST_XML(xpath_paths_axes_descendant_or_self, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>") { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.child(STR("child")).attribute(STR("attr")), n.child(STR("child"))); CHECK_XPATH_NODESET(c, STR("descendant-or-self:: node()")); @@ -165,12 +193,14 @@ TEST_XML(xpath_paths_axes_descendant_or_self, "<node attr='value'><child attr='v CHECK_XPATH_NODESET(n, STR("last/descendant-or-self:: node()")) % 9; // last CHECK_XPATH_NODESET(n, STR("child/@attr/descendant-or-self::node()")) % 5; // @attr + CHECK_XPATH_NODESET(na, STR("descendant-or-self::node()")) % 5; // @attr } TEST_XML(xpath_paths_axes_ancestor_or_self, "<node attr='value'><child attr='value'><subchild/></child><another><subchild/></another><last/></node>") { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.child(STR("child")).attribute(STR("attr")), n.child(STR("child"))); CHECK_XPATH_NODESET(c, STR("ancestor-or-self:: node()")); @@ -181,6 +211,7 @@ TEST_XML(xpath_paths_axes_ancestor_or_self, "<node attr='value'><child attr='val CHECK_XPATH_NODESET(doc, STR("ancestor-or-self:: node()")) % 1; // root CHECK_XPATH_NODESET(n, STR("ancestor-or-self:: node()")) % 2 % 1; // root, node CHECK_XPATH_NODESET(n, STR("last/ancestor-or-self::node()")) % 9 % 2 % 1; // root, node, last + CHECK_XPATH_NODESET(na, STR("ancestor-or-self:: node()")) % 5 % 4 % 2 % 1; // @attr, child, node, root } TEST_XML(xpath_paths_axes_abbrev, "<node attr='value'><foo/></node>") @@ -331,15 +362,18 @@ TEST_XML(xpath_paths_absolute, "<node><foo><foo/><foo/></foo></node>") { xml_node c; xml_node n = doc.child(STR("node")); + xpath_node na(n.attribute(STR("attr")), n); CHECK_XPATH_NODESET(c, STR("/foo")); CHECK_XPATH_NODESET(n, STR("/foo")); CHECK_XPATH_NODESET(n, STR("/node/foo")) % 3; CHECK_XPATH_NODESET(n.child(STR("foo")), STR("/node/foo")) % 3; + CHECK_XPATH_NODESET(na, STR("/node/foo")) % 3; CHECK_XPATH_NODESET(c, STR("/")); CHECK_XPATH_NODESET(n, STR("/")) % 1; CHECK_XPATH_NODESET(n.child(STR("foo")), STR("/")) % 1; + CHECK_XPATH_NODESET(na, STR("/")) % 1; } TEST_XML(xpath_paths_step_abbrev, "<node><foo/></node>") |