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