From 050a633009d91f74e58a36f28b4de515b48c4341 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Tue, 3 Nov 2009 18:29:57 +0000 Subject: tests: Added helper for node set testing, added several tests git-svn-id: http://pugixml.googlecode.com/svn/trunk@200 99668b35-9821-0410-8761-19e4c4f06640 --- Jamfile.jam | 2 +- tests/test.hpp | 69 ++++++++++++++++++++++++++++++++++-------- tests/test_xpath_functions.cpp | 39 ++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 14 deletions(-) diff --git a/Jamfile.jam b/Jamfile.jam index b66b04c..1769175 100644 --- a/Jamfile.jam +++ b/Jamfile.jam @@ -34,7 +34,7 @@ else BUILD = build/$(toolset)/standard/$(configuration) ; } -if ( $(toolset) = "mingw" ) +if ( $(toolset:I=^mingw) ) { CCFLAGS = -fprofile-arcs -ftest-coverage ; LDFLAGS = -fprofile-arcs ; diff --git a/tests/test.hpp b/tests/test.hpp index e29e5c9..a4e80df 100644 --- a/tests/test.hpp +++ b/tests/test.hpp @@ -18,6 +18,27 @@ typedef int intptr_t; #include +struct test_runner +{ + test_runner(const char* name) + { + _name = name; + _next = _tests; + _tests = this; + } + + virtual ~test_runner() {} + + virtual void run() = 0; + + const char* _name; + test_runner* _next; + + static test_runner* _tests; + static size_t _memory_fail_threshold; + static jmp_buf _failure; +}; + inline bool test_string_equal(const char* lhs, const char* rhs) { return (!lhs || !rhs) ? lhs == rhs : strcmp(lhs, rhs) == 0; @@ -93,29 +114,50 @@ inline bool test_xpath_fail_compile(const char* query) return true; } } -#endif -struct test_runner +struct xpath_node_set_tester { - test_runner(const char* name) + pugi::xpath_node_set result; + unsigned int last; + const char* message; + + void check(bool condition) { - _name = name; - _next = _tests; - _tests = this; + if (!condition) longjmp(test_runner::_failure, (int)(intptr_t)message); } - virtual ~test_runner() {} + xpath_node_set_tester(const pugi::xml_node& node, const char* query, const char* message): last(0), message(message) + { + pugi::xpath_query q(query); + result = q.evaluate_node_set(node); + } - virtual void run() = 0; + ~xpath_node_set_tester() + { + // check that we processed everything + check(last == result.size()); + } - const char* _name; - test_runner* _next; + xpath_node_set_tester& operator%(unsigned int expected) + { + // check element count + check(last < result.size()); - static test_runner* _tests; - static size_t _memory_fail_threshold; - static jmp_buf _failure; + // 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 + last++; + + return *this; + } }; +#endif + struct dummy_fixture {}; #define TEST_FIXTURE(name, fixture) \ @@ -179,6 +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)) #endif #endif diff --git a/tests/test_xpath_functions.cpp b/tests/test_xpath_functions.cpp index ef04667..e098813 100644 --- a/tests/test_xpath_functions.cpp +++ b/tests/test_xpath_functions.cpp @@ -498,6 +498,45 @@ TEST(xpath_string_translate) CHECK_XPATH_FAIL("translate('a', 'b', 'c', 'd')"); } +TEST_XML(xpath_nodeset_count, "") +{ + xml_node c; + xml_node n = doc.child("node"); + + // count with 0 arguments + CHECK_XPATH_FAIL("count()"); + + // count with 1 non-node-set argument + CHECK_XPATH_FAIL("count(1)"); + CHECK_XPATH_FAIL("count(true())"); + CHECK_XPATH_FAIL("count('')"); + + // count with 1 node-set argument + CHECK_XPATH_NUMBER(c, "count(.)", 0); + CHECK_XPATH_NUMBER(n, "count(.)", 1); + CHECK_XPATH_NUMBER(n, "count(c1)", 2); + CHECK_XPATH_NUMBER(n, "count(c2)", 1); + CHECK_XPATH_NUMBER(n, "count(c3)", 4); + CHECK_XPATH_NUMBER(n, "count(c4)", 0); + + // count with 2 arguments + CHECK_XPATH_FAIL("count(x, y)"); +} + +TEST_XML(xpath_nodeset_id, "") +{ + xml_node n = doc.child("node"); + + // id with 0 arguments + CHECK_XPATH_FAIL("id()"); + + // id with 1 argument - no DTD => no id + CHECK_XPATH_NODESET(n, "id('foo')"); + + // id with 2 arguments + CHECK_XPATH_FAIL("id(1, 2)"); +} + TEST(xpath_function_arguments) { xml_node c; -- cgit v1.2.3