From e08c065820d734d4f5768470ed6a4f123cbb56ef Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Sun, 29 Aug 2010 15:27:51 +0000 Subject: tests: Added XPath tests for attribute context git-svn-id: http://pugixml.googlecode.com/svn/trunk@664 99668b35-9821-0410-8761-19e4c4f06640 --- tests/test.cpp | 8 ++++---- tests/test.hpp | 10 +++++----- tests/test_xpath_api.cpp | 21 +++++++++++++++++++-- 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 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, "") CHECK(ns.size() == 1 && ns[0].attribute() == doc.child(STR("node")).attribute(STR("attr"))); } +TEST_XML(xpath_api_evaluate_attr, "") +{ + 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, "") { @@ -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, "") { 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, "") { 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, "") { 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, "") { 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, "") { 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, "") { 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, "") { 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, "") { 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, "") { 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, "") { 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, "") { 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, "") { 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, "") @@ -331,15 +362,18 @@ TEST_XML(xpath_paths_absolute, "") { 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, "") -- cgit v1.2.3