1
// (C) Copyright Gennadiy Rozental 2005.
1
// (C) Copyright Gennadiy Rozental 2001-2008.
2
2
// Distributed under the Boost Software License, Version 1.0.
3
3
// (See accompanying file LICENSE_1_0.txt or copy at
4
4
// http://www.boost.org/LICENSE_1_0.txt)
6
6
// See http://www.boost.org/libs/test for the library home page.
8
// File : $RCSfile: unit_test_suite.hpp,v $
10
// Version : $Revision: 1.32 $
12
// Description : defines test_unit, test_case, test_case_results, test_suite and test_tree_visitor
10
// Version : $Revision: 57992 $
12
// Description : defines Unit Test Framework public API
13
13
// ***************************************************************************
15
15
#ifndef BOOST_TEST_UNIT_TEST_SUITE_HPP_071894GER
16
16
#define BOOST_TEST_UNIT_TEST_SUITE_HPP_071894GER
19
#include <boost/test/detail/config.hpp>
20
#include <boost/test/detail/global_typedef.hpp>
21
#include <boost/test/utils/class_properties.hpp>
22
#include <boost/test/utils/callback.hpp>
23
#include <boost/test/detail/fwd_decl.hpp>
24
#include <boost/test/detail/workaround.hpp>
27
#include <boost/shared_ptr.hpp>
30
#include <string> // for std::string
31
#include <list> // for std::list
32
#include <vector> // for std::list
34
#include <boost/test/detail/suppress_warnings.hpp>
19
#include <boost/test/unit_test_suite_impl.hpp>
20
#include <boost/test/framework.hpp>
36
22
//____________________________________________________________________________//
38
#define BOOST_TEST_CASE( function ) \
39
boost::unit_test::make_test_case( boost::unit_test::callback0<>(function), BOOST_TEST_STRINGIZE( function ) )
40
#define BOOST_CLASS_TEST_CASE( function, tc_instance ) \
41
boost::unit_test::make_test_case((function), BOOST_TEST_STRINGIZE( function ), tc_instance )
24
// ************************************************************************** //
25
// ************** Non-auto (explicit) test case interface ************** //
26
// ************************************************************************** //
28
#define BOOST_TEST_CASE( test_function ) \
29
boost::unit_test::make_test_case( boost::unit_test::callback0<>(test_function), BOOST_TEST_STRINGIZE( test_function ) )
30
#define BOOST_CLASS_TEST_CASE( test_function, tc_instance ) \
31
boost::unit_test::make_test_case((test_function), BOOST_TEST_STRINGIZE( test_function ), tc_instance )
33
// ************************************************************************** //
34
// ************** BOOST_TEST_SUITE ************** //
35
// ************************************************************************** //
42
37
#define BOOST_TEST_SUITE( testsuite_name ) \
43
38
( new boost::unit_test::test_suite( testsuite_name ) )
49
// ************************************************************************** //
50
// ************** test_unit ************** //
51
// ************************************************************************** //
55
enum { type = tut_any };
58
test_unit( const_string tu_name, test_unit_type t );
60
// dependencies management
61
void depends_on( test_unit* tu );
62
bool check_dependencies() const;
64
// Public r/o properties
65
typedef BOOST_READONLY_PROPERTY(test_unit_id,(framework_impl)) id_t;
66
readonly_property<test_unit_type> p_type; // type for this test unit
67
readonly_property<const_string> p_type_name; // "case"/"suite"
68
id_t p_id; // unique id for this test unit
70
// Public r/w properties
71
readwrite_property<std::string> p_name; // name for this test unit
72
readwrite_property<unsigned> p_timeout; // timeout for the test unit execution
73
readwrite_property<counter_t> p_expected_failures; // number of expected failured in this test unit
77
std::list<test_unit_id> m_dependencies;
80
// ************************************************************************** //
81
// ************** test_case_generator ************** //
82
// ************************************************************************** //
84
class test_unit_generator {
86
virtual test_unit* next() const = 0;
89
BOOST_TEST_PROTECTED_VIRTUAL ~test_unit_generator() {}
92
// ************************************************************************** //
93
// ************** test_case ************** //
94
// ************************************************************************** //
96
class test_case : public test_unit {
98
enum { type = tut_case };
101
test_case( const_string tc_name, callback0<> const& test_func );
104
callback0<> const& test_func() const { return m_test_func; }
107
friend class framework_impl;
110
// BOOST_MSVC <= 1200 have problems with callback as property
112
callback0<> m_test_func;
115
// ************************************************************************** //
116
// ************** test_suite ************** //
117
// ************************************************************************** //
119
class test_suite : public test_unit {
121
enum { type = tut_suite };
124
explicit test_suite( const_string ts_name = "Master" );
126
// test case list management
127
void add( test_unit* tu, counter_t expected_failures = 0, unsigned timeout = 0 );
128
void add( test_unit_generator const& gen, unsigned timeout = 0 );
131
friend void traverse_test_tree( test_suite const&, test_tree_visitor& );
132
friend class framework_impl;
133
virtual ~test_suite() {}
137
std::vector<test_unit_id> m_members;
140
// ************************************************************************** //
141
// ************** test_tree_visitor ************** //
142
// ************************************************************************** //
144
class test_tree_visitor {
146
// test tree visitor interface
147
virtual void visit( test_case const& ) {}
148
virtual bool test_suite_start( test_suite const& ) { return true; }
149
virtual void test_suite_finish( test_suite const& ) {}
152
BOOST_TEST_PROTECTED_VIRTUAL ~test_tree_visitor() {}
155
// ************************************************************************** //
156
// ************** traverse_test_tree ************** //
157
// ************************************************************************** //
159
void traverse_test_tree( test_case const&, test_tree_visitor& );
160
void traverse_test_tree( test_suite const&, test_tree_visitor& );
161
void traverse_test_tree( test_unit_id id, test_tree_visitor& );
163
//____________________________________________________________________________//
166
traverse_test_tree( test_unit const& tu, test_tree_visitor& V )
168
if( tu.p_type == tut_case )
169
traverse_test_tree( static_cast<test_case const&>( tu ), V );
171
traverse_test_tree( static_cast<test_suite const&>( tu ), V );
174
//____________________________________________________________________________//
176
// ************************************************************************** //
177
// ************** test_case_counter ************** //
178
// ************************************************************************** //
180
struct test_case_counter : test_tree_visitor {
181
test_case_counter() : m_count( 0 ) {}
183
void visit( test_case const& ) { m_count++; }
188
// ************************************************************************** //
189
// ************** test_aborted ************** //
190
// ************************************************************************** //
192
struct test_aborted {};
194
// ************************************************************************** //
195
// ************** object generators ************** //
196
// ************************************************************************** //
198
namespace ut_detail {
200
std::string normalize_test_case_name( const_string tu_name );
202
template<typename UserTestCase>
203
struct user_tc_method_invoker {
204
typedef void (UserTestCase::*test_method )();
206
user_tc_method_invoker( shared_ptr<UserTestCase> inst, test_method tm )
207
: m_inst( inst ), m_test_method( tm ) {}
209
void operator()() { ((*m_inst).*m_test_method)(); }
211
shared_ptr<UserTestCase> m_inst;
212
test_method m_test_method;
40
// ************************************************************************** //
41
// ************** BOOST_AUTO_TEST_SUITE ************** //
42
// ************************************************************************** //
44
#define BOOST_AUTO_TEST_SUITE( suite_name ) \
45
namespace suite_name { \
46
BOOST_AUTO_TU_REGISTRAR( suite_name )( BOOST_STRINGIZE( suite_name ) ); \
49
// ************************************************************************** //
50
// ************** BOOST_FIXTURE_TEST_SUITE ************** //
51
// ************************************************************************** //
53
#define BOOST_FIXTURE_TEST_SUITE( suite_name, F ) \
54
BOOST_AUTO_TEST_SUITE( suite_name ) \
55
typedef F BOOST_AUTO_TEST_CASE_FIXTURE; \
58
// ************************************************************************** //
59
// ************** BOOST_AUTO_TEST_SUITE_END ************** //
60
// ************************************************************************** //
62
#define BOOST_AUTO_TEST_SUITE_END() \
63
BOOST_AUTO_TU_REGISTRAR( BOOST_JOIN( end_suite, __LINE__ ) )( 1 ); \
67
// ************************************************************************** //
68
// ************** BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES ************** //
69
// ************************************************************************** //
71
#define BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES( test_name, n ) \
72
struct BOOST_AUTO_TC_UNIQUE_ID( test_name ); \
74
static struct BOOST_JOIN( test_name, _exp_fail_num_spec ) \
75
: boost::unit_test::ut_detail:: \
76
auto_tc_exp_fail<BOOST_AUTO_TC_UNIQUE_ID( test_name ) > \
78
BOOST_JOIN( test_name, _exp_fail_num_spec )() \
79
: boost::unit_test::ut_detail:: \
80
auto_tc_exp_fail<BOOST_AUTO_TC_UNIQUE_ID( test_name ) >( n ) \
82
} BOOST_JOIN( test_name, _exp_fail_num_spec_inst ); \
86
// ************************************************************************** //
87
// ************** BOOST_FIXTURE_TEST_CASE ************** //
88
// ************************************************************************** //
90
#define BOOST_FIXTURE_TEST_CASE( test_name, F ) \
91
struct test_name : public F { void test_method(); }; \
93
static void BOOST_AUTO_TC_INVOKER( test_name )() \
99
struct BOOST_AUTO_TC_UNIQUE_ID( test_name ) {}; \
101
BOOST_AUTO_TU_REGISTRAR( test_name )( \
102
boost::unit_test::make_test_case( \
103
&BOOST_AUTO_TC_INVOKER( test_name ), #test_name ), \
104
boost::unit_test::ut_detail::auto_tc_exp_fail< \
105
BOOST_AUTO_TC_UNIQUE_ID( test_name )>::instance()->value() ); \
107
void test_name::test_method() \
110
// ************************************************************************** //
111
// ************** BOOST_AUTO_TEST_CASE ************** //
112
// ************************************************************************** //
114
#define BOOST_AUTO_TEST_CASE( test_name ) \
115
BOOST_FIXTURE_TEST_CASE( test_name, BOOST_AUTO_TEST_CASE_FIXTURE )
118
// ************************************************************************** //
119
// ************** BOOST_FIXTURE_TEST_CASE_TEMPLATE ************** //
120
// ************************************************************************** //
122
#define BOOST_FIXTURE_TEST_CASE_TEMPLATE( test_name, type_name, TL, F ) \
123
template<typename type_name> \
124
struct test_name : public F \
125
{ void test_method(); }; \
127
struct BOOST_AUTO_TC_INVOKER( test_name ) { \
128
template<typename TestType> \
129
static void run( boost::type<TestType>* = 0 ) \
131
test_name<TestType> t; \
136
BOOST_AUTO_TU_REGISTRAR( test_name )( \
137
boost::unit_test::ut_detail::template_test_case_gen< \
138
BOOST_AUTO_TC_INVOKER( test_name ),TL >( \
139
BOOST_STRINGIZE( test_name ) ) ); \
141
template<typename type_name> \
142
void test_name<type_name>::test_method() \
145
// ************************************************************************** //
146
// ************** BOOST_AUTO_TEST_CASE_TEMPLATE ************** //
147
// ************************************************************************** //
149
#define BOOST_AUTO_TEST_CASE_TEMPLATE( test_name, type_name, TL ) \
150
BOOST_FIXTURE_TEST_CASE_TEMPLATE( test_name, type_name, TL, BOOST_AUTO_TEST_CASE_FIXTURE )
152
// ************************************************************************** //
153
// ************** BOOST_TEST_CASE_TEMPLATE ************** //
154
// ************************************************************************** //
156
#define BOOST_TEST_CASE_TEMPLATE( name, typelist ) \
157
boost::unit_test::ut_detail::template_test_case_gen<name,typelist >( \
158
BOOST_TEST_STRINGIZE( name ) ) \
161
// ************************************************************************** //
162
// ************** BOOST_TEST_CASE_TEMPLATE_FUNCTION ************** //
163
// ************************************************************************** //
165
#define BOOST_TEST_CASE_TEMPLATE_FUNCTION( name, type_name ) \
166
template<typename type_name> \
167
void BOOST_JOIN( name, _impl )( boost::type<type_name>* ); \
170
template<typename TestType> \
171
static void run( boost::type<TestType>* frwrd = 0 ) \
173
BOOST_JOIN( name, _impl )( frwrd ); \
177
template<typename type_name> \
178
void BOOST_JOIN( name, _impl )( boost::type<type_name>* ) \
181
// ************************************************************************** //
182
// ************** BOOST_GLOBAL_FIXURE ************** //
183
// ************************************************************************** //
185
#define BOOST_GLOBAL_FIXTURE( F ) \
186
static boost::unit_test::ut_detail::global_fixture_impl<F> BOOST_JOIN( gf_, F ) ; \
189
// ************************************************************************** //
190
// ************** BOOST_AUTO_TEST_CASE_FIXTURE ************** //
191
// ************************************************************************** //
193
namespace boost { namespace unit_test { namespace ut_detail {
215
197
} // namespace ut_detail
217
//____________________________________________________________________________//
220
make_test_case( callback0<> const& test_func, const_string tc_name )
222
return new test_case( ut_detail::normalize_test_case_name( tc_name ), test_func );
225
//____________________________________________________________________________//
227
template<typename UserTestCase>
229
make_test_case( void (UserTestCase::*test_method )(),
230
const_string tc_name,
231
boost::shared_ptr<UserTestCase> const& user_test_case )
233
return new test_case( ut_detail::normalize_test_case_name( tc_name ),
234
ut_detail::user_tc_method_invoker<UserTestCase>( user_test_case, test_method ) );
237
//____________________________________________________________________________//
241
199
} // namespace boost
201
// Intentionally is in global namespace, so that FIXURE_TEST_SUITE can reset it in user code.
202
typedef ::boost::unit_test::ut_detail::nil_t BOOST_AUTO_TEST_CASE_FIXTURE;
204
// ************************************************************************** //
205
// ************** Auto registration facility helper macros ************** //
206
// ************************************************************************** //
208
#define BOOST_AUTO_TU_REGISTRAR( test_name ) \
209
static boost::unit_test::ut_detail::auto_test_unit_registrar BOOST_JOIN( BOOST_JOIN( test_name, _registrar ), __LINE__ )
210
#define BOOST_AUTO_TC_INVOKER( test_name ) BOOST_JOIN( test_name, _invoker )
211
#define BOOST_AUTO_TC_UNIQUE_ID( test_name ) BOOST_JOIN( test_name, _id )
213
// ************************************************************************** //
214
// ************** BOOST_TEST_MAIN ************** //
215
// ************************************************************************** //
217
#if defined(BOOST_TEST_MAIN)
219
#ifdef BOOST_TEST_ALTERNATIVE_INIT_API
220
bool init_unit_test() {
222
::boost::unit_test::test_suite*
223
init_unit_test_suite( int, char* [] ) {
226
#ifdef BOOST_TEST_MODULE
227
using namespace ::boost::unit_test;
228
assign_op( framework::master_test_suite().p_name.value, BOOST_TEST_STRINGIZE( BOOST_TEST_MODULE ).trim( "\"" ), 0 );
232
#ifdef BOOST_TEST_ALTERNATIVE_INIT_API
243
242
//____________________________________________________________________________//
245
#include <boost/test/detail/enable_warnings.hpp>
247
// ***************************************************************************
248
// Revision History :
250
// $Log: unit_test_suite.hpp,v $
251
// Revision 1.32 2005/05/02 06:00:10 rogeeff
252
// restore a parameterized user case method based testing
254
// Revision 1.31 2005/04/18 04:55:30 rogeeff
255
// test unit name made read/write
257
// Revision 1.30 2005/03/22 06:57:29 rogeeff
258
// allow to inherit test_suite
260
// Revision 1.29 2005/02/21 10:25:54 rogeeff
261
// use std::vector so we could employ random_shuffle
263
// Revision 1.28 2005/02/20 08:27:06 rogeeff
264
// This a major update for Boost.Test framework. See release docs for complete list of fixes/updates
266
// Revision 1.27 2005/02/01 06:40:06 rogeeff
268
// old log entries removed
269
// minor stylistic changes
270
// deprecated tools removed
272
// Revision 1.26 2005/01/30 03:22:07 rogeeff
273
// interface changed to use const_string
274
// use BOOST_TEST_STRINGIZE
276
// Revision 1.25 2005/01/22 19:22:12 rogeeff
277
// implementation moved into headers section to eliminate dependency of included/minimal component on src directory
279
// ***************************************************************************
281
244
#endif // BOOST_TEST_UNIT_TEST_SUITE_HPP_071894GER