diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/common.hpp | 5 | ||||
| -rw-r--r-- | tests/main.cpp | 43 | ||||
| -rw-r--r-- | tests/test.hpp | 59 | ||||
| -rw-r--r-- | tests/test_dom_traverse.cpp | 51 | 
4 files changed, 153 insertions, 5 deletions
diff --git a/tests/common.hpp b/tests/common.hpp new file mode 100644 index 0000000..0091e1c --- /dev/null +++ b/tests/common.hpp @@ -0,0 +1,5 @@ +#include "test.hpp"
 +
 +#include "../src/pugixml.hpp"
 +
 +using namespace pugi;
 diff --git a/tests/main.cpp b/tests/main.cpp index ab3187c..55903b9 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -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;
 +
 +	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);
 -	// doc.select_single_node("//"); - fails? why? :)
 +	return failed;
  }
 diff --git a/tests/test.hpp b/tests/test.hpp new file mode 100644 index 0000000..09d7024 --- /dev/null +++ b/tests/test.hpp @@ -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
 diff --git a/tests/test_dom_traverse.cpp b/tests/test_dom_traverse.cpp new file mode 100644 index 0000000..bbee076 --- /dev/null +++ b/tests/test_dom_traverse.cpp @@ -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());
 +}
  | 
