| 1 |
#==================================================================================================# |
| 2 |
# supported macros # |
| 3 |
# - TEST_CASE, # |
| 4 |
# - TEMPLATE_TEST_CASE # |
| 5 |
# - SCENARIO, # |
| 6 |
# - TEST_CASE_METHOD, # |
| 7 |
# - CATCH_TEST_CASE, # |
| 8 |
# - CATCH_TEMPLATE_TEST_CASE # |
| 9 |
# - CATCH_SCENARIO, # |
| 10 |
# - CATCH_TEST_CASE_METHOD. # |
| 11 |
# # |
| 12 |
# Usage # |
| 13 |
# 1. make sure this module is in the path or add this otherwise: # |
| 14 |
# set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake.modules/") # |
| 15 |
# 2. make sure that you've enabled testing option for the project by the call: # |
| 16 |
# enable_testing() # |
| 17 |
# 3. add the lines to the script for testing target (sample CMakeLists.txt): # |
| 18 |
# project(testing_target) # |
| 19 |
# set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake.modules/") # |
| 20 |
# enable_testing() # |
| 21 |
# # |
| 22 |
# find_path(CATCH_INCLUDE_DIR "catch.hpp") # |
| 23 |
# include_directories(${INCLUDE_DIRECTORIES} ${CATCH_INCLUDE_DIR}) # |
| 24 |
# # |
| 25 |
# file(GLOB SOURCE_FILES "*.cpp") # |
| 26 |
# list(FILTER SOURCE_FILES EXCLUDE REGEX "^\\._") # |
| 27 |
# add_executable(${PROJECT_NAME} ${SOURCE_FILES}) # |
| 28 |
# # |
| 29 |
# include(ParseAndAddCatchTests) # |
| 30 |
# ParseAndAddCatchTests(${PROJECT_NAME}) # |
| 31 |
# # |
| 32 |
# The following variables affect the behavior of the script: # |
| 33 |
# # |
| 34 |
# PARSE_CATCH_TESTS_VERBOSE (Default OFF) # |
| 35 |
# -- enables debug messages # |
| 36 |
# PARSE_CATCH_TESTS_NO_HIDDEN_TESTS (Default OFF) # |
| 37 |
# -- excludes tests marked with [!hide], [.] or [.foo] tags # |
| 38 |
# PARSE_CATCH_TESTS_ADD_FIXTURE_IN_TEST_NAME (Default ON) # |
| 39 |
# -- adds fixture class name to the test name # |
| 40 |
# PARSE_CATCH_TESTS_ADD_TARGET_IN_TEST_NAME (Default ON) # |
| 41 |
# -- adds cmake target name to the test name # |
| 42 |
# PARSE_CATCH_TESTS_ADD_TO_CONFIGURE_DEPENDS (Default OFF) # |
| 43 |
# -- causes CMake to rerun when file with tests changes so that new tests will be discovered # |
| 44 |
# # |
| 45 |
# One can also set (locally) the optional variable OptionalCatchTestLauncher to precise the way # |
| 46 |
# a test should be run. For instance to use test MPI, one can write # |
| 47 |
# set(OptionalCatchTestLauncher ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${NUMPROC}) # |
| 48 |
# just before calling this ParseAndAddCatchTests function # |
| 49 |
# # |
| 50 |
# The AdditionalCatchParameters optional variable can be used to pass extra argument to the test # |
| 51 |
# command. For example, to include successful tests in the output, one can write # |
| 52 |
# set(AdditionalCatchParameters --success) # |
| 53 |
# # |
| 54 |
# After the script, the ParseAndAddCatchTests_TESTS property for the target, and for each source # |
| 55 |
# file in the target is set, and contains the list of the tests extracted from that target, or # |
| 56 |
# from that file. This is useful, for example to add further labels or properties to the tests. # |
| 57 |
# # |
| 58 |
#==================================================================================================# |
| 59 |
|
| 60 |
if (CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.8) |
| 61 |
message(FATAL_ERROR "ParseAndAddCatchTests requires CMake 2.8.8 or newer") |
| 62 |
endif() |
| 63 |
|
| 64 |
option(PARSE_CATCH_TESTS_VERBOSE "Print Catch to CTest parser debug messages" OFF) |
| 65 |
option(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS "Exclude tests with [!hide], [.] or [.foo] tags" OFF) |
| 66 |
option(PARSE_CATCH_TESTS_ADD_FIXTURE_IN_TEST_NAME "Add fixture class name to the test name" ON) |
| 67 |
option(PARSE_CATCH_TESTS_ADD_TARGET_IN_TEST_NAME "Add target name to the test name" ON) |
| 68 |
option(PARSE_CATCH_TESTS_ADD_TO_CONFIGURE_DEPENDS "Add test file to CMAKE_CONFIGURE_DEPENDS property" OFF) |
| 69 |
|
| 70 |
function(ParseAndAddCatchTests_PrintDebugMessage) |
| 71 |
if(PARSE_CATCH_TESTS_VERBOSE) |
| 72 |
message(STATUS "ParseAndAddCatchTests: ${ARGV}") |
| 73 |
endif() |
| 74 |
endfunction() |
| 75 |
|
| 76 |
# This removes the contents between |
| 77 |
# - block comments (i.e. /* ... */) |
| 78 |
# - full line comments (i.e. // ... ) |
| 79 |
# contents have been read into '${CppCode}'. |
| 80 |
# !keep partial line comments |
| 81 |
function(ParseAndAddCatchTests_RemoveComments CppCode) |
| 82 |
string(ASCII 2 CMakeBeginBlockComment) |
| 83 |
string(ASCII 3 CMakeEndBlockComment) |
| 84 |
string(REGEX REPLACE "/\\*" "${CMakeBeginBlockComment}" ${CppCode} "${${CppCode}}") |
| 85 |
string(REGEX REPLACE "\\*/" "${CMakeEndBlockComment}" ${CppCode} "${${CppCode}}") |
| 86 |
string(REGEX REPLACE "${CMakeBeginBlockComment}[^${CMakeEndBlockComment}]*${CMakeEndBlockComment}" "" ${CppCode} "${${CppCode}}") |
| 87 |
string(REGEX REPLACE "\n[ \t]*//+[^\n]+" "\n" ${CppCode} "${${CppCode}}") |
| 88 |
|
| 89 |
set(${CppCode} "${${CppCode}}" PARENT_SCOPE) |
| 90 |
endfunction() |
| 91 |
|
| 92 |
# Worker function |
| 93 |
function(ParseAndAddCatchTests_ParseFile SourceFile TestTarget) |
| 94 |
# If SourceFile is an object library, do not scan it (as it is not a file). Exit without giving a warning about a missing file. |
| 95 |
if(SourceFile MATCHES "\\\$<TARGET_OBJECTS:.+>") |
| 96 |
ParseAndAddCatchTests_PrintDebugMessage("Detected OBJECT library: ${SourceFile} this will not be scanned for tests.") |
| 97 |
return() |
| 98 |
endif() |
| 99 |
# According to CMake docs EXISTS behavior is well-defined only for full paths. |
| 100 |
get_filename_component(SourceFile ${SourceFile} ABSOLUTE) |
| 101 |
if(NOT EXISTS ${SourceFile}) |
| 102 |
message(WARNING "Cannot find source file: ${SourceFile}") |
| 103 |
return() |
| 104 |
endif() |
| 105 |
ParseAndAddCatchTests_PrintDebugMessage("parsing ${SourceFile}") |
| 106 |
file(STRINGS ${SourceFile} Contents NEWLINE_CONSUME) |
| 107 |
|
| 108 |
# Remove block and fullline comments |
| 109 |
ParseAndAddCatchTests_RemoveComments(Contents) |
| 110 |
|
| 111 |
# Find definition of test names |
| 112 |
# https://regex101.com/r/JygOND/1 |
| 113 |
string(REGEX MATCHALL "[ \t]*(CATCH_)?(TEMPLATE_)?(TEST_CASE_METHOD|SCENARIO|TEST_CASE)[ \t]*\\([ \t\n]*\"[^\"]*\"[ \t\n]*(,[ \t\n]*\"[^\"]*\")?(,[ \t\n]*[^\,\)]*)*\\)[ \t\n]*\{+[ \t]*(//[^\n]*[Tt][Ii][Mm][Ee][Oo][Uu][Tt][ \t]*[0-9]+)*" Tests "${Contents}") |
| 114 |
|
| 115 |
if(PARSE_CATCH_TESTS_ADD_TO_CONFIGURE_DEPENDS AND Tests) |
| 116 |
ParseAndAddCatchTests_PrintDebugMessage("Adding ${SourceFile} to CMAKE_CONFIGURE_DEPENDS property") |
| 117 |
set_property( |
| 118 |
DIRECTORY |
| 119 |
APPEND |
| 120 |
PROPERTY CMAKE_CONFIGURE_DEPENDS ${SourceFile} |
| 121 |
) |
| 122 |
endif() |
| 123 |
|
| 124 |
# check CMP0110 policy for new add_test() behavior |
| 125 |
if(POLICY CMP0110) |
| 126 |
cmake_policy(GET CMP0110 _cmp0110_value) # new add_test() behavior |
| 127 |
else() |
| 128 |
# just to be thorough explicitly set the variable |
| 129 |
set(_cmp0110_value) |
| 130 |
endif() |
| 131 |
|
| 132 |
foreach(TestName ${Tests}) |
| 133 |
# Strip newlines |
| 134 |
string(REGEX REPLACE "\\\\\n|\n" "" TestName "${TestName}") |
| 135 |
|
| 136 |
# Get test type and fixture if applicable |
| 137 |
string(REGEX MATCH "(CATCH_)?(TEMPLATE_)?(TEST_CASE_METHOD|SCENARIO|TEST_CASE)[ \t]*\\([^,^\"]*" TestTypeAndFixture "${TestName}") |
| 138 |
string(REGEX MATCH "(CATCH_)?(TEMPLATE_)?(TEST_CASE_METHOD|SCENARIO|TEST_CASE)" TestType "${TestTypeAndFixture}") |
| 139 |
string(REGEX REPLACE "${TestType}\\([ \t]*" "" TestFixture "${TestTypeAndFixture}") |
| 140 |
|
| 141 |
# Get string parts of test definition |
| 142 |
string(REGEX MATCHALL "\"+([^\\^\"]|\\\\\")+\"+" TestStrings "${TestName}") |
| 143 |
|
| 144 |
# Strip wrapping quotation marks |
| 145 |
string(REGEX REPLACE "^\"(.*)\"$" "\\1" TestStrings "${TestStrings}") |
| 146 |
string(REPLACE "\";\"" ";" TestStrings "${TestStrings}") |
| 147 |
|
| 148 |
# Validate that a test name and tags have been provided |
| 149 |
list(LENGTH TestStrings TestStringsLength) |
| 150 |
if(TestStringsLength GREATER 2 OR TestStringsLength LESS 1) |
| 151 |
message(FATAL_ERROR "You must provide a valid test name and tags for all tests in ${SourceFile}") |
| 152 |
endif() |
| 153 |
|
| 154 |
# Assign name and tags |
| 155 |
list(GET TestStrings 0 Name) |
| 156 |
if("${TestType}" STREQUAL "SCENARIO") |
| 157 |
set(Name "Scenario: ${Name}") |
| 158 |
endif() |
| 159 |
if(PARSE_CATCH_TESTS_ADD_FIXTURE_IN_TEST_NAME AND "${TestType}" MATCHES "(CATCH_)?TEST_CASE_METHOD" AND TestFixture ) |
| 160 |
set(CTestName "${TestFixture}:${Name}") |
| 161 |
else() |
| 162 |
set(CTestName "${Name}") |
| 163 |
endif() |
| 164 |
if(PARSE_CATCH_TESTS_ADD_TARGET_IN_TEST_NAME) |
| 165 |
set(CTestName "${TestTarget}:${CTestName}") |
| 166 |
endif() |
| 167 |
# add target to labels to enable running all tests added from this target |
| 168 |
set(Labels ${TestTarget}) |
| 169 |
if(TestStringsLength EQUAL 2) |
| 170 |
list(GET TestStrings 1 Tags) |
| 171 |
string(TOLOWER "${Tags}" Tags) |
| 172 |
# remove target from labels if the test is hidden |
| 173 |
if("${Tags}" MATCHES ".*\\[!?(hide|\\.)\\].*") |
| 174 |
list(REMOVE_ITEM Labels ${TestTarget}) |
| 175 |
endif() |
| 176 |
string(REPLACE "]" ";" Tags "${Tags}") |
| 177 |
string(REPLACE "[" "" Tags "${Tags}") |
| 178 |
else() |
| 179 |
# unset tags variable from previous loop |
| 180 |
unset(Tags) |
| 181 |
endif() |
| 182 |
|
| 183 |
list(APPEND Labels ${Tags}) |
| 184 |
|
| 185 |
set(HiddenTagFound OFF) |
| 186 |
foreach(label ${Labels}) |
| 187 |
string(REGEX MATCH "^!hide|^\\." result ${label}) |
| 188 |
if(result) |
| 189 |
set(HiddenTagFound ON) |
| 190 |
break() |
| 191 |
endif(result) |
| 192 |
endforeach(label) |
| 193 |
if(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS AND ${HiddenTagFound} AND ${CMAKE_VERSION} VERSION_LESS "3.9") |
| 194 |
ParseAndAddCatchTests_PrintDebugMessage("Skipping test \"${CTestName}\" as it has [!hide], [.] or [.foo] label") |
| 195 |
else() |
| 196 |
ParseAndAddCatchTests_PrintDebugMessage("Adding test \"${CTestName}\"") |
| 197 |
if(Labels) |
| 198 |
ParseAndAddCatchTests_PrintDebugMessage("Setting labels to ${Labels}") |
| 199 |
endif() |
| 200 |
|
| 201 |
# Escape commas in the test spec |
| 202 |
string(REPLACE "," "\\," Name ${Name}) |
| 203 |
|
| 204 |
# Work around CMake 3.18.0 change in `add_test()`, before the escaped quotes were necessary, |
| 205 |
# only with CMake 3.18.0 the escaped double quotes confuse the call. This change is reverted in 3.18.1 |
| 206 |
# And properly introduced in 3.19 with the CMP0110 policy |
| 207 |
if(_cmp0110_value STREQUAL "NEW" OR ${CMAKE_VERSION} VERSION_EQUAL "3.18") |
| 208 |
ParseAndAddCatchTests_PrintDebugMessage("CMP0110 set to NEW, no need for add_test(\"\") workaround") |
| 209 |
else() |
| 210 |
ParseAndAddCatchTests_PrintDebugMessage("CMP0110 set to OLD adding \"\" for add_test() workaround") |
| 211 |
set(CTestName "\"${CTestName}\"") |
| 212 |
endif() |
| 213 |
|
| 214 |
# Handle template test cases |
| 215 |
if("${TestTypeAndFixture}" MATCHES ".*TEMPLATE_.*") |
| 216 |
set(Name "${Name} - *") |
| 217 |
endif() |
| 218 |
|
| 219 |
# Add the test and set its properties |
| 220 |
add_test(NAME "${CTestName}" COMMAND ${OptionalCatchTestLauncher} $<TARGET_FILE:${TestTarget}> ${Name} ${AdditionalCatchParameters}) |
| 221 |
# Old CMake versions do not document VERSION_GREATER_EQUAL, so we use VERSION_GREATER with 3.8 instead |
| 222 |
if(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS AND ${HiddenTagFound} AND ${CMAKE_VERSION} VERSION_GREATER "3.8") |
| 223 |
ParseAndAddCatchTests_PrintDebugMessage("Setting DISABLED test property") |
| 224 |
set_tests_properties("${CTestName}" PROPERTIES DISABLED ON) |
| 225 |
else() |
| 226 |
set_tests_properties("${CTestName}" PROPERTIES FAIL_REGULAR_EXPRESSION "No tests ran" |
| 227 |
LABELS "${Labels}") |
| 228 |
endif() |
| 229 |
set_property( |
| 230 |
TARGET ${TestTarget} |
| 231 |
APPEND |
| 232 |
PROPERTY ParseAndAddCatchTests_TESTS "${CTestName}") |
| 233 |
set_property( |
| 234 |
SOURCE ${SourceFile} |
| 235 |
APPEND |
| 236 |
PROPERTY ParseAndAddCatchTests_TESTS "${CTestName}") |
| 237 |
endif() |
| 238 |
|
| 239 |
|
| 240 |
endforeach() |
| 241 |
endfunction() |
| 242 |
|
| 243 |
# entry point |
| 244 |
function(ParseAndAddCatchTests TestTarget) |
| 245 |
message(DEPRECATION "ParseAndAddCatchTest: function deprecated because of possibility of missed test cases. Consider using 'catch_discover_tests' from 'Catch.cmake'") |
| 246 |
ParseAndAddCatchTests_PrintDebugMessage("Started parsing ${TestTarget}") |
| 247 |
get_target_property(SourceFiles ${TestTarget} SOURCES) |
| 248 |
ParseAndAddCatchTests_PrintDebugMessage("Found the following sources: ${SourceFiles}") |
| 249 |
foreach(SourceFile ${SourceFiles}) |
| 250 |
ParseAndAddCatchTests_ParseFile(${SourceFile} ${TestTarget}) |
| 251 |
endforeach() |
| 252 |
ParseAndAddCatchTests_PrintDebugMessage("Finished parsing ${TestTarget}") |
| 253 |
endfunction() |