mirror of
https://github.com/zeux/pugixml.git
synced 2024-12-27 13:33:17 +08:00
tests: Added basic XPath tests
git-svn-id: http://pugixml.googlecode.com/svn/trunk@170 99668b35-9821-0410-8761-19e4c4f06640
This commit is contained in:
parent
cbdce99d5c
commit
40c0a5f09a
@ -25,6 +25,40 @@ inline bool test_node(const pugi::xml_node& node, const char* contents, const ch
|
||||
return oss.str() == contents;
|
||||
}
|
||||
|
||||
inline bool test_xpath_string(const pugi::xml_node& node, const char* query, const char* expected)
|
||||
{
|
||||
pugi::xpath_query q(query);
|
||||
|
||||
return q.evaluate_string(node) == expected;
|
||||
}
|
||||
|
||||
inline bool test_xpath_boolean(const pugi::xml_node& node, const char* query, bool expected)
|
||||
{
|
||||
pugi::xpath_query q(query);
|
||||
|
||||
return q.evaluate_boolean(node) == expected;
|
||||
}
|
||||
|
||||
inline bool test_xpath_number(const pugi::xml_node& node, const char* query, double expected)
|
||||
{
|
||||
pugi::xpath_query q(query);
|
||||
|
||||
return fabs(q.evaluate_number(node) - expected) < 1e-8f;
|
||||
}
|
||||
|
||||
inline bool test_xpath_fail_compile(const char* query)
|
||||
{
|
||||
try
|
||||
{
|
||||
pugi::xpath_query q(query);
|
||||
return false;
|
||||
}
|
||||
catch (const pugi::xpath_exception& e)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
struct test_runner
|
||||
{
|
||||
test_runner(const char* name)
|
||||
@ -90,4 +124,9 @@ struct dummy_fixture {};
|
||||
#define CHECK_NODE_EX(node, expected, indent, flags) CHECK_TEXT(test_node(node, expected, indent, flags), #node " contents does not match " #expected)
|
||||
#define CHECK_NODE(node, expected) CHECK_NODE_EX(node, expected, "", pugi::format_raw)
|
||||
|
||||
#define CHECK_XPATH_STRING(node, query, expected) CHECK_TEXT(test_xpath_string(node, query, expected), #query " does not evaluate to " #expected " in context " #node)
|
||||
#define CHECK_XPATH_BOOLEAN(node, query, expected) CHECK_TEXT(test_xpath_boolean(node, query, expected), #query " does not evaluate to " #expected " in context " #node)
|
||||
#define CHECK_XPATH_NUMBER(node, query, expected) CHECK_TEXT(test_xpath_number(node, query, expected), #query " does not evaluate to " #expected " in context " #node)
|
||||
#define CHECK_XPATH_FAIL(query) CHECK_TEXT(test_xpath_fail_compile(query), #query " should not compile")
|
||||
|
||||
#endif
|
||||
|
33
tests/test_xpath_parse.cpp
Normal file
33
tests/test_xpath_parse.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "common.hpp"
|
||||
|
||||
TEST(xpath_literal_parse)
|
||||
{
|
||||
xml_node c;
|
||||
CHECK_XPATH_STRING(c, "'a\"b'", "a\"b");
|
||||
CHECK_XPATH_STRING(c, "\"a'b\"", "a'b");
|
||||
CHECK_XPATH_STRING(c, "\"\"", "");
|
||||
CHECK_XPATH_STRING(c, "\'\'", "");
|
||||
}
|
||||
|
||||
TEST(xpath_literal_error)
|
||||
{
|
||||
CHECK_XPATH_FAIL("\"");
|
||||
CHECK_XPATH_FAIL("\'");
|
||||
}
|
||||
|
||||
TEST(xpath_number_parse)
|
||||
{
|
||||
xml_node c;
|
||||
CHECK_XPATH_NUMBER(c, "0", 0);
|
||||
CHECK_XPATH_NUMBER(c, "123", 123);
|
||||
CHECK_XPATH_NUMBER(c, "123.456", 123.456);
|
||||
CHECK_XPATH_NUMBER(c, ".123", 0.123);
|
||||
}
|
||||
|
||||
TEST(xpath_number_error)
|
||||
{
|
||||
xml_node c;
|
||||
CHECK_XPATH_FAIL("123a");
|
||||
CHECK_XPATH_FAIL("123.a");
|
||||
CHECK_XPATH_FAIL(".123a");
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user