CatchAddTests.cmake 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. set(prefix "${TEST_PREFIX}")
  4. set(suffix "${TEST_SUFFIX}")
  5. set(spec ${TEST_SPEC})
  6. set(extra_args ${TEST_EXTRA_ARGS})
  7. set(properties ${TEST_PROPERTIES})
  8. set(script)
  9. set(suite)
  10. set(tests)
  11. function(add_command NAME)
  12. set(_args "")
  13. foreach(_arg ${ARGN})
  14. if(_arg MATCHES "[^-./:a-zA-Z0-9_]")
  15. set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument
  16. else()
  17. set(_args "${_args} ${_arg}")
  18. endif()
  19. endforeach()
  20. set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE)
  21. endfunction()
  22. macro(_add_catch_test_labels LINE)
  23. # convert to list of tags
  24. string(REPLACE "][" "]\\;[" tags ${line})
  25. add_command(
  26. set_tests_properties "${prefix}${test}${suffix}"
  27. PROPERTIES
  28. LABELS "${tags}"
  29. )
  30. endmacro()
  31. macro(_add_catch_test LINE)
  32. set(test ${line})
  33. # use escape commas to handle properly test cases with commans inside the name
  34. string(REPLACE "," "\\," test_name ${test})
  35. # ...and add to script
  36. add_command(
  37. add_test "${prefix}${test}${suffix}"
  38. ${TEST_EXECUTOR}
  39. "${TEST_EXECUTABLE}"
  40. "${test_name}"
  41. ${extra_args}
  42. )
  43. add_command(
  44. set_tests_properties "${prefix}${test}${suffix}"
  45. PROPERTIES
  46. WORKING_DIRECTORY "${TEST_WORKING_DIR}"
  47. ${properties}
  48. )
  49. list(APPEND tests "${prefix}${test}${suffix}")
  50. endmacro()
  51. # Run test executable to get list of available tests
  52. if(NOT EXISTS "${TEST_EXECUTABLE}")
  53. message(FATAL_ERROR
  54. "Specified test executable '${TEST_EXECUTABLE}' does not exist"
  55. )
  56. endif()
  57. execute_process(
  58. COMMAND ${TEST_EXECUTOR} "${TEST_EXECUTABLE}" ${spec} --list-tests
  59. OUTPUT_VARIABLE output
  60. RESULT_VARIABLE result
  61. )
  62. # Catch --list-test-names-only reports the number of tests, so 0 is... surprising
  63. if(${result} EQUAL 0)
  64. message(WARNING
  65. "Test executable '${TEST_EXECUTABLE}' contains no tests!\n"
  66. )
  67. elseif(${result} LESS 0)
  68. message(FATAL_ERROR
  69. "Error running test executable '${TEST_EXECUTABLE}':\n"
  70. " Result: ${result}\n"
  71. " Output: ${output}\n"
  72. )
  73. endif()
  74. string(REPLACE "\n" ";" output "${output}")
  75. set(test)
  76. set(tags_regex "(\\[([^\\[]*)\\])+$")
  77. # Parse output
  78. foreach(line ${output})
  79. # lines without leading whitespaces are catch output not tests
  80. if(${line} MATCHES "^[ \t]+")
  81. # strip leading spaces and tabs
  82. string(REGEX REPLACE "^[ \t]+" "" line ${line})
  83. if(${line} MATCHES "${tags_regex}")
  84. _add_catch_test_labels(${line})
  85. else()
  86. _add_catch_test(${line})
  87. endif()
  88. endif()
  89. endforeach()
  90. # Create a list of all discovered tests, which users may use to e.g. set
  91. # properties on the tests
  92. add_command(set ${TEST_LIST} ${tests})
  93. # Write CTest script
  94. file(WRITE "${CTEST_FILE}" "${script}")