From 61ceb10baf347bb19078afdb945a6cdec85777d7 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Sun, 29 Aug 2010 15:39:43 +0000 Subject: tests: Added more XPath variable tests git-svn-id: http://pugixml.googlecode.com/svn/trunk@681 99668b35-9821-0410-8761-19e4c4f06640 --- tests/test.cpp | 50 +++++++---- tests/test.hpp | 12 ++- tests/test_xpath_parse.cpp | 3 +- tests/test_xpath_variables.cpp | 184 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 230 insertions(+), 19 deletions(-) (limited to 'tests') diff --git a/tests/test.cpp b/tests/test.cpp index 9091f14..a48ef48 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -70,55 +70,75 @@ bool test_double_nan(double value) } #ifndef PUGIXML_NO_XPATH -bool test_xpath_string(const pugi::xpath_node& node, const pugi::char_t* query, const pugi::char_t* expected) +bool test_xpath_string(const pugi::xpath_node& node, const pugi::xpath_query& query, const pugi::char_t* expected) { - pugi::xpath_query q(query); - const size_t capacity = 64; pugi::char_t result[capacity]; - size_t size = q.evaluate_string(result, capacity, node); + size_t size = query.evaluate_string(result, capacity, node); if (size <= capacity) return test_string_equal(result, expected); std::basic_string buffer(size, ' '); - return q.evaluate_string(&buffer[0], size, node) == size && test_string_equal(buffer.c_str(), expected); + return query.evaluate_string(&buffer[0], size, node) == size && test_string_equal(buffer.c_str(), expected); +} + +bool test_xpath_boolean(const pugi::xpath_node& node, const pugi::xpath_query& query, bool expected) +{ + return query.evaluate_boolean(node) == expected; +} + +bool test_xpath_number(const pugi::xpath_node& node, const pugi::xpath_query& query, double expected) +{ + double value = query.evaluate_number(node); + double absolute_error = fabs(value - expected); + + const double tolerance = 1e-15f; + return absolute_error < tolerance || absolute_error < fabs(expected) * tolerance; +} + +bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::xpath_query& query) +{ + return test_double_nan(query.evaluate_number(node)); +} + +bool test_xpath_string(const pugi::xpath_node& node, const pugi::char_t* query, const pugi::char_t* expected) +{ + pugi::xpath_query q(query); + + return q && test_xpath_string(node, q, 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; + return q && test_xpath_boolean(node, q, expected); } bool test_xpath_number(const pugi::xpath_node& node, const pugi::char_t* query, double expected) { pugi::xpath_query q(query); - double value = q.evaluate_number(node); - double absolute_error = fabs(value - expected); - - const double tolerance = 1e-15f; - return absolute_error < tolerance || absolute_error < fabs(expected) * tolerance; + return q && test_xpath_number(node, q, expected); } bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::char_t* query) { pugi::xpath_query q(query); - return test_double_nan(q.evaluate_number(node)); + return q && test_xpath_number_nan(node, q); } -bool test_xpath_fail_compile(const pugi::char_t* query) +bool test_xpath_fail_compile(const pugi::char_t* query, pugi::xpath_variable_set* variables) { #ifdef PUGIXML_NO_EXCEPTIONS - return !pugi::xpath_query(query); + return !pugi::xpath_query(query, variables); #else try { - pugi::xpath_query q(query); + pugi::xpath_query q(query, variables); return false; } catch (const pugi::xpath_exception&) diff --git a/tests/test.hpp b/tests/test.hpp index 37f78c8..7dd4df3 100644 --- a/tests/test.hpp +++ b/tests/test.hpp @@ -38,11 +38,17 @@ bool test_node(const pugi::xml_node& node, const pugi::char_t* contents, const p bool test_double_nan(double value); #ifndef PUGIXML_NO_XPATH +bool test_xpath_string(const pugi::xpath_node& node, const pugi::xpath_query& query, const pugi::char_t* expected); +bool test_xpath_boolean(const pugi::xpath_node& node, const pugi::xpath_query& query, bool expected); +bool test_xpath_number(const pugi::xpath_node& node, const pugi::xpath_query& query, double expected); +bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::xpath_query& 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); + +bool test_xpath_fail_compile(const pugi::char_t* query, pugi::xpath_variable_set* variables = 0); struct xpath_node_set_tester { @@ -128,7 +134,9 @@ 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(xpath_query(query).evaluate_node_set(node), CHECK_JOIN2(STRINGIZE(query) " does not evaluate to expected set in context " STRINGIZE(node), " at "__FILE__ ":", __LINE__)) +#define CHECK_XPATH_FAIL_VAR(query, variables) CHECK_TEXT(test_xpath_fail_compile(query, variables), STRINGIZE(query) " should not compile") +#define CHECK_XPATH_NODESET_Q(node, query) xpath_node_set_tester(query.evaluate_node_set(node), CHECK_JOIN2(STRINGIZE(query) " does not evaluate to expected set in context " STRINGIZE(node), " at "__FILE__ ":", __LINE__)) +#define CHECK_XPATH_NODESET(node, query) CHECK_XPATH_NODESET_Q(node, pugi::xpath_query(query)) #endif #define STR(text) PUGIXML_TEXT(text) diff --git a/tests/test_xpath_parse.cpp b/tests/test_xpath_parse.cpp index f17357b..f996b24 100644 --- a/tests/test_xpath_parse.cpp +++ b/tests/test_xpath_parse.cpp @@ -41,8 +41,9 @@ TEST(xpath_number_error) TEST(xpath_variables) { - CHECK_XPATH_FAIL(STR("$var")); // not implemented + CHECK_XPATH_FAIL(STR("$var")); // no variable var CHECK_XPATH_FAIL(STR("$1")); + CHECK_XPATH_FAIL(STR("$")); } TEST(xpath_empty_expression) diff --git a/tests/test_xpath_variables.cpp b/tests/test_xpath_variables.cpp index 77ca53f..1ea8b11 100644 --- a/tests/test_xpath_variables.cpp +++ b/tests/test_xpath_variables.cpp @@ -176,7 +176,7 @@ TEST(xpath_variables_set_out_of_memory) CHECK(!var); } -TEST(xpath_variable_out_of_memory) +TEST(xpath_variables_out_of_memory) { test_runner::_memory_fail_threshold = 64; @@ -188,4 +188,186 @@ TEST(xpath_variable_out_of_memory) CHECK(!var->set(STR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))); } +TEST_XML(xpath_variables_evaluate, "") +{ + xpath_variable_set set; + set.set(STR("var1"), true); + set.set(STR("var2"), 0.5); + set.set(STR("var3"), STR("value")); + set.set(STR("var4"), doc.select_nodes(STR("*"))); + + CHECK_XPATH_BOOLEAN(doc, xpath_query(STR("$var1"), &set), true); + CHECK_XPATH_NUMBER(doc, xpath_query(STR("$var2"), &set), 0.5); + CHECK_XPATH_STRING(doc, xpath_query(STR("$var3"), &set), STR("value")); + CHECK_XPATH_NODESET_Q(doc, xpath_query(STR("$var4"), &set)) % 2; +} + +TEST_XML(xpath_variables_evaluate_conversion, "3") +{ + xpath_variable_set set; + set.set(STR("var"), doc.select_nodes(STR("*"))); + + xpath_query query(STR("$var"), &set); + + CHECK_XPATH_BOOLEAN(doc, query, true); + CHECK_XPATH_NUMBER(doc, query, 3); + CHECK_XPATH_STRING(doc, query, STR("3")); + CHECK_XPATH_NODESET_Q(doc, query) % 2; +} + +TEST(xpath_variables_evaluate_node_set_fail) +{ + xpath_variable_set set; + set.set(STR("var"), false); + + xpath_query q(STR("$var"), &set); + +#ifdef PUGIXML_NO_EXCEPTIONS + CHECK_XPATH_NODESET_Q(xml_node(), q); +#else + try + { + q.evaluate_node_set(xml_node()); + + CHECK_FORCE_FAIL("Expected exception"); + } + catch (const xpath_exception&) + { + } +#endif +} + +TEST_XML(xpath_variables_multiple_documents, "") +{ + xml_document doc1; + CHECK(doc1.load(STR(""))); + + xml_document doc2; + CHECK(doc2.load(STR(""))); + + xpath_variable_set set; + set.set(STR("var1"), doc1.select_nodes(STR("*"))); + set.set(STR("var2"), doc2.select_nodes(STR("*"))); + + xpath_node_set ns = doc.select_nodes(STR("$var1 | $var2 | node"), &set); + ns.sort(); + + CHECK(ns.size() == 3); + CHECK(ns[0] != ns[1] && ns[0] != ns[2]); + + xml_node n0 = doc.child(STR("node")), n1 = doc1.child(STR("node")), n2 = doc2.child(STR("node")); + + CHECK(n0 == ns[0].node() || n0 == ns[1].node() || n0 == ns[2].node()); + CHECK(n1 == ns[0].node() || n1 == ns[1].node() || n1 == ns[2].node()); + CHECK(n2 == ns[0].node() || n2 == ns[1].node() || n2 == ns[2].node()); +} + +TEST(xpath_variables_long_name) +{ + xpath_variable_set set; + set.set(STR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), true); + + CHECK_XPATH_BOOLEAN(xml_node(), xpath_query(STR("$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), &set), true); +} + +TEST_XML(xpath_variables_select, "") +{ + xpath_variable_set set; + set.set(STR("one"), 1.0); + + xpath_node_set ns = doc.select_nodes(STR("node[@attr=$one+1]"), &set); + CHECK(ns.size() == 1 && ns[0].node() == doc.last_child()); + + xpath_node n = doc.select_single_node(STR("node[@attr=$one+1]"), &set); + CHECK(n == ns[0]); +} + +TEST(xpath_variables_empty_name) +{ + xpath_variable_set set; + CHECK(!set.add(STR(""), xpath_type_number)); +} + +TEST_XML(xpath_variables_inside_filter, "") +{ + xpath_variable_set set; + set.set(STR("one"), 1.0); + + xpath_node_set ns = doc.select_nodes(STR("(node[@key = $one])[@value = $one]"), &set); + CHECK(ns.size() == 1 && ns[0].node() == doc.last_child()); +} + +TEST_XML(xpath_variables_step, "") +{ + xpath_variable_set set; + set.set(STR("root"), doc.select_nodes(STR("node"))); + + CHECK_XPATH_NODESET_Q(xml_node(), xpath_query(STR("$root/child"), &set)) % 3 % 4 % 5; + CHECK_XPATH_NODESET_Q(xml_node(), xpath_query(STR("$root//child"), &set)) % 3 % 4 % 5 % 6; +} + +TEST_XML(xpath_variables_index, "") +{ + xpath_variable_set set; + set.set(STR("index"), 2.0); + + CHECK_XPATH_NODESET_Q(doc, xpath_query(STR("node/child[$index]"), &set)) % 4; + CHECK_XPATH_NODESET_Q(doc, xpath_query(STR("node/child[position()=$index]"), &set)) % 4; +} + +TEST(xpath_variables_qname) +{ + xpath_variable_set set; + set.set(STR("foo:bar"), true); + + CHECK_XPATH_BOOLEAN(xml_node(), xpath_query(STR("$foo:bar"), &set), true); +} + +TEST(xpath_variables_qname_error) +{ + xpath_variable_set set; + set.set(STR("foo:"), true); + set.set(STR(":bar"), true); + set.set(STR("foo:*"), true); + + CHECK_XPATH_FAIL_VAR(STR("$foo:"), &set); + CHECK_XPATH_FAIL_VAR(STR("$:bar"), &set); + CHECK_XPATH_FAIL_VAR(STR("$foo:*"), &set); + CHECK_XPATH_FAIL_VAR(STR("$foo:bar:baz"), &set); +} + +TEST(xpath_variables_empty_string) +{ + xpath_variable_set set; + set.add(STR("empty"), xpath_type_string); + + CHECK_XPATH_BOOLEAN(xml_node(), xpath_query(STR("$empty = substring-before('a', 'z')"), &set), true); +} + +TEST(xpath_variables_name_underscore) +{ + xpath_variable_set set; + set.set(STR("_foo_bar"), true); + + CHECK_XPATH_BOOLEAN(xml_node(), xpath_query(STR("$_foo_bar"), &set), true); +} + +TEST(xpath_variables_name_case) +{ + xpath_variable_set set; + set.set(STR("i"), 5.0); + set.set(STR("I"), 2.0); + + CHECK_XPATH_NUMBER(xml_node(), xpath_query(STR("$i div $I"), &set), 2.5); +} + +TEST_XML(xpath_variables_count_sum, "122334") +{ + xpath_variable_set set; + set.set(STR("c12"), doc.select_nodes(STR("node/c1 | node/c2"))); + set.set(STR("c3"), doc.select_nodes(STR("node/c3"))); + set.set(STR("c"), doc.select_nodes(STR("node/*"))); + + CHECK_XPATH_NUMBER(xml_node(), xpath_query(STR("sum($c12) * count($c) - sum($c3)"), &set), 71); +} #endif -- cgit v1.2.3