Skip to content
Snippets Groups Projects
Unverified Commit c5a1ddb7 authored by Tomáš Pecka's avatar Tomáš Pecka
Browse files

Catch2: Update to 2.9.2

parent 121a52b4
No related branches found
No related tags found
1 merge request!106Master merge jt
...@@ -51,8 +51,11 @@ string(REPLACE "\n" ";" output "${output}") ...@@ -51,8 +51,11 @@ string(REPLACE "\n" ";" output "${output}")
# Parse output # Parse output
foreach(line ${output}) foreach(line ${output})
set(test ${line}) set(test ${line})
# use escape commas to handle properly test cases with commans inside the name # Escape characters in test case names that would be parsed by Catch2
string(REPLACE "," "\\," test_name ${test}) set(test_name ${test})
foreach(char , [ ])
string(REPLACE ${char} "\\${char}" test_name ${test_name})
endforeach(char)
# ...and add to script # ...and add to script
add_command(add_test add_command(add_test
"${prefix}${test}${suffix}" "${prefix}${test}${suffix}"
......
...@@ -44,9 +44,19 @@ ...@@ -44,9 +44,19 @@
# set(OptionalCatchTestLauncher ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${NUMPROC}) # # set(OptionalCatchTestLauncher ${MPIEXEC} ${MPIEXEC_NUMPROC_FLAG} ${NUMPROC}) #
# just before calling this ParseAndAddCatchTests function # # just before calling this ParseAndAddCatchTests function #
# # # #
# The AdditionalCatchParameters optional variable can be used to pass extra argument to the test #
# command. For example, to include successful tests in the output, one can write #
# set(AdditionalCatchParameters --success) #
# #
# After the script, the ParseAndAddCatchTests_TESTS property for the target, and for each source #
# file in the target is set, and contains the list of the tests extracted from that target, or #
# from that file. This is useful, for example to add further labels or properties to the tests. #
# #
#==================================================================================================# #==================================================================================================#
   
cmake_minimum_required(VERSION 2.8.8) if (CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.8)
message(FATAL_ERROR "ParseAndAddCatchTests requires CMake 2.8.8 or newer")
endif()
   
option(PARSE_CATCH_TESTS_VERBOSE "Print Catch to CTest parser debug messages" OFF) option(PARSE_CATCH_TESTS_VERBOSE "Print Catch to CTest parser debug messages" OFF)
option(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS "Exclude tests with [!hide], [.] or [.foo] tags" OFF) option(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS "Exclude tests with [!hide], [.] or [.foo] tags" OFF)
...@@ -54,7 +64,7 @@ option(PARSE_CATCH_TESTS_ADD_FIXTURE_IN_TEST_NAME "Add fixture class name to the ...@@ -54,7 +64,7 @@ option(PARSE_CATCH_TESTS_ADD_FIXTURE_IN_TEST_NAME "Add fixture class name to the
option(PARSE_CATCH_TESTS_ADD_TARGET_IN_TEST_NAME "Add target name to the test name" ON) option(PARSE_CATCH_TESTS_ADD_TARGET_IN_TEST_NAME "Add target name to the test name" ON)
option(PARSE_CATCH_TESTS_ADD_TO_CONFIGURE_DEPENDS "Add test file to CMAKE_CONFIGURE_DEPENDS property" OFF) option(PARSE_CATCH_TESTS_ADD_TO_CONFIGURE_DEPENDS "Add test file to CMAKE_CONFIGURE_DEPENDS property" OFF)
   
function(PrintDebugMessage) function(ParseAndAddCatchTests_PrintDebugMessage)
if(PARSE_CATCH_TESTS_VERBOSE) if(PARSE_CATCH_TESTS_VERBOSE)
message(STATUS "ParseAndAddCatchTests: ${ARGV}") message(STATUS "ParseAndAddCatchTests: ${ARGV}")
endif() endif()
...@@ -65,7 +75,7 @@ endfunction() ...@@ -65,7 +75,7 @@ endfunction()
# - full line comments (i.e. // ... ) # - full line comments (i.e. // ... )
# contents have been read into '${CppCode}'. # contents have been read into '${CppCode}'.
# !keep partial line comments # !keep partial line comments
function(RemoveComments CppCode) function(ParseAndAddCatchTests_RemoveComments CppCode)
string(ASCII 2 CMakeBeginBlockComment) string(ASCII 2 CMakeBeginBlockComment)
string(ASCII 3 CMakeEndBlockComment) string(ASCII 3 CMakeEndBlockComment)
string(REGEX REPLACE "/\\*" "${CMakeBeginBlockComment}" ${CppCode} "${${CppCode}}") string(REGEX REPLACE "/\\*" "${CMakeBeginBlockComment}" ${CppCode} "${${CppCode}}")
...@@ -77,24 +87,29 @@ function(RemoveComments CppCode) ...@@ -77,24 +87,29 @@ function(RemoveComments CppCode)
endfunction() endfunction()
   
# Worker function # Worker function
function(ParseFile SourceFile TestTarget) function(ParseAndAddCatchTests_ParseFile SourceFile TestTarget)
# 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.
if(SourceFile MATCHES "\\\$<TARGET_OBJECTS:.+>")
ParseAndAddCatchTests_PrintDebugMessage("Detected OBJECT library: ${SourceFile} this will not be scanned for tests.")
return()
endif()
# According to CMake docs EXISTS behavior is well-defined only for full paths. # According to CMake docs EXISTS behavior is well-defined only for full paths.
get_filename_component(SourceFile ${SourceFile} ABSOLUTE) get_filename_component(SourceFile ${SourceFile} ABSOLUTE)
if(NOT EXISTS ${SourceFile}) if(NOT EXISTS ${SourceFile})
message(WARNING "Cannot find source file: ${SourceFile}") message(WARNING "Cannot find source file: ${SourceFile}")
return() return()
endif() endif()
PrintDebugMessage("parsing ${SourceFile}") ParseAndAddCatchTests_PrintDebugMessage("parsing ${SourceFile}")
file(STRINGS ${SourceFile} Contents NEWLINE_CONSUME) file(STRINGS ${SourceFile} Contents NEWLINE_CONSUME)
   
# Remove block and fullline comments # Remove block and fullline comments
RemoveComments(Contents) ParseAndAddCatchTests_RemoveComments(Contents)
   
# Find definition of test names # Find definition of test names
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}") 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}")
   
if(PARSE_CATCH_TESTS_ADD_TO_CONFIGURE_DEPENDS AND Tests) if(PARSE_CATCH_TESTS_ADD_TO_CONFIGURE_DEPENDS AND Tests)
PrintDebugMessage("Adding ${SourceFile} to CMAKE_CONFIGURE_DEPENDS property") ParseAndAddCatchTests_PrintDebugMessage("Adding ${SourceFile} to CMAKE_CONFIGURE_DEPENDS property")
set_property( set_property(
DIRECTORY DIRECTORY
APPEND APPEND
...@@ -155,7 +170,6 @@ function(ParseFile SourceFile TestTarget) ...@@ -155,7 +170,6 @@ function(ParseFile SourceFile TestTarget)
   
list(APPEND Labels ${Tags}) list(APPEND Labels ${Tags})
   
list(FIND Labels "!hide" IndexOfHideLabel)
set(HiddenTagFound OFF) set(HiddenTagFound OFF)
foreach(label ${Labels}) foreach(label ${Labels})
string(REGEX MATCH "^!hide|^\\." result ${label}) string(REGEX MATCH "^!hide|^\\." result ${label})
...@@ -165,23 +179,34 @@ function(ParseFile SourceFile TestTarget) ...@@ -165,23 +179,34 @@ function(ParseFile SourceFile TestTarget)
endif(result) endif(result)
endforeach(label) endforeach(label)
if(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS AND ${HiddenTagFound} AND ${CMAKE_VERSION} VERSION_LESS "3.9") if(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS AND ${HiddenTagFound} AND ${CMAKE_VERSION} VERSION_LESS "3.9")
PrintDebugMessage("Skipping test \"${CTestName}\" as it has [!hide], [.] or [.foo] label") ParseAndAddCatchTests_PrintDebugMessage("Skipping test \"${CTestName}\" as it has [!hide], [.] or [.foo] label")
else() else()
PrintDebugMessage("Adding test \"${CTestName}\"") ParseAndAddCatchTests_PrintDebugMessage("Adding test \"${CTestName}\"")
if(Labels) if(Labels)
PrintDebugMessage("Setting labels to ${Labels}") ParseAndAddCatchTests_PrintDebugMessage("Setting labels to ${Labels}")
endif() endif()
   
# Escape commas in the test spec
string(REPLACE "," "\\," Name ${Name})
# Add the test and set its properties # Add the test and set its properties
add_test(NAME "\"${CTestName}\"" COMMAND ${OptionalCatchTestLauncher} ${TestTarget} ${Name} ${AdditionalCatchParameters}) add_test(NAME "\"${CTestName}\"" COMMAND ${OptionalCatchTestLauncher} $<TARGET_FILE:${TestTarget}> ${Name} ${AdditionalCatchParameters})
# Old CMake versions do not document VERSION_GREATER_EQUAL, so we use VERSION_GREATER with 3.8 instead # Old CMake versions do not document VERSION_GREATER_EQUAL, so we use VERSION_GREATER with 3.8 instead
if(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS AND ${HiddenTagFound} AND ${CMAKE_VERSION} VERSION_GREATER "3.8") if(PARSE_CATCH_TESTS_NO_HIDDEN_TESTS AND ${HiddenTagFound} AND ${CMAKE_VERSION} VERSION_GREATER "3.8")
PrintDebugMessage("Setting DISABLED test property") ParseAndAddCatchTests_PrintDebugMessage("Setting DISABLED test property")
set_tests_properties("\"${CTestName}\"" PROPERTIES DISABLED ON) set_tests_properties("\"${CTestName}\"" PROPERTIES DISABLED ON)
else() else()
set_tests_properties("\"${CTestName}\"" PROPERTIES FAIL_REGULAR_EXPRESSION "No tests ran" set_tests_properties("\"${CTestName}\"" PROPERTIES FAIL_REGULAR_EXPRESSION "No tests ran"
LABELS "${Labels}") LABELS "${Labels}")
endif() endif()
set_property(
TARGET ${TestTarget}
APPEND
PROPERTY ParseAndAddCatchTests_TESTS "\"${CTestName}\"")
set_property(
SOURCE ${SourceFile}
APPEND
PROPERTY ParseAndAddCatchTests_TESTS "\"${CTestName}\"")
endif() endif()
   
   
...@@ -190,11 +215,11 @@ endfunction() ...@@ -190,11 +215,11 @@ endfunction()
   
# entry point # entry point
function(ParseAndAddCatchTests TestTarget) function(ParseAndAddCatchTests TestTarget)
PrintDebugMessage("Started parsing ${TestTarget}") ParseAndAddCatchTests_PrintDebugMessage("Started parsing ${TestTarget}")
get_target_property(SourceFiles ${TestTarget} SOURCES) get_target_property(SourceFiles ${TestTarget} SOURCES)
PrintDebugMessage("Found the following sources: ${SourceFiles}") ParseAndAddCatchTests_PrintDebugMessage("Found the following sources: ${SourceFiles}")
foreach(SourceFile ${SourceFiles}) foreach(SourceFile ${SourceFiles})
ParseFile(${SourceFile} ${TestTarget}) ParseAndAddCatchTests_ParseFile(${SourceFile} ${TestTarget})
endforeach() endforeach()
PrintDebugMessage("Finished parsing ${TestTarget}") ParseAndAddCatchTests_PrintDebugMessage("Finished parsing ${TestTarget}")
endfunction() endfunction()
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment