|
| 1 | +# - Add tests using boost::test |
| 2 | +# |
| 3 | +# Add this line to your test files in place of including a basic boost test header: |
| 4 | +# #include <BoostTestTargetConfig.h> |
| 5 | +# |
| 6 | +# If you cannot do that and must use the included form for a given test, |
| 7 | +# include the line |
| 8 | +# // OVERRIDE_BOOST_TEST_INCLUDED_WARNING |
| 9 | +# in the same file with the boost test include. |
| 10 | +# |
| 11 | +# include(BoostTestTargets) |
| 12 | +# add_boost_test(<testdriver_name> SOURCES <source1> [<more sources...>] |
| 13 | +# [LIBRARIES <library> [<library>...]] |
| 14 | +# [RESOURCES <resource> [<resource>...]] |
| 15 | +# [TESTS <testcasename> [<testcasename>...]]) |
| 16 | +# |
| 17 | +# If for some reason you need access to the executable target created, |
| 18 | +# it is ${BOOST_TEST_TARGET_PREFIX}${testdriver_name} as specified when |
| 19 | +# you called add_boost_test |
| 20 | +# |
| 21 | +# Requires CMake 2.6 or newer (uses the 'function' command) |
| 22 | +# |
| 23 | +# Requires: |
| 24 | +# GetForceIncludeDefinitions |
| 25 | +# CopyResourcesToBuildTree |
| 26 | +# |
| 27 | +# Original Author: |
| 28 | + |
| 29 | +# http://academic.cleardefinition.com |
| 30 | +# Iowa State University HCI Graduate Program/VRAC |
| 31 | + |
| 32 | +if(__add_boost_test) |
| 33 | + return() |
| 34 | +endif() |
| 35 | +set(__add_boost_test YES) |
| 36 | + |
| 37 | +set(BOOST_TEST_TARGET_PREFIX "boosttesttarget_") |
| 38 | + |
| 39 | +if(NOT Boost_FOUND) |
| 40 | + find_package(Boost 1.34.0 QUIET) |
| 41 | +endif() |
| 42 | +if("${Boost_VERSION}0" LESS "1034000") |
| 43 | + set(_shared_msg "NOTE: boost::test-based targets and tests cannot " |
| 44 | + "be added: boost >= 1.34.0 required but not found. " |
| 45 | + "(found: '${Boost_VERSION}'; want >=103400) ") |
| 46 | + if(BUILD_TESTING) |
| 47 | + message(FATAL_ERROR ${_shared_msg} |
| 48 | + "You may disable BUILD_TESTING to continue without the " |
| 49 | + "tests.") |
| 50 | + else() |
| 51 | + message(STATUS ${_shared_msg} |
| 52 | + "BUILD_TESTING disabled, so continuing anyway.") |
| 53 | + endif() |
| 54 | +endif() |
| 55 | + |
| 56 | +include(GetForceIncludeDefinitions) |
| 57 | +include(CopyResourcesToBuildTree) |
| 58 | + |
| 59 | +if(Boost_FOUND AND NOT "${Boost_VERSION}0" LESS "1034000") |
| 60 | + if(NOT Boost_UNIT_TEST_FRAMEWORK_LIBRARY) |
| 61 | + find_package(Boost 1.34.0 QUIET COMPONENTS unit_test_framework) |
| 62 | + endif() |
| 63 | + if(Boost_UNIT_TEST_FRAMEWORK_LIBRARY) |
| 64 | + set(_boosttesttargets_libs ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}) |
| 65 | + if(Boost_USE_STATIC_LIBS) |
| 66 | + set(_boostConfig "BoostTestTargetsStatic.h") |
| 67 | + else() |
| 68 | + set(_boostConfig "BoostTestTargetsDynamic.h") |
| 69 | + endif() |
| 70 | + else() |
| 71 | + set(_boosttesttargets_libs) |
| 72 | + set(_boostConfig "BoostTestTargetsIncluded.h") |
| 73 | + endif() |
| 74 | + get_filename_component(_moddir ${CMAKE_CURRENT_LIST_FILE} PATH) |
| 75 | + configure_file("${_moddir}/${_boostConfig}" "${CMAKE_CURRENT_BINARY_DIR}/BoostTestTargetConfig.h" COPYONLY) |
| 76 | + include_directories("${CMAKE_CURRENT_BINARY_DIR}") |
| 77 | +endif() |
| 78 | + |
| 79 | +function(add_boost_test _name) |
| 80 | + if(NOT BUILD_TESTING) |
| 81 | + return() |
| 82 | + endif() |
| 83 | + # parse arguments |
| 84 | + set(_nowhere) |
| 85 | + set(_curdest _nowhere) |
| 86 | + set(_val_args |
| 87 | + SOURCES |
| 88 | + LIBRARIES |
| 89 | + RESOURCES |
| 90 | + TESTS) |
| 91 | + set(_bool_args |
| 92 | + USE_COMPILED_LIBRARY) |
| 93 | + foreach(_arg ${_val_args} ${_bool_args}) |
| 94 | + set(${_arg}) |
| 95 | + endforeach() |
| 96 | + foreach(_element ${ARGN}) |
| 97 | + list(FIND _val_args "${_element}" _val_arg_find) |
| 98 | + list(FIND _bool_args "${_element}" _bool_arg_find) |
| 99 | + if("${_val_arg_find}" GREATER "-1") |
| 100 | + set(_curdest "${_element}") |
| 101 | + elseif("${_bool_arg_find}" GREATER "-1") |
| 102 | + set("${_element}" ON) |
| 103 | + set(_curdest _nowhere) |
| 104 | + else() |
| 105 | + list(APPEND ${_curdest} "${_element}") |
| 106 | + endif() |
| 107 | + endforeach() |
| 108 | + |
| 109 | + if(_nowhere) |
| 110 | + message(FATAL_ERROR "Syntax error in use of add_boost_test!") |
| 111 | + endif() |
| 112 | + |
| 113 | + if(NOT SOURCES) |
| 114 | + message(FATAL_ERROR "Syntax error in use of add_boost_test: at least one source file required!") |
| 115 | + endif() |
| 116 | + |
| 117 | + if(Boost_FOUND AND NOT "${Boost_VERSION}0" LESS "1034000") |
| 118 | + |
| 119 | + include_directories(${Boost_INCLUDE_DIRS}) |
| 120 | + |
| 121 | + set(includeType) |
| 122 | + foreach(src ${SOURCES}) |
| 123 | + file(READ ${src} thefile) |
| 124 | + if("${thefile}" MATCHES ".*BoostTestTargetConfig.h.*") |
| 125 | + set(includeType CONFIGURED) |
| 126 | + set(includeFileLoc ${src}) |
| 127 | + break() |
| 128 | + elseif("${thefile}" MATCHES ".*boost/test/included/unit_test.hpp.*") |
| 129 | + set(includeType INCLUDED) |
| 130 | + set(includeFileLoc ${src}) |
| 131 | + set(_boosttesttargets_libs) # clear this out - linking would be a bad idea |
| 132 | + if(NOT "${thefile}" MATCHES ".*OVERRIDE_BOOST_TEST_INCLUDED_WARNING.*") |
| 133 | + message("Please replace the include line in ${src} with this alternate include line instead:") |
| 134 | + message(" \#include <BoostTestTargetConfig.h>") |
| 135 | + message("Once you've saved your changes, re-run CMake. (See BoostTestTargets.cmake for more info)") |
| 136 | + endif() |
| 137 | + break() |
| 138 | + endif() |
| 139 | + endforeach() |
| 140 | + |
| 141 | + if(NOT _boostTestTargetsNagged${_name} STREQUAL "${includeType}") |
| 142 | + if("includeType" STREQUAL "CONFIGURED") |
| 143 | + message(STATUS "Test '${_name}' uses the CMake-configurable form of the boost test framework - congrats! (Including File: ${includeFileLoc})") |
| 144 | + elseif("${includeType}" STREQUAL "INCLUDED") |
| 145 | + message("In test '${_name}': ${includeFileLoc} uses the 'included' form of the boost unit test framework.") |
| 146 | + else() |
| 147 | + message("In test '${_name}': Didn't detect the CMake-configurable boost test include.") |
| 148 | + message("Please replace your existing boost test include in that test with the following:") |
| 149 | + message(" \#include <BoostTestTargetConfig.h>") |
| 150 | + message("Once you've saved your changes, re-run CMake. (See BoostTestTargets.cmake for more info)") |
| 151 | + endif() |
| 152 | + endif() |
| 153 | + set(_boostTestTargetsNagged${_name} "${includeType}" CACHE INTERNAL "" FORCE) |
| 154 | + |
| 155 | + |
| 156 | + if(RESOURCES) |
| 157 | + list(APPEND SOURCES ${RESOURCES}) |
| 158 | + endif() |
| 159 | + |
| 160 | + add_executable(${BOOST_TEST_TARGET_PREFIX}${_name} ${SOURCES}) |
| 161 | + |
| 162 | + #if(USE_COMPILED_LIBRARY) |
| 163 | + list(APPEND LIBRARIES ${_boosttesttargets_libs}) |
| 164 | + #endif() |
| 165 | + if(LIBRARIES) |
| 166 | + target_link_libraries(${BOOST_TEST_TARGET_PREFIX}${_name} ${LIBRARIES}) |
| 167 | + endif() |
| 168 | + |
| 169 | + if(RESOURCES) |
| 170 | + set_property(TARGET |
| 171 | + ${BOOST_TEST_TARGET_PREFIX}${_name} |
| 172 | + PROPERTY |
| 173 | + RESOURCE |
| 174 | + ${RESOURCES}) |
| 175 | + copy_resources_to_build_tree(${BOOST_TEST_TARGET_PREFIX}${_name}) |
| 176 | + endif() |
| 177 | + |
| 178 | + if(NOT Boost_TEST_FLAGS) |
| 179 | +# set(Boost_TEST_FLAGS --catch_system_error=yes --output_format=XML) |
| 180 | + set(Boost_TEST_FLAGS --catch_system_error=yes) |
| 181 | + endif() |
| 182 | + |
| 183 | + # TODO: Figure out why only recent boost handles individual test running properly |
| 184 | + if(TESTS AND ("${Boost_VERSION}" VERSION_GREATER "103799")) |
| 185 | + foreach(_test ${TESTS}) |
| 186 | + add_test(${_name}-${_test} ${BOOST_TEST_TARGET_PREFIX}${_name} --run_test=${_test} ${Boost_TEST_FLAGS}) |
| 187 | + endforeach() |
| 188 | + else() |
| 189 | + add_test(${_name}-boost::test ${BOOST_TEST_TARGET_PREFIX}${_name} ${Boost_TEST_FLAGS}) |
| 190 | + endif() |
| 191 | + |
| 192 | + # CppCheck the test if we can. |
| 193 | + if(COMMAND add_cppcheck) |
| 194 | + add_cppcheck(${BOOST_TEST_TARGET_PREFIX}${_name} STYLE UNUSED_FUNCTIONS) |
| 195 | + endif() |
| 196 | + |
| 197 | + endif() |
| 198 | +endfunction() |
0 commit comments