Commits

Christian Fischer committed 7078c0b

added unit test support

  • Participants
  • Parent commits 0c5cd71

Comments (0)

Files changed (9)

build/build.cmake

 
 # paths
 set(WIESEL_SRC_DIR       ${WIESEL_DIR}/src)
+set(WIESEL_TESTS_DIR     ${WIESEL_DIR}/tests)
 set(WIESEL_MODULES_DIR   ${WIESEL_DIR}/libraries/desktop)
 
 # load standard modules
-include(wiesel-module)
+include(wiesel-module)
+include(wiesel-testsuite)
 
 
 # load optional config includes

build/cmake/modules/wiesel-module.cmake

 	if (NOT MODULE_INCLUDE_DIRECTORIES STREQUAL "MODULE_INCLUDE_DIRECTORIES-NOTFOUND")
 		wiesel_target_add_includes(${parent_target} ${MODULE_INCLUDE_DIRECTORIES})
 	endif()
-
-	# also add exported libraries to the parent target
-#	get_target_property(MODULE_LIBRARIES ${module} EXPORT_LIBRARIES)
-#	if (NOT MODULE_LIBRARIES STREQUAL "MODULE_LIBRARIES-NOTFOUND")
-#		wiesel_target_add_libraries(${parent_target} ${MODULE_LIBRARIES})
-#	endif()
-	
-#	get_target_property(INCLUDES_2 ${parent_target} INCLUDE_DIRECTORIES)
-#	message("includes for ${parent_target}:  ${INCLUDES_2}")
 	
 endfunction(wiesel_module_add_dependency)
 
 
 
 function(wiesel_module_get_files var path)
