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
6//  See http://www.boost.org/libs/test for the library home page.
7//
8//  File        : $RCSfile$
9//
10//  Version     : $Revision: 57992 $
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/unit_test_suite_impl.hpp>
21#include <boost/test/results_collector.hpp>
22#include <boost/test/framework.hpp>
23#include <boost/test/output/plain_report_formatter.hpp>
24#include <boost/test/output/xml_report_formatter.hpp>
25
26#include <boost/test/detail/unit_test_parameters.hpp>
27
28// Boost
29#include <boost/scoped_ptr.hpp>
30#include <boost/io/ios_state.hpp>
31typedef ::boost::io::ios_base_all_saver io_saver_type;
32
33// STL
34#include <iostream>
35
36#include <boost/test/detail/suppress_warnings.hpp>
37
38//____________________________________________________________________________//
39
40namespace boost {
41
42namespace unit_test {
43
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_output( runtime_config::report_sink() )
56    , m_stream_state_saver( new io_saver_type( *m_output ) )
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_output );
65        m_formatter->test_unit_report_finish( tc, *m_output );
66    }
67    bool    test_suite_start( test_suite const& ts )
68    {
69        m_formatter->test_unit_report_start( ts, *m_output );
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_output );
75        return false;
76    }
77    void    test_suite_finish( test_suite const& ts )
78    {
79        m_formatter->test_unit_report_finish( ts, *m_output );
80    }
81
82    typedef scoped_ptr<io_saver_type> saver_ptr;
83
84    // Data members
85    std::ostream*       m_output;
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_output = &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_output;
121}
122
123//____________________________________________________________________________//
124
125void
126set_format( output_format rf )
127{
128    switch( rf ) {
129    case CLF:
130        set_format( new output::plain_report_formatter );
131        break;
132    case XML:
133        set_format( new output::xml_report_formatter );
134        break;
135    default:
136        break;
137    }
138}
139
140//____________________________________________________________________________//
141
142void
143set_format( results_reporter::format* f )
144{
145    if( f )
146        s_rr_impl().m_formatter.reset( f );
147}
148
149//____________________________________________________________________________//
150
151// ************************************************************************** //
152// **************               report initiation              ************** //
153// ************************************************************************** //
154
155void
156make_report( report_level l, test_unit_id id )
157{
158    if( l == INV_REPORT_LEVEL )
159        l = s_rr_impl().m_report_level;
160
161    if( l == NO_REPORT )
162        return;
163
164    if( id == INV_TEST_UNIT_ID )
165        id = framework::master_test_suite().p_id;
166
167    s_rr_impl().m_stream_state_saver->restore();
168
169    report_level bkup = s_rr_impl().m_report_level;
170    s_rr_impl().m_report_level = l;
171
172    s_rr_impl().m_formatter->results_report_start( *s_rr_impl().m_output );
173
174    switch( l ) {
175    case CONFIRMATION_REPORT:
176        s_rr_impl().m_formatter->do_confirmation_report( framework::get<test_unit>( id ), *s_rr_impl().m_output );
177        break;
178    case SHORT_REPORT:
179    case DETAILED_REPORT:
180        traverse_test_tree( id, s_rr_impl() );
181        break;
182    default:
183        break;
184    }
185
186    s_rr_impl().m_formatter->results_report_finish( *s_rr_impl().m_output );
187    s_rr_impl().m_report_level = bkup;
188}
189
190//____________________________________________________________________________//
191
192} // namespace results_reporter
193
194} // namespace unit_test
195
196} // namespace boost
197
198//____________________________________________________________________________//
199
200#include <boost/test/detail/enable_warnings.hpp>
201
202#endif // BOOST_TEST_RESULTS_REPORTER_IPP_020105GER
203