| 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: auto_unit_test.hpp,v $ |
|---|
| 9 | // |
|---|
| 10 | // Version : $Revision: 1.17 $ |
|---|
| 11 | // |
|---|
| 12 | // Description : support for automated test cases registration mechanism |
|---|
| 13 | // for simple function based test cases |
|---|
| 14 | // *************************************************************************** |
|---|
| 15 | |
|---|
| 16 | #ifndef BOOST_TEST_AUTO_UNIT_TEST_HPP_071894GER |
|---|
| 17 | #define BOOST_TEST_AUTO_UNIT_TEST_HPP_071894GER |
|---|
| 18 | |
|---|
| 19 | // Boost.Test |
|---|
| 20 | #include <boost/test/unit_test.hpp> |
|---|
| 21 | |
|---|
| 22 | #include <boost/test/detail/suppress_warnings.hpp> |
|---|
| 23 | |
|---|
| 24 | // STL |
|---|
| 25 | #include <list> |
|---|
| 26 | |
|---|
| 27 | //____________________________________________________________________________// |
|---|
| 28 | |
|---|
| 29 | // ************************************************************************** // |
|---|
| 30 | // ************** auto_test_unit_registrar ************** // |
|---|
| 31 | // ************************************************************************** // |
|---|
| 32 | |
|---|
| 33 | namespace boost { |
|---|
| 34 | namespace unit_test { |
|---|
| 35 | |
|---|
| 36 | struct auto_unit_test_suite_t : test_suite { |
|---|
| 37 | auto_unit_test_suite_t() |
|---|
| 38 | : test_suite( "Master Test Suite" ) |
|---|
| 39 | , argc( 0 ) |
|---|
| 40 | , argv( 0 ) |
|---|
| 41 | {} |
|---|
| 42 | |
|---|
| 43 | // Data members |
|---|
| 44 | int argc; |
|---|
| 45 | char** argv; |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | //____________________________________________________________________________// |
|---|
| 49 | |
|---|
| 50 | inline auto_unit_test_suite_t* |
|---|
| 51 | auto_unit_test_suite() |
|---|
| 52 | { |
|---|
| 53 | static auto_unit_test_suite_t* inst = new auto_unit_test_suite_t; |
|---|
| 54 | |
|---|
| 55 | return inst; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | //____________________________________________________________________________// |
|---|
| 59 | |
|---|
| 60 | namespace ut_detail { |
|---|
| 61 | |
|---|
| 62 | struct auto_test_unit_registrar |
|---|
| 63 | { |
|---|
| 64 | // Constructor |
|---|
| 65 | explicit auto_test_unit_registrar( test_case* tc, counter_t exp_fail ) |
|---|
| 66 | { |
|---|
| 67 | curr_ts_store().back()->add( tc, exp_fail ); |
|---|
| 68 | } |
|---|
| 69 | explicit auto_test_unit_registrar( test_suite* ts ) |
|---|
| 70 | { |
|---|
| 71 | curr_ts_store().back()->add( ts ); |
|---|
| 72 | |
|---|
| 73 | curr_ts_store().push_back( ts ); |
|---|
| 74 | } |
|---|
| 75 | explicit auto_test_unit_registrar( test_unit_generator const& tc_gen ) |
|---|
| 76 | { |
|---|
| 77 | curr_ts_store().back()->add( tc_gen ); |
|---|
| 78 | } |
|---|
| 79 | explicit auto_test_unit_registrar( int ) |
|---|
| 80 | { |
|---|
| 81 | if( curr_ts_store().size() > 1 ) |
|---|
| 82 | curr_ts_store().pop_back(); |
|---|
| 83 | // else report error |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | private: |
|---|
| 87 | static std::list<test_suite*>& curr_ts_store() |
|---|
| 88 | { |
|---|
| 89 | static std::list<test_suite*> inst( 1, auto_unit_test_suite() ); |
|---|
| 90 | return inst; |
|---|
| 91 | } |
|---|
| 92 | }; |
|---|
| 93 | |
|---|
| 94 | //____________________________________________________________________________// |
|---|
| 95 | |
|---|
| 96 | template<typename T> |
|---|
| 97 | struct auto_tc_exp_fail { |
|---|
| 98 | enum { value = 0 }; |
|---|
| 99 | }; |
|---|
| 100 | |
|---|
| 101 | } // namespace ut_detail |
|---|
| 102 | |
|---|
| 103 | } // namespace unit_test |
|---|
| 104 | } // namespace boost |
|---|
| 105 | |
|---|
| 106 | #define BOOST_AUTO_TC_REGISTRAR( test_name ) \ |
|---|
| 107 | static boost::unit_test::ut_detail::auto_test_unit_registrar BOOST_JOIN( test_name, _registrar ) |
|---|
| 108 | #define BOOST_AUTO_TC_INVOKER( test_name ) BOOST_JOIN( test_name, _invoker ) |
|---|
| 109 | #define BOOST_AUTO_TC_UNIQUE_ID( test_name ) BOOST_JOIN( test_name, _id ) |
|---|
| 110 | |
|---|
| 111 | // ************************************************************************** // |
|---|
| 112 | // ************** BOOST_AUTO_TEST_SUITE ************** // |
|---|
| 113 | // ************************************************************************** // |
|---|
| 114 | |
|---|
| 115 | #define BOOST_AUTO_TEST_SUITE( suite_name ) \ |
|---|
| 116 | BOOST_AUTO_TC_REGISTRAR( suite_name )( BOOST_TEST_SUITE( \ |
|---|
| 117 | BOOST_STRINGIZE( suite_name ) ) ) \ |
|---|
| 118 | /**/ |
|---|
| 119 | |
|---|
| 120 | // ************************************************************************** // |
|---|
| 121 | // ************** BOOST_AUTO_TEST_SUITE_END ************** // |
|---|
| 122 | // ************************************************************************** // |
|---|
| 123 | |
|---|
| 124 | #define BOOST_AUTO_TEST_SUITE_END() \ |
|---|
| 125 | BOOST_AUTO_TC_REGISTRAR( BOOST_JOIN( end_suite, __LINE__ ) )( 1 ) \ |
|---|
| 126 | /**/ |
|---|
| 127 | |
|---|
| 128 | // ************************************************************************** // |
|---|
| 129 | // ************** BOOST_AUTO_TEST_CASE ************** // |
|---|
| 130 | // ************************************************************************** // |
|---|
| 131 | |
|---|
| 132 | #define BOOST_AUTO_TEST_CASE( test_name ) \ |
|---|
| 133 | struct BOOST_AUTO_TC_UNIQUE_ID( test_name ) {}; \ |
|---|
| 134 | \ |
|---|
| 135 | static void test_name(); \ |
|---|
| 136 | BOOST_AUTO_TC_REGISTRAR( test_name )( BOOST_TEST_CASE( test_name ), \ |
|---|
| 137 | boost::unit_test::ut_detail::auto_tc_exp_fail< \ |
|---|
| 138 | BOOST_AUTO_TC_UNIQUE_ID( test_name )>::value ); \ |
|---|
| 139 | static void test_name() \ |
|---|
| 140 | /**/ |
|---|
| 141 | |
|---|
| 142 | // ************************************************************************** // |
|---|
| 143 | // ************** BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES ************** // |
|---|
| 144 | // ************************************************************************** // |
|---|
| 145 | |
|---|
| 146 | #define BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES( test_name, n ) \ |
|---|
| 147 | struct BOOST_AUTO_TC_UNIQUE_ID( test_name ); \ |
|---|
| 148 | namespace boost { namespace unit_test { namespace ut_detail { \ |
|---|
| 149 | \ |
|---|
| 150 | template<> \ |
|---|
| 151 | struct auto_tc_exp_fail<BOOST_AUTO_TC_UNIQUE_ID( test_name ) > { \ |
|---|
| 152 | enum { value = n }; \ |
|---|
| 153 | }; \ |
|---|
| 154 | \ |
|---|
| 155 | }}} \ |
|---|
| 156 | /**/ |
|---|
| 157 | |
|---|
| 158 | // ************************************************************************** // |
|---|
| 159 | // ************** BOOST_FIXTURE_TEST_CASE ************** // |
|---|
| 160 | // ************************************************************************** // |
|---|
| 161 | |
|---|
| 162 | #define BOOST_FIXTURE_TEST_CASE( test_name, F ) \ |
|---|
| 163 | struct test_name : public F { void test_method(); }; \ |
|---|
| 164 | \ |
|---|
| 165 | void BOOST_AUTO_TC_INVOKER( test_name )() \ |
|---|
| 166 | { \ |
|---|
| 167 | test_name t; \ |
|---|
| 168 | t.test_method(); \ |
|---|
| 169 | } \ |
|---|
| 170 | \ |
|---|
| 171 | struct BOOST_AUTO_TC_UNIQUE_ID( test_name ) {}; \ |
|---|
| 172 | \ |
|---|
| 173 | BOOST_AUTO_TC_REGISTRAR( test_name )( \ |
|---|
| 174 | boost::unit_test::make_test_case( \ |
|---|
| 175 | &BOOST_AUTO_TC_INVOKER( test_name ), #test_name ), \ |
|---|
| 176 | boost::unit_test::ut_detail::auto_tc_exp_fail< \ |
|---|
| 177 | BOOST_AUTO_TC_UNIQUE_ID( test_name )>::value ); \ |
|---|
| 178 | \ |
|---|
| 179 | void test_name::test_method() \ |
|---|
| 180 | /**/ |
|---|
| 181 | |
|---|
| 182 | // ************************************************************************** // |
|---|
| 183 | // ************** BOOST_AUTO_TEST_CASE_TEMPLATE ************** // |
|---|
| 184 | // ************************************************************************** // |
|---|
| 185 | |
|---|
| 186 | #define BOOST_AUTO_TEST_CASE_TEMPLATE( test_name, type_name, TL ) \ |
|---|
| 187 | template<typename type_name> \ |
|---|
| 188 | void test_name( boost::type<type_name>* ); \ |
|---|
| 189 | \ |
|---|
| 190 | struct BOOST_AUTO_TC_INVOKER( test_name ) { \ |
|---|
| 191 | template<typename TestType> \ |
|---|
| 192 | static void run( boost::type<TestType>* frwrd = 0 ) \ |
|---|
| 193 | { \ |
|---|
| 194 | test_name( frwrd ); \ |
|---|
| 195 | } \ |
|---|
| 196 | }; \ |
|---|
| 197 | \ |
|---|
| 198 | BOOST_AUTO_TC_REGISTRAR( test_nase )( \ |
|---|
| 199 | boost::unit_test::ut_detail::template_test_case_gen< \ |
|---|
| 200 | BOOST_AUTO_TC_INVOKER( test_name ),TL >( \ |
|---|
| 201 | BOOST_STRINGIZE( test_name ) ) ); \ |
|---|
| 202 | \ |
|---|
| 203 | template<typename type_name> \ |
|---|
| 204 | void test_name( boost::type<type_name>* ) \ |
|---|
| 205 | /**/ |
|---|
| 206 | |
|---|
| 207 | // ************************************************************************** // |
|---|
| 208 | // ************** BOOST_AUTO_TEST_MAIN ************** // |
|---|
| 209 | // ************************************************************************** // |
|---|
| 210 | |
|---|
| 211 | #ifdef BOOST_AUTO_TEST_MAIN |
|---|
| 212 | boost::unit_test::test_suite* |
|---|
| 213 | init_unit_test_suite( int argc, char* argv[] ) { |
|---|
| 214 | boost::unit_test::auto_unit_test_suite_t* master_test_suite = boost::unit_test::auto_unit_test_suite(); |
|---|
| 215 | |
|---|
| 216 | boost::unit_test::const_string new_name = boost::unit_test::const_string( BOOST_AUTO_TEST_MAIN ); |
|---|
| 217 | |
|---|
| 218 | if( !new_name.is_empty() ) |
|---|
| 219 | boost::unit_test::assign_op( master_test_suite->p_name.value, new_name, 0 ); |
|---|
| 220 | |
|---|
| 221 | master_test_suite->argc = argc; |
|---|
| 222 | master_test_suite->argv = argv; |
|---|
| 223 | |
|---|
| 224 | return master_test_suite; |
|---|
| 225 | } |
|---|
| 226 | #endif |
|---|
| 227 | |
|---|
| 228 | //____________________________________________________________________________// |
|---|
| 229 | |
|---|
| 230 | // deprecated |
|---|
| 231 | #define BOOST_AUTO_UNIT_TEST( f ) BOOST_AUTO_TEST_CASE( f ) |
|---|
| 232 | |
|---|
| 233 | //____________________________________________________________________________// |
|---|
| 234 | |
|---|
| 235 | #include <boost/test/detail/enable_warnings.hpp> |
|---|
| 236 | |
|---|
| 237 | // *************************************************************************** |
|---|
| 238 | // Revision History : |
|---|
| 239 | // |
|---|
| 240 | // $Log: auto_unit_test.hpp,v $ |
|---|
| 241 | // Revision 1.17 2005/05/08 08:55:00 rogeeff |
|---|
| 242 | // typos and missing descriptions fixed |
|---|
| 243 | // |
|---|
| 244 | // Revision 1.16 2005/05/03 03:38:35 rogeeff |
|---|
| 245 | // bug in fixture test cases fixed |
|---|
| 246 | // |
|---|
| 247 | // Revision 1.15 2005/04/18 04:54:36 rogeeff |
|---|
| 248 | // Major rework in auto unit test facilities\n1. auto test suite ability introduced\n2.fixtures abilities introduced\n3. Expected failures support\n4. Master test suite renaming support |
|---|
| 249 | // |
|---|
| 250 | // Revision 1.14 2005/03/22 06:56:13 rogeeff |
|---|
| 251 | // provided access to argc/argv in auto facilities |
|---|
| 252 | // |
|---|
| 253 | // Revision 1.13 2005/02/20 08:27:05 rogeeff |
|---|
| 254 | // This a major update for Boost.Test framework. See release docs for complete list of fixes/updates |
|---|
| 255 | // |
|---|
| 256 | // Revision 1.12 2005/02/01 06:40:06 rogeeff |
|---|
| 257 | // copyright update |
|---|
| 258 | // old log entries removed |
|---|
| 259 | // minor stilistic changes |
|---|
| 260 | // depricated tools removed |
|---|
| 261 | // |
|---|
| 262 | // *************************************************************************** |
|---|
| 263 | |
|---|
| 264 | #endif // BOOST_TEST_AUTO_UNIT_TEST_HPP_071894GER |
|---|