]> git.mxchange.org Git - simgear.git/blob - CMakeModules/BoostTestTargets.cmake
Add smart pointer tests (finally using Boost.Test)
[simgear.git] / CMakeModules / BoostTestTargets.cmake
1 # - Add tests using boost::test
2 #
3 # Add this line to your test files in place of including a basic boost test header:
4 #  #include <BoostTestTargetConfig.h>
5 #
6 # If you cannot do that and must use the included form for a given test,
7 # include the line
8 #  // OVERRIDE_BOOST_TEST_INCLUDED_WARNING
9 # in the same file with the boost test include.
10 #
11 #  include(BoostTestTargets)
12 #  add_boost_test(<testdriver_name> SOURCES <source1> [<more sources...>]
13 #   [FAIL_REGULAR_EXPRESSION <additional fail regex>]
14 #   [LAUNCHER <generic launcher script>]
15 #   [LIBRARIES <library> [<library>...]]
16 #   [RESOURCES <resource> [<resource>...]]
17 #   [TESTS <testcasename> [<testcasename>...]])
18 #
19 #  If for some reason you need access to the executable target created,
20 #  it can be found in ${${testdriver_name}_TARGET_NAME} as specified when
21 #  you called add_boost_test
22 #
23 # Requires CMake 2.6 or newer (uses the 'function' command)
24 #
25 # Requires:
26 #       GetForceIncludeDefinitions
27 #       CopyResourcesToBuildTree
28 #
29 # Original Author:
30 # 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
31 # http://academic.cleardefinition.com
32 # Iowa State University HCI Graduate Program/VRAC
33 #
34 # Copyright Iowa State University 2009-2010.
35 # Distributed under the Boost Software License, Version 1.0.
36 # (See accompanying file LICENSE_1_0.txt or copy at
37 # http://www.boost.org/LICENSE_1_0.txt)
38
39 if(__add_boost_test)
40         return()
41 endif()
42 set(__add_boost_test YES)
43
44 set(BOOST_TEST_TARGET_PREFIX "boosttest")
45
46 if(NOT Boost_FOUND)
47         find_package(Boost 1.34.0 QUIET)
48 endif()
49 if("${Boost_VERSION}0" LESS "1034000")
50         set(_shared_msg
51                 "NOTE: boost::test-based targets and tests cannot "
52                 "be added: boost >= 1.34.0 required but not found. "
53                 "(found: '${Boost_VERSION}'; want >=103400) ")
54         if(BUILD_TESTING)
55                 message(FATAL_ERROR
56                         ${_shared_msg}
57                         "You may disable BUILD_TESTING to continue without the "
58                         "tests.")
59         else()
60                 message(STATUS
61                         ${_shared_msg}
62                         "BUILD_TESTING disabled, so continuing anyway.")
63         endif()
64 endif()
65
66 include(GetForceIncludeDefinitions)
67 include(CopyResourcesToBuildTree)
68
69 if(Boost_FOUND AND NOT "${Boost_VERSION}0" LESS "1034000")
70         set(_boosttesttargets_libs)
71         set(_boostConfig "BoostTestTargetsIncluded.h")
72         if(NOT Boost_UNIT_TEST_FRAMEWORK_LIBRARY)
73                 find_package(Boost 1.34.0 QUIET COMPONENTS unit_test_framework)
74         endif()
75         if(Boost_UNIT_TEST_FRAMEWORK_LIBRARY)
76                 set(_boosttesttargets_libs "${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}")
77                 if(Boost_USE_STATIC_LIBS)
78                         set(_boostConfig "BoostTestTargetsStatic.h")
79                 else()
80                         if(NOT APPLE)
81                                 set(_boostConfig "BoostTestTargetsDynamic.h")
82                         endif()
83                 endif()
84         endif()
85         get_filename_component(_moddir ${CMAKE_CURRENT_LIST_FILE} PATH)
86         configure_file("${_moddir}/${_boostConfig}"
87                 "${CMAKE_CURRENT_BINARY_DIR}/BoostTestTargetConfig.h"
88                 COPYONLY)
89         include_directories("${CMAKE_CURRENT_BINARY_DIR}")
90 endif()
91
92 function(add_boost_test _name)
93         if(NOT BUILD_TESTING)
94                 return()
95         endif()
96 # TODO check if 2.6 is enough
97 #       if("${CMAKE_VERSION}" VERSION_LESS "2.8.0")
98 #               if(NOT "${_boost_test_cmakever_pestered}x" EQUAL "${CMAKE_VERSION}x")
99 #                       message(STATUS
100 #                               "Not adding boost::test targets - CMake 2.8.0 or newer required, using ${CMAKE_VERSION}")
101 #                       set(_boost_test_cmakever_pestered
102 #                               "${CMAKE_VERSION}"
103 #                               CACHE
104 #                               INTERNAL
105 #                               ""
106 #                               FORCE)
107 #               endif()
108 #               return()
109 #       endif()
110
111         # parse arguments
112         set(_nowhere)
113         set(_curdest _nowhere)
114         set(_val_args
115                 SOURCES
116                 FAIL_REGULAR_EXPRESSION
117                 LAUNCHER
118                 LIBRARIES
119                 RESOURCES
120                 TESTS)
121         set(_bool_args
122                 USE_COMPILED_LIBRARY)
123         foreach(_arg ${_val_args} ${_bool_args})
124                 set(${_arg})
125         endforeach()
126         foreach(_element ${ARGN})
127                 list(FIND _val_args "${_element}" _val_arg_find)
128                 list(FIND _bool_args "${_element}" _bool_arg_find)
129                 if("${_val_arg_find}" GREATER "-1")
130                         set(_curdest "${_element}")
131                 elseif("${_bool_arg_find}" GREATER "-1")
132                         set("${_element}" ON)
133                         set(_curdest _nowhere)
134                 else()
135                         list(APPEND ${_curdest} "${_element}")
136                 endif()
137         endforeach()
138
139         if(_nowhere)
140                 message(FATAL_ERROR "Syntax error in use of add_boost_test!")
141         endif()
142
143         if(NOT SOURCES)
144                 message(FATAL_ERROR
145                         "Syntax error in use of add_boost_test: at least one source file required!")
146         endif()
147
148         if(Boost_FOUND AND NOT "${Boost_VERSION}0" LESS "1034000")
149
150                 include_directories(${Boost_INCLUDE_DIRS})
151
152                 set(includeType)
153                 foreach(src ${SOURCES})
154                         file(READ ${src} thefile)
155                         if("${thefile}" MATCHES ".*BoostTestTargetConfig.h.*")
156                                 set(includeType CONFIGURED)
157                                 set(includeFileLoc ${src})
158                                 break()
159                         elseif("${thefile}" MATCHES ".*boost/test/included/unit_test.hpp.*")
160                                 set(includeType INCLUDED)
161                                 set(includeFileLoc ${src})
162                                 set(_boosttesttargets_libs)     # clear this out - linking would be a bad idea
163                                 if(NOT
164                                         "${thefile}"
165                                         MATCHES
166                                         ".*OVERRIDE_BOOST_TEST_INCLUDED_WARNING.*")
167                                         message("Please replace the include line in ${src} with this alternate include line instead:")
168                                         message("  \#include <BoostTestTargetConfig.h>")
169                                         message("Once you've saved your changes, re-run CMake. (See BoostTestTargets.cmake for more info)")
170                                 endif()
171                                 break()
172                         endif()
173                 endforeach()
174
175                 if(NOT _boostTestTargetsNagged${_name} STREQUAL "${includeType}")
176                         if("includeType" STREQUAL "CONFIGURED")
177                                 message(STATUS
178                                         "Test '${_name}' uses the CMake-configurable form of the boost test framework - congrats! (Including File: ${includeFileLoc})")
179                         elseif("${includeType}" STREQUAL "INCLUDED")
180                                 message("In test '${_name}': ${includeFileLoc} uses the 'included' form of the boost unit test framework.")
181                         else()
182                                 message("In test '${_name}': Didn't detect the CMake-configurable boost test include.")
183                                 message("Please replace your existing boost test include in that test with the following:")
184                                 message("  \#include <BoostTestTargetConfig.h>")
185                                 message("Once you've saved your changes, re-run CMake. (See BoostTestTargets.cmake for more info)")
186                         endif()
187                 endif()
188                 set(_boostTestTargetsNagged${_name}
189                         "${includeType}"
190                         CACHE
191                         INTERNAL
192                         ""
193                         FORCE)
194
195
196                 if(RESOURCES)
197                         list(APPEND SOURCES ${RESOURCES})
198                 endif()
199
200                 # Generate a unique target name, using the relative binary dir
201                 # and provided name. (transform all / into _ and remove all other
202                 # non-alphabet characters)
203                 file(RELATIVE_PATH
204                         targetpath
205                         "${CMAKE_BINARY_DIR}"
206                         "${CMAKE_CURRENT_BINARY_DIR}")
207                 string(REGEX REPLACE "[^A-Za-z/_]" "" targetpath "${targetpath}")
208                 string(REPLACE "/" "_" targetpath "${targetpath}")
209
210                 set(_target_name ${BOOST_TEST_TARGET_PREFIX}-${targetpath}-${_name})
211                 set(${_name}_TARGET_NAME "${_target_name}" PARENT_SCOPE)
212
213                 # Build the test.
214                 add_executable(${_target_name} ${SOURCES})
215
216                 list(APPEND LIBRARIES ${_boosttesttargets_libs})
217
218                 if(LIBRARIES)
219                         target_link_libraries(${_target_name} ${LIBRARIES})
220                 endif()
221
222                 if(RESOURCES)
223                         set_property(TARGET ${_target_name} PROPERTY RESOURCE ${RESOURCES})
224                         copy_resources_to_build_tree(${_target_name})
225                 endif()
226
227                 if(NOT Boost_TEST_FLAGS)
228 #                       set(Boost_TEST_FLAGS --catch_system_error=yes --output_format=XML)
229                         set(Boost_TEST_FLAGS --catch_system_error=yes)
230                 endif()
231
232                 # TODO: Figure out why only recent boost handles individual test running properly
233
234                 if(LAUNCHER)
235                         set(_test_command ${LAUNCHER} "\$<TARGET_FILE:${_target_name}>")
236                 else()
237                         set(_test_command ${_target_name})
238                 endif()
239
240                 if(TESTS AND ( "${Boost_VERSION}" VERSION_GREATER "103799" ))
241                         foreach(_test ${TESTS})
242                                 add_test(NAME
243                                         ${_name}-${_test}
244                                         COMMAND
245                                         ${_test_command}
246                                         --run_test=${_test}
247                                         ${Boost_TEST_FLAGS})
248                                 if(FAIL_REGULAR_EXPRESSION)
249                                         set_tests_properties(${_name}-${_test}
250                                                 PROPERTIES
251                                                 FAIL_REGULAR_EXPRESSION
252                                                 "${FAIL_REGULAR_EXPRESSION}")
253                                 endif()
254                         endforeach()
255                 else()
256                         add_test(NAME
257                                 ${_name}-boost_test
258                                 COMMAND
259                                 ${_test_command}
260                                 ${Boost_TEST_FLAGS})
261                         if(FAIL_REGULAR_EXPRESSION)
262                                 set_tests_properties(${_name}-${_test}
263                                         PROPERTIES
264                                         FAIL_REGULAR_EXPRESSION
265                                         "${FAIL_REGULAR_EXPRESSION}")
266                         endif()
267                 endif()
268
269                 # CppCheck the test if we can.
270                 if(COMMAND add_cppcheck)
271                         add_cppcheck(${_target_name} STYLE UNUSED_FUNCTIONS)
272                 endif()
273
274         endif()
275 endfunction()