results_reporter.ipp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 : 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/results_collector.hpp>
  18. #include <boost/test/framework.hpp>
  19. #include <boost/test/output/plain_report_formatter.hpp>
  20. #include <boost/test/output/xml_report_formatter.hpp>
  21. #include <boost/test/tree/visitor.hpp>
  22. #include <boost/test/tree/test_unit.hpp>
  23. #include <boost/test/tree/traverse.hpp>
  24. // Boost
  25. #include <boost/scoped_ptr.hpp>
  26. #include <boost/io/ios_state.hpp>
  27. typedef ::boost::io::ios_base_all_saver io_saver_type;
  28. // STL
  29. #include <iostream>
  30. #include <boost/test/detail/suppress_warnings.hpp>
  31. //____________________________________________________________________________//
  32. namespace boost {
  33. namespace unit_test {
  34. namespace results_reporter {
  35. // ************************************************************************** //
  36. // ************** result reporter implementation ************** //
  37. // ************************************************************************** //
  38. namespace {
  39. struct results_reporter_impl : test_tree_visitor {
  40. // Constructor
  41. results_reporter_impl()
  42. : m_stream( &std::cerr )
  43. , m_stream_state_saver( new io_saver_type( std::cerr ) )
  44. , m_report_level( CONFIRMATION_REPORT )
  45. , m_formatter( new output::plain_report_formatter )
  46. {}
  47. // test tree visitor interface implementation
  48. void visit( test_case const& tc )
  49. {
  50. m_formatter->test_unit_report_start( tc, *m_stream );
  51. m_formatter->test_unit_report_finish( tc, *m_stream );
  52. }
  53. bool test_suite_start( test_suite const& ts )
  54. {
  55. m_formatter->test_unit_report_start( ts, *m_stream );
  56. if( m_report_level == DETAILED_REPORT && !results_collector.results( ts.p_id ).p_skipped )
  57. return true;
  58. m_formatter->test_unit_report_finish( ts, *m_stream );
  59. return false;
  60. }
  61. void test_suite_finish( test_suite const& ts )
  62. {
  63. m_formatter->test_unit_report_finish( ts, *m_stream );
  64. }
  65. typedef scoped_ptr<io_saver_type> saver_ptr;
  66. // Data members
  67. std::ostream* m_stream;
  68. saver_ptr m_stream_state_saver;
  69. report_level m_report_level;
  70. scoped_ptr<format> m_formatter;
  71. };
  72. results_reporter_impl& s_rr_impl() { static results_reporter_impl the_inst; return the_inst; }
  73. } // local namespace
  74. // ************************************************************************** //
  75. // ************** report configuration ************** //
  76. // ************************************************************************** //
  77. void
  78. set_level( report_level l )
  79. {
  80. if( l != INV_REPORT_LEVEL )
  81. s_rr_impl().m_report_level = l;
  82. }
  83. //____________________________________________________________________________//
  84. void
  85. set_stream( std::ostream& ostr )
  86. {
  87. s_rr_impl().m_stream = &ostr;
  88. s_rr_impl().m_stream_state_saver.reset( new io_saver_type( ostr ) );
  89. }
  90. //____________________________________________________________________________//
  91. std::ostream&
  92. get_stream()
  93. {
  94. return *s_rr_impl().m_stream;
  95. }
  96. //____________________________________________________________________________//
  97. void
  98. set_format( output_format rf )
  99. {
  100. switch( rf ) {
  101. default:
  102. case OF_CLF:
  103. set_format( new output::plain_report_formatter );
  104. break;
  105. case OF_XML:
  106. set_format( new output::xml_report_formatter );
  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_stream );
  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_stream );
  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_stream );
  146. s_rr_impl().m_report_level = bkup;
  147. }
  148. //____________________________________________________________________________//
  149. } // namespace results_reporter
  150. } // namespace unit_test
  151. } // namespace boost
  152. #include <boost/test/detail/enable_warnings.hpp>
  153. #endif // BOOST_TEST_RESULTS_REPORTER_IPP_020105GER