ParseAndAddCatchTests.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. if (CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.8)
  57. message(FATAL_ERROR "ParseAndAddCatchTests requires CMake 2.8.8 or newer")
  58. endif()
  59. option(PARSE_CATCH_TESTS_VERBOSE "Print Catch to CTest parser debug messages" OFF)
  60. option(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS "Exclude tests with [!hide], [.] or [.foo] tags" OFF)
  61. option(PARSE_CATCH_TESTS_ADD_FIXTURE_IN_TEST_NAME "Add fixture class name to the test name" ON)
  62. option(PARSE_CATCH_TESTS_ADD_TARGET_IN_TEST_NAME "Add target name to the test name" ON)
  63. option(PARSE_CATCH_TESTS_ADD_TO_CONFIGURE_DEPENDS "Add test file to CMAKE_CONFIGURE_DEPENDS property" OFF)
  64. function(ParseAndAddCatchTests_PrintDebugMessage)
  65. if(PARSE_CATCH_TESTS_VERBOSE)
  66. message(STATUS "ParseAndAddCatchTests: ${ARGV}")
  67. endif()
  68. endfunction()
  69. # This removes the contents between
  70. # - block comments (i.e. /* ... */)
  71. # - full line comments (i.e. // ... )
  72. # contents have been read into '${CppCode}'.
  73. # !keep partial line comments
  74. function(ParseAndAddCatchTests_RemoveComments CppCode)
  75. string(ASCII 2 CMakeBeginBlockComment)
  76. string(ASCII 3 CMakeEndBlockComment)
  77. string(REGEX REPLACE "/\\*" "${CMakeBeginBlockComment}" ${CppCode} "${${CppCode}}")
  78. string(REGEX REPLACE "\\*/" "${CMakeEndBlockComment}" ${CppCode} "${${CppCode}}")
  79. string(REGEX REPLACE "${CMakeBeginBlockComment}[^${CMakeEndBlockComment}]*${CMakeEndBlockComment}" "" ${CppCode} "${${CppCode}}")
  80. string(REGEX REPLACE "\n[ \t]*//+[^\n]+" "\n" ${CppCode} "${${CppCode}}")
  81. set(${CppCode} "${${CppCode}}" PARENT_SCOPE)
  82. endfunction()
  83. # Worker function
  84. function(ParseAndAddCatchTests_ParseFile SourceFile TestTarget)
  85. # 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.
  86. if(SourceFile MATCHES "\\\$<TARGET_OBJECTS:.+>")
  87. ParseAndAddCatchTests_PrintDebugMessage("Detected OBJECT library: ${SourceFile} this will not be scanned for tests.")
  88. return()
  89. endif()
  90. # According to CMake docs EXISTS behavior is well-defined only for full paths.
  91. get_filename_component(SourceFile ${SourceFile} ABSOLUTE)
  92. if(NOT EXISTS ${SourceFile})
  93. message(WARNING "Cannot find source file: ${SourceFile}")
  94. return()
  95. endif()
  96. ParseAndAddCatchTests_PrintDebugMessage("parsing ${SourceFile}")
  97. file(STRINGS ${SourceFile} Contents NEWLINE_CONSUME)
  98. # Remove block and fullline comments
  99. ParseAndAddCatchTests_RemoveComments(Contents)
  100. # Find definition of test names
  101. 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}")
  102. if(PARSE_CATCH_TESTS_ADD_TO_CONFIGURE_DEPENDS AND Tests)
  103. ParseAndAddCatchTests_PrintDebugMessage("Adding ${SourceFile} to CMAKE_CONFIGURE_DEPENDS property")
  104. set_property(
  105. DIRECTORY
  106. APPEND
  107. PROPERTY CMAKE_CONFIGURE_DEPENDS ${SourceFile}
  108. )
  109. endif()
  110. foreach(TestName ${Tests})
  111. # Strip newlines
  112. string(REGEX REPLACE "\\\\\n|\n" "" TestName "${TestName}")
  113. # Get test type and fixture if applicable
  114. string(REGEX MATCH "(CATCH_)?(TEST_CASE_METHOD|SCENARIO|TEST_CASE)[ \t]*\\([^,^\"]*" TestTypeAndFixture "${TestName}")
  115. string(REGEX MATCH "(CATCH_)?(TEST_CASE_METHOD|SCENARIO|TEST_CASE)" TestType "${TestTypeAndFixture}")
  116. string(REGEX REPLACE "${TestType}\\([ \t]*" "" TestFixture "${TestTypeAndFixture}")
  117. # Get string parts of test definition
  118. string(REGEX MATCHALL "\"+([^\\^\"]|\\\\\")+\"+" TestStrings "${TestName}")
  119. # Strip wrapping quotation marks
  120. string(REGEX REPLACE "^\"(.*)\"$" "\\1" TestStrings "${TestStrings}")
  121. string(REPLACE "\";\"" ";" TestStrings "${TestStrings}")
  122. # Validate that a test name and tags have been provided
  123. list(LENGTH TestStrings TestStringsLength)
  124. if(TestStringsLength GREATER 2 OR TestStringsLength LESS 1)
  125. message(FATAL_ERROR "You must provide a valid test name and tags for all tests in ${SourceFile}")
  126. endif()
  127. # Assign name and tags
  128. list(GET TestStrings 0 Name)
  129. if("${TestType}" STREQUAL "SCENARIO")
  130. set(Name "Scenario: ${Name}")
  131. endif()
  132. if(PARSE_CATCH_TESTS_ADD_FIXTURE_IN_TEST_NAME AND TestFixture)
  133. set(CTestName "${TestFixture}:${Name}")
  134. else()
  135. set(CTestName "${Name}")
  136. endif()
  137. if(PARSE_CATCH_TESTS_ADD_TARGET_IN_TEST_NAME)
  138. set(CTestName "${TestTarget}:${CTestName}")
  139. endif()
  140. # add target to labels to enable running all tests added from this target
  141. set(Labels ${TestTarget})
  142. if(TestStringsLength EQUAL 2)
  143. list(GET TestStrings 1 Tags)
  144. string(TOLOWER "${Tags}" Tags)
  145. # remove target from labels if the test is hidden
  146. if("${Tags}" MATCHES ".*\\[!?(hide|\\.)\\].*")
  147. list(REMOVE_ITEM Labels ${TestTarget})
  148. endif()
  149. string(REPLACE "]" ";" Tags "${Tags}")
  150. string(REPLACE "[" "" Tags "${Tags}")
  151. else()
  152. # unset tags variable from previous loop
  153. unset(Tags)
  154. endif()
  155. list(APPEND Labels ${Tags})
  156. set(HiddenTagFound OFF)
  157. foreach(label ${Labels})
  158. string(REGEX MATCH "^!hide|^\\." result ${label})
  159. if(result)
  160. set(HiddenTagFound ON)
  161. break()
  162. endif(result)
  163. endforeach(label)
  164. if(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS AND ${HiddenTagFound} AND ${CMAKE_VERSION} VERSION_LESS "3.9")
  165. ParseAndAddCatchTests_PrintDebugMessage("Skipping test \"${CTestName}\" as it has [!hide], [.] or [.foo] label")
  166. else()
  167. ParseAndAddCatchTests_PrintDebugMessage("Adding test \"${CTestName}\"")
  168. if(Labels)
  169. ParseAndAddCatchTests_PrintDebugMessage("Setting labels to ${Labels}")
  170. endif()
  171. # Escape commas in the test spec
  172. string(REPLACE "," "\\," Name ${Name})
  173. # Add the test and set its properties
  174. add_test(NAME "\"${CTestName}\"" COMMAND ${OptionalCatchTestLauncher} $<TARGET_FILE:${TestTarget}> ${Name} ${AdditionalCatchParameters})
  175. # Old CMake versions do not document VERSION_GREATER_EQUAL, so we use VERSION_GREATER with 3.8 instead
  176. if(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS AND ${HiddenTagFound} AND ${CMAKE_VERSION} VERSION_GREATER "3.8")
  177. ParseAndAddCatchTests_PrintDebugMessage("Setting DISABLED test property")
  178. set_tests_properties("\"${CTestName}\"" PROPERTIES DISABLED ON)
  179. else()
  180. set_tests_properties("\"${CTestName}\"" PROPERTIES FAIL_REGULAR_EXPRESSION "No tests ran"
  181. LABELS "${Labels}")
  182. endif()
  183. set_property(
  184. TARGET ${TestTarget}
  185. APPEND
  186. PROPERTY ParseAndAddCatchTests_TESTS "\"${CTestName}\"")
  187. set_property(
  188. SOURCE ${SourceFile}
  189. APPEND
  190. PROPERTY ParseAndAddCatchTests_TESTS "\"${CTestName}\"")
  191. endif()
  192. endforeach()
  193. endfunction()
  194. # entry point
  195. function(ParseAndAddCatchTests TestTarget)
  196. ParseAndAddCatchTests_PrintDebugMessage("Started parsing ${TestTarget}")
  197. get_target_property(SourceFiles ${TestTarget} SOURCES)
  198. ParseAndAddCatchTests_PrintDebugMessage("Found the following sources: ${SourceFiles}")
  199. foreach(SourceFile ${SourceFiles})
  200. ParseAndAddCatchTests_ParseFile(${SourceFile} ${TestTarget})
  201. endforeach()
  202. ParseAndAddCatchTests_PrintDebugMessage("Finished parsing ${TestTarget}")
  203. endfunction()