~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to tools/cmake/FindCxxTest.cmake

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# - Find CxxTest
 
2
# Find the CxxTest suite and declare a helper macro for creating unit tests
 
3
# and integrating them with CTest.
 
4
# For more details on CxxTest see http://cxxtest.tigris.org
 
5
#
 
6
# INPUT Variables
 
7
#
 
8
#   CXXTEST_USE_PYTHON
 
9
#       If true, the CXXTEST_ADD_TEST macro will use
 
10
#       the Python test generator instead of Perl.
 
11
#
 
12
# OUTPUT Variables
 
13
#
 
14
#   CXXTEST_FOUND
 
15
#       True if the CxxTest framework was found
 
16
#   CXXTEST_INCLUDE_DIR
 
17
#       Where to find the CxxTest include directory
 
18
#   CXXTEST_PERL_TESTGEN_EXECUTABLE
 
19
#       The perl-based test generator.
 
20
#   CXXTEST_PYTHON_TESTGEN_EXECUTABLE
 
21
#       The python-based test generator.
 
22
#
 
23
# MACROS for optional use by CMake users:
 
24
#
 
25
#    CXXTEST_ADD_TEST(<test_name> <gen_source_file> <input_files_to_testgen...>)
 
26
#       Creates a CxxTest runner and adds it to the CTest testing suite
 
27
#       Parameters:
 
28
#           test_name               The name of the test
 
29
#           gen_source_file         The generated source filename to be generated by CxxTest
 
30
#           input_files_to_testgen  The list of header files containing the
 
31
#                                   CxxTest::TestSuite's to be included in this runner
 
32
#
 
33
#       #==============
 
34
#       Example Usage:
 
35
#
 
36
#           find_package(CxxTest)
 
37
#           if(CXXTEST_FOUND)
 
38
#               include_directories(${CXXTEST_INCLUDE_DIR})
 
39
#               enable_testing()
 
40
#
 
41
#               CXXTEST_ADD_TEST(unittest_foo foo_test.cc
 
42
#                                 ${CMAKE_CURRENT_SOURCE_DIR}/foo_test.h other.cpp)
 
43
#               target_link_libraries(unittest_foo foo) # as needed
 
44
#           endif()
 
45
#
 
46
#              This will (if CxxTest is found):
 
47
#              1. Invoke the testgen executable to autogenerate foo_test.cc in the
 
48
#                 binary tree from "foo_test.h" in the current source directory.
 
49
#              2. Create an executable and test called unittest_foo.
 
50
#              3. Link additionally against other.cpp
 
51
#
 
52
#      #=============
 
53
#      Example foo_test.h:
 
54
#
 
55
#          #include <cxxtest/TestSuite.h>
 
56
#
 
57
#          class MyTestSuite : public CxxTest::TestSuite
 
58
#          {
 
59
#          public:
 
60
#             void testAddition( void )
 
61
#             {
 
62
#                TS_ASSERT( 1 + 1 > 1 );
 
63
#                TS_ASSERT_EQUALS( 1 + 1, 2 );
 
64
#             }
 
65
#          };
 
66
#
 
67
#
 
68
# Version 1.2 (3/2/08)
 
69
#     Included patch from Tyler Roscoe to have the perl & python binaries
 
70
#     detected based on CXXTEST_INCLUDE_DIR
 
71
# Version 1.1 (2/9/08)
 
72
#     Clarified example to illustrate need to call target_link_libraries()
 
73
#     Changed commands to lowercase
 
74
#     Added licensing info
 
75
# Version 1.0 (1/8/08)
 
76
#     Fixed CXXTEST_INCLUDE_DIRS so it will work properly
 
77
#     Eliminated superfluous CXXTEST_FOUND assignment
 
78
#     Cleaned up and added more documentation
 
79
#
 
80
# FindCxxTest.cmake
 
81
# Copyright (c) 2008-2009
 
82
#     Philip Lowman <philip@yhbt.com>
 
83
#
 
84
#  Redistribution AND use is allowed according to the terms of the New
 
85
#  BSD license.
 
86
#  For details see the accompanying COPYING-CMAKE-SCRIPTS file.
 
87
 
 
88
#=============================================================
 
89
# CXXTEST_ADD_TEST (public macro)
 
90
#=============================================================
 
91
macro( CXXTEST_ADD_TEST _cxxtest_testname _cxxtest_outfname _cxxtest_testsuite )
 
92
    set(_cxxtest_real_outfname ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_outfname})
 
93
    if(CXXTEST_USE_PYTHON)
 
94
        set(_cxxtest_executable ${CXXTEST_PYTHON_TESTGEN_EXECUTABLE})
 
95
    else()
 
96
        set(_cxxtest_executable ${CXXTEST_PERL_TESTGEN_EXECUTABLE})
 
97
    endif()
 
