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
6//  See http://www.boost.org/libs/test for the library home page.
7//
8//  File        : $RCSfile$
9//
10//  Version     : $Revision$
11//
12//  Description : result reporting facilties
13// ***************************************************************************
14
15#ifndef BOOST_TEST_RESULTS_REPORTER_IPP_020105GER
16#define BOOST_TEST_RESULTS_REPORTER_IPP_020105GER
17
18// Boost.Test
19#include <boost/test/results_reporter.hpp>
20#include <boost/test/results_collector.hpp>
21#include <boost/test/framework.hpp>
22
23#include <boost/test/output/plain_report_formatter.hpp>
24#include <boost/test/output/xml_report_formatter.hpp>
25
26#include <boost/test/tree/visitor.hpp>
27#include <boost/test/tree/test_unit.hpp>
28#include <boost/test/tree/traverse.hpp>
29
30// Boost
31#include <boost/scoped_ptr.hpp>
32#include <boost/io/ios_state.hpp>
33typedef ::boost::io::ios_base_all_saver io_saver_type;
34
35// STL
36#include <iostream>
37
38#include <boost/test/detail/suppress_warnings.hpp>
39
40//____________________________________________________________________________//
41
42namespace boost {
43namespace unit_test {
44namespace results_reporter {
45
46// ************************************************************************** //
47// **************        result reporter implementation        ************** //
48// ************************************************************************** //
49
50namespace {
51
52struct results_reporter_impl : test_tree_visitor {
53    // Constructor
54    results_reporter_impl()
55    : m_stream( &std::cerr )
56    , m_stream_state_saver( new io_saver_type( std::cerr ) )
57    , m_report_level( CONFIRMATION_REPORT )
58    , m_formatter( new output::plain_report_formatter )
59    {}
60
61    // test tree visitor interface implementation
62    void    visit( test_case const& tc )
63    {
64        m_formatter->test_unit_report_start( tc, *m_stream );
65        m_formatter->test_unit_report_finish( tc, *m_stream );
66    }
67    bool    test_suite_start( test_suite const& ts )
68    {
69        m_formatter->test_unit_report_start( ts, *m_stream );
70
71        if( m_report_level == DETAILED_REPORT && !results_collector.results( ts.p_id ).p_skipped )
72            return true;
73
74        m_formatter->test_unit_report_finish( ts, *m_stream );
75        return false;
76    }
77    void    test_suite_finish( test_suite const& ts )
78    {
79        m_formatter->test_unit_report_finish( ts, *m_stream );
80    }
81
82    typedef scoped_ptr<io_saver_type> saver_ptr;
83
84    // Data members
85    std::ostream*       m_stream;
86    saver_ptr           m_stream_state_saver;
87    report_level        m_report_level;
88    scoped_ptr<format>  m_formatter;
89};
90
91results_reporter_impl& s_rr_impl() { static results_reporter_impl the_inst; return the_inst; }
92
93} // local namespace
94
95// ************************************************************************** //
96// **************              report configuration            ************** //
97// ************************************************************************** //
98
99void
100set_level( report_level l )
101{
102    if( l != INV_REPORT_LEVEL )
103        s_rr_impl().m_report_level = l;
104}
105
106//____________________________________________________________________________//
107
108void
109set_stream( std::ostream& ostr )
110{
111    s_rr_impl().m_stream = &ostr;
112    s_rr_impl().m_stream_state_saver.reset( new io_saver_type( ostr ) );
113}
114
115//____________________________________________________________________________//
116
117std::ostream&
118get_stream()
119{
120    return *s_rr_impl().m_stream;
121}
122
123//____________________________________________________________________________//
124
125void
126set_format( output_format rf )
127{
128    switch( rf ) {
129    default:
130    case OF_CLF:
131        set_format( new output::plain_report_formatter );
132        break;
133    case OF_XML:
134        set_format( new output::xml_report_formatter );
135        break;
136    }
137}
138
139//____________________________________________________________________________//
140
141void
142set_format( results_reporter::format* f )
143{
144    if( f )
145        s_rr_impl().m_formatter.reset( f );
146}
147
148//____________________________________________________________________________//
149
150// ************************************************************************** //
151// **************               report initiation              ************** //
152// ************************************************************************** //
153
154void
155make_report( report_level l, test_unit_id id )
156{
157    if( l == INV_REPORT_LEVEL )
158        l = s_rr_impl().m_report_level;
159
160    if( l == NO_REPORT )
161        return;
162
163    if( id == INV_TEST_UNIT_ID )
164        id = framework::master_test_suite().p_id;
165
166    s_rr_impl().m_stream_state_saver->restore();
167
168    report_level bkup = s_rr_impl().m_report_level;
169    s_rr_impl().m_report_level = l;
170
171    s_rr_impl().m_formatter->results_report_start( *s_rr_impl().m_stream );
172
173    switch( l ) {
174    case CONFIRMATION_REPORT:
175        s_rr_impl().m_formatter->do_confirmation_report( framework::get<test_unit>( id ), *s_rr_impl().m_stream );
176        break;
177    case SHORT_REPORT:
178    case DETAILED_REPORT:
179        traverse_test_tree( id, s_rr_impl() );
180        break;
181    default:
182        break;
183    }
184
185    s_rr_impl().m_formatter->results_report_finish( *s_rr_impl().m_stream );
186    s_rr_impl().m_report_level = bkup;
187}
188
189//____________________________________________________________________________//
190
191} // namespace results_reporter
192} // namespace unit_test
193} // namespace boost
194
195#include <boost/test/detail/enable_warnings.hpp>
196
197#endif // BOOST_TEST_RESULTS_REPORTER_IPP_020105GER
198