unit_test_main.ipp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision$
  10. //
  11. // Description : main function implementation for Unit Test Framework
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_UNIT_TEST_MAIN_IPP_012205GER
  14. #define BOOST_TEST_UNIT_TEST_MAIN_IPP_012205GER
  15. // Boost.Test
  16. #include <boost/test/framework.hpp>
  17. #include <boost/test/results_collector.hpp>
  18. #include <boost/test/results_reporter.hpp>
  19. #include <boost/test/tree/visitor.hpp>
  20. #include <boost/test/tree/test_unit.hpp>
  21. #include <boost/test/tree/traverse.hpp>
  22. #include <boost/test/unit_test_parameters.hpp>
  23. #include <boost/test/utils/foreach.hpp>
  24. #include <boost/test/utils/basic_cstring/io.hpp>
  25. // Boost
  26. #include <boost/cstdlib.hpp>
  27. // STL
  28. #include <cstdio>
  29. #include <stdexcept>
  30. #include <iostream>
  31. #include <iomanip>
  32. #include <set>
  33. #include <boost/test/detail/suppress_warnings.hpp>
  34. //____________________________________________________________________________//
  35. namespace boost {
  36. namespace unit_test {
  37. namespace ut_detail {
  38. // ************************************************************************** //
  39. // ************** hrf_content_reporter ************** //
  40. // ************************************************************************** //
  41. struct hrf_content_reporter : test_tree_visitor {
  42. explicit hrf_content_reporter( std::ostream& os ) : m_os( os ), m_indent( -4 ) {} // skip master test suite
  43. private:
  44. void report_test_unit( test_unit const& tu )
  45. {
  46. m_os << std::setw( m_indent ) << "" << tu.p_name;
  47. m_os << (tu.p_default_status == test_unit::RS_ENABLED ? "*" : " ");
  48. //m_os << '[' << tu.p_sibling_rank << ']';
  49. if( !tu.p_description->empty() )
  50. m_os << ": " << tu.p_description;
  51. m_os << "\n";
  52. }
  53. virtual void visit( test_case const& tc ) { report_test_unit( tc ); }
  54. virtual bool test_suite_start( test_suite const& ts )
  55. {
  56. if( m_indent >= 0 )
  57. report_test_unit( ts );
  58. m_indent += 4;
  59. return true;
  60. }
  61. virtual void test_suite_finish( test_suite const& )
  62. {
  63. m_indent -= 4;
  64. }
  65. // Data members
  66. std::ostream& m_os;
  67. int m_indent;
  68. };
  69. // ************************************************************************** //
  70. // ************** dot_content_reporter ************** //
  71. // ************************************************************************** //
  72. struct dot_content_reporter : test_tree_visitor {
  73. explicit dot_content_reporter( std::ostream& os ) : m_os( os ) {}
  74. private:
  75. void report_test_unit( test_unit const& tu )
  76. {
  77. bool master_ts = tu.p_parent_id == INV_TEST_UNIT_ID;
  78. m_os << "tu" << tu.p_id;
  79. m_os << (master_ts ? "[shape=ellipse,peripheries=2" : "[shape=Mrecord" );
  80. m_os << ",fontname=Helvetica";
  81. m_os << (tu.p_default_status == test_unit::RS_ENABLED ? ",color=green" : ",color=yellow");
  82. if( master_ts )
  83. m_os << ",label=\"" << tu.p_name << "\"];\n";
  84. else {
  85. m_os << ",label=\"" << tu.p_name << "|" << tu.p_file_name << "(" << tu.p_line_num << ")";
  86. if( tu.p_timeout > 0 )
  87. m_os << "|timeout=" << tu.p_timeout;
  88. if( tu.p_expected_failures != 0 )
  89. m_os << "|expected failures=" << tu.p_expected_failures;
  90. if( !tu.p_labels->empty() ) {
  91. m_os << "|labels:";
  92. BOOST_TEST_FOREACH( std::string const&, l, tu.p_labels.get() )
  93. m_os << " @" << l;
  94. }
  95. m_os << "\"];\n";
  96. }
  97. if( !master_ts )
  98. m_os << "tu" << tu.p_parent_id << " -> " << "tu" << tu.p_id << ";\n";
  99. BOOST_TEST_FOREACH( test_unit_id, dep_id, tu.p_dependencies.get() ) {
  100. test_unit const& dep = framework::get( dep_id, TUT_ANY );
  101. m_os << "tu" << tu.p_id << " -> " << "tu" << dep.p_id << "[color=red,style=dotted,constraint=false];\n";
  102. }
  103. }
  104. virtual void visit( test_case const& tc )
  105. {
  106. report_test_unit( tc );
  107. }
  108. virtual bool test_suite_start( test_suite const& ts )
  109. {
  110. if( ts.p_parent_id == INV_TEST_UNIT_ID )
  111. m_os << "digraph G {rankdir=LR;\n";
  112. report_test_unit( ts );
  113. m_os << "{\n";
  114. return true;
  115. }
  116. virtual void test_suite_finish( test_suite const& ts )
  117. {
  118. m_os << "}\n";
  119. if( ts.p_parent_id == INV_TEST_UNIT_ID )
  120. m_os << "}\n";
  121. }
  122. std::ostream& m_os;
  123. };
  124. // ************************************************************************** //
  125. // ************** labels_collector ************** //
  126. // ************************************************************************** //
  127. struct labels_collector : test_tree_visitor {
  128. std::set<std::string> const& labels() const { return m_labels; }
  129. private:
  130. virtual bool visit( test_unit const& tu )
  131. {
  132. m_labels.insert( tu.p_labels->begin(), tu.p_labels->end() );
  133. return true;
  134. }
  135. // Data members
  136. std::set<std::string> m_labels;
  137. };
  138. struct framework_shutdown_helper {
  139. ~framework_shutdown_helper() {
  140. try {
  141. framework::shutdown();
  142. }
  143. catch(...) {
  144. std::cerr << "Boost.Test shutdown exception caught" << std::endl;
  145. }
  146. }
  147. };
  148. } // namespace ut_detail
  149. // ************************************************************************** //
  150. // ************** unit_test_main ************** //
  151. // ************************************************************************** //
  152. int BOOST_TEST_DECL
  153. unit_test_main( init_unit_test_func init_func, int argc, char* argv[] )
  154. {
  155. int result_code = 0;
  156. ut_detail::framework_shutdown_helper shutdown_helper;
  157. BOOST_TEST_I_TRY {
  158. framework::init( init_func, argc, argv );
  159. if( runtime_config::get<bool>( runtime_config::btrt_wait_for_debugger ) ) {
  160. results_reporter::get_stream() << "Press any key to continue..." << std::endl;
  161. // getchar is defined as a macro in uClibc. Use parenthesis to fix
  162. // gcc bug 58952 for gcc <= 4.8.2.
  163. (std::getchar)();
  164. results_reporter::get_stream() << "Continuing..." << std::endl;
  165. }
  166. framework::finalize_setup_phase();
  167. output_format list_cont = runtime_config::get<output_format>( runtime_config::btrt_list_content );
  168. if( list_cont != unit_test::OF_INVALID ) {
  169. if( list_cont == unit_test::OF_DOT ) {
  170. ut_detail::dot_content_reporter reporter( results_reporter::get_stream() );
  171. traverse_test_tree( framework::master_test_suite().p_id, reporter, true );
  172. }
  173. else {
  174. ut_detail::hrf_content_reporter reporter( results_reporter::get_stream() );
  175. traverse_test_tree( framework::master_test_suite().p_id, reporter, true );
  176. }
  177. return boost::exit_success;
  178. }
  179. if( runtime_config::get<bool>( runtime_config::btrt_list_labels ) ) {
  180. ut_detail::labels_collector collector;
  181. traverse_test_tree( framework::master_test_suite().p_id, collector, true );
  182. results_reporter::get_stream() << "Available labels:\n ";
  183. std::copy( collector.labels().begin(), collector.labels().end(),
  184. std::ostream_iterator<std::string>( results_reporter::get_stream(), "\n " ) );
  185. results_reporter::get_stream() << "\n";
  186. return boost::exit_success;
  187. }
  188. framework::run();
  189. result_code = !runtime_config::get<bool>( runtime_config::btrt_result_code )
  190. ? boost::exit_success
  191. : results_collector.results( framework::master_test_suite().p_id ).result_code();
  192. }
  193. BOOST_TEST_I_CATCH( framework::nothing_to_test, ex ) {
  194. result_code = ex.m_result_code;
  195. }
  196. BOOST_TEST_I_CATCH( framework::internal_error, ex ) {
  197. results_reporter::get_stream() << "Boost.Test framework internal error: " << ex.what() << std::endl;
  198. result_code = boost::exit_exception_failure;
  199. }
  200. BOOST_TEST_I_CATCH( framework::setup_error, ex ) {
  201. results_reporter::get_stream() << "Test setup error: " << ex.what() << std::endl;
  202. result_code = boost::exit_exception_failure;
  203. }
  204. BOOST_TEST_I_CATCH( std::logic_error, ex ) {
  205. results_reporter::get_stream() << "Test setup error: " << ex.what() << std::endl;
  206. result_code = boost::exit_exception_failure;
  207. }
  208. BOOST_TEST_I_CATCHALL() {
  209. results_reporter::get_stream() << "Boost.Test framework internal error: unknown reason" << std::endl;
  210. result_code = boost::exit_exception_failure;
  211. }
  212. return result_code;
  213. }
  214. } // namespace unit_test
  215. } // namespace boost
  216. #if !defined(BOOST_TEST_DYN_LINK) && !defined(BOOST_TEST_NO_MAIN)
  217. // ************************************************************************** //
  218. // ************** main function for tests using lib ************** //
  219. // ************************************************************************** //
  220. int BOOST_TEST_CALL_DECL
  221. main( int argc, char* argv[] )
  222. {
  223. // prototype for user's unit test init function
  224. #ifdef BOOST_TEST_ALTERNATIVE_INIT_API
  225. extern bool init_unit_test();
  226. boost::unit_test::init_unit_test_func init_func = &init_unit_test;
  227. #else
  228. extern ::boost::unit_test::test_suite* init_unit_test_suite( int argc, char* argv[] );
  229. boost::unit_test::init_unit_test_func init_func = &init_unit_test_suite;
  230. #endif
  231. return ::boost::unit_test::unit_test_main( init_func, argc, argv );
  232. }
  233. #endif // !BOOST_TEST_DYN_LINK && !BOOST_TEST_NO_MAIN
  234. //____________________________________________________________________________//
  235. #include <boost/test/detail/enable_warnings.hpp>
  236. #endif // BOOST_TEST_UNIT_TEST_MAIN_IPP_012205GER