FindCxxTest.cmake 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindCxxTest
  5. -----------
  6. Finds `CxxTest`_, a C++ unit testing framework suite, and provides a helper
  7. command to create test runners and integrate them with CTest:
  8. .. code-block:: cmake
  9. find_package(CxxTest [...])
  10. .. _`CxxTest`: https://github.com/CxxTest/cxxtest
  11. Result Variables
  12. ^^^^^^^^^^^^^^^^
  13. This module defines the following variables:
  14. ``CXXTEST_FOUND``
  15. Boolean indicating whether the CxxTest framework was found.
  16. ``CXXTEST_INCLUDE_DIRS``
  17. Include directories containing headers needed to use CxxTest.
  18. ``CXXTEST_TESTGEN_EXECUTABLE``
  19. The path to the found CxxTest test generator script (Perl- or Python-based),
  20. selected based on the found interpreter or user-specified preference.
  21. ``CXXTEST_TESTGEN_INTERPRETER``
  22. The path to the found Perl or Python interpreter used to run the test
  23. generator script, if needed (e.g., on platforms where script shebang lines are
  24. not supported).
  25. Cache Variables
  26. ^^^^^^^^^^^^^^^
  27. The following cache variables may also be set:
  28. ``CXXTEST_PERL_TESTGEN_EXECUTABLE``
  29. The path to the Perl-based CxxTest test generator script.
  30. ``CXXTEST_PYTHON_TESTGEN_EXECUTABLE``
  31. The path to the Python-based CxxTest test generator script.
  32. Hints
  33. ^^^^^
  34. This module accepts the following variables before calling
  35. ``find_package(CxxTest)``:
  36. ``CXXTEST_TESTGEN_ARGS``
  37. This variable can be set to specify a semicolon-separated list of command-line
  38. options to pass to the CxxTest code generator. If not set, the default value
  39. is ``--error-printer``.
  40. Commands
  41. ^^^^^^^^
  42. This module provides the following command if CxxTest is found:
  43. .. command:: cxxtest_add_test
  44. Creates a CxxTest runner and adds it to the CTest testing suite:
  45. .. code-block:: cmake
  46. cxxtest_add_test(<test-name> <gen-source-file> <input-files-to-testgen>...)
  47. Parameters:
  48. ``<test-name>``
  49. The name of the test executable target to be created and registered as a
  50. test in the CTest suite.
  51. ``<gen-source-file>``
  52. The name of the source file to be generated by the CxxTest code generator.
  53. This must be a relative path. It is interpreted relative to the
  54. current binary directory (:variable:`CMAKE_CURRENT_BINARY_DIR`).
  55. ``<input-files-to-testgen>``
  56. A list of header files containing test suite classes derived from the C++
  57. class ``CxxTest::TestSuite``, to be included in the test runner. These must
  58. be given as absolute paths.
  59. Deprecated Variables
  60. ^^^^^^^^^^^^^^^^^^^^
  61. The following variables are deprecated and provided for backward compatibility:
  62. ``CXXTEST_USE_PYTHON``
  63. .. deprecated:: 2.8.3
  64. In earlier versions of CMake, this hint variable was used to force the use
  65. of the Python-based test generator instead of the Perl one, regardless of
  66. which scripting language was installed. It is now only considered when both
  67. Perl and Python interpreters are found.
  68. A boolean hint variable that, when set to true, prefers the Python code
  69. generator over the Perl one if both interpreters are found. This variable is
  70. only relevant when using CxxTest version 3.
  71. Examples
  72. ^^^^^^^^
  73. The following example demonstrates how CxxTest can be used in CMake with this
  74. module. If CxxTest is found:
  75. * Additional interface :ref:`imported target <Imported Targets>` is created
  76. manually in the project to encapsulate the CxxTest usage requirements and
  77. link it to specified tests. Such target is useful, for example, when dealing
  78. with multiple tests.
  79. * Test generator is invoked to create ``foo_test.cc`` in the current binary
  80. directory from the input header ``foo_test.h`` located in the current source
  81. directory.
  82. * An executable named ``unit_test_foo`` is built and registered as a test in
  83. CTest.
  84. .. code-block:: cmake
  85. :caption: ``CMakeLists.txt``
  86. find_package(CxxTest)
  87. # Create interface imported target:
  88. if(CXXTEST_FOUND AND NOT TARGET CxxTest::CxxTest)
  89. add_library(CxxTest::CxxTest INTERFACE IMPORTED)
  90. set_target_properties(
  91. CxxTest::CxxTest
  92. PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${CXXTEST_INCLUDE_DIRS}"
  93. )
  94. endif()
  95. # Add test:
  96. if(CXXTEST_FOUND)
  97. enable_testing()
  98. cxxtest_add_test(
  99. unit_test_foo
  100. foo_test.cc
  101. ${CMAKE_CURRENT_SOURCE_DIR}/foo_test.h
  102. )
  103. target_link_libraries(
  104. unit_test_foo
  105. PRIVATE
  106. CxxTest::CxxTest
  107. # Link any project targets as needed, if test depends on them:
  108. foo
  109. )
  110. endif()
  111. .. code-block:: c++
  112. :caption: ``foo_test.h``
  113. #include <cxxtest/TestSuite.h>
  114. class MyTestSuite : public CxxTest::TestSuite
  115. {
  116. public:
  117. void testAddition(void)
  118. {
  119. TS_ASSERT(1 + 1 > 1);
  120. TS_ASSERT_EQUALS(1 + 1, 2);
  121. }
  122. };
  123. #]=======================================================================]
  124. # Version 1.4 (11/18/10) (CMake 2.8.4)
  125. # Issue 11384: Added support to the CXX_ADD_TEST macro so header
  126. # files (containing the tests themselves) show up in
  127. # Visual Studio and other IDEs.
  128. #
  129. # Version 1.3 (8/19/10) (CMake 2.8.3)
  130. # Included patch by Simone Rossetto to check if either Python or Perl
  131. # are present in the system. Whichever interpreter that is detected
  132. # is now used to run the test generator program. If both interpreters
  133. # are detected, the CXXTEST_USE_PYTHON variable is obeyed.
  134. #
  135. # Also added support for CXXTEST_TESTGEN_ARGS, for manually specifying
  136. # options to the CxxTest code generator.
  137. # Version 1.2 (3/2/08)
  138. # Included patch from Tyler Roscoe to have the perl & python binaries
  139. # detected based on CXXTEST_INCLUDE_DIR
  140. # Version 1.1 (2/9/08)
  141. # Clarified example to illustrate need to call target_link_libraries()
  142. # Changed commands to lowercase
  143. # Added licensing info
  144. # Version 1.0 (1/8/08)
  145. # Fixed CXXTEST_INCLUDE_DIRS so it will work properly
  146. # Eliminated superfluous CXXTEST_FOUND assignment
  147. # Cleaned up and added more documentation
  148. #=============================================================
  149. # cxxtest_add_test (public macro)
  150. #=============================================================
  151. macro(CXXTEST_ADD_TEST _cxxtest_testname _cxxtest_outfname)
  152. set(_cxxtest_real_outfname ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_outfname})
  153. add_custom_command(
  154. OUTPUT ${_cxxtest_real_outfname}
  155. DEPENDS ${ARGN}
  156. COMMAND ${CXXTEST_TESTGEN_INTERPRETER}
  157. ${CXXTEST_TESTGEN_EXECUTABLE} ${CXXTEST_TESTGEN_ARGS} -o ${_cxxtest_real_outfname} ${ARGN}
  158. )
  159. set_source_files_properties(${_cxxtest_real_outfname} PROPERTIES GENERATED true)
  160. add_executable(${_cxxtest_testname} ${_cxxtest_real_outfname} ${ARGN})
  161. # There's no target used for these commands, so we don't need to do
  162. # anything here for CMP0178.
  163. if(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
  164. add_test(${_cxxtest_testname} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${_cxxtest_testname})
  165. elseif(EXECUTABLE_OUTPUT_PATH)
  166. add_test(${_cxxtest_testname} ${EXECUTABLE_OUTPUT_PATH}/${_cxxtest_testname})
  167. else()
  168. add_test(${_cxxtest_testname} ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_testname})
  169. endif()
  170. endmacro()
  171. #=============================================================
  172. # main()
  173. #=============================================================
  174. if(NOT DEFINED CXXTEST_TESTGEN_ARGS)
  175. set(CXXTEST_TESTGEN_ARGS --error-printer)
  176. endif()
  177. find_package(Python QUIET)
  178. find_package(Perl QUIET)
  179. find_path(CXXTEST_INCLUDE_DIR cxxtest/TestSuite.h)
  180. find_program(CXXTEST_PYTHON_TESTGEN_EXECUTABLE
  181. NAMES cxxtestgen cxxtestgen.py
  182. PATHS ${CXXTEST_INCLUDE_DIR})
  183. find_program(CXXTEST_PERL_TESTGEN_EXECUTABLE cxxtestgen.pl
  184. PATHS ${CXXTEST_INCLUDE_DIR})
  185. if(PYTHON_FOUND OR Perl_FOUND)
  186. include(FindPackageHandleStandardArgs)
  187. if(PYTHON_FOUND AND (CXXTEST_USE_PYTHON OR NOT Perl_FOUND OR NOT DEFINED CXXTEST_USE_PYTHON))
  188. set(CXXTEST_TESTGEN_EXECUTABLE ${CXXTEST_PYTHON_TESTGEN_EXECUTABLE})
  189. execute_process(COMMAND ${CXXTEST_PYTHON_TESTGEN_EXECUTABLE} --version
  190. OUTPUT_VARIABLE _CXXTEST_OUT ERROR_VARIABLE _CXXTEST_OUT RESULT_VARIABLE _CXXTEST_RESULT)
  191. if(_CXXTEST_RESULT EQUAL 0)
  192. set(CXXTEST_TESTGEN_INTERPRETER "")
  193. else()
  194. set(CXXTEST_TESTGEN_INTERPRETER ${Python_EXECUTABLE})
  195. endif()
  196. find_package_handle_standard_args(CxxTest DEFAULT_MSG
  197. CXXTEST_INCLUDE_DIR CXXTEST_PYTHON_TESTGEN_EXECUTABLE)
  198. elseif(Perl_FOUND)
  199. set(CXXTEST_TESTGEN_EXECUTABLE ${CXXTEST_PERL_TESTGEN_EXECUTABLE})
  200. set(CXXTEST_TESTGEN_INTERPRETER ${PERL_EXECUTABLE})
  201. find_package_handle_standard_args(CxxTest DEFAULT_MSG
  202. CXXTEST_INCLUDE_DIR CXXTEST_PERL_TESTGEN_EXECUTABLE)
  203. endif()
  204. if(CXXTEST_FOUND)
  205. set(CXXTEST_INCLUDE_DIRS ${CXXTEST_INCLUDE_DIR})
  206. endif()
  207. else()
  208. set(CXXTEST_FOUND false)
  209. if(NOT CxxTest_FIND_QUIETLY)
  210. if(CxxTest_FIND_REQUIRED)
  211. message(FATAL_ERROR "Neither Python nor Perl found, cannot use CxxTest, aborting!")
  212. else()
  213. message(STATUS "Neither Python nor Perl found, CxxTest will not be used.")
  214. endif()
  215. endif()
  216. endif()