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:
parent
ab28c3b45e
commit
6db04f4320
@ -13,6 +13,6 @@ LDFLAGS = -fprofile-arcs ;
|
|||||||
GCOVFLAGS = -n ;
|
GCOVFLAGS = -n ;
|
||||||
|
|
||||||
Library pugixml : src/pugixml.cpp src/pugixpath.cpp ;
|
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 ;
|
Test run_tests : tests ;
|
||||||
Coverage coverage : run_tests ;
|
Coverage coverage : run_tests ;
|
||||||
|
27
Jamrules.jam
27
Jamrules.jam
@ -25,16 +25,21 @@ actions RunAction
|
|||||||
$(>:\\)
|
$(>:\\)
|
||||||
}
|
}
|
||||||
|
|
||||||
actions quietly ignore MakeDir
|
actions quietly ignore MakeDirAction
|
||||||
{
|
{
|
||||||
mkdir $(<:\\) >nul 2>&1
|
mkdir $(<:\\) >nul 2>&1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
actions quietly ignore DeleteAction
|
||||||
|
{
|
||||||
|
del /F $(>:\\) >nul 2>&1
|
||||||
|
}
|
||||||
|
|
||||||
rule MakeFileDir TARGET
|
rule MakeFileDir TARGET
|
||||||
{
|
{
|
||||||
local DIR = $(TARGET:D) ;
|
local DIR = $(TARGET:D) ;
|
||||||
|
|
||||||
MakeDir $(DIR) ;
|
MakeDirAction $(DIR) ;
|
||||||
Needs $(TARGET) : $(DIR) ;
|
Needs $(TARGET) : $(DIR) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,6 +123,21 @@ rule Application TARGET : SOURCES : LIBRARIES
|
|||||||
$(TARGET)_objects = $(OBJECTS) $($(LIBRARIES)_objects) ;
|
$(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
|
rule Test TARGET : SOURCE
|
||||||
{
|
{
|
||||||
# make alias
|
# make alias
|
||||||
@ -128,6 +148,9 @@ rule Test TARGET : SOURCE
|
|||||||
|
|
||||||
# remember executable objects for coverage
|
# remember executable objects for coverage
|
||||||
$(TARGET)_objects = $($(SOURCE)_objects) ;
|
$(TARGET)_objects = $($(SOURCE)_objects) ;
|
||||||
|
|
||||||
|
# clean coverage files before run
|
||||||
|
CleanCoverage $(TARGET) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
rule Coverage TARGET : SOURCE
|
rule Coverage TARGET : SOURCE
|
||||||
|
5
tests/common.hpp
Normal file
5
tests/common.hpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#include "test.hpp"
|
||||||
|
|
||||||
|
#include "../src/pugixml.hpp"
|
||||||
|
|
||||||
|
using namespace pugi;
|
@ -1,10 +1,43 @@
|
|||||||
#include "../src/pugixml.hpp"
|
#include "test.hpp"
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
test_runner* test_runner::_tests = 0;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
pugi::xml_document doc;
|
unsigned int total = 0;
|
||||||
doc.load("<node/>");
|
unsigned int passed = 0;
|
||||||
doc.select_single_node("node");
|
|
||||||
|
|
||||||
// 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
59
tests/test.hpp
Normal 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
|
51
tests/test_dom_traverse.cpp
Normal file
51
tests/test_dom_traverse.cpp
Normal 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());
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user