From 6278505a0b71ed7ff061ad7c24855c26d8e403ff Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 2 Oct 2023 10:40:25 +0800 Subject: [PATCH 01/13] Add wrapper functions for tests --- docs/src/index.rst | 1 + src/PackageProject.cmake | 13 -- src/Tests.cmake | 367 +++++++++++++++++++++++++++++++++++++++ src/Utilities.cmake | 14 ++ 4 files changed, 382 insertions(+), 13 deletions(-) diff --git a/docs/src/index.rst b/docs/src/index.rst index 3cd360d7..34f8bfb5 100644 --- a/docs/src/index.rst +++ b/docs/src/index.rst @@ -7,6 +7,7 @@ .. cmake-module:: ../../src/DynamicProjectOptions.cmake .. cmake-module:: ../../src/StaticAnalyzers.cmake .. cmake-module:: ../../src/Sanitizers.cmake +.. cmake-module:: ../../src/Tests.cmake .. cmake-module:: ../../src/Linker.cmake .. cmake-module:: ../../src/CrossCompiler.cmake .. cmake-module:: ../../src/Cuda.cmake diff --git a/src/PackageProject.cmake b/src/PackageProject.cmake index 1fae5228..70aa31bb 100644 --- a/src/PackageProject.cmake +++ b/src/PackageProject.cmake @@ -319,19 +319,6 @@ function(package_project) include("${_ycm_SOURCE_DIR}/modules/AddUninstallTarget.cmake") endfunction() -function(set_or_append_target_property target property new_values) - get_target_property(_AllValues ${target} ${property}) - - if(NOT _AllValues) # If the property hasn't set - set(_AllValues "${new_values}") - else() - list(APPEND _AllValues ${new_values}) - endif() - list(REMOVE_DUPLICATES _AllValues) - - set_target_properties(${target} PROPERTIES ${property} "${_AllValues}") -endfunction() - #[[.rst: ``target_include_interface_directories`` diff --git a/src/Tests.cmake b/src/Tests.cmake index 2f439b63..415571b2 100644 --- a/src/Tests.cmake +++ b/src/Tests.cmake @@ -1,5 +1,8 @@ include_guard() +include("${CMAKE_CURRENT_LIST_DIR}/Utilities.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/Symlink.cmake") + # Enable coverage reporting for gcc/clang function(enable_coverage _project_name) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") @@ -7,3 +10,367 @@ function(enable_coverage _project_name) target_link_libraries(${_project_name} INTERFACE --coverage) endif() endfunction() + +function(_configure_target target_name type) + set(options) + set(one_value_args) + set(multi_value_args + SOURCES + INCLUDES + SYSTEM_INCLUDES + DEPENDENCIES_CONFIG + DEPENDENCIES + LIBRARIES + SYSTEM_LIBRARIES + COMPILE_DEFINITIONS + COMPILE_OPTIONS + COMPILE_FEATURES + ) + cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) + + if(${type} STREQUAL "library_test") + add_executable(${target_name}) + set(scope PRIVATE) + elseif(${type} STREQUAL "test_config") + if(NOT TARGET ${target_name}) + add_library(${target_name} INTERFACE) + endif() + + set(scope INTERFACE) + endif() + + target_sources(${target_name} + ${scope} + ${args_SOURCES} + ) + target_include_directories(${target_name} + ${scope} + ${args_INCLUDES} + ) + target_link_libraries(${target_name} + ${scope} + ${args_LIBRARIES} + ) + target_include_system_directories(${target_name} + ${scope} + ${args_SYSTEM_INCLUDES} + ) + target_find_dependencies(${target_name} + "${scope}_CONFIG" + ${args_DEPENDENCIES_CONFIG} + + ${scope} + ${args_DEPENDENCIES} + ) + target_link_system_libraries(${target_name} + ${scope} + ${args_SYSTEM_LIBRARIES} + ) + target_compile_definitions(${target_name} + ${scope} + ${args_COMPILE_DEFINITIONS} + ) + target_compile_options(${target_name} + ${scope} + ${args_COMPILE_OPTIONS} + ) + target_compile_features(${target_name} + ${scope} + ${args_COMPILE_FEATURES} + ) +endfunction() + +function(_Set_config_execute_args target_name execute_args) + set_or_append_target_property(${target_name} PROJECT_OPTIONS_EXECUTE_ARGS ${execute_args}) +endfunction() + +#[[.rst: + +``add_test_config`` +====================== + +.. code:: cmake + + add_test_config( + [SOURCES ] + [INCLUDES ] + [SYSTEM_INCLUDES ] + [DEPENDENCIES_CONFIG ] # find_package( CONFIG REQUIRED) + [DEPENDENCIES ] # find_package( REQUIRED) + [LIBRARIES ] + [SYSTEM_LIBRARIES ] + [COMPILE_DEFINITIONS ] + [COMPILE_OPTIONS ] + [COMPILE_FEATURES ] + [EXECUTE_ARGS ] # Args used as command args running the test + ) + +This function generates a INTERFACE library named ``test_config.``, +so ``add_library_test`` and ``add_executable_test`` can simply reuse test configs. + +You can call this function with the same ```` multiple times +to add more args. + +.. code:: cmake + + add_test_config(common + COMPILE_DEFINITIONS + BOOST_UT_DISABLE_MODULE=1 + ) + add_test_config(common + DEPENDENCIES_CONFIG + ut + + LIBRARIES + boost-ext-ut::ut + ) + +]] +function(add_test_config config_name) + set(options) + set(one_value_args) + set(multi_value_args + EXECUTE_ARGS + + SOURCES + INCLUDES + SYSTEM_INCLUDES + DEPENDENCIES_CONFIG + DEPENDENCIES + LIBRARIES + SYSTEM_LIBRARIES + COMPILE_DEFINITIONS + COMPILE_OPTIONS + COMPILE_FEATURES + ) + cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) + + set(target_name "test_config.${config_name}") + _configure_target(${target_name} test_config + SOURCES + ${args_SOURCES} + INCLUDES + ${args_INCLUDES} + SYSTEM_INCLUDES + ${args_SYSTEM_INCLUDES} + DEPENDENCIES_CONFIG + ${args_DEPENDENCIES_CONFIG} + DEPENDENCIES + ${args_DEPENDENCIES} + LIBRARIES + ${args_LIBRARIES} + SYSTEM_LIBRARIES + ${args_SYSTEM_LIBRARIES} + COMPILE_DEFINITIONS + ${args_COMPILE_DEFINITIONS} + COMPILE_OPTIONS + ${args_COMPILE_OPTIONS} + COMPILE_FEATURES + ${args_COMPILE_FEATURES} + ) + + _Set_config_execute_args(${target_name} "${args_EXECUTE_ARGS}") +endfunction() + +function(_get_configs_execute_args variable_name) + set(value) + + foreach(config IN LISTS ARGN) + get_target_property(execute_args ${config} PROJECT_OPTIONS_EXECUTE_ARGS) + list(APPEND variable_name ${execute_args}) + endforeach() + + set(${variable_name} ${value} PARENT_SCOPE) +endfunction() + +function(_add_configs_prefix variable_name) + set(value) + + foreach(config IN LISTS ARGN) + if(${config} MATCHES "test_config\..*") + list(APPEND value "${config}") + else() + list(APPEND value "test_config.${config}") + endif() + endforeach() + + set(${variable_name} ${value} PARENT_SCOPE) +endfunction() + +#[[.rst: + +``add_library_test`` +====================== + +.. code:: cmake + + add_library_test( + [CONFIGS ] + [SOURCES ] + [INCLUDES ] + [SYSTEM_INCLUDES ] + [DEPENDENCIES_CONFIG ] # find_package( CONFIG REQUIRED) + [DEPENDENCIES ] # find_package( REQUIRED) + [LIBRARIES ] + [SYSTEM_LIBRARIES ] + [COMPILE_DEFINITIONS ] + [COMPILE_OPTIONS ] + [COMPILE_FEATURES ] + [EXECUTE_ARGS ] # Args used as command args running the test + [WORKING_DIRECTOY ] + ) + +This function generates an executable target named +``test..`` that tests the ````, and registers +this target using ``add_test``. + +- ``CONFIGS``: Accepts both ```` and the full name + ``test_config.``. If multiple configs are given, they will be + merged. + +you can't call this function with the same ```` and ```` +multiple times to add more args. + +.. code:: cmake + + add_test_config(common + DEPENDENCIES_CONFIG + ut + + LIBRARIES + boost-ext-ut::ut + ) + + add_test_config(constexpr + COMPILE_DEFINITIONS + -DENABLE_CONSTEXPR + ) + + # Same name for test and config is ok, since they are differently prefixed + add_library_test(lib constexpr CONFIGS common constexpr SOURCES constexpr.cpp) + +]] +function(add_library_test library test_name) + set(options) + set(one_value_args + WORKING_DIRECTOY + ) + set(multi_value_args + CONFIGS + EXECUTE_ARGS + + SOURCES + INCLUDES + SYSTEM_INCLUDES + DEPENDENCIES_CONFIG + DEPENDENCIES + LIBRARIES + SYSTEM_LIBRARIES + COMPILE_DEFINITIONS + COMPILE_OPTIONS + COMPILE_FEATURES + ) + cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) + + if(NOT args_WORKING_DIRECTORY) + set(args_WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + endif() + + _add_configs_prefix(prefixed_configs ${args_CONFIGS}) + + set(target_name "test.${library}.${test_name}") + _configure_target(${target_name} library_test + SOURCES + ${args_SOURCES} + INCLUDES + ${args_INCLUDES} + SYSTEM_INCLUDES + ${args_SYSTEM_INCLUDES} + DEPENDENCIES_CONFIG + ${args_DEPENDENCIES_CONFIG} + DEPENDENCIES + ${args_DEPENDENCIES} + LIBRARIES + ${args_LIBRARIES} + SYSTEM_LIBRARIES + ${args_SYSTEM_LIBRARIES} + COMPILE_DEFINITIONS + ${args_COMPILE_DEFINITIONS} + COMPILE_OPTIONS + ${args_COMPILE_OPTIONS} + COMPILE_FEATURES + ${args_COMPILE_FEATURES} + ) + + target_link_libraries(${target_name} + PRIVATE + ${prefixed_configs} + ${library} + ) + + _get_configs_execute_args(configs_execute_args ${prefixed_configs}) + add_test( + NAME ${target_name} + COMMAND ${target_name} ${configs_execute_args} ${args_EXECUTE_ARGS} + WORKING_DIRECTORY ${args_WORKING_DIRECTORY} + ) +endfunction() + +#[[.rst: + +``add_executable_test`` +====================== + +.. code:: cmake + + add_executable_test( + [CONFIGS ] # Only accepts the EXECUTE_ARGS part in CONFIGS + [EXECUTE_ARGS ] # Args used as command args running the test + ) + +This function registers a test named ``test..`` that +runs the ```` using ``EXECUTE_ARGS``. + +- ``CONFIGS``: Accepts both ```` and the full name + ``test_config.``. If multiple configs are given, they will be + merged. + +You can't call this function with the same ```` and ```` +multiple times to add more args. + +.. code:: cmake + + add_test_config(report + EXECUTE_ARGS + --reporter xml + ) + + add_executable_test(exe no_arg) + add_executable_test(exe report + CONFIGS report + EXECUTE_ARGS --verbose + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + ) + +]] +function(add_executable_test executable test_name) + set(options) + set(one_value_args WORKING_DIRECTORY) + set(multi_value_args CONFIGS EXECUTE_ARGS) + cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) + + if(NOT args_WORKING_DIRECTORY) + set(args_WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + endif() + + _add_configs_prefix(prefixed_configs ${args_CONFIGS}) + + set(target_name "test.${executable}.${test_name}") + + _get_configs_execute_args(configs_execute_args ${prefixed_configs}) + add_test( + NAME ${target_name} + COMMAND ${executable} ${configs_execute_args} ${args_EXECUTE_ARGS} + WORKING_DIRECTORY ${args_WORKING_DIRECTORY} + ) +endfunction() \ No newline at end of file diff --git a/src/Utilities.cmake b/src/Utilities.cmake index 25086238..a38dfdc6 100644 --- a/src/Utilities.cmake +++ b/src/Utilities.cmake @@ -172,4 +172,18 @@ function(convert_genex_semicolons genex output) endwhile() set("${output}" "${result}" PARENT_SCOPE) +endfunction() + +function(set_or_append_target_property target property new_values) + get_target_property(_AllValues ${target} ${property}) + + if(NOT _AllValues) # If the property hasn't set + set(_AllValues "${new_values}") + else() + list(APPEND _AllValues ${new_values}) + endif() + + list(REMOVE_DUPLICATES _AllValues) + + set_target_properties(${target} PROPERTIES ${property} "${_AllValues}") endfunction() \ No newline at end of file From b38fc9ac8266a06bf56b69a7927cd4b244a5f7b2 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 2 Oct 2023 10:53:13 +0800 Subject: [PATCH 02/13] Use set_property(APPEND) --- src/PackageProject.cmake | 10 +++++----- src/Tests.cmake | 3 +-- src/Utilities.cmake | 14 -------------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/src/PackageProject.cmake b/src/PackageProject.cmake index 70aa31bb..5d27270c 100644 --- a/src/PackageProject.cmake +++ b/src/PackageProject.cmake @@ -1,7 +1,5 @@ include_guard() -include("${CMAKE_CURRENT_LIST_DIR}/Utilities.cmake") - # Uses ycm (permissive BSD-3-Clause license) and ForwardArguments (permissive MIT license) function(get_property_of_targets) @@ -365,7 +363,7 @@ function(target_include_interface_directories target) endif() # Append include_dir to target property PROJECT_OPTIONS_INTERFACE_DIRECTORIES - set_or_append_target_property(${target} "PROJECT_OPTIONS_INTERFACE_DIRECTORIES" ${include_dir}) + set_property(TARGET ${target} APPEND PROPERTY PROJECT_OPTIONS_INTERFACE_DIRECTORIES ${include_dir}) # Include the interface directory get_target_property(_HasSourceFiles ${target} SOURCES) @@ -468,8 +466,10 @@ macro(target_find_dependencies target) endforeach() # Append to target property - set_or_append_target_property( - ${target} "PROJECT_OPTIONS_${property}_DEPENDENCIES" "${args_${property}}" + set_property(TARGET ${target} + APPEND + PROPERTY "PROJECT_OPTIONS_${property}_DEPENDENCIES" + "${args_${property}}" ) endmacro() diff --git a/src/Tests.cmake b/src/Tests.cmake index 415571b2..c74275f8 100644 --- a/src/Tests.cmake +++ b/src/Tests.cmake @@ -1,6 +1,5 @@ include_guard() -include("${CMAKE_CURRENT_LIST_DIR}/Utilities.cmake") include("${CMAKE_CURRENT_LIST_DIR}/Symlink.cmake") # Enable coverage reporting for gcc/clang @@ -81,7 +80,7 @@ function(_configure_target target_name type) endfunction() function(_Set_config_execute_args target_name execute_args) - set_or_append_target_property(${target_name} PROJECT_OPTIONS_EXECUTE_ARGS ${execute_args}) + set_property(TARGET ${target_name} APPEND PROPERTY PROJECT_OPTIONS_EXECUTE_ARGS ${execute_args}) endfunction() #[[.rst: diff --git a/src/Utilities.cmake b/src/Utilities.cmake index a38dfdc6..25086238 100644 --- a/src/Utilities.cmake +++ b/src/Utilities.cmake @@ -172,18 +172,4 @@ function(convert_genex_semicolons genex output) endwhile() set("${output}" "${result}" PARENT_SCOPE) -endfunction() - -function(set_or_append_target_property target property new_values) - get_target_property(_AllValues ${target} ${property}) - - if(NOT _AllValues) # If the property hasn't set - set(_AllValues "${new_values}") - else() - list(APPEND _AllValues ${new_values}) - endif() - - list(REMOVE_DUPLICATES _AllValues) - - set_target_properties(${target} PROPERTIES ${property} "${_AllValues}") endfunction() \ No newline at end of file From ea0c49397a071a820b1e07ac998870a71f3aee4f Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 2 Oct 2023 10:54:38 +0800 Subject: [PATCH 03/13] typo: SymLink -> SystemLink --- src/Tests.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tests.cmake b/src/Tests.cmake index c74275f8..aad435c8 100644 --- a/src/Tests.cmake +++ b/src/Tests.cmake @@ -1,6 +1,6 @@ include_guard() -include("${CMAKE_CURRENT_LIST_DIR}/Symlink.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/Systemlink.cmake") # Enable coverage reporting for gcc/clang function(enable_coverage _project_name) From d8fce36c4e48c0c217449f8400c1e4790927427e Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 2 Oct 2023 10:56:17 +0800 Subject: [PATCH 04/13] Add WORKING_DIRECTORY to add_executable_test docs [skip ci] --- src/Tests.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Tests.cmake b/src/Tests.cmake index aad435c8..4b6d9770 100644 --- a/src/Tests.cmake +++ b/src/Tests.cmake @@ -325,6 +325,7 @@ endfunction() add_executable_test( [CONFIGS ] # Only accepts the EXECUTE_ARGS part in CONFIGS [EXECUTE_ARGS ] # Args used as command args running the test + [WORKING_DIRECTOY ] ) This function registers a test named ``test..`` that From a104d9eaae0bfd975cabe0a5e6f27800b4de2f55 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 2 Oct 2023 11:00:25 +0800 Subject: [PATCH 05/13] Add option WILL_FAIL --- src/Tests.cmake | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Tests.cmake b/src/Tests.cmake index 4b6d9770..e088da80 100644 --- a/src/Tests.cmake +++ b/src/Tests.cmake @@ -196,6 +196,10 @@ function(_add_configs_prefix variable_name) set(${variable_name} ${value} PARENT_SCOPE) endfunction() +function(_set_will_fail test_name) + set_property(TEST ${test_name} PROPERTY WILL_FAIL TRUE) +endfunction() + #[[.rst: ``add_library_test`` @@ -313,6 +317,10 @@ function(add_library_test library test_name) COMMAND ${target_name} ${configs_execute_args} ${args_EXECUTE_ARGS} WORKING_DIRECTORY ${args_WORKING_DIRECTORY} ) + + if(args_WILL_FAIL) + _set_will_fail(${target_name}) + endif() endfunction() #[[.rst: @@ -326,6 +334,7 @@ endfunction() [CONFIGS ] # Only accepts the EXECUTE_ARGS part in CONFIGS [EXECUTE_ARGS ] # Args used as command args running the test [WORKING_DIRECTOY ] + [WILL_FAIL] # The test should exists with code non-zero ) This function registers a test named ``test..`` that @@ -350,11 +359,12 @@ multiple times to add more args. CONFIGS report EXECUTE_ARGS --verbose WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + WILL_FAIL ) ]] function(add_executable_test executable test_name) - set(options) + set(options WILL_FAIL) set(one_value_args WORKING_DIRECTORY) set(multi_value_args CONFIGS EXECUTE_ARGS) cmake_parse_arguments(args "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) @@ -373,4 +383,8 @@ function(add_executable_test executable test_name) COMMAND ${executable} ${configs_execute_args} ${args_EXECUTE_ARGS} WORKING_DIRECTORY ${args_WORKING_DIRECTORY} ) + + if(args_WILL_FAIL) + _set_will_fail(${target_name}) + endif() endfunction() \ No newline at end of file From 16fa82bbfddf8d81913d44a8d8f0801389bf6bb2 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 2 Oct 2023 11:08:12 +0800 Subject: [PATCH 06/13] Forbid multiple call of add test wrappers --- src/Tests.cmake | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/Tests.cmake b/src/Tests.cmake index e088da80..62c3eecb 100644 --- a/src/Tests.cmake +++ b/src/Tests.cmake @@ -31,10 +31,7 @@ function(_configure_target target_name type) add_executable(${target_name}) set(scope PRIVATE) elseif(${type} STREQUAL "test_config") - if(NOT TARGET ${target_name}) - add_library(${target_name} INTERFACE) - endif() - + add_library(${target_name} INTERFACE) set(scope INTERFACE) endif() @@ -107,8 +104,8 @@ endfunction() This function generates a INTERFACE library named ``test_config.``, so ``add_library_test`` and ``add_executable_test`` can simply reuse test configs. -You can call this function with the same ```` multiple times -to add more args. +To avoid confusion (the name says ``add``), you can't call this function with the +same ```` multiple times to add more args. .. code:: cmake @@ -116,13 +113,6 @@ to add more args. COMPILE_DEFINITIONS BOOST_UT_DISABLE_MODULE=1 ) - add_test_config(common - DEPENDENCIES_CONFIG - ut - - LIBRARIES - boost-ext-ut::ut - ) ]] function(add_test_config config_name) @@ -231,8 +221,8 @@ this target using ``add_test``. ``test_config.``. If multiple configs are given, they will be merged. -you can't call this function with the same ```` and ```` -multiple times to add more args. +To avoid confusion (the name says ``add``), you can't call this function with +the same ```` and ```` multiple times to add more args. .. code:: cmake @@ -344,8 +334,8 @@ runs the ```` using ``EXECUTE_ARGS``. ``test_config.``. If multiple configs are given, they will be merged. -You can't call this function with the same ```` and ```` -multiple times to add more args. +To avoid confusion (the name says ``add``), You can't call this function with +the same ```` and ```` multiple times to add more args. .. code:: cmake From b8836b05824d343a6f0c0ca253d1e97dd70f947e Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 2 Oct 2023 11:17:53 +0800 Subject: [PATCH 07/13] Insert newline at eof --- src/Tests.cmake | 2 +- src/Utilities.cmake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tests.cmake b/src/Tests.cmake index 62c3eecb..e58aad48 100644 --- a/src/Tests.cmake +++ b/src/Tests.cmake @@ -377,4 +377,4 @@ function(add_executable_test executable test_name) if(args_WILL_FAIL) _set_will_fail(${target_name}) endif() -endfunction() \ No newline at end of file +endfunction() diff --git a/src/Utilities.cmake b/src/Utilities.cmake index 25086238..06cfea9c 100644 --- a/src/Utilities.cmake +++ b/src/Utilities.cmake @@ -172,4 +172,4 @@ function(convert_genex_semicolons genex output) endwhile() set("${output}" "${result}" PARENT_SCOPE) -endfunction() \ No newline at end of file +endfunction() From e9f4e4b8aca9d5f5c34733041df1bb514e63e71f Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 2 Oct 2023 11:20:20 +0800 Subject: [PATCH 08/13] typo: Systemlink -> SystemLink --- src/Tests.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tests.cmake b/src/Tests.cmake index e58aad48..b71aced7 100644 --- a/src/Tests.cmake +++ b/src/Tests.cmake @@ -1,6 +1,6 @@ include_guard() -include("${CMAKE_CURRENT_LIST_DIR}/Systemlink.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/SystemLink.cmake") # Enable coverage reporting for gcc/clang function(enable_coverage _project_name) From 1aa43bec8907a1b560898b4ae31e024e097b7b72 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Tue, 17 Oct 2023 15:31:46 +0800 Subject: [PATCH 09/13] Add tests for test wrappers --- tests/install/CMakeLists.txt | 2 +- tests/myproj/CMakeLists.txt | 13 ++++++++++++- tests/myproj/tests/mylib/lib.cpp | 9 +++++++++ tests/myproj/tests/mylib2/lib.cpp | 9 +++++++++ tests/myproj/vcpkg.json | 4 ++++ 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/myproj/tests/mylib/lib.cpp create mode 100644 tests/myproj/tests/mylib2/lib.cpp diff --git a/tests/install/CMakeLists.txt b/tests/install/CMakeLists.txt index e4a78b9d..9ae5d314 100644 --- a/tests/install/CMakeLists.txt +++ b/tests/install/CMakeLists.txt @@ -48,4 +48,4 @@ target_link_libraries(another_main PRIVATE myproj::lib myproj::lib2) # tests enable_testing() -add_test(NAME another_main COMMAND another_main) +add_executable_test(another_main no_arg) diff --git a/tests/myproj/CMakeLists.txt b/tests/myproj/CMakeLists.txt index 1a29ddbf..63432943 100644 --- a/tests/myproj/CMakeLists.txt +++ b/tests/myproj/CMakeLists.txt @@ -97,7 +97,7 @@ add_subdirectory(libs) ## tests enable_testing() -add_test(NAME main COMMAND main) +add_executable_test(main no_arg) # Header-only library add_library(lib INTERFACE) @@ -120,6 +120,17 @@ target_link_libraries(lib2 PRIVATE myproj_project_options myproj_project_warning target_link_system_libraries(lib2 PRIVATE fmt::fmt Eigen3::Eigen) target_link_system_libraries(lib2 PRIVATE mythirdpartylib) +add_test_config(common + DEPENDENCIES_CONFIG + ut + + SYSTEM_LIBRARIES + Boost::ut +) + +add_library_test(lib lib CONFIGS common SOURCES tests/mylib/lib.cpp) +add_library_test(lib2 lib CONFIGS common SOURCES tests/mylib2/lib.cpp) + # package everything automatically package_project( # NAME diff --git a/tests/myproj/tests/mylib/lib.cpp b/tests/myproj/tests/mylib/lib.cpp new file mode 100644 index 00000000..09e7e355 --- /dev/null +++ b/tests/myproj/tests/mylib/lib.cpp @@ -0,0 +1,9 @@ +#include "mylib/lib.hpp" + +#include + +int main() { // NOLINT(bugprone-exception-escape) + using namespace boost::ut; // NOLINT(*using-namespace*) + + "lib"_test = []() { expect(some_fun() == 0); }; +} \ No newline at end of file diff --git a/tests/myproj/tests/mylib2/lib.cpp b/tests/myproj/tests/mylib2/lib.cpp new file mode 100644 index 00000000..b1ef69ae --- /dev/null +++ b/tests/myproj/tests/mylib2/lib.cpp @@ -0,0 +1,9 @@ +#include "mylib2/lib.hpp" + +#include + +int main() { // NOLINT(bugprone-exception-escape) + using namespace boost::ut; // NOLINT(*using-namespace*) + + "lib"_test = []() { expect(some_fun2() == 0); }; +} \ No newline at end of file diff --git a/tests/myproj/vcpkg.json b/tests/myproj/vcpkg.json index c4cbbf8c..d0d974f5 100644 --- a/tests/myproj/vcpkg.json +++ b/tests/myproj/vcpkg.json @@ -11,6 +11,10 @@ { "name": "fmt", "version>=": "9.1.0#1" + }, + { + "name": "bext-ut", + "version>=": "1.1.9" } ] } From d915c0b803d8a9d20f1c449392e80147a463c687 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Tue, 17 Oct 2023 15:56:54 +0800 Subject: [PATCH 10/13] Remove dependency on boost-ext-ut --- tests/myproj/CMakeLists.txt | 7 ++----- tests/myproj/tests/mylib/lib.cpp | 8 ++------ tests/myproj/tests/mylib2/lib.cpp | 8 ++------ tests/myproj/vcpkg.json | 4 ---- 4 files changed, 6 insertions(+), 21 deletions(-) diff --git a/tests/myproj/CMakeLists.txt b/tests/myproj/CMakeLists.txt index 63432943..b9f1c9c4 100644 --- a/tests/myproj/CMakeLists.txt +++ b/tests/myproj/CMakeLists.txt @@ -121,11 +121,8 @@ target_link_system_libraries(lib2 PRIVATE fmt::fmt Eigen3::Eigen) target_link_system_libraries(lib2 PRIVATE mythirdpartylib) add_test_config(common - DEPENDENCIES_CONFIG - ut - - SYSTEM_LIBRARIES - Boost::ut + COMPILE_DEFINITIONS + EXPECTED_RESULT=0 ) add_library_test(lib lib CONFIGS common SOURCES tests/mylib/lib.cpp) diff --git a/tests/myproj/tests/mylib/lib.cpp b/tests/myproj/tests/mylib/lib.cpp index 09e7e355..ab6a656b 100644 --- a/tests/myproj/tests/mylib/lib.cpp +++ b/tests/myproj/tests/mylib/lib.cpp @@ -1,9 +1,5 @@ #include "mylib/lib.hpp" -#include - -int main() { // NOLINT(bugprone-exception-escape) - using namespace boost::ut; // NOLINT(*using-namespace*) - - "lib"_test = []() { expect(some_fun() == 0); }; +int main() { // NOLINT(bugprone-exception-escape) + return some_fun() == EXPECTED_RESULT ? 0 : 1; } \ No newline at end of file diff --git a/tests/myproj/tests/mylib2/lib.cpp b/tests/myproj/tests/mylib2/lib.cpp index b1ef69ae..ec11316c 100644 --- a/tests/myproj/tests/mylib2/lib.cpp +++ b/tests/myproj/tests/mylib2/lib.cpp @@ -1,9 +1,5 @@ #include "mylib2/lib.hpp" -#include - -int main() { // NOLINT(bugprone-exception-escape) - using namespace boost::ut; // NOLINT(*using-namespace*) - - "lib"_test = []() { expect(some_fun2() == 0); }; +int main() { // NOLINT(bugprone-exception-escape) + return some_fun2() == EXPECTED_RESULT ? 0 : 1; } \ No newline at end of file diff --git a/tests/myproj/vcpkg.json b/tests/myproj/vcpkg.json index d0d974f5..c4cbbf8c 100644 --- a/tests/myproj/vcpkg.json +++ b/tests/myproj/vcpkg.json @@ -11,10 +11,6 @@ { "name": "fmt", "version>=": "9.1.0#1" - }, - { - "name": "bext-ut", - "version>=": "1.1.9" } ] } From da028891ca9703ffb57bd01ad5fa81eff747cae8 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Wed, 18 Oct 2023 23:16:10 +0800 Subject: [PATCH 11/13] Check property existence before use --- src/Tests.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Tests.cmake b/src/Tests.cmake index b71aced7..f24a3a58 100644 --- a/src/Tests.cmake +++ b/src/Tests.cmake @@ -166,7 +166,9 @@ function(_get_configs_execute_args variable_name) foreach(config IN LISTS ARGN) get_target_property(execute_args ${config} PROJECT_OPTIONS_EXECUTE_ARGS) - list(APPEND variable_name ${execute_args}) + if(execute_args) + list(APPEND variable_name ${execute_args}) + endif() endforeach() set(${variable_name} ${value} PARENT_SCOPE) From f62419dee470d5d788a9722a8759dc1595b40bdc Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Wed, 14 Feb 2024 14:49:46 +0800 Subject: [PATCH 12/13] Add an executable test with args in tests --- tests/myproj/CMakeLists.txt | 3 +++ tests/myproj/src/main/main.cpp | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/myproj/CMakeLists.txt b/tests/myproj/CMakeLists.txt index b9f1c9c4..6c526bc9 100644 --- a/tests/myproj/CMakeLists.txt +++ b/tests/myproj/CMakeLists.txt @@ -98,6 +98,9 @@ add_subdirectory(libs) ## tests enable_testing() add_executable_test(main no_arg) +add_executable_test(main with_arg + EXECUTE_ARGS hello world +) # Header-only library add_library(lib INTERFACE) diff --git a/tests/myproj/src/main/main.cpp b/tests/myproj/src/main/main.cpp index 65c8ad70..b97e16cb 100644 --- a/tests/myproj/src/main/main.cpp +++ b/tests/myproj/src/main/main.cpp @@ -15,7 +15,11 @@ #include #include -int main() { +int main(int argc, char* argv[]) { + if (argc > 1) { + fmt::print("[{}]", fmt::join(argv + 1, argv + argc, ", ")); + } + fmt::print("Hello from fmt{}", "!"); Eigen::VectorXd eigen_vec = Eigen::Vector3d(1, 2, 3); From 2c07ac9c065e2ca69381aec26f71148fcbc18722 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Mon, 6 May 2024 14:54:21 +0800 Subject: [PATCH 13/13] Distinguish library test config types based on sources --- src/Tests.cmake | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Tests.cmake b/src/Tests.cmake index f24a3a58..f07ca6c1 100644 --- a/src/Tests.cmake +++ b/src/Tests.cmake @@ -29,14 +29,22 @@ function(_configure_target target_name type) if(${type} STREQUAL "library_test") add_executable(${target_name}) + set(source_scope PRIVATE) set(scope PRIVATE) elseif(${type} STREQUAL "test_config") - add_library(${target_name} INTERFACE) - set(scope INTERFACE) + if(args_SOURCES) + add_library(${target_name} STATIC) + set(source_scope PRIVATE) + set(scope PUBLIC) + else() + add_library(${target_name} INTERFACE) + set(source_scope INTERFACE) + set(scope INTERFACE) + endif() endif() target_sources(${target_name} - ${scope} + ${source_scope} ${args_SOURCES} ) target_include_directories(${target_name}