results_reporter.ipp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // (C) Copyright Gennadiy Rozental 2005-2008.
  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: 57992 $
  10. //
  11. // Description : result reporting facilties
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_RESULTS_REPORTER_IPP_020105GER
  14. #define BOOST_TEST_RESULTS_REPORTER_IPP_020105GER
  15. // Boost.Test
  16. #include <boost/test/results_reporter.hpp>
  17. #include <boost/test/unit_test_suite_impl.hpp>
  18. #include <boost/test/results_collector.hpp>
  19. #include <boost/test/framework.hpp>
  20. #include <boost/test/output/plain_report_formatter.hpp>
  21. #include <boost/test/output/xml_report_formatter.hpp>
  22. #include <boost/test/detail/unit_test_parameters.hpp>
  23. // Boost
  24. #include <boost/scoped_ptr.hpp>
  25. #include <boost/io/ios_state.hpp>
  26. typedef ::boost::io::ios_base_all_saver io_saver_type;
  27. // STL
  28. #include <iostream>
  29. #include <boost/test/detail/suppress_warnings.hpp>
  30. //____________________________________________________________________________//
  31. namespace boost {
  32. namespace unit_test {
  33. namespace results_reporter {
  34. // ************************************************************************** //
  35. // ************** result reporter implementation ************** //
  36. // ************************************************************************** //
  37. namespace {
  38. struct results_reporter_impl : test_tree_visitor {
  39. // Constructor
  40. results_reporter_impl()
  41. : m_output( runtime_config::report_sink() )
  42. , m_stream_state_saver( new io_saver_type( *m_output ) )
  43. , m_report_level( CONFIRMATION_REPORT )
  44. , m_formatter( new output::plain_report_formatter )
  45. {}
  46. // test tree visitor interface implementation
  47. void visit( test_case const& tc )
  48. {
  49. m_formatter->test_unit_report_start( tc, *m_output );
  50. m_formatter->test_unit_report_finish( tc, *m_output );
  51. }
  52. bool test_suite_start( test_suite const& ts )
  53. {
  54. m_formatter->test_unit_report_start( ts, *m_output );
  55. if( m_report_level == DETAILED_REPORT && !results_collector.results( ts.p_id ).p_skipped )
  56. return true;
  57. m_formatter->test_unit_report_finish( ts, *m_output );
  58. return false;
  59. }
  60. void test_suite_finish( test_suite const& ts )
  61. {
  62. m_formatter->test_unit_report_finish( ts, *m_output );
  63. }
  64. typedef scoped_ptr<io_saver_type> saver_ptr;
  65. // Data members
  66. std::ostream* m_output;
  67. saver_ptr m_stream_state_saver;
  68. report_level m_report_level;
  69. scoped_ptr<format> m_formatter;
  70. };
  71. results_reporter_impl& s_rr_impl() { static results_reporter_impl the_inst; return the_inst; }
  72. } // local namespace
  73. // ************************************************************************** //
  74. // ************** report configuration ************** //
  75. // ************************************************************************** //
  76. void
  77. set_level( report_level l )
  78. {
  79. if( l != INV_REPORT_LEVEL )
  80. s_rr_impl().m_report_level = l;
  81. }
  82. //____________________________________________________________________________//
  83. void
  84. set_stream( std::ostream& ostr )
  85. {
  86. s_rr_impl().m_output = &ostr;
  87. s_rr_impl().m_stream_state_saver.reset( new io_saver_type( ostr ) );
  88. }
  89. //____________________________________________________________________________//
  90. std::ostream&
  91. get_stream()
  92. {
  93. return *s_rr_impl().m_output;
  94. }
  95. //____________________________________________________________________________//
  96. void
  97. set_format( output_format rf )
  98. {
  99. switch( rf ) {
  100. case CLF:
  101. set_format( new output::plain_report_formatter );
  102. break;
  103. case XML:
  104. set_format( new output::xml_report_formatter );
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. //____________________________________________________________________________//
  111. void
  112. set_format( results_reporter::format* f )
  113. {
  114. if( f )
  115. s_rr_impl().m_formatter.reset( f );
  116. }
  117. //____________________________________________________________________________//
  118. // ************************************************************************** //
  119. // ************** report initiation ************** //
  120. // ************************************************************************** //
  121. void
  122. make_report( report_level l, test_unit_id id )
  123. {
  124. if( l == INV_REPORT_LEVEL )
  125. l = s_rr_impl().m_report_level;
  126. if( l == NO_REPORT )
  127. return;
  128. if( id == INV_TEST_UNIT_ID )
  129. id = framework::master_test_suite().p_id;
  130. s_rr_impl().m_stream_state_saver->restore();
  131. report_level bkup = s_rr_impl().m_report_level;
  132. s_rr_impl().m_report_level = l;
  133. s_rr_impl().m_formatter->results_report_start( *s_rr_impl().m_output );
  134. switch( l ) {
  135. case CONFIRMATION_REPORT:
  136. s_rr_impl().m_formatter->do_confirmation_report( framework::get<test_unit>( id ), *s_rr_impl().m_output );
  137. break;
  138. case SHORT_REPORT:
  139. case DETAILED_REPORT:
  140. traverse_test_tree( id, s_rr_impl() );
  141. break;
  142. default:
  143. break;
  144. }
  145. s_rr_impl().m_formatter->results_report_finish( *s_rr_impl().m_output );
  146. s_rr_impl().m_report_level = bkup;
  147. }
  148. //____________________________________________________________________________//
  149. } // namespace results_reporter
  150. } // namespace unit_test
  151. } // namespace boost
  152. //____________________________________________________________________________//
  153. #include <boost/test/detail/enable_warnings.hpp>
  154. #endif // BOOST_TEST_RESULTS_REPORTER_IPP_020105GER