2009-10-11 01:36:03 +04:00
# ifndef HEADER_TEST_HPP
# define HEADER_TEST_HPP
2009-10-11 01:58:44 +04:00
# include <string.h>
2009-10-11 10:49:30 +04:00
# include <math.h>
2009-10-11 14:24:12 +04:00
# include <sstream>
# include "../src/pugixml.hpp"
2009-10-11 01:58:44 +04:00
inline bool test_string_equal ( const char * lhs , const char * rhs )
{
return ( ! lhs | | ! rhs ) ? lhs = = rhs : strcmp ( lhs , rhs ) = = 0 ;
}
2009-10-11 10:49:30 +04:00
template < typename Node > inline bool test_node_name_value ( const Node & node , const char * name , const char * value )
{
return test_string_equal ( node . name ( ) , name ) & & test_string_equal ( node . value ( ) , value ) ;
}
2009-10-12 20:26:18 +04:00
inline bool test_node ( const pugi : : xml_node & node , const char * contents , const char * indent , unsigned int flags )
2009-10-11 14:24:12 +04:00
{
std : : ostringstream oss ;
2009-10-12 20:26:18 +04:00
node . print ( oss , indent , flags ) ;
2009-10-11 14:24:12 +04:00
return oss . str ( ) = = contents ;
}
2009-10-11 01:36:03 +04:00
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 ;
2009-10-21 01:36:29 +04:00
static size_t _memory_fail_threshold ;
2009-10-11 01:36:03 +04:00
} ;
struct dummy_fixture { } ;
# define TEST_FIXTURE(name, fixture) \
struct test_runner_helper_ # # name : fixture \
{ \
void run ( ) ; \
} ; \
static struct test_runner_ # # name : test_runner \
{ \
test_runner_ # # name ( ) : test_runner ( # name ) { } \
\
virtual void run ( ) \
{ \
test_runner_helper_ # # name helper ; \
helper . run ( ) ; \
} \
} test_runner_instance_ # # name ; \
void test_runner_helper_ # # name : : run ( )
# define TEST(name) TEST_FIXTURE(name, dummy_fixture)
2009-10-11 10:49:30 +04:00
# define TEST_XML_FLAGS(name, xml, flags) \
2009-10-11 01:36:03 +04:00
struct test_fixture_ # # name \
{ \
pugi : : xml_document doc ; \
\
test_fixture_ # # name ( ) \
{ \
2009-10-11 10:49:30 +04:00
CHECK ( doc . load ( xml , flags ) ) ; \
2009-10-11 01:36:03 +04:00
} \
} ; \
\
TEST_FIXTURE ( name , test_fixture_ # # name )
2009-10-11 10:49:30 +04:00
# define TEST_XML(name, xml) TEST_XML_FLAGS(name, xml, pugi::parse_default)
2009-10-12 20:26:18 +04:00
# define CHECK_TEXT(condition, text) if (condition) ; else throw text
# define CHECK(condition) CHECK_TEXT(condition, #condition " is false")
# define CHECK_STRING(value, expected) CHECK_TEXT(test_string_equal(value, expected), #value " is not equal to " #expected)
# define CHECK_DOUBLE(value, expected) CHECK_TEXT(fabs(value - expected) < 1e-6, #value " is not equal to " #expected)
# define CHECK_NAME_VALUE(node, name, value) CHECK_TEXT(test_node_name_value(node, name, value), #node " name / value do not match " #name " and " #value)
# 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)
2009-10-11 01:36:03 +04:00
# endif