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