0
0
mirror of https://github.com/zeux/pugixml.git synced 2025-01-14 01:47:55 +08:00

tests: Added simple test framework, added a couple of tests

git-svn-id: http://pugixml.googlecode.com/svn/trunk@140 99668b35-9821-0410-8761-19e4c4f06640
This commit is contained in:
arseny.kapoulkine 2009-10-10 21:36:03 +00:00
parent ab28c3b45e
commit 6db04f4320
6 changed files with 179 additions and 8 deletions

View File

@ -13,6 +13,6 @@ LDFLAGS = -fprofile-arcs ;
GCOVFLAGS = -n ;
Library pugixml : src/pugixml.cpp src/pugixpath.cpp ;
Application tests : tests/main.cpp : pugixml ;
Application tests : tests/main.cpp [ Glob tests : test_*.cpp ] : pugixml ;
Test run_tests : tests ;
Coverage coverage : run_tests ;

View File

@ -25,16 +25,21 @@ actions RunAction
$(>:\\)
}
actions quietly ignore MakeDir
actions quietly ignore MakeDirAction
{
mkdir $(<:\\) >nul 2>&1
}
actions quietly ignore DeleteAction
{
del /F $(>:\\) >nul 2>&1
}
rule MakeFileDir TARGET
{
local DIR = $(TARGET:D) ;
MakeDir $(DIR) ;
MakeDirAction $(DIR) ;
Needs $(TARGET) : $(DIR) ;
}
@ -118,6 +123,21 @@ rule Application TARGET : SOURCES : LIBRARIES
$(TARGET)_objects = $(OBJECTS) $($(LIBRARIES)_objects) ;
}
rule CleanCoverage TARGET
{
# make target
local CLEAN_TARGET = $(TARGET)_clean_coverage ;
NotFile $(CLEAN_TARGET) ;
Always $(CLEAN_TARGET) ;
Depends $(TARGET) : $(CLEAN_TARGET) ;
# clean object files
local FILES = $($(SOURCE)_objects:S=.gcda) ;
DeleteAction $(CLEAN_TARGET) : $(FILES) ;
}
rule Test TARGET : SOURCE
{
# make alias
@ -128,6 +148,9 @@ rule Test TARGET : SOURCE
# remember executable objects for coverage
$(TARGET)_objects = $($(SOURCE)_objects) ;
# clean coverage files before run
CleanCoverage $(TARGET) ;
}
rule Coverage TARGET : SOURCE

5
tests/common.hpp Normal file
View File

@ -0,0 +1,5 @@
#include "test.hpp"
#include "../src/pugixml.hpp"
using namespace pugi;

View File

@ -1,10 +1,43 @@
#include "../src/pugixml.hpp"
#include "test.hpp"
#include <exception>
#include <stdio.h>
test_runner* test_runner::_tests = 0;
int main()
{
pugi::xml_document doc;
doc.load("<node/>");
doc.select_single_node("node");
unsigned int total = 0;
unsigned int passed = 0;
// doc.select_single_node("//"); - fails? why? :)
for (test_runner* test = test_runner::_tests; test; test = test->_next)
{
try
{
total++;
test->run();
passed++;
}
catch (const std::exception& e)
{
printf("Test %s failed: exception %s\n", test->_name, e.what());
}
catch (const char* e)
{
printf("Test %s failed: %s\n", test->_name, e);
}
catch (...)
{
printf("Test %s failed for unknown reason\n", test->_name);
}
}
unsigned int failed = total - passed;
if (failed != 0)
printf("FAILURE: %u out of %u tests failed.\n", failed, total);
else
printf("Success: %d tests passed.\n", total);
return failed;
}

59
tests/test.hpp Normal file
View File

@ -0,0 +1,59 @@
#ifndef HEADER_TEST_HPP
#define HEADER_TEST_HPP
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;
};
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)
#define TEST_XML(name, xml) \
struct test_fixture_##name \
{ \
pugi::xml_document doc; \
\
test_fixture_##name() \
{ \
CHECK(doc.load(xml)); \
} \
}; \
\
TEST_FIXTURE(name, test_fixture_##name)
#define CHECK(condition) if (condition) ; else throw #condition " is false"
#endif

View File

@ -0,0 +1,51 @@
#include "common.hpp"
TEST_XML(dom_attr_bool_ops, "<node attr='1'/>")
{
xml_attribute attr1;
xml_attribute attr2 = doc.child("node").attribute("attr");
CHECK(!attr1);
CHECK(attr2);
CHECK(!!attr2);
bool attr1b = attr1;
bool attr2b = attr2;
CHECK(!attr1b);
CHECK(attr2b);
}
TEST_XML(dom_attr_empty, "<node attr='1'/>")
{
xml_attribute attr1;
xml_attribute attr2 = doc.child("node").attribute("attr");
CHECK(attr1.empty());
CHECK(!attr2.empty());
}
TEST_XML(dom_node_bool_ops, "<node/>")
{
xml_node node1;
xml_node node2 = doc.child("node");
CHECK(!node1);
CHECK(node2);
CHECK(!!node2);
bool node1b = node1;
bool node2b = node2;
CHECK(!node1b);
CHECK(node2b);
}
TEST_XML(dom_node_empty, "<node/>")
{
xml_node node1;
xml_node node2 = doc.child("node");
CHECK(node1.empty());
CHECK(!node2.empty());
}