From 8a180c289785710f331c1d9e88c90fb282b4666c Mon Sep 17 00:00:00 2001 From: Jens Luedicke Date: Sat, 22 Jan 2011 21:13:37 +0100 Subject: [PATCH] Added unittest (requires Boost unit test framework). --- CMakeLists.txt | 6 +++- unittest/CMakeLists.txt | 9 ++++++ unittest/unittest.cpp | 70 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 unittest/CMakeLists.txt create mode 100644 unittest/unittest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f266f4..a78a8e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,13 @@ cmake_minimum_required (VERSION 2.6) project (libsettable) -find_package(Boost) +find_package(Boost 1.42 COMPONENTS system unit_test_framework) include_directories(${PROJECT_SOURCE_DIR}/src ${Boost_INCLUDE_DIRS}) add_subdirectory(examples) + +enable_testing() + +add_subdirectory(unittest) diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt new file mode 100644 index 0000000..3d04248 --- /dev/null +++ b/unittest/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required (VERSION 2.6) + +link_directories(${Boost_LIBRARY_DIRS}) + +add_executable(unittest unittest.cpp) + +target_link_libraries(unittest ${Boost_LIBRARIES}) + +add_test(unittest unittest) diff --git a/unittest/unittest.cpp b/unittest/unittest.cpp new file mode 100644 index 0000000..e93cdc9 --- /dev/null +++ b/unittest/unittest.cpp @@ -0,0 +1,70 @@ +#include "attributes.hpp" + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE attributes_test + +#include + +#include +#include + +namespace string2type { + +template<> +int convert(const std::string &value) { + return boost::lexical_cast(value); +} + +} + +struct test : attributes { + test() : x(0) { + REGISTER_ATTRIBUTE(int, x); + } + + int x; +}; + +struct test_fixture { + test_fixture() : object() { + BOOST_TEST_MESSAGE( "setup fixture" ); + } + + ~test_fixture() { + BOOST_TEST_MESSAGE( "teardown fixture" ); + } + + test object; +}; + +BOOST_AUTO_TEST_SUITE(string2type_test) + +BOOST_AUTO_TEST_CASE( test1 ) +{ + int x = string2type::convert("42"); + + BOOST_CHECK( x == 42 ); +} + +BOOST_AUTO_TEST_SUITE_END() + +BOOST_FIXTURE_TEST_SUITE( test_suite, test_fixture ) + +//____________________________________________________________________________// + +BOOST_AUTO_TEST_CASE( test2 ) +{ + BOOST_CHECK( object.x == 0 ); + + object.set_attribute("x", "42"); + + BOOST_CHECK( object.x == 42 ); + + object.set_attribute("x", "0"); + + BOOST_CHECK( object.x == 0 ); +} + +//____________________________________________________________________________// + +BOOST_AUTO_TEST_SUITE_END()