summaryrefslogtreecommitdiffstats
path: root/cmake/UnitTest.cmake
diff options
context:
space:
mode:
Diffstat (limited to 'cmake/UnitTest.cmake')
-rw-r--r--cmake/UnitTest.cmake49
1 files changed, 49 insertions, 0 deletions
diff --git a/cmake/UnitTest.cmake b/cmake/UnitTest.cmake
new file mode 100644
index 00000000..1cbb2b60
--- /dev/null
+++ b/cmake/UnitTest.cmake
@@ -0,0 +1,49 @@
+find_package(Qt5Test REQUIRED)
+
+set(TEST_RESOURCE_PATH ${CMAKE_CURRENT_LIST_DIR})
+
+message(${TEST_RESOURCE_PATH})
+
+function(add_unit_test name)
+ set(options "")
+ set(oneValueArgs DATA)
+ set(multiValueArgs SOURCES LIBS QT)
+
+ cmake_parse_arguments(OPT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
+
+ if(WIN32)
+ add_executable(tst_${name} ${OPT_SOURCES} ${TEST_RESOURCE_PATH}/UnitTest/test.rc)
+ else()
+ add_executable(tst_${name} ${OPT_SOURCES})
+ endif()
+
+ if(NOT "${OPT_DATA}" STREQUAL "")
+ set(TEST_DATA_PATH "${CMAKE_CURRENT_BINARY_DIR}/data")
+ set(TEST_DATA_PATH_SRC "${CMAKE_CURRENT_SOURCE_DIR}/${OPT_DATA}")
+ message("From ${TEST_DATA_PATH_SRC} to ${TEST_DATA_PATH}")
+ string(REGEX REPLACE "[/\\:]" "_" DATA_TARGET_NAME "${TEST_DATA_PATH_SRC}")
+ if(UNIX)
+ # on unix we get the third / from the filename
+ set(TEST_DATA_URL "file://${TEST_DATA_PATH}")
+ else()
+ # we don't on windows, so we have to add it ourselves
+ set(TEST_DATA_URL "file:///${TEST_DATA_PATH}")
+ endif()
+ if(NOT TARGET "${DATA_TARGET_NAME}")
+ add_custom_target(${DATA_TARGET_NAME})
+ add_dependencies(tst_${name} ${DATA_TARGET_NAME})
+ add_custom_command(
+ TARGET ${DATA_TARGET_NAME}
+ COMMAND ${CMAKE_COMMAND} "-DTEST_DATA_URL=${TEST_DATA_URL}" -DSOURCE=${TEST_DATA_PATH_SRC} -DDESTINATION=${TEST_DATA_PATH} -P ${TEST_RESOURCE_PATH}/UnitTest/generate_test_data.cmake
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+ )
+ endif()
+ endif()
+
+ target_link_libraries(tst_${name} ${OPT_LIBS})
+ qt5_use_modules(tst_${name} Test ${OPT_QT})
+
+ target_include_directories(tst_${name} PRIVATE "${TEST_RESOURCE_PATH}/UnitTest/")
+
+ add_test(NAME ${name} COMMAND tst_${name})
+endfunction()