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:
parent
04d4a6d9c2
commit
050a633009
@ -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 ;
|
||||
|
||||
@ -18,6 +18,27 @@ typedef int intptr_t;
|
||||
|
||||
#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)
|
||||
{
|
||||
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
|
||||
|
||||
@ -498,6 +498,45 @@ TEST(xpath_string_translate)
|
||||
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)
|
||||
{
|
||||
xml_node c;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user