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.
10 // Version : $Revision$
12 // Description : main function implementation for Unit Test Framework
13 // ***************************************************************************
15 #ifndef BOOST_TEST_UNIT_TEST_MAIN_IPP_012205GER
16 #define BOOST_TEST_UNIT_TEST_MAIN_IPP_012205GER
19 #include <boost/test/framework.hpp>
20 #include <boost/test/results_collector.hpp>
21 #include <boost/test/results_reporter.hpp>
23 #include <boost/test/tree/visitor.hpp>
24 #include <boost/test/tree/test_unit.hpp>
25 #include <boost/test/tree/traverse.hpp>
27 #include <boost/test/unit_test_parameters.hpp>
29 #include <boost/test/utils/foreach.hpp>
30 #include <boost/test/utils/basic_cstring/io.hpp>
33 #include <boost/cstdlib.hpp>
42 #include <boost/test/detail/suppress_warnings.hpp>
44 //____________________________________________________________________________//
51 // ************************************************************************** //
52 // ************** hrf_content_reporter ************** //
53 // ************************************************************************** //
55 struct hrf_content_reporter : test_tree_visitor {
56 explicit hrf_content_reporter( std::ostream& os ) : m_os( os ), m_indent( -4 ) {} // skip master test suite
59 void report_test_unit( test_unit const& tu )
61 m_os << std::setw( m_indent ) << "" << tu.p_name;
62 m_os << (tu.p_default_status == test_unit::RS_ENABLED ? "*" : " ");
63 //m_os << '[' << tu.p_sibling_rank << ']';
64 if( !tu.p_description->empty() )
65 m_os << ": " << tu.p_description;
69 virtual void visit( test_case const& tc ) { report_test_unit( tc ); }
70 virtual bool test_suite_start( test_suite const& ts )
73 report_test_unit( ts );
77 virtual void test_suite_finish( test_suite const& )
87 // ************************************************************************** //
88 // ************** dot_content_reporter ************** //
89 // ************************************************************************** //
91 struct dot_content_reporter : test_tree_visitor {
92 explicit dot_content_reporter( std::ostream& os ) : m_os( os ) {}
95 void report_test_unit( test_unit const& tu )
97 bool master_ts = tu.p_parent_id == INV_TEST_UNIT_ID;
99 m_os << "tu" << tu.p_id;
101 m_os << (master_ts ? "[shape=ellipse,peripheries=2" : "[shape=Mrecord" );
103 m_os << ",fontname=Helvetica";
105 m_os << (tu.p_default_status == test_unit::RS_ENABLED ? ",color=green" : ",color=yellow");
108 m_os << ",label=\"" << tu.p_name << "\"];\n";
110 m_os << ",label=\"" << tu.p_name << "|" << tu.p_file_name << "(" << tu.p_line_num << ")";
111 if( tu.p_timeout > 0 )
112 m_os << "|timeout=" << tu.p_timeout;
113 if( tu.p_expected_failures != 0 )
114 m_os << "|expected failures=" << tu.p_expected_failures;
115 if( !tu.p_labels->empty() ) {
118 BOOST_TEST_FOREACH( std::string const&, l, tu.p_labels.get() )
125 m_os << "tu" << tu.p_parent_id << " -> " << "tu" << tu.p_id << ";\n";
127 BOOST_TEST_FOREACH( test_unit_id, dep_id, tu.p_dependencies.get() ) {
128 test_unit const& dep = framework::get( dep_id, TUT_ANY );
130 m_os << "tu" << tu.p_id << " -> " << "tu" << dep.p_id << "[color=red,style=dotted,constraint=false];\n";
134 virtual void visit( test_case const& tc )
136 report_test_unit( tc );
138 virtual bool test_suite_start( test_suite const& ts )
140 if( ts.p_parent_id == INV_TEST_UNIT_ID )
141 m_os << "digraph G {rankdir=LR;\n";
143 report_test_unit( ts );
149 virtual void test_suite_finish( test_suite const& ts )
152 if( ts.p_parent_id == INV_TEST_UNIT_ID )
159 // ************************************************************************** //
160 // ************** labels_collector ************** //
161 // ************************************************************************** //
163 struct labels_collector : test_tree_visitor {
164 std::set<std::string> const& labels() const { return m_labels; }
167 virtual bool visit( test_unit const& tu )
169 m_labels.insert( tu.p_labels->begin(), tu.p_labels->end() );
174 std::set<std::string> m_labels;
177 } // namespace ut_detail
179 // ************************************************************************** //
180 // ************** unit_test_main ************** //
181 // ************************************************************************** //
184 unit_test_main( init_unit_test_func init_func, int argc, char* argv[] )
189 framework::init( init_func, argc, argv );
191 if( runtime_config::get<bool>( runtime_config::btrt_wait_for_debugger ) ) {
192 results_reporter::get_stream() << "Press any key to continue..." << std::endl;
194 // getchar is defined as a macro in uClibc. Use parenthesis to fix
195 // gcc bug 58952 for gcc <= 4.8.2.
197 results_reporter::get_stream() << "Continuing..." << std::endl;
200 framework::finalize_setup_phase();
202 output_format list_cont = runtime_config::get<output_format>( runtime_config::btrt_list_content );
203 if( list_cont != unit_test::OF_INVALID ) {
204 if( list_cont == unit_test::OF_DOT ) {
205 ut_detail::dot_content_reporter reporter( results_reporter::get_stream() );
207 traverse_test_tree( framework::master_test_suite().p_id, reporter, true );
210 ut_detail::hrf_content_reporter reporter( results_reporter::get_stream() );
212 traverse_test_tree( framework::master_test_suite().p_id, reporter, true );
215 return boost::exit_success;
218 if( runtime_config::get<bool>( runtime_config::btrt_list_labels ) ) {
219 ut_detail::labels_collector collector;
221 traverse_test_tree( framework::master_test_suite().p_id, collector, true );
223 results_reporter::get_stream() << "Available labels:\n ";
224 std::copy( collector.labels().begin(), collector.labels().end(),
225 std::ostream_iterator<std::string>( results_reporter::get_stream(), "\n " ) );
226 results_reporter::get_stream() << "\n";
228 return boost::exit_success;
233 results_reporter::make_report();
235 result_code = !runtime_config::get<bool>( runtime_config::btrt_result_code )
236 ? boost::exit_success
237 : results_collector.results( framework::master_test_suite().p_id ).result_code();
239 BOOST_TEST_I_CATCH( framework::nothing_to_test, ex ) {
240 result_code = ex.m_result_code;
242 BOOST_TEST_I_CATCH( framework::internal_error, ex ) {
243 results_reporter::get_stream() << "Boost.Test framework internal error: " << ex.what() << std::endl;
245 result_code = boost::exit_exception_failure;
247 BOOST_TEST_I_CATCH( framework::setup_error, ex ) {
248 results_reporter::get_stream() << "Test setup error: " << ex.what() << std::endl;
250 result_code = boost::exit_exception_failure;
252 BOOST_TEST_I_CATCHALL() {
253 results_reporter::get_stream() << "Boost.Test framework internal error: unknown reason" << std::endl;
255 result_code = boost::exit_exception_failure;
258 framework::shutdown();
263 } // namespace unit_test
266 #if !defined(BOOST_TEST_DYN_LINK) && !defined(BOOST_TEST_NO_MAIN)
268 // ************************************************************************** //
269 // ************** main function for tests using lib ************** //
270 // ************************************************************************** //
272 int BOOST_TEST_CALL_DECL
273 main( int argc, char* argv[] )
275 // prototype for user's unit test init function
276 #ifdef BOOST_TEST_ALTERNATIVE_INIT_API
277 extern bool init_unit_test();
279 boost::unit_test::init_unit_test_func init_func = &init_unit_test;
281 extern ::boost::unit_test::test_suite* init_unit_test_suite( int argc, char* argv[] );
283 boost::unit_test::init_unit_test_func init_func = &init_unit_test_suite;
286 return ::boost::unit_test::unit_test_main( init_func, argc, argv );
289 #endif // !BOOST_TEST_DYN_LINK && !BOOST_TEST_NO_MAIN
291 //____________________________________________________________________________//
293 #include <boost/test/detail/enable_warnings.hpp>
295 #endif // BOOST_TEST_UNIT_TEST_MAIN_IPP_012205GER