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