98
 
 
99
    add_custom_command(
 
100
        OUTPUT  ${_cxxtest_real_outfname}
 
101
        DEPENDS ${_cxxtest_testsuite} ${ARGN}
 
102
        COMMAND ${_cxxtest_executable}
 
103
        --error-printer -o ${_cxxtest_real_outfname} ${_cxxtest_testsuite}
 
104
    )
 
105
 
 
106
    set_source_files_properties(${_cxxtest_real_outfname} PROPERTIES GENERATED true)
 
107
    add_executable(${_cxxtest_testname} ${_cxxtest_real_outfname} ${ARGN})
 
108
 
 
109
    if(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
 
110
        add_test(${_cxxtest_testname} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${_cxxtest_testname})
 
111
    elseif(EXECUTABLE_OUTPUT_PATH)
 
112
        add_test(${_cxxtest_testname} ${EXECUTABLE_OUTPUT_PATH}/${_cxxtest_testname})
 
113
    else()
 
114
        add_test(${_cxxtest_testname} ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_testname})
 
115
    endif()
 
116
 
 
117
endmacro(CXXTEST_ADD_TEST)
 
118
 
 
119
#=============================================================
 
120
# CXXTEST_ADD_TEST_FROM_LIST (public macro)
 
121
#=============================================================
 
122
#
 
123
# Usage:
 
124
#   CXXTEST_ADD_TESTS_FROM_LIST( A, B, ... )
 
125
#
 
126
#   1. A is a list of cpp and/or h files where the class name is stripped of
 
127
#   2. then out of the class name the test targets and testsuite names are generated
 
128
#   3. finally they are linked with all libs present in parameter B
 
129
#   4. If there are more arguments (ARGN) then they are exclude from A before 1-3 starts)
 
130
#
 
131
FUNCTION( CXXTEST_ADD_TESTS_FROM_LIST _SourceList _TestLibs )
 
132
    # remove unwanted tests
 
133
    IF( ${ARGC} GREATER 2 )
 
134
        FOREACH( fname ${ARGN} )
 
135
            LIST( REMOVE_ITEM _SourceList ${CMAKE_CURRENT_SOURCE_DIR}/${fname} )
 
136
        ENDFOREACH( fname )
 
137
    ENDIF()
 
138
 
 
139
    # extract class names from source files
 
140
    FOREACH( _File ${_SourceList} )
 
141
      STRING( REGEX REPLACE "^.*/" "" _StrippedPath "${_File}" )
 
142
      STRING( REGEX REPLACE "\\..*$" "" _StrippedExtension "${_StrippedPath}" )
 
143
      STRING( REGEX MATCH ".*\\.h" _IsHeader "${_File}" ) #Match Header Files
 
144
      IF( NOT _IsHeader ) # Do not generate test for header files
 
145
        LIST( APPEND _TestList ${_StrippedExtension} ) 
 
146
      ENDIF()
 
147
    ENDFOREACH( _File )
 
148
 
 
149
    # generate for each class a unit test if there is a testsiute for it
 
150
    FOREACH( _ClassName ${_TestList} )
 
151
        SET( _TestName ${_ClassName}_test )
 
152
        SET( _TestTarget unittest_${_ClassName} )
 
153
        SET( _TestSuitePath "${CMAKE_CURRENT_SOURCE_DIR}/test/${_TestName}.h" )
 
154
 
 
155
        # check if testsuite is present and generate code if true
 
156
        IF( EXISTS ${_TestSuitePath} )
 
157
            CXXTEST_ADD_TEST( ${_TestTarget}
 
158
                ${_TestName}.cc
 
159
                ${_TestSuitePath}
 
160
                )
 
161
 
 
162
            TARGET_LINK_LIBRARIES( ${_TestTarget} ${_TestLibs} )
 
163
        ELSE()
 
164
            MESSAGE( STATUS "WARNING: Skipping ${_ClassName}, no unit test available." )
 
165
        ENDIF()
 
166
    ENDFOREACH( _ClassName )
 
167
ENDFUNCTION( CXXTEST_ADD_TESTS_FROM_LIST )
 
168
 
 
169
#=============================================================
 
170
# main()
 
171
#=============================================================
 
172
 
 
173
find_path(CXXTEST_INCLUDE_DIR cxxtest/TestSuite.h)
 
174
find_program(CXXTEST_PERL_TESTGEN_EXECUTABLE cxxtestgen.pl
 
175
    PATHS ${CXXTEST_INCLUDE_DIR})
 
176
find_program(CXXTEST_PYTHON_TESTGEN_EXECUTABLE cxxtestgen.py
 
177
    PATHS ${CXXTEST_INCLUDE_DIR})
 
178
 
 
179
include(FindPackageHandleStandardArgs)
 
180
FIND_PACKAGE_HANDLE_STANDARD_ARGS(CxxTest DEFAULT_MSG CXXTEST_INCLUDE_DIR)
 
181
 
 
182
set(CXXTEST_INCLUDE_DIRS ${CXXTEST_INCLUDE_DIR})