File indexing completed on 2025-01-30 10:01:00
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef BOOST_TEST_RESULTS_COLLECTOR_IPP_021105GER
0014 #define BOOST_TEST_RESULTS_COLLECTOR_IPP_021105GER
0015
0016
0017 #include <boost/test/unit_test_log.hpp>
0018 #include <boost/test/results_collector.hpp>
0019 #include <boost/test/framework.hpp>
0020 #include <boost/test/execution_monitor.hpp>
0021
0022 #include <boost/test/tree/test_unit.hpp>
0023 #include <boost/test/tree/visitor.hpp>
0024 #include <boost/test/tree/test_case_counter.hpp>
0025 #include <boost/test/tree/traverse.hpp>
0026
0027
0028 #include <boost/cstdlib.hpp>
0029
0030
0031 #include <map>
0032
0033 #include <boost/test/detail/suppress_warnings.hpp>
0034
0035
0036
0037 namespace boost {
0038 namespace unit_test {
0039
0040
0041
0042
0043
0044 test_results::test_results()
0045 {
0046 clear();
0047 }
0048
0049
0050
0051 bool
0052 test_results::passed() const
0053 {
0054
0055
0056 return !p_skipped &&
0057 p_test_cases_failed == 0 &&
0058 p_assertions_failed <= p_expected_failures &&
0059
0060 !p_timed_out &&
0061 p_test_cases_timed_out == 0 &&
0062 !aborted();
0063 }
0064
0065
0066
0067 bool
0068 test_results::aborted() const
0069 {
0070 return p_aborted;
0071 }
0072
0073
0074
0075 bool
0076 test_results::skipped() const
0077 {
0078 return p_skipped;
0079 }
0080
0081
0082
0083 int
0084 test_results::result_code() const
0085 {
0086 return passed() ? exit_success
0087 : ( (p_assertions_failed > p_expected_failures || p_skipped || p_timed_out || p_test_cases_timed_out )
0088 ? exit_test_failure
0089 : exit_exception_failure );
0090 }
0091
0092
0093
0094 void
0095 test_results::operator+=( test_results const& tr )
0096 {
0097 p_test_suites.value += tr.p_test_suites;
0098 p_assertions_passed.value += tr.p_assertions_passed;
0099 p_assertions_failed.value += tr.p_assertions_failed;
0100 p_warnings_failed.value += tr.p_warnings_failed;
0101 p_test_cases_passed.value += tr.p_test_cases_passed;
0102 p_test_cases_warned.value += tr.p_test_cases_warned;
0103 p_test_cases_failed.value += tr.p_test_cases_failed;
0104 p_test_cases_skipped.value += tr.p_test_cases_skipped;
0105 p_test_cases_aborted.value += tr.p_test_cases_aborted;
0106 p_test_cases_timed_out.value += tr.p_test_cases_timed_out;
0107 p_test_suites_timed_out.value += tr.p_test_suites_timed_out;
0108 p_duration_microseconds.value += tr.p_duration_microseconds;
0109 }
0110
0111
0112
0113 void
0114 test_results::clear()
0115 {
0116 p_test_suites.value = 0;
0117 p_assertions_passed.value = 0;
0118 p_assertions_failed.value = 0;
0119 p_warnings_failed.value = 0;
0120 p_expected_failures.value = 0;
0121 p_test_cases_passed.value = 0;
0122 p_test_cases_warned.value = 0;
0123 p_test_cases_failed.value = 0;
0124 p_test_cases_skipped.value = 0;
0125 p_test_cases_aborted.value = 0;
0126 p_test_cases_timed_out.value = 0;
0127 p_test_suites_timed_out.value = 0;
0128 p_duration_microseconds.value= 0;
0129 p_aborted.value = false;
0130 p_skipped.value = false;
0131 p_timed_out.value = false;
0132 }
0133
0134
0135
0136
0137
0138
0139
0140 namespace {
0141
0142 struct results_collector_impl {
0143 std::map<test_unit_id,test_results> m_results_store;
0144 };
0145
0146 results_collector_impl& s_rc_impl() { static results_collector_impl the_inst; return the_inst; }
0147
0148
0149 class clear_subtree_result : public test_tree_visitor {
0150 public:
0151 clear_subtree_result(results_collector_impl& store)
0152 : m_store( store )
0153 {}
0154
0155 private:
0156 bool visit( test_unit const& tu) BOOST_OVERRIDE
0157 {
0158 typedef std::map<test_unit_id,test_results>::iterator iterator;
0159 iterator found = m_store.m_results_store.find(tu.p_id);
0160 if(found != m_store.m_results_store.end()) {
0161 m_store.m_results_store.erase( found );
0162 }
0163 return true;
0164 }
0165
0166 results_collector_impl& m_store;
0167 };
0168
0169 }
0170
0171
0172
0173 BOOST_TEST_SINGLETON_CONS_IMPL( results_collector_t )
0174
0175
0176
0177 void
0178 results_collector_t::test_start( counter_t, test_unit_id id )
0179 {
0180
0181 clear_subtree_result tree_clear(s_rc_impl());
0182 traverse_test_tree( id, tree_clear );
0183 }
0184
0185
0186
0187 void
0188 results_collector_t::test_unit_start( test_unit const& tu )
0189 {
0190
0191 test_results& tr = s_rc_impl().m_results_store[tu.p_id];
0192
0193 tr.clear();
0194
0195 tr.p_expected_failures.value = tu.p_expected_failures;
0196 }
0197
0198
0199
0200 class results_collect_helper : public test_tree_visitor {
0201 public:
0202 explicit results_collect_helper( test_results& tr, test_unit const& ts ) : m_tr( tr ), m_ts( ts ) {}
0203
0204 void visit( test_case const& tc ) BOOST_OVERRIDE
0205 {
0206 test_results const& tr = results_collector.results( tc.p_id );
0207 m_tr += tr;
0208
0209 if( tr.passed() ) {
0210 if( tr.p_warnings_failed )
0211 m_tr.p_test_cases_warned.value++;
0212 else
0213 m_tr.p_test_cases_passed.value++;
0214 }
0215 else if( tr.p_timed_out ) {
0216 m_tr.p_test_cases_timed_out.value++;
0217 }
0218 else if( tr.p_skipped || !tc.is_enabled() ) {
0219 m_tr.p_test_cases_skipped.value++;
0220 }
0221 else {
0222 if( tr.p_aborted )
0223 m_tr.p_test_cases_aborted.value++;
0224
0225 m_tr.p_test_cases_failed.value++;
0226 }
0227 }
0228 bool test_suite_start( test_suite const& ts ) BOOST_OVERRIDE
0229 {
0230 if( m_ts.p_id == ts.p_id )
0231 return true;
0232
0233 m_tr += results_collector.results( ts.p_id );
0234 m_tr.p_test_suites.value++;
0235
0236 if( results_collector.results( ts.p_id ).p_timed_out )
0237 m_tr.p_test_suites_timed_out.value++;
0238 return false;
0239 }
0240
0241 private:
0242
0243 test_results& m_tr;
0244 test_unit const& m_ts;
0245 };
0246
0247
0248
0249 void
0250 results_collector_t::test_unit_finish( test_unit const& tu, unsigned long elapsed_in_microseconds )
0251 {
0252 test_results & tr = s_rc_impl().m_results_store[tu.p_id];
0253 if( tu.p_type == TUT_SUITE ) {
0254 results_collect_helper ch( tr, tu );
0255 traverse_test_tree( tu, ch, true );
0256 }
0257 else {
0258 bool num_failures_match = tr.p_aborted || tr.p_assertions_failed >= tr.p_expected_failures;
0259 if( !num_failures_match )
0260 BOOST_TEST_FRAMEWORK_MESSAGE( "Test case " << tu.full_name() << " has fewer failures than expected" );
0261
0262 bool check_any_assertions = tr.p_aborted || (tr.p_assertions_failed != 0) || (tr.p_assertions_passed != 0);
0263 if( !check_any_assertions )
0264 BOOST_TEST_FRAMEWORK_MESSAGE( "Test case " << tu.full_name() << " did not check any assertions" );
0265 }
0266 tr.p_duration_microseconds.value = elapsed_in_microseconds;
0267 }
0268
0269
0270
0271 void
0272 results_collector_t::test_unit_skipped( test_unit const& tu, const_string )
0273 {
0274 test_results& tr = s_rc_impl().m_results_store[tu.p_id];
0275 tr.clear();
0276
0277 tr.p_skipped.value = true;
0278
0279 if( tu.p_type == TUT_SUITE ) {
0280 test_case_counter tcc(true);
0281 traverse_test_tree( tu, tcc, true );
0282
0283 tr.p_test_cases_skipped.value = tcc.p_count;
0284 }
0285 }
0286
0287
0288
0289 void
0290 results_collector_t::test_unit_timed_out(test_unit const& tu)
0291 {
0292 test_results& tr = s_rc_impl().m_results_store[tu.p_id];
0293 tr.p_timed_out.value = true;
0294 }
0295
0296
0297
0298 void
0299 results_collector_t::assertion_result( unit_test::assertion_result ar )
0300 {
0301 test_results& tr = s_rc_impl().m_results_store[framework::current_test_case_id()];
0302
0303 switch( ar ) {
0304 case AR_PASSED: tr.p_assertions_passed.value++; break;
0305 case AR_FAILED: tr.p_assertions_failed.value++; break;
0306 case AR_TRIGGERED: tr.p_warnings_failed.value++; break;
0307 }
0308
0309 if( tr.p_assertions_failed == 1 )
0310 first_failed_assertion();
0311 }
0312
0313
0314
0315 void
0316 results_collector_t::exception_caught( execution_exception const& ex)
0317 {
0318 test_results& tr = s_rc_impl().m_results_store[framework::current_test_case_id()];
0319
0320 tr.p_assertions_failed.value++;
0321 if( ex.code() == execution_exception::timeout_error ) {
0322 tr.p_timed_out.value = true;
0323 }
0324 }
0325
0326
0327
0328 void
0329 results_collector_t::test_unit_aborted( test_unit const& tu )
0330 {
0331 s_rc_impl().m_results_store[tu.p_id].p_aborted.value = true;
0332 }
0333
0334
0335
0336 test_results const&
0337 results_collector_t::results( test_unit_id id ) const
0338 {
0339 return s_rc_impl().m_results_store[id];
0340 }
0341
0342
0343
0344 }
0345 }
0346
0347 #include <boost/test/detail/enable_warnings.hpp>
0348
0349 #endif