-
-	file(GLOB_RECURSE MY_FILES ${path})
+	file(GLOB_RECURSE MY_FILES ${path}/*.c ${path}/*.cpp)
 	set(${var} ${MY_FILES} PARENT_SCOPE)
-	
 endfunction(wiesel_module_get_files)
 
 
 
-function(wiesel_create_executable target source_dir filter)
+function(wiesel_create_executable target source_dir)
 	# get all sources from this directory
-	wiesel_module_get_files(MODULE_SRC_FILES ${source_dir}/${filter})
+	wiesel_module_get_files(MODULE_SRC_FILES ${source_dir})
 	
 	# add sources to the module's target
 	add_executable(${target} ${MODULE_SRC_FILES})
+
+	# also create according test package
+	# SORRY! no test support for executables yet :-/
+	#wiesel_create_test_package_for(${target} "${test_dir}")
 	
 	# add the source dir as include directory to the target
 	wiesel_target_add_includes(${target} ${source_dir})
-	
 endfunction(wiesel_create_executable)
 
 
 
-function(wiesel_create_module target source_dir filter)
+function(wiesel_create_module target source_dir test_dir)
 	# get all sources from this directory
-	wiesel_module_get_files(MODULE_SRC_FILES ${source_dir}/${filter})
+	wiesel_module_get_files(MODULE_SRC_FILES ${source_dir})
 	
 	# add sources to the module's target
 	add_library(${target} SHARED ${MODULE_SRC_FILES})
 	
+	# also create according test package
+	wiesel_create_test_package_for(${target} "${test_dir}")
+	
 	# add the source dir as include directory to the target
 	wiesel_target_add_includes(${target} ${source_dir})
-	
 endfunction(wiesel_create_module)
 
 
 	foreach(incl ${ARGN})
 		wiesel_target_add_compileflags(${target} -I${incl})
 	endforeach()
-	
+
+	# also add the include to the according test target
+	if (WIESEL_BUILD_TESTS)
+		wiesel_target_has_valid_testpackage(HAS_TEST_PACKAGE ${target})
+		wiesel_test_package_name(TEST_PACKAGE_NAME ${target})
+		
+		if (${HAS_TEST_PACKAGE})
+			wiesel_target_add_includes(${TEST_PACKAGE_NAME} ${ARGN})
+		endif()
+	endif(WIESEL_BUILD_TESTS)
 endfunction(wiesel_target_add_includes)
 
 
 
 function(wiesel_target_add_libraries target)
 	target_link_libraries(${target} ${ARGN})
+
+	# also add the libraries to the according test target
+	if (WIESEL_BUILD_TESTS)
+		wiesel_target_has_valid_testpackage(HAS_TEST_PACKAGE ${target})
+		wiesel_test_package_name(TEST_PACKAGE_NAME ${target})
+		
+		if (${HAS_TEST_PACKAGE})
+			wiesel_target_add_libraries(${TEST_PACKAGE_NAME} ${ARGN})
+		endif()
+	endif(WIESEL_BUILD_TESTS)
 endfunction(wiesel_target_add_libraries)
 
 
 
 function(wiesel_target_add_compileflags target)
+	# get the original compile flags
 	get_property(TARGET_COMPILE_FLAGS TARGET ${target} PROPERTY COMPILE_FLAGS)
 	
+	# append each argument
 	foreach(flag ${ARGN})
 		set(TARGET_COMPILE_FLAGS "${TARGET_COMPILE_FLAGS} ${flag}")
 	endforeach()
 	
+	# now write the flags property back
 	set_property(TARGET ${target} PROPERTY COMPILE_FLAGS ${TARGET_COMPILE_FLAGS})
+
+	# also add the compile flags to the according test target
+	if (WIESEL_BUILD_TESTS)
+		wiesel_target_has_valid_testpackage(HAS_TEST_PACKAGE ${target})
+		wiesel_test_package_name(TEST_PACKAGE_NAME ${target})
+		
+		if (${HAS_TEST_PACKAGE})
+			wiesel_target_add_compileflags(${TEST_PACKAGE_NAME} ${ARGN})
+		endif()
+	endif(WIESEL_BUILD_TESTS)
 endfunction(wiesel_target_add_compileflags)

build/cmake/modules/wiesel-testsuite.cmake

+
+option(WIESEL_BUILD_TESTS "Adds testing support to the current build." ON)
+
+if(WIESEL_BUILD_TESTS)
+	# enable testing for the whole project
+	enable_testing()
+
+	# we require the ExternalProject module
+	include(ExternalProject)
+
+	# set CppUnit source properties
+	set(CPPUNIT_DOWNLOAD_URL				"http://sourceforge.net/projects/cppunit/files/cppunit/1.12.1/cppunit-1.12.1.tar.gz/download")
+	set(CPPUNIT_DOWNLOAD_MD5				"bd30e9cf5523cdfc019b94f5e1d7fd19")
+	set(CPPUNIT_SVN_CHECKOUT_URL			"https://cppunit.svn.sourceforge.net/svnroot/cppunit")
+	set(CPPUNIT_SVN_CHECKOUT_REVISION		"HEAD")
+
+	# set Googletest source properties
+	set(GOOGLETEST_DOWNLOAD_URL				"http://googletest.googlecode.com/files/gtest-1.6.0.zip")
+	set(GOOGLETEST_DOWNLOAD_MD5				"4577b49f2973c90bf9ba69aa8166b786")
+	set(GOOGLETEST_SVN_CHECKOUT_URL			"http://googletest.googlecode.com/svn/trunk/")
+	set(GOOGLETEST_SVN_CHECKOUT_REVISION	"HEAD")
+
+	set(GOOGLETEST_CMAKE_ARGS
+	)
+
+	# for MinGW we need to disable threading, because it's missing some POSIX functions
+	if (MINGW OR MSYS)
+		set(
+				GOOGLETEST_CMAKE_ARGS
+				${GOOGLETEST_CMAKE_ARGS}
+				-Dgtest_disable_pthreads=ON
+		)
+	endif()
+
+
+	# get external project
+	# the source of GoogleTest should be downloaded from the original SVN repository
+	# if SVN is not available, try the download URL
+	if (Subversion_SVN_EXECUTABLE)
+		ExternalProject_Add(
+				GoogleTest
+				PREFIX				GoogleTest
+				SVN_REPOSITORY		${GOOGLETEST_SVN_CHECKOUT_URL}
+				SVN_REVISION		${GOOGLETEST_SVN_CHECKOUT_REVISION}
+
+				# cmake config
+				CMAKE_GENERATOR		"${CMAKE_GENERATOR}"
+				CMAKE_ARGS			${GOOGLETEST_CMAKE_ARGS}
+
+				# skip installation
+				INSTALL_COMMAND 	""
+		)
+	else()
+		ExternalProject_Add(
+				GoogleTest
+				PREFIX				GoogleTest
+				URL					${GOOGLETEST_DOWNLOAD_URL}
+				URL_MD5				${GOOGLETEST_DOWNLOAD_MD5}
+
+				# cmake config
+				CMAKE_GENERATOR		"${CMAKE_GENERATOR}"
+				CMAKE_ARGS			"${GOOGLETEST_CMAKE_ARGS}"
+
+				# skip installation
+				INSTALL_COMMAND 	""
+		)
+	endif()
+
+
+	# get GoogleTest directories
+	ExternalProject_Get_Property(GoogleTest SOURCE_DIR)
+	ExternalProject_Get_Property(GoogleTest BINARY_DIR)
+	
+	# set GoogleTest include & libraries
+	set(GOOGLETEST_INCLUDE_DIR       ${SOURCE_DIR}/include)
+	set(GOOGLETEST_LINK_LIBRARIES    ${BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX})
+	
+endif(WIESEL_BUILD_TESTS)
+
+
+
+
+function(wiesel_create_test_package_for target test_dir)
+	if (WIESEL_BUILD_TESTS AND (NOT "${test_dir}" STREQUAL ""))
+		# get test name and all sources from this directory
+		wiesel_test_package_name(TEST_NAME ${target})
+		wiesel_module_get_files(TEST_SRC_FILES ${test_dir})
+
+		# create test runner
+		add_executable(${TEST_NAME} ${WIESEL_TESTS_DIR}/testrunner.cpp ${TEST_SRC_FILES})
+
+		# add include directories
+		wiesel_target_add_includes(${TEST_NAME} ${GOOGLETEST_INCLUDE_DIR})
+
+		# link this library to the GoogleTest libs
+		add_dependencies(${TEST_NAME} GoogleTest)
+		target_link_libraries(${TEST_NAME} ${GOOGLETEST_LINK_LIBRARIES})
+		wiesel_target_add_includes(${TEST_NAME} ${GOOGLETEST_INCLUDE_DIR})
+		
+		# add dependency to it's module target
+		add_dependencies(${TEST_NAME} ${target})
+		target_link_libraries(${TEST_NAME} ${target})
+
+		# finally create the test
+		add_test(
+				NAME				${target}
+				WORKING_DIRECTORY	${CMAKE_SOURCE_DIR}
+				COMMAND				${CMAKE_BINARY_DIR}/${TEST_NAME}${CMAKE_EXECUTABLE_SUFFIX}
+		)
+	endif()
+endfunction(wiesel_create_test_package_for)
+
+
+
+function(wiesel_test_package_name var target)
+	set(${var} "runtests-${target}" PARENT_SCOPE)
+endfunction(wiesel_test_package_name)
+
+
+
+function(wiesel_is_test_package var target)
+	string(FIND "${target}" "runtests-" tmp)
+	if (tmp EQUAL -1)
+		set(${var} TRUE PARENT_SCOPE)
+	else()
+		set(${var} FALSE PARENT_SCOPE)
+	endif()
+endfunction(wiesel_is_test_package)
+
+
+
+function(wiesel_target_has_valid_testpackage var target)
+	wiesel_is_test_package(IS_TEST_PACKAGE ${target})
+
+	if((IS_TEST_PACKAGE) AND (NOT ${target} STREQUAL "TestRunner") AND (TARGET "runtests-${target}"))
+		set(${var} TRUE PARENT_SCOPE)
+	else()
+		set(${var} FALSE PARENT_SCOPE)
+	endif()
+endfunction(wiesel_target_has_valid_testpackage)
+

libraries/desktop/third-party/glee/module.cmake

 
 
 # create the library
-wiesel_create_module(glee ${WIESEL_SRC_DIR}/third-party/glee-5.4 *.c)
+wiesel_create_module(glee ${WIESEL_SRC_DIR}/third-party/glee-5.4 "")
 
 # export include directories of this library for other targets
 wiesel_module_export_includes(glee ${WIESEL_SRC_DIR}/third-party/glee-5.4)

libraries/desktop/wiesel-base/module.cmake

 
 
 # create the library
-wiesel_create_module(wiesel-base ${WIESEL_SRC_DIR}/base *.cpp)
+wiesel_create_module(wiesel-base ${WIESEL_SRC_DIR}/base ${WIESEL_TESTS_DIR}/base)
 
 # export include directories of this library for other targets
 wiesel_module_export_includes(wiesel-base ${WIESEL_SRC_DIR}/base)

libraries/desktop/wiesel-common/module.cmake

 
 
 # collect all files which belongs to this library
-wiesel_create_module(wiesel-common ${WIESEL_SRC_DIR}/common *.cpp)
+wiesel_create_module(wiesel-common ${WIESEL_SRC_DIR}/common ${WIESEL_TESTS_DIR}/tests)
 
 # export include directories of this library for other targets
 wiesel_module_export_includes(wiesel-common ${WIESEL_SRC_DIR}/common)

samples/HelloWiesel/CMakeLists.txt

 # ensure a correct version of cmake is installed
 cmake_minimum_required(VERSION 2.8)
 
+# compatibility
+set(CMAKE_LEGACY_CYGWIN_WIN32 0) # Remove when CMake >= 2.8.4 is required
+
+
+# set the project name
+project(HelloWiesel)
+
 
 # in this case, the engine folder is in the same repository,
 # so we always know the correct location.
 
 
 
-# set the project name
-project(HelloWiesel)
-
-
-
-wiesel_create_executable(HelloWiesel src *.cpp)
+wiesel_create_executable(HelloWiesel ./src)
 
 
 # add required libraries

tests/base/matrix.cpp

+#include "gtest/gtest.h"
+
+#include <wiesel/math/matrix.h>
+
+using namespace wiesel;
+
+
+
+/**
+ * Checks the comparision operators
+ */
+TEST(MatrixTest, Comparision) {
+	EXPECT_TRUE (matrix4x4::identity == matrix4x4::identity);
+	EXPECT_FALSE(matrix4x4::identity != matrix4x4::identity);
+}

tests/testrunner.cpp

+#include "gtest/gtest.h"
+
+
+int main(int argc, char **argv) {
+	testing::InitGoogleTest(&argc, argv);
+	return RUN_ALL_TESTS();
+}