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
This commit is contained in:
arseny.kapoulkine 2009-11-03 18:29:57 +00:00
parent 04d4a6d9c2
commit 050a633009
3 changed files with 96 additions and 14 deletions

View File

@ -34,7 +34,7 @@ else
BUILD = build/$(toolset)/standard/$(configuration) ; BUILD = build/$(toolset)/standard/$(configuration) ;
} }
if ( $(toolset) = "mingw" ) if ( $(toolset:I=^mingw) )
{ {
CCFLAGS = -fprofile-arcs -ftest-coverage ; CCFLAGS = -fprofile-arcs -ftest-coverage ;
LDFLAGS = -fprofile-arcs ; LDFLAGS = -fprofile-arcs ;

View File

@ -18,6 +18,27 @@ typedef int intptr_t;
#include <string> #include <string>
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) inline bool test_string_equal(const char* lhs, const char* rhs)
{ {
return (!lhs || !rhs) ? lhs == rhs : strcmp(lhs, rhs) == 0; return (!lhs || !rhs) ? lhs == rhs : strcmp(lhs, rhs) == 0;
@ -93,29 +114,50 @@ inline bool test_xpath_fail_compile(const char* query)
return true; 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; if (!condition) longjmp(test_runner::_failure, (int)(intptr_t)message);
_next = _tests;
_tests = this;
} }
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; xpath_node_set_tester& operator%(unsigned int expected)
test_runner* _next; {
// check element count
check(last < result.size());
static test_runner* _tests; // check document order
static size_t _memory_fail_threshold; pugi::xpath_node node = result.begin()[last];
static jmp_buf _failure; 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 {}; struct dummy_fixture {};
#define TEST_FIXTURE(name, 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(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_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_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
#endif #endif

View File

@ -498,6 +498,45 @@ TEST(xpath_string_translate)
CHECK_XPATH_FAIL("translate('a', 'b', 'c', 'd')"); CHECK_XPATH_FAIL("translate('a', 'b', 'c', 'd')");
} }
TEST_XML(xpath_nodeset_count, "<node><c1/><c1/><c2/><c3/><c3/><c3/><c3/></node>")
{
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, "<node id='foo'/>")
{
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) TEST(xpath_function_arguments)
{ {
xml_node c; xml_node c;