Debian Codegraph @

gulrak-filesystem/test/cmake/ParseAndAddCatchTests.cmake

raw
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
57 if (CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.8)
58 message(FATAL_ERROR "ParseAndAddCatchTests requires CMake 2.8.8 or newer")
59 endif()
60
61 option(PARSE_CATCH_TESTS_VERBOSE "Print Catch to CTest parser debug messages" OFF)
62 option(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS "Exclude tests with [!hide], [.] or [.foo] tags" OFF)
63 option(PARSE_CATCH_TESTS_ADD_FIXTURE_IN_TEST_NAME "Add fixture class name to the test name" ON)
64 option(PARSE_CATCH_TESTS_ADD_TARGET_IN_TEST_NAME "Add target name to the test name" ON)
65 option(PARSE_CATCH_TESTS_ADD_TO_CONFIGURE_DEPENDS "Add test file to CMAKE_CONFIGURE_DEPENDS property" OFF)
66
67 function(ParseAndAddCatchTests_PrintDebugMessage)
68 if(PARSE_CATCH_TESTS_VERBOSE)
69 message(STATUS "ParseAndAddCatchTests: ${ARGV}")
70 endif()
71 endfunction()
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
78 function(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)
87 endfunction()
88
89 # Worker function
90 function(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 "${TestType}" MATCHES "(CATCH_)?TEST_CASE_METHOD" 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 # Work around CMake 3.18.0 change in `add_test()`, before the escaped quotes were neccessary,
193 # only with CMake 3.18.0 the escaped double quotes confuse the call. This change is reverted in 3.18.1
194 if(NOT ${CMAKE_VERSION} VERSION_EQUAL "3.18")
195 set(CTestName "\"${CTestName}\"")
196 endif()
197 # Add the test and set its properties
198 add_test(NAME "${CTestName}" COMMAND ${OptionalCatchTestLauncher} $<TARGET_FILE:${TestTarget}> ${Name} ${AdditionalCatchParameters})
199 # Old CMake versions do not document VERSION_GREATER_EQUAL, so we use VERSION_GREATER with 3.8 instead
200 if(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS AND ${HiddenTagFound} AND ${CMAKE_VERSION} VERSION_GREATER "3.8")
201 ParseAndAddCatchTests_PrintDebugMessage("Setting DISABLED test property")
202 set_tests_properties("${CTestName}" PROPERTIES DISABLED ON)
203 else()
204 set_tests_properties("${CTestName}" PROPERTIES FAIL_REGULAR_EXPRESSION "No tests ran"
205 LABELS "${Labels}")
206 endif()
207 set_property(
208 TARGET ${TestTarget}
209 APPEND
210 PROPERTY ParseAndAddCatchTests_TESTS "${CTestName}")
211 set_property(
212 SOURCE ${SourceFile}
213 APPEND
214 PROPERTY ParseAndAddCatchTests_TESTS "${CTestName}")
215 endif()
216
217
218 endforeach()
219 endfunction()
220
221 # entry point
222 function(ParseAndAddCatchTests TestTarget)
223 ParseAndAddCatchTests_PrintDebugMessage("Started parsing ${TestTarget}")
224 get_target_property(SourceFiles ${TestTarget} SOURCES)
225 ParseAndAddCatchTests_PrintDebugMessage("Found the following sources: ${SourceFiles}")
226 foreach(SourceFile ${SourceFiles})
227 ParseAndAddCatchTests_ParseFile(${SourceFile} ${TestTarget})
228 endforeach()
229 ParseAndAddCatchTests_PrintDebugMessage("Finished parsing ${TestTarget}")
230 endfunction()