| 1 | // (C) Copyright Gennadiy Rozental 2001-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: unit_test_suite_impl.hpp,v $ |
|---|
| 9 | // |
|---|
| 10 | // Version : $Revision: 1.2 $ |
|---|
| 11 | // |
|---|
| 12 | // Description : defines test_unit, test_case, test_case_results, test_suite and test_tree_visitor |
|---|
| 13 | // *************************************************************************** |
|---|
| 14 | |
|---|
| 15 | #ifndef BOOST_TEST_UNIT_TEST_SUITE_IMPL_HPP_071894GER |
|---|
| 16 | #define BOOST_TEST_UNIT_TEST_SUITE_IMPL_HPP_071894GER |
|---|
| 17 | |
|---|
| 18 | // Boost.Test |
|---|
| 19 | #include <boost/test/detail/config.hpp> |
|---|
| 20 | #include <boost/test/detail/global_typedef.hpp> |
|---|
| 21 | #include <boost/test/utils/class_properties.hpp> |
|---|
| 22 | #include <boost/test/utils/callback.hpp> |
|---|
| 23 | #include <boost/test/detail/fwd_decl.hpp> |
|---|
| 24 | #include <boost/test/detail/workaround.hpp> |
|---|
| 25 | #include <boost/test/test_observer.hpp> |
|---|
| 26 | #include <boost/test/framework.hpp> |
|---|
| 27 | |
|---|
| 28 | // Boost |
|---|
| 29 | #include <boost/shared_ptr.hpp> |
|---|
| 30 | |
|---|
| 31 | // STL |
|---|
| 32 | #include <string> // for std::string |
|---|
| 33 | #include <list> // for std::list |
|---|
| 34 | #include <vector> // for std::vector |
|---|
| 35 | |
|---|
| 36 | #include <boost/test/detail/suppress_warnings.hpp> |
|---|
| 37 | |
|---|
| 38 | //____________________________________________________________________________// |
|---|
| 39 | |
|---|
| 40 | namespace boost { |
|---|
| 41 | |
|---|
| 42 | namespace unit_test { |
|---|
| 43 | |
|---|
| 44 | // ************************************************************************** // |
|---|
| 45 | // ************** test_unit ************** // |
|---|
| 46 | // ************************************************************************** // |
|---|
| 47 | |
|---|
| 48 | class BOOST_TEST_DECL test_unit { |
|---|
| 49 | public: |
|---|
| 50 | enum { type = tut_any }; |
|---|
| 51 | |
|---|
| 52 | // Constructor |
|---|
| 53 | test_unit( const_string tu_name, test_unit_type t ); |
|---|
| 54 | |
|---|
| 55 | // dependencies management |
|---|
| 56 | void depends_on( test_unit* tu ); |
|---|
| 57 | bool check_dependencies() const; |
|---|
| 58 | |
|---|
| 59 | // Public r/o properties |
|---|
| 60 | typedef BOOST_READONLY_PROPERTY(test_unit_id,(framework_impl)) id_t; |
|---|
| 61 | typedef BOOST_READONLY_PROPERTY(test_unit_id,(test_suite)) parent_id_t; |
|---|
| 62 | readonly_property<test_unit_type> p_type; // type for this test unit |
|---|
| 63 | readonly_property<const_string> p_type_name; // "case"/"suite" |
|---|
| 64 | id_t p_id; // unique id for this test unit |
|---|
| 65 | parent_id_t p_parent_id; // parent test suite id |
|---|
| 66 | |
|---|
| 67 | // Public r/w properties |
|---|
| 68 | readwrite_property<std::string> p_name; // name for this test unit |
|---|
| 69 | readwrite_property<unsigned> p_timeout; // timeout for the test unit execution |
|---|
| 70 | readwrite_property<counter_t> p_expected_failures; // number of expected failured in this test unit |
|---|
| 71 | |
|---|
| 72 | private: |
|---|
| 73 | // Data members |
|---|
| 74 | std::list<test_unit_id> m_dependencies; |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | // ************************************************************************** // |
|---|
| 78 | // ************** test_case_generator ************** // |
|---|
| 79 | // ************************************************************************** // |
|---|
| 80 | |
|---|
| 81 | class BOOST_TEST_DECL test_unit_generator { |
|---|
| 82 | public: |
|---|
| 83 | virtual test_unit* next() const = 0; |
|---|
| 84 | |
|---|
| 85 | protected: |
|---|
| 86 | BOOST_TEST_PROTECTED_VIRTUAL ~test_unit_generator() {} |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | // ************************************************************************** // |
|---|
| 90 | // ************** test_case ************** // |
|---|
| 91 | // ************************************************************************** // |
|---|
| 92 | |
|---|
| 93 | class BOOST_TEST_DECL test_case : public test_unit { |
|---|
| 94 | public: |
|---|
| 95 | enum { type = tut_case }; |
|---|
| 96 | |
|---|
| 97 | // Constructor |
|---|
| 98 | test_case( const_string tc_name, callback0<> const& test_func ); |
|---|
| 99 | |
|---|
| 100 | // Access methods |
|---|
| 101 | callback0<> const& test_func() const { return m_test_func; } |
|---|
| 102 | |
|---|
| 103 | private: |
|---|
| 104 | friend class framework_impl; |
|---|
| 105 | ~test_case() {} |
|---|
| 106 | |
|---|
| 107 | // BOOST_MSVC <= 1200 have problems with callback as property |
|---|
| 108 | // Data members |
|---|
| 109 | callback0<> m_test_func; |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | // ************************************************************************** // |
|---|
| 113 | // ************** test_suite ************** // |
|---|
| 114 | // ************************************************************************** // |
|---|
| 115 | |
|---|
| 116 | class BOOST_TEST_DECL test_suite : public test_unit { |
|---|
| 117 | public: |
|---|
| 118 | enum { type = tut_suite }; |
|---|
| 119 | |
|---|
| 120 | // Constructor |
|---|
| 121 | explicit test_suite( const_string ts_name ); |
|---|
| 122 | |
|---|
| 123 | // test case list management |
|---|
| 124 | void add( test_unit* tu, counter_t expected_failures = 0, unsigned timeout = 0 ); |
|---|
| 125 | void add( test_unit_generator const& gen, unsigned timeout = 0 ); |
|---|
| 126 | |
|---|
| 127 | protected: |
|---|
| 128 | friend BOOST_TEST_DECL void traverse_test_tree( test_suite const&, test_tree_visitor& ); |
|---|
| 129 | friend class framework_impl; |
|---|
| 130 | virtual ~test_suite() {} |
|---|
| 131 | |
|---|
| 132 | private: |
|---|
| 133 | // Data members |
|---|
| 134 | std::vector<test_unit_id> m_members; |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | // ************************************************************************** // |
|---|
| 138 | // ************** master_test_suite ************** // |
|---|
| 139 | // ************************************************************************** // |
|---|
| 140 | |
|---|
| 141 | class BOOST_TEST_DECL master_test_suite_t : public test_suite { |
|---|
| 142 | public: |
|---|
| 143 | master_test_suite_t() : test_suite( "Master Test Suite" ) |
|---|
| 144 | , argc( 0 ) |
|---|
| 145 | , argv( 0 ) |
|---|
| 146 | {} |
|---|
| 147 | |
|---|
| 148 | // Data members |
|---|
| 149 | int argc; |
|---|
| 150 | char** argv; |
|---|
| 151 | }; |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | // ************************************************************************** // |
|---|
| 155 | // ************** test_tree_visitor ************** // |
|---|
| 156 | // ************************************************************************** // |
|---|
| 157 | |
|---|
| 158 | class BOOST_TEST_DECL test_tree_visitor { |
|---|
| 159 | public: |
|---|
| 160 | // test tree visitor interface |
|---|
| 161 | virtual void visit( test_case const& ) {} |
|---|
| 162 | virtual bool test_suite_start( test_suite const& ) { return true; } |
|---|
| 163 | virtual void test_suite_finish( test_suite const& ) {} |
|---|
| 164 | |
|---|
| 165 | protected: |
|---|
| 166 | BOOST_TEST_PROTECTED_VIRTUAL ~test_tree_visitor() {} |
|---|
| 167 | }; |
|---|
| 168 | |
|---|
| 169 | // ************************************************************************** // |
|---|
| 170 | // ************** traverse_test_tree ************** // |
|---|
| 171 | // ************************************************************************** // |
|---|
| 172 | |
|---|
| 173 | BOOST_TEST_DECL void traverse_test_tree( test_case const&, test_tree_visitor& ); |
|---|
| 174 | BOOST_TEST_DECL void traverse_test_tree( test_suite const&, test_tree_visitor& ); |
|---|
| 175 | BOOST_TEST_DECL void traverse_test_tree( test_unit_id id, test_tree_visitor& ); |
|---|
| 176 | |
|---|
| 177 | //____________________________________________________________________________// |
|---|
| 178 | |
|---|
| 179 | inline void |
|---|
| 180 | traverse_test_tree( test_unit const& tu, test_tree_visitor& V ) |
|---|
| 181 | { |
|---|
| 182 | if( tu.p_type == tut_case ) |
|---|
| 183 | traverse_test_tree( static_cast<test_case const&>( tu ), V ); |
|---|
| 184 | else |
|---|
| 185 | traverse_test_tree( static_cast<test_suite const&>( tu ), V ); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | //____________________________________________________________________________// |
|---|
| 189 | |
|---|
| 190 | // ************************************************************************** // |
|---|
| 191 | // ************** test_case_counter ************** // |
|---|
| 192 | // ************************************************************************** // |
|---|
| 193 | |
|---|
| 194 | struct test_case_counter : test_tree_visitor { |
|---|
| 195 | test_case_counter() : m_count( 0 ) {} |
|---|
| 196 | |
|---|
| 197 | void visit( test_case const& ) { m_count++; } |
|---|
| 198 | |
|---|
| 199 | counter_t m_count; |
|---|
| 200 | }; |
|---|
| 201 | |
|---|
| 202 | // ************************************************************************** // |
|---|
| 203 | // ************** test_being_aborted ************** // |
|---|
| 204 | // ************************************************************************** // |
|---|
| 205 | |
|---|
| 206 | struct BOOST_TEST_DECL test_being_aborted {}; |
|---|
| 207 | |
|---|
| 208 | // ************************************************************************** // |
|---|
| 209 | // ************** object generators ************** // |
|---|
| 210 | // ************************************************************************** // |
|---|
| 211 | |
|---|
| 212 | namespace ut_detail { |
|---|
| 213 | |
|---|
| 214 | BOOST_TEST_DECL std::string normalize_test_case_name( const_string tu_name ); |
|---|
| 215 | |
|---|
| 216 | template<typename UserTestCase> |
|---|
| 217 | struct user_tc_method_invoker { |
|---|
| 218 | typedef void (UserTestCase::*test_method )(); |
|---|
| 219 | |
|---|
| 220 | user_tc_method_invoker( shared_ptr<UserTestCase> inst, test_method test_method ) |
|---|
| 221 | : m_inst( inst ), m_test_method( test_method ) {} |
|---|
| 222 | |
|---|
| 223 | void operator()() { ((*m_inst).*m_test_method)(); } |
|---|
| 224 | |
|---|
| 225 | shared_ptr<UserTestCase> m_inst; |
|---|
| 226 | test_method m_test_method; |
|---|
| 227 | }; |
|---|
| 228 | |
|---|
| 229 | } // namespace ut_detail |
|---|
| 230 | |
|---|
| 231 | //____________________________________________________________________________// |
|---|
| 232 | |
|---|
| 233 | inline test_case* |
|---|
| 234 | make_test_case( callback0<> const& test_func, const_string tc_name ) |
|---|
| 235 | { |
|---|
| 236 | return new test_case( ut_detail::normalize_test_case_name( tc_name ), test_func ); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | //____________________________________________________________________________// |
|---|
| 240 | |
|---|
| 241 | template<typename UserTestCase> |
|---|
| 242 | inline test_case* |
|---|
| 243 | make_test_case( void (UserTestCase::*test_method )(), |
|---|
| 244 | const_string tc_name, |
|---|
| 245 | boost::shared_ptr<UserTestCase> const& user_test_case ) |
|---|
| 246 | { |
|---|
| 247 | return new test_case( ut_detail::normalize_test_case_name( tc_name ), |
|---|
| 248 | ut_detail::user_tc_method_invoker<UserTestCase>( user_test_case, test_method ) ); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | //____________________________________________________________________________// |
|---|
| 252 | |
|---|
| 253 | // ************************************************************************** // |
|---|
| 254 | // ************** auto_test_unit_registrar ************** // |
|---|
| 255 | // ************************************************************************** // |
|---|
| 256 | |
|---|
| 257 | namespace ut_detail { |
|---|
| 258 | |
|---|
| 259 | struct BOOST_TEST_DECL auto_test_unit_registrar |
|---|
| 260 | { |
|---|
| 261 | // Constructor |
|---|
| 262 | explicit auto_test_unit_registrar( test_case* tc, counter_t exp_fail ) |
|---|
| 263 | { |
|---|
| 264 | curr_ts_store().back()->add( tc, exp_fail ); |
|---|
| 265 | } |
|---|
| 266 | explicit auto_test_unit_registrar( test_suite* ts ) |
|---|
| 267 | { |
|---|
| 268 | curr_ts_store().back()->add( ts ); |
|---|
| 269 | |
|---|
| 270 | curr_ts_store().push_back( ts ); |
|---|
| 271 | } |
|---|
| 272 | explicit auto_test_unit_registrar( test_unit_generator const& tc_gen ) |
|---|
| 273 | { |
|---|
| 274 | curr_ts_store().back()->add( tc_gen ); |
|---|
| 275 | } |
|---|
| 276 | explicit auto_test_unit_registrar( int ) |
|---|
| 277 | { |
|---|
| 278 | if( curr_ts_store().size() > 1 ) |
|---|
| 279 | curr_ts_store().pop_back(); |
|---|
| 280 | // else report error |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | private: |
|---|
| 284 | static std::list<test_suite*>& curr_ts_store() |
|---|
| 285 | { |
|---|
| 286 | static std::list<test_suite*> inst( 1, &framework::master_test_suite() ); |
|---|
| 287 | return inst; |
|---|
| 288 | } |
|---|
| 289 | }; |
|---|
| 290 | |
|---|
| 291 | //____________________________________________________________________________// |
|---|
| 292 | |
|---|
| 293 | template<typename T> |
|---|
| 294 | struct auto_tc_exp_fail { |
|---|
| 295 | enum { value = 0 }; |
|---|
| 296 | }; |
|---|
| 297 | |
|---|
| 298 | //____________________________________________________________________________// |
|---|
| 299 | |
|---|
| 300 | } // namespace ut_detail |
|---|
| 301 | |
|---|
| 302 | // ************************************************************************** // |
|---|
| 303 | // ************** global_fixture ************** // |
|---|
| 304 | // ************************************************************************** // |
|---|
| 305 | |
|---|
| 306 | class BOOST_TEST_DECL global_fixture : public test_observer { |
|---|
| 307 | public: |
|---|
| 308 | // Constructor |
|---|
| 309 | global_fixture() { framework::register_observer( *this ); } |
|---|
| 310 | }; |
|---|
| 311 | |
|---|
| 312 | //____________________________________________________________________________// |
|---|
| 313 | |
|---|
| 314 | namespace ut_detail { |
|---|
| 315 | |
|---|
| 316 | template<typename F> |
|---|
| 317 | struct global_fixture_impl : public global_fixture { |
|---|
| 318 | // Constructor |
|---|
| 319 | global_fixture_impl(): m_fixure( 0 ) {} |
|---|
| 320 | |
|---|
| 321 | // test observer interface |
|---|
| 322 | virtual void test_start( counter_t ) { m_fixure = new F; } |
|---|
| 323 | virtual void test_finish() { delete m_fixure; m_fixure = 0; } |
|---|
| 324 | virtual void test_aborted() { delete m_fixure; m_fixure = 0; } |
|---|
| 325 | |
|---|
| 326 | private: |
|---|
| 327 | // Data members |
|---|
| 328 | F* m_fixure; |
|---|
| 329 | }; |
|---|
| 330 | |
|---|
| 331 | } // namespace ut_detail |
|---|
| 332 | |
|---|
| 333 | } // unit_test |
|---|
| 334 | |
|---|
| 335 | } // namespace boost |
|---|
| 336 | |
|---|
| 337 | #include <boost/test/detail/enable_warnings.hpp> |
|---|
| 338 | |
|---|
| 339 | // *************************************************************************** |
|---|
| 340 | // Revision History : |
|---|
| 341 | // |
|---|
| 342 | // $Log: unit_test_suite_impl.hpp,v $ |
|---|
| 343 | // Revision 1.2 2006/01/28 07:09:34 rogeeff |
|---|
| 344 | // tm->test_method |
|---|
| 345 | // |
|---|
| 346 | // Revision 1.1 2005/12/14 05:24:55 rogeeff |
|---|
| 347 | // dll support introduced |
|---|
| 348 | // split into 2 files |
|---|
| 349 | // |
|---|
| 350 | // *************************************************************************** |
|---|
| 351 | |
|---|
| 352 | #endif // BOOST_TEST_UNIT_TEST_SUITE_IMPL_HPP_071894GER |
|---|
| 353 | |
|---|