1 // (C) Copyright Gennadiy Rozental 2001.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/test for the library home page.
9 /// Provides core implementation for Unit Test Framework.
10 /// Extensions can be provided in separate files
11 // ***************************************************************************
13 #ifndef BOOST_TEST_UNIT_TEST_SUITE_IPP_012205GER
14 #define BOOST_TEST_UNIT_TEST_SUITE_IPP_012205GER
17 #include <boost/detail/workaround.hpp>
19 #include <boost/test/framework.hpp>
20 #include <boost/test/results_collector.hpp>
22 #include <boost/test/tree/test_unit.hpp>
23 #include <boost/test/tree/visitor.hpp>
24 #include <boost/test/tree/traverse.hpp>
25 #include <boost/test/tree/auto_registration.hpp>
26 #include <boost/test/tree/global_fixture.hpp>
28 #include <boost/test/utils/foreach.hpp>
29 #include <boost/test/utils/basic_cstring/io.hpp>
31 #include <boost/test/unit_test_parameters.hpp>
34 #include <boost/timer.hpp>
40 #include <boost/test/detail/suppress_warnings.hpp>
42 //____________________________________________________________________________//
47 // ************************************************************************** //
48 // ************** test_unit ************** //
49 // ************************************************************************** //
51 test_unit::test_unit( const_string name, const_string file_name, std::size_t line_num, test_unit_type t )
53 , p_type_name( t == TUT_CASE ? "case" : "suite" )
54 , p_file_name( file_name )
55 , p_line_num( line_num )
56 , p_id( INV_TEST_UNIT_ID )
57 , p_parent_id( INV_TEST_UNIT_ID )
58 , p_name( std::string( name.begin(), name.size() ) )
60 , p_expected_failures( 0 )
61 , p_default_status( RS_INHERIT )
62 , p_run_status( RS_INVALID )
67 //____________________________________________________________________________//
69 test_unit::test_unit( const_string module_name )
71 , p_type_name( "module" )
73 , p_id( INV_TEST_UNIT_ID )
74 , p_parent_id( INV_TEST_UNIT_ID )
75 , p_name( std::string( module_name.begin(), module_name.size() ) )
77 , p_expected_failures( 0 )
78 , p_default_status( RS_INHERIT )
79 , p_run_status( RS_INVALID )
84 //____________________________________________________________________________//
86 test_unit::~test_unit()
88 framework::deregister_test_unit( this );
91 //____________________________________________________________________________//
94 test_unit::depends_on( test_unit* tu )
96 BOOST_TEST_SETUP_ASSERT( p_id != framework::master_test_suite().p_id,
97 "Can't add dependency to the master test suite" );
99 p_dependencies.value.push_back( tu->p_id );
102 //____________________________________________________________________________//
105 test_unit::add_precondition( precondition_t const& pc )
107 p_preconditions.value.push_back( pc );
110 //____________________________________________________________________________//
112 test_tools::assertion_result
113 test_unit::check_preconditions() const
115 BOOST_TEST_FOREACH( test_unit_id, dep_id, p_dependencies.get() ) {
116 test_unit const& dep = framework::get( dep_id, TUT_ANY );
118 if( !dep.is_enabled() ) {
119 test_tools::assertion_result res(false);
120 res.message() << "dependency test " << dep.p_type_name << " \"" << dep.full_name() << "\" is disabled";
124 test_results const& test_rslt = unit_test::results_collector.results( dep_id );
125 if( !test_rslt.passed() ) {
126 test_tools::assertion_result res(false);
127 res.message() << "dependency test " << dep.p_type_name << " \"" << dep.full_name() << "\" has failed";
131 if( test_rslt.p_test_cases_skipped > 0 ) {
132 test_tools::assertion_result res(false);
133 res.message() << "dependency test " << dep.p_type_name << " \"" << dep.full_name() << "\" has skipped test cases";
138 BOOST_TEST_FOREACH( precondition_t, precondition, p_preconditions.get() ) {
139 test_tools::assertion_result res = precondition( p_id );
141 test_tools::assertion_result res_out(false);
142 res_out.message() << "precondition failed";
143 if( !res.has_empty_message() )
144 res_out.message() << ": " << res.message();
152 //____________________________________________________________________________//
155 test_unit::increase_exp_fail( counter_t num )
157 p_expected_failures.value += num;
159 if( p_parent_id != INV_TEST_UNIT_ID )
160 framework::get<test_suite>( p_parent_id ).increase_exp_fail( num );
163 //____________________________________________________________________________//
166 test_unit::full_name() const
168 if( p_parent_id == INV_TEST_UNIT_ID || p_parent_id == framework::master_test_suite().p_id )
171 std::string res = framework::get<test_suite>( p_parent_id ).full_name();
174 res.append( p_name );
179 //____________________________________________________________________________//
182 test_unit::add_label( const_string l )
184 p_labels.value.push_back( std::string() + l );
187 //____________________________________________________________________________//
190 test_unit::has_label( const_string l ) const
192 return std::find( p_labels->begin(), p_labels->end(), l ) != p_labels->end();
195 //____________________________________________________________________________//
197 // ************************************************************************** //
198 // ************** test_case ************** //
199 // ************************************************************************** //
201 test_case::test_case( const_string name, boost::function<void ()> const& test_func )
202 : test_unit( name, "", 0, static_cast<test_unit_type>(type) )
203 , p_test_func( test_func )
205 framework::register_test_unit( this );
208 //____________________________________________________________________________//
210 test_case::test_case( const_string name, const_string file_name, std::size_t line_num, boost::function<void ()> const& test_func )
211 : test_unit( name, file_name, line_num, static_cast<test_unit_type>(type) )
212 , p_test_func( test_func )
214 framework::register_test_unit( this );
217 //____________________________________________________________________________//
219 // ************************************************************************** //
220 // ************** test_suite ************** //
221 // ************************************************************************** //
223 //____________________________________________________________________________//
225 test_suite::test_suite( const_string name, const_string file_name, std::size_t line_num )
226 : test_unit( name, file_name, line_num, static_cast<test_unit_type>(type) )
228 framework::register_test_unit( this );
231 //____________________________________________________________________________//
233 test_suite::test_suite( const_string module_name )
234 : test_unit( module_name )
236 framework::register_test_unit( this );
239 //____________________________________________________________________________//
242 test_suite::add( test_unit* tu, counter_t expected_failures, unsigned timeout )
244 tu->p_timeout.value = timeout;
246 m_children.push_back( tu->p_id );
247 tu->p_parent_id.value = p_id;
249 if( tu->p_expected_failures != 0 )
250 increase_exp_fail( tu->p_expected_failures );
252 if( expected_failures )
253 tu->increase_exp_fail( expected_failures );
256 //____________________________________________________________________________//
259 test_suite::add( test_unit_generator const& gen, unsigned timeout )
262 while((tu = gen.next()) != 0)
263 add( tu, 0, timeout );
266 //____________________________________________________________________________//
269 test_suite::add( test_unit_generator const& gen, decorator::collector& decorators )
272 while((tu = gen.next()) != 0) {
273 decorators.store_in( *tu );
280 //____________________________________________________________________________//
283 test_suite::remove( test_unit_id id )
285 test_unit_id_list::iterator it = std::find( m_children.begin(), m_children.end(), id );
287 if( it != m_children.end() )
288 m_children.erase( it );
291 //____________________________________________________________________________//
294 test_suite::get( const_string tu_name ) const
296 BOOST_TEST_FOREACH( test_unit_id, id, m_children ) {
297 if( tu_name == framework::get( id, ut_detail::test_id_2_unit_type( id ) ).p_name.get() )
301 return INV_TEST_UNIT_ID;
304 //____________________________________________________________________________//
306 // ************************************************************************** //
307 // ************** master_test_suite ************** //
308 // ************************************************************************** //
310 master_test_suite_t::master_test_suite_t()
311 : test_suite( "Master Test Suite" )
315 p_default_status.value = RS_ENABLED;
318 // ************************************************************************** //
319 // ************** traverse_test_tree ************** //
320 // ************************************************************************** //
323 traverse_test_tree( test_case const& tc, test_tree_visitor& V, bool ignore_status )
325 if( tc.is_enabled() || ignore_status )
329 //____________________________________________________________________________//
332 traverse_test_tree( test_suite const& suite, test_tree_visitor& V, bool ignore_status )
334 // skip disabled test suite unless we asked to ignore this condition
335 if( !ignore_status && !suite.is_enabled() )
338 // Invoke test_suite_start callback
339 if( !V.test_suite_start( suite ) )
342 // Recurse into children
343 std::size_t total_children = suite.m_children.size();
344 for( std::size_t i=0; i < total_children; ) {
345 // this statement can remove the test unit from this list
346 traverse_test_tree( suite.m_children[i], V, ignore_status );
347 if( total_children > suite.m_children.size() )
348 total_children = suite.m_children.size();
353 // Invoke test_suite_finish callback
354 V.test_suite_finish( suite );
357 //____________________________________________________________________________//
360 traverse_test_tree( test_unit_id id, test_tree_visitor& V, bool ignore_status )
362 if( ut_detail::test_id_2_unit_type( id ) == TUT_CASE )
363 traverse_test_tree( framework::get<test_case>( id ), V, ignore_status );
365 traverse_test_tree( framework::get<test_suite>( id ), V, ignore_status );
368 //____________________________________________________________________________//
370 // ************************************************************************** //
371 // ************** object generators ************** //
372 // ************************************************************************** //
374 namespace ut_detail {
377 normalize_test_case_name( const_string name )
379 std::string norm_name( name.begin(), name.size() );
382 norm_name = norm_name.substr( 1 );
384 std::replace(norm_name.begin(), norm_name.end(), ' ', '_');
389 //____________________________________________________________________________//
391 // ************************************************************************** //
392 // ************** auto_test_unit_registrar ************** //
393 // ************************************************************************** //
395 auto_test_unit_registrar::auto_test_unit_registrar( test_case* tc, decorator::collector& decorators, counter_t exp_fail )
397 framework::current_auto_test_suite().add( tc, exp_fail );
399 decorators.store_in( *tc );
403 //____________________________________________________________________________//
405 auto_test_unit_registrar::auto_test_unit_registrar( const_string ts_name, const_string ts_file, std::size_t ts_line, decorator::collector& decorators )
407 test_unit_id id = framework::current_auto_test_suite().get( ts_name );
411 if( id != INV_TEST_UNIT_ID ) {
412 ts = &framework::get<test_suite>( id );
413 BOOST_ASSERT( ts->p_parent_id == framework::current_auto_test_suite().p_id );
416 ts = new test_suite( ts_name, ts_file, ts_line );
417 framework::current_auto_test_suite().add( ts );
420 decorators.store_in( *ts );
423 framework::current_auto_test_suite( ts );
426 //____________________________________________________________________________//
428 auto_test_unit_registrar::auto_test_unit_registrar( test_unit_generator const& tc_gen, decorator::collector& decorators )
430 framework::current_auto_test_suite().add( tc_gen, decorators );
433 //____________________________________________________________________________//
435 auto_test_unit_registrar::auto_test_unit_registrar( int )
437 framework::current_auto_test_suite( 0, false );
440 //____________________________________________________________________________//
442 } // namespace ut_detail
444 // ************************************************************************** //
445 // ************** global_fixture ************** //
446 // ************************************************************************** //
448 global_fixture::global_fixture()
450 framework::register_observer( *this );
453 //____________________________________________________________________________//
455 } // namespace unit_test
458 #include <boost/test/detail/enable_warnings.hpp>
460 #endif // BOOST_TEST_UNIT_TEST_SUITE_IPP_012205GER