Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / FindCxxTest.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 FindCxxTest
6 -----------
7
8 Find CxxTest unit testing framework.
9
10 Find the CxxTest suite and declare a helper macro for creating unit
11 tests and integrating them with CTest.  For more details on CxxTest
12 see https://cxxtest.com
13
14 INPUT Variables
15
16 ::
17
18    CXXTEST_USE_PYTHON [deprecated since 1.3]
19        Only used in the case both Python & Perl
20        are detected on the system to control
21        which CxxTest code generator is used.
22        Valid only for CxxTest version 3.
23
24
25
26 ::
27
28        NOTE: In older versions of this Find Module,
29        this variable controlled if the Python test
30        generator was used instead of the Perl one,
31        regardless of which scripting language the
32        user had installed.
33
34
35
36 ::
37
38    CXXTEST_TESTGEN_ARGS (since CMake 2.8.3)
39        Specify a list of options to pass to the CxxTest code
40        generator.  If not defined, --error-printer is
41        passed.
42
43
44
45 OUTPUT Variables
46
47 ::
48
49    CXXTEST_FOUND
50        True if the CxxTest framework was found
51    CXXTEST_INCLUDE_DIRS
52        Where to find the CxxTest include directory
53    CXXTEST_PERL_TESTGEN_EXECUTABLE
54        The perl-based test generator
55    CXXTEST_PYTHON_TESTGEN_EXECUTABLE
56        The python-based test generator
57    CXXTEST_TESTGEN_EXECUTABLE (since CMake 2.8.3)
58        The test generator that is actually used (chosen using user preferences
59        and interpreters found in the system)
60    CXXTEST_TESTGEN_INTERPRETER (since CMake 2.8.3)
61        The full path to the Perl or Python executable on the system, on
62        platforms where the script cannot be executed using its shebang line.
63
64
65
66 MACROS for optional use by CMake users:
67
68 ::
69
70     CXXTEST_ADD_TEST(<test_name> <gen_source_file> <input_files_to_testgen...>)
71        Creates a CxxTest runner and adds it to the CTest testing suite
72        Parameters:
73            test_name               The name of the test
74            gen_source_file         The generated source filename to be
75                                    generated by CxxTest
76            input_files_to_testgen  The list of header files containing the
77                                    CxxTest::TestSuite's to be included in
78                                    this runner
79
80
81
82 ::
83
84        #==============
85        Example Usage:
86
87
88
89 ::
90
91            find_package(CxxTest)
92            if(CXXTEST_FOUND)
93                include_directories(${CXXTEST_INCLUDE_DIR})
94                enable_testing()
95
96
97
98 ::
99
100                CXXTEST_ADD_TEST(unittest_foo foo_test.cc
101                                  ${CMAKE_CURRENT_SOURCE_DIR}/foo_test.h)
102                target_link_libraries(unittest_foo foo) # as needed
103            endif()
104
105
106
107 ::
108
109               This will (if CxxTest is found):
110               1. Invoke the testgen executable to autogenerate foo_test.cc in the
111                  binary tree from "foo_test.h" in the current source directory.
112               2. Create an executable and test called unittest_foo.
113
114
115
116 ::
117
118       #=============
119       Example foo_test.h:
120
121
122
123 ::
124
125           #include <cxxtest/TestSuite.h>
126
127
128
129 ::
130
131           class MyTestSuite : public CxxTest::TestSuite
132           {
133           public:
134              void testAddition( void )
135              {
136                 TS_ASSERT( 1 + 1 > 1 );
137                 TS_ASSERT_EQUALS( 1 + 1, 2 );
138              }
139           };
140 #]=======================================================================]
141
142 # Version 1.4 (11/18/10) (CMake 2.8.4)
143 #     Issue 11384: Added support to the CXX_ADD_TEST macro so header
144 #                  files (containing the tests themselves) show up in
145 #                  Visual Studio and other IDEs.
146 #
147 # Version 1.3 (8/19/10) (CMake 2.8.3)
148 #     Included patch by Simone Rossetto to check if either Python or Perl
149 #     are present in the system.  Whichever interpreter that is detected
150 #     is now used to run the test generator program.  If both interpreters
151 #     are detected, the CXXTEST_USE_PYTHON variable is obeyed.
152 #
153 #     Also added support for CXXTEST_TESTGEN_ARGS, for manually specifying
154 #     options to the CxxTest code generator.
155 # Version 1.2 (3/2/08)
156 #     Included patch from Tyler Roscoe to have the perl & python binaries
157 #     detected based on CXXTEST_INCLUDE_DIR
158 # Version 1.1 (2/9/08)
159 #     Clarified example to illustrate need to call target_link_libraries()
160 #     Changed commands to lowercase
161 #     Added licensing info
162 # Version 1.0 (1/8/08)
163 #     Fixed CXXTEST_INCLUDE_DIRS so it will work properly
164 #     Eliminated superfluous CXXTEST_FOUND assignment
165 #     Cleaned up and added more documentation
166
167 #=============================================================
168 # CXXTEST_ADD_TEST (public macro)
169 #=============================================================
170 macro(CXXTEST_ADD_TEST _cxxtest_testname _cxxtest_outfname)
171     set(_cxxtest_real_outfname ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_outfname})
172
173     add_custom_command(
174         OUTPUT  ${_cxxtest_real_outfname}
175         DEPENDS ${ARGN}
176         COMMAND ${CXXTEST_TESTGEN_INTERPRETER}
177         ${CXXTEST_TESTGEN_EXECUTABLE} ${CXXTEST_TESTGEN_ARGS} -o ${_cxxtest_real_outfname} ${ARGN}
178     )
179
180     set_source_files_properties(${_cxxtest_real_outfname} PROPERTIES GENERATED true)
181     add_executable(${_cxxtest_testname} ${_cxxtest_real_outfname} ${ARGN})
182
183     if(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
184         add_test(${_cxxtest_testname} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${_cxxtest_testname})
185     elseif(EXECUTABLE_OUTPUT_PATH)
186         add_test(${_cxxtest_testname} ${EXECUTABLE_OUTPUT_PATH}/${_cxxtest_testname})
187     else()
188         add_test(${_cxxtest_testname} ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_testname})
189     endif()
190
191 endmacro()
192
193 #=============================================================
194 # main()
195 #=============================================================
196 if(NOT DEFINED CXXTEST_TESTGEN_ARGS)
197   set(CXXTEST_TESTGEN_ARGS --error-printer)
198 endif()
199
200 find_package(Python QUIET)
201 find_package(Perl QUIET)
202
203 find_path(CXXTEST_INCLUDE_DIR cxxtest/TestSuite.h)
204 find_program(CXXTEST_PYTHON_TESTGEN_EXECUTABLE
205          NAMES cxxtestgen cxxtestgen.py
206          PATHS ${CXXTEST_INCLUDE_DIR})
207 find_program(CXXTEST_PERL_TESTGEN_EXECUTABLE cxxtestgen.pl
208          PATHS ${CXXTEST_INCLUDE_DIR})
209
210 if(PYTHON_FOUND OR PERL_FOUND)
211   include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
212
213   if(PYTHON_FOUND AND (CXXTEST_USE_PYTHON OR NOT PERL_FOUND OR NOT DEFINED CXXTEST_USE_PYTHON))
214     set(CXXTEST_TESTGEN_EXECUTABLE ${CXXTEST_PYTHON_TESTGEN_EXECUTABLE})
215     execute_process(COMMAND ${CXXTEST_PYTHON_TESTGEN_EXECUTABLE} --version
216       OUTPUT_VARIABLE _CXXTEST_OUT ERROR_VARIABLE _CXXTEST_OUT RESULT_VARIABLE _CXXTEST_RESULT)
217     if(_CXXTEST_RESULT EQUAL 0)
218       set(CXXTEST_TESTGEN_INTERPRETER "")
219     else()
220       set(CXXTEST_TESTGEN_INTERPRETER ${Python_EXECUTABLE})
221     endif()
222     FIND_PACKAGE_HANDLE_STANDARD_ARGS(CxxTest DEFAULT_MSG
223         CXXTEST_INCLUDE_DIR CXXTEST_PYTHON_TESTGEN_EXECUTABLE)
224
225   elseif(PERL_FOUND)
226     set(CXXTEST_TESTGEN_EXECUTABLE ${CXXTEST_PERL_TESTGEN_EXECUTABLE})
227     set(CXXTEST_TESTGEN_INTERPRETER ${PERL_EXECUTABLE})
228     FIND_PACKAGE_HANDLE_STANDARD_ARGS(CxxTest DEFAULT_MSG
229         CXXTEST_INCLUDE_DIR CXXTEST_PERL_TESTGEN_EXECUTABLE)
230   endif()
231
232   if(CXXTEST_FOUND)
233     set(CXXTEST_INCLUDE_DIRS ${CXXTEST_INCLUDE_DIR})
234   endif()
235
236 else()
237
238   set(CXXTEST_FOUND false)
239   if(NOT CxxTest_FIND_QUIETLY)
240     if(CxxTest_FIND_REQUIRED)
241       message(FATAL_ERROR "Neither Python nor Perl found, cannot use CxxTest, aborting!")
242     else()
243       message(STATUS "Neither Python nor Perl found, CxxTest will not be used.")
244     endif()
245   endif()
246
247 endif()