From 53972ed5f21cc4a0d7bd63846fb008ff6200945e Mon Sep 17 00:00:00 2001 From: = Date: Thu, 29 Nov 2018 18:38:27 +0000 Subject: [PATCH] Use sfml_add_test macro for unit tests and copy dlls to output directory if required --- CMakeLists.txt | 8 ++++++-- cmake/Macros.cmake | 36 ++++++++++++++++++++++++++++++++++++ test/CMakeLists.txt | 16 +++++----------- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eefd9fea..abb18a80 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -285,8 +285,12 @@ if(SFML_BUILD_DOC) add_subdirectory(doc) endif() if(SFML_BUILD_TEST_SUITE) - enable_testing() - add_subdirectory(test) + if (SFML_OS_IOS) + message( WARNING "Unit testing not supported on iOS") + else() + enable_testing() + add_subdirectory(test) + endif() endif() # on Linux and BSD-like OS, install pkg-config files by default diff --git a/cmake/Macros.cmake b/cmake/Macros.cmake index f92cf796..f8e19f1f 100644 --- a/cmake/Macros.cmake +++ b/cmake/Macros.cmake @@ -297,6 +297,42 @@ macro(sfml_add_example target) endmacro() +# add a new target which is a SFML test +# example: sfml_add_test(sfml-test +# ftp.cpp ... +# sfml-network) +function(sfml_add_test target SOURCES DEPENDS) + + # set a source group for the source files + source_group("" FILES ${SOURCES}) + + # check whether resources must be added in target + set(target_input ${SOURCES}) + + # create the target + add_executable(${target} ${target_input}) + + # set the target's folder (for IDEs that support it, e.g. Visual Studio) + set_target_properties(${target} PROPERTIES FOLDER "Tests") + + # link the target to its SFML dependencies + if(DEPENDS) + target_link_libraries(${target} PRIVATE ${DEPENDS}) + endif() + + # Add the test + add_test(${target} ${target}) + + # If building shared libs on windows we must copy the dependencies into the folder + if (WIN32 AND BUILD_SHARED_LIBS) + foreach (DEPENDENCY ${DEPENDS}) + add_custom_command(TARGET ${target} PRE_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + $ + $) + endforeach() + endif() +endfunction() # Create an interface library for an external dependency. This virtual target can provide # link specifications and include directories to be used by dependees. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 86dd8128..7bbcc155 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -12,9 +12,7 @@ SET(SYSTEM_SRC "${SRCROOT}/TestUtilities/SystemUtil.hpp" "${SRCROOT}/TestUtilities/SystemUtil.cpp" ) -add_executable(systemtest ${SYSTEM_SRC}) -target_link_libraries(systemtest sfml-system) -add_test(System-module-tests systemtest) +sfml_add_test(systemtest "${SYSTEM_SRC}" sfml-system) if(SFML_BUILD_WINDOW) SET(WINDOW_SRC @@ -22,9 +20,7 @@ if(SFML_BUILD_WINDOW) "${SRCROOT}/TestUtilities/WindowUtil.hpp" "${SRCROOT}/TestUtilities/WindowUtil.cpp" ) - add_executable(windowtest ${WINDOW_SRC}) - target_link_libraries(windowtest sfml-window) - add_test(Window-module-tests windowtest) + sfml_add_test(windowtest "${WINDOW_SRC}" sfml-window) endif() if(SFML_BUILD_GRAPHICS) @@ -34,17 +30,15 @@ if(SFML_BUILD_GRAPHICS) "${SRCROOT}/TestUtilities/GraphicsUtil.hpp" "${SRCROOT}/TestUtilities/GraphicsUtil.cpp" ) - add_executable(graphicstest ${GRAPHICS_SRC}) - target_link_libraries(graphicstest sfml-graphics) - add_test(Graphics-module-tests graphicstest) + sfml_add_test(graphicstest "${GRAPHICS_SRC}" sfml-graphics) endif() # Automatically run the tests at the end of the build add_custom_target(runtests ALL DEPENDS systemtest windowtest graphicstest ) + add_custom_command(TARGET runtests COMMENT "Run tests" POST_BUILD COMMAND ctest ARGS --output-on-failure - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} -) +) \ No newline at end of file