source: NonGTP/Boost/boost/test/impl/results_collector.ipp @ 857

Revision 857, 8.1 KB checked in by igarcia, 19 years ago (diff)
Line 
1//  (C) Copyright Gennadiy Rozental 2005.
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: results_collector.ipp,v $
9//
10//  Version     : $Revision: 1.2 $
11//
12//  Description : implements Unit Test results collecting facility.
13// ***************************************************************************
14
15#ifndef BOOST_TEST_RESULTS_COLLECTOR_IPP_021105GER
16#define BOOST_TEST_RESULTS_COLLECTOR_IPP_021105GER
17
18// Boost.Test
19#include <boost/test/unit_test_suite.hpp>
20#include <boost/test/test_tools.hpp>
21#include <boost/test/results_collector.hpp>
22#include <boost/test/framework.hpp>
23
24// Boost
25#include <boost/cstdlib.hpp>
26
27// STL
28#include <map>
29
30#include <boost/test/detail/suppress_warnings.hpp>
31
32//____________________________________________________________________________//
33
34namespace boost {
35
36namespace unit_test {
37
38// ************************************************************************** //
39// **************                 test_results                 ************** //
40// ************************************************************************** //
41
42test_results::test_results()
43{
44    clear();
45}
46
47//____________________________________________________________________________//
48
49bool
50test_results::passed() const
51{
52    return  !p_skipped                                  &&
53            p_test_cases_failed == 0                    &&
54            p_assertions_failed <= p_expected_failures  &&
55            !p_aborted;
56}
57
58//____________________________________________________________________________//
59
60int
61test_results::result_code() const
62{
63    return passed() ? exit_success
64           : ( (p_assertions_failed > p_expected_failures || p_skipped )
65                    ? exit_test_failure
66                    : exit_exception_failure );
67}
68
69//____________________________________________________________________________//
70
71void
72test_results::operator+=( test_results const& tr )
73{
74    p_assertions_passed.value   += tr.p_assertions_passed;
75    p_assertions_failed.value   += tr.p_assertions_failed;
76    p_test_cases_passed.value   += tr.p_test_cases_passed;
77    p_test_cases_failed.value   += tr.p_test_cases_failed;
78    p_test_cases_skipped.value  += tr.p_test_cases_skipped;
79}
80
81//____________________________________________________________________________//
82
83void
84test_results::clear()
85{
86    p_assertions_passed.value    = 0;
87    p_assertions_failed.value    = 0;
88    p_expected_failures.value    = 0;
89    p_test_cases_passed.value    = 0;
90    p_test_cases_failed.value    = 0;
91    p_test_cases_skipped.value   = 0;
92    p_aborted.value              = false;
93    p_skipped.value              = true;
94}
95
96//____________________________________________________________________________//
97   
98// ************************************************************************** //
99// **************               results_collector              ************** //
100// ************************************************************************** //
101
102namespace {
103
104struct results_collector_impl {
105    std::map<test_unit_id,test_results> m_results_store;
106};
107
108results_collector_impl& s_rc_impl() { static results_collector_impl the_inst; return the_inst; }
109
110} // local namespace
111
112//____________________________________________________________________________//
113
114void
115results_collector_t::test_start( counter_t )
116{
117    s_rc_impl().m_results_store.clear();
118}
119
120//____________________________________________________________________________//
121
122void
123results_collector_t::test_finish()
124{
125    // do nothing
126}
127
128//____________________________________________________________________________//
129
130void
131results_collector_t::test_aborted()
132{
133    // do nothing
134}
135
136//____________________________________________________________________________//
137
138void
139results_collector_t::test_unit_start( test_unit const& tu )
140{
141    // init test_results entry
142    test_results& tr = s_rc_impl().m_results_store[tu.p_id];
143
144    tr.clear();
145   
146    tr.p_expected_failures.value    = tu.p_expected_failures;
147    tr.p_skipped.value              = false;
148}
149
150//____________________________________________________________________________//
151
152class results_collect_helper : public test_tree_visitor {
153public:
154    explicit results_collect_helper( test_results& tr, test_unit const& ts ) : m_tr( tr ), m_ts( ts ) {}
155
156    void    visit( test_case const& tc )
157    {
158        test_results const& tr = results_collector.results( tc.p_id );
159        m_tr += tr;
160
161        if( tr.passed() )
162            m_tr.p_test_cases_passed.value++;
163        else if( tr.p_skipped )
164            m_tr.p_test_cases_skipped.value++;
165        else
166            m_tr.p_test_cases_failed.value++;
167    }
168    bool    test_suite_start( test_suite const& ts )
169    {
170        if( m_ts.p_id == ts.p_id )
171            return true;
172        else {
173            m_tr += results_collector.results( ts.p_id );
174            return false;
175        }
176    }
177
178private:
179    // Data members
180    test_results&       m_tr;
181    test_unit const&    m_ts;
182};
183
184//____________________________________________________________________________//
185
186void
187results_collector_t::test_unit_finish( test_unit const& tu, unsigned long )
188{
189    if( tu.p_type == tut_suite ) {
190        results_collect_helper ch( s_rc_impl().m_results_store[tu.p_id], tu );
191
192        traverse_test_tree( tu, ch );
193    }
194    else {
195        test_results const& tr = s_rc_impl().m_results_store[tu.p_id];
196       
197        BOOST_WARN_MESSAGE( tr.p_aborted || tr.p_assertions_failed >= tr.p_expected_failures, "Test case has less failures then expected" );
198    }
199}
200
201//____________________________________________________________________________//
202
203void
204results_collector_t::test_unit_skipped( test_unit const& tu )
205{
206    if( tu.p_type == tut_suite ) {
207        test_case_counter tcc;
208        traverse_test_tree( tu, tcc );
209
210        test_results& tr = s_rc_impl().m_results_store[tu.p_id];
211
212        tr.clear();
213   
214        tr.p_skipped.value              = true;
215        tr.p_test_cases_skipped.value   = tcc.m_count;
216    }
217}
218
219//____________________________________________________________________________//
220
221void
222results_collector_t::assertion_result( bool passed )
223{
224    test_results& tr = s_rc_impl().m_results_store[framework::current_test_case().p_id];
225
226    if( passed )
227        tr.p_assertions_passed.value++;
228    else
229        tr.p_assertions_failed.value++;
230
231    if( tr.p_assertions_failed == 1 )
232        first_failed_assertion();
233}
234
235//____________________________________________________________________________//
236
237void
238results_collector_t::exception_caught( execution_exception const& )
239{
240    // do nothing
241}
242
243//____________________________________________________________________________//
244
245void
246results_collector_t::test_unit_aborted( test_unit const& tu )
247{
248    s_rc_impl().m_results_store[tu.p_id].p_aborted.value = true;
249}
250
251//____________________________________________________________________________//
252
253test_results const&
254results_collector_t::results( test_unit_id id ) const
255{
256    return s_rc_impl().m_results_store[id];
257}
258
259//____________________________________________________________________________//
260
261} // namespace unit_test
262
263} // namespace boost
264
265//____________________________________________________________________________//
266
267#include <boost/test/detail/enable_warnings.hpp>
268
269// ***************************************************************************
270//  Revision History :
271//
272//  $Log: results_collector.ipp,v $
273//  Revision 1.2  2005/03/24 04:02:33  rogeeff
274//  portability fixes
275//
276//  Revision 1.1  2005/02/20 08:27:07  rogeeff
277//  This a major update for Boost.Test framework. See release docs for complete list of fixes/updates
278//
279// ***************************************************************************
280
281#endif // BOOST_TEST_RESULTS_COLLECTOR_IPP_021105GER
Note: See TracBrowser for help on using the repository browser.