|
|
|||
File indexing completed on 2025-12-15 10:09:21
0001 // (C) Copyright Gennadiy Rozental 2001. 0002 // Distributed under the Boost Software License, Version 1.0. 0003 // (See accompanying file LICENSE_1_0.txt or copy at 0004 // http://www.boost.org/LICENSE_1_0.txt) 0005 0006 // See http://www.boost.org/libs/test for the library home page. 0007 // 0008 //!@file 0009 //!@brief Defines Unit Test Framework mono-state interfaces. 0010 //! The framework interfaces are based on Monostate design pattern. 0011 // *************************************************************************** 0012 0013 #ifndef BOOST_TEST_FRAMEWORK_HPP_020805GER 0014 #define BOOST_TEST_FRAMEWORK_HPP_020805GER 0015 0016 // Boost.Test 0017 #include <boost/test/detail/global_typedef.hpp> 0018 #include <boost/test/detail/fwd_decl.hpp> 0019 #include <boost/test/detail/throw_exception.hpp> 0020 0021 #include <boost/test/detail/suppress_warnings.hpp> 0022 0023 // STL 0024 #include <stdexcept> 0025 0026 //____________________________________________________________________________// 0027 0028 namespace boost { 0029 0030 /// Main namespace for the Unit Test Framework interfaces and implementation 0031 namespace unit_test { 0032 0033 // ************************************************************************** // 0034 // ************** init_unit_test_func ************** // 0035 // ************************************************************************** // 0036 0037 /// Test module initialization routine signature 0038 0039 /// Different depending on whether BOOST_TEST_ALTERNATIVE_INIT_API is defined or not 0040 #ifdef BOOST_TEST_ALTERNATIVE_INIT_API 0041 typedef bool (*init_unit_test_func)(); 0042 #else 0043 typedef test_suite* (*init_unit_test_func)( int, char* [] ); 0044 #endif 0045 0046 // ************************************************************************** // 0047 // ************** framework ************** // 0048 // ************************************************************************** // 0049 0050 /// Namespace of the Unit Test Framework mono-state 0051 namespace framework { 0052 0053 /// @name Unit Test Framework initialization and shutdown 0054 /// @{ 0055 0056 /// @brief This function performs initialization of the framework mono-state. 0057 /// 0058 /// It needs to be called every time before the test is started. 0059 /// @param[in] init_func test module initialization routine 0060 /// @param[in] argc command line arguments collection 0061 /// @param[in] argv command line arguments collection 0062 BOOST_TEST_DECL void init( init_unit_test_func init_func, int argc, char* argv[] ); 0063 0064 /// This function applies all the decorators and figures out default run status. This argument facilitates an 0065 /// ability of the test cases to prepare some other test units (primarily used internally for self testing). 0066 /// @param[in] tu Optional id of the test unit representing root of test tree. If absent, master test suite is used 0067 BOOST_TEST_DECL void finalize_setup_phase( test_unit_id tu = INV_TEST_UNIT_ID); 0068 0069 /// This function returns true when testing is in progress (setup is finished). 0070 BOOST_TEST_DECL bool test_in_progress(); 0071 0072 /// This function shuts down the framework and clears up its mono-state. 0073 /// 0074 /// It needs to be at the very end of test module execution 0075 BOOST_TEST_DECL void shutdown(); 0076 /// @} 0077 0078 /// @name Test unit registration 0079 /// @{ 0080 0081 /// Provides both read and write access to current "leaf" auto test suite during the test unit registration phase. 0082 /// 0083 /// During auto-registration phase the framework maintain a FIFO queue of test units being registered. New test units become children 0084 /// of the current "leaf" test suite and if this is test suite it is pushed back into queue and becomes a new leaf. 0085 /// When test suite registration is completed, a test suite is popped from the back of the queue. Only automatically registered test suites 0086 /// should be added to this queue. Master test suite is always a zero element in this queue, so if no other test suites are registered 0087 /// all test cases are added to master test suite. 0088 0089 /// This function facilitates all three possible actions: 0090 /// - if no argument are provided it returns the current queue leaf test suite 0091 /// - if test suite is provided and no second argument are set, test suite is added to the queue 0092 /// - if no test suite are provided and last argument is false, the semantic of this function is similar to queue pop: last element is popped from the queue 0093 /// @param[in] ts test suite to push back to the queue 0094 /// @param[in] push_or_pop should we push ts to the queue or pop leaf test suite instead 0095 /// @returns a reference to the currently active/"leaf" test suite 0096 BOOST_TEST_DECL test_suite& current_auto_test_suite( test_suite* ts = 0, bool push_or_pop = true ); 0097 0098 /// This function add new test case into the global collection of test units the framework aware of. 0099 0100 /// This function also assignes unique test unit id for every test case. Later on one can use this id to locate 0101 /// the test case if necessary. This is the way for the framework to maintain weak references between test units. 0102 /// @param[in] tc test case to register 0103 BOOST_TEST_DECL void register_test_unit( test_case* tc ); 0104 0105 /// This function add new test suite into the global collection of test units the framework aware of. 0106 0107 /// This function also assignes unique test unit id for every test suite. Later on one can use this id to locate 0108 /// the test case if necessary. This is the way for the framework to maintain weak references between test units. 0109 /// @param[in] ts test suite to register 0110 BOOST_TEST_DECL void register_test_unit( test_suite* ts ); 0111 0112 /// This function removes the test unit from the collection of known test units and destroys the test unit object. 0113 0114 /// This function also assigns unique test unit id for every test case. Later on one can use this id to located 0115 /// the test case if necessary. This is the way for the framework to maintain weak references between test units. 0116 /// @param[in] tu test unit to deregister 0117 BOOST_TEST_DECL void deregister_test_unit( test_unit* tu ); 0118 0119 // This function clears up the framework mono-state. 0120 0121 /// After this call the framework can be reinitialized to perform a second test run during the same program lifetime. 0122 BOOST_TEST_DECL void clear(); 0123 /// @} 0124 0125 /// @name Test observer registration 0126 /// @{ 0127 /// Adds new test execution observer object into the framework's list of test observers. 0128 0129 /// Observer lifetime should exceed the the testing execution timeframe 0130 /// @param[in] to test observer object to add 0131 BOOST_TEST_DECL void register_observer( test_observer& to ); 0132 0133 /// Excludes the observer object form the framework's list of test observers 0134 /// @param[in] to test observer object to exclude 0135 BOOST_TEST_DECL void deregister_observer( test_observer& to ); 0136 0137 /// @} 0138 0139 /// @name Global fixtures registration 0140 /// @{ 0141 0142 /// Adds a new global fixture to be setup before any other tests starts and tore down after 0143 /// any other tests finished. 0144 /// Test unit fixture lifetime should exceed the testing execution timeframe 0145 /// @param[in] tuf fixture to add 0146 BOOST_TEST_DECL void register_global_fixture( global_fixture& tuf ); 0147 0148 /// Removes a test global fixture from the framework 0149 /// 0150 /// Test unit fixture lifetime should exceed the testing execution timeframe 0151 /// @param[in] tuf fixture to remove 0152 BOOST_TEST_DECL void deregister_global_fixture( global_fixture& tuf ); 0153 /// @} 0154 0155 /// @name Assertion/uncaught exception context support 0156 /// @{ 0157 /// Context accessor 0158 struct BOOST_TEST_DECL context_generator { 0159 context_generator() : m_curr_frame( 0 ) {} 0160 0161 /// Is there any context? 0162 bool is_empty() const; 0163 0164 /// Give me next frame; empty - last frame 0165 const_string next() const; 0166 0167 private: 0168 // Data members 0169 mutable unsigned m_curr_frame; 0170 }; 0171 0172 /// Records context frame message. 0173 0174 /// Some context frames are sticky - they can only explicitly cleared by specifying context id. Other (non sticky) context frames cleared after every assertion. 0175 /// @param[in] context_descr context frame message 0176 /// @param[in] sticky is this sticky frame or not 0177 /// @returns id of the newly created frame 0178 BOOST_TEST_DECL int add_context( lazy_ostream const& context_descr, bool sticky ); 0179 /// Erases context frame (when test exits context scope) 0180 0181 /// If context_id is passed clears that specific context frame identified by this id, otherwise clears all non sticky contexts. 0182 BOOST_TEST_DECL void clear_context( int context_id = -1 ); 0183 /// Produces an instance of small "delegate" object, which facilitates access to collected context. 0184 BOOST_TEST_DECL context_generator get_context(); 0185 /// @} 0186 0187 /// @name Access to registered test units. 0188 /// @{ 0189 /// This function provides access to the master test suite. 0190 0191 /// There is only only master test suite per test module. 0192 /// @returns a reference the master test suite instance 0193 BOOST_TEST_DECL master_test_suite_t& master_test_suite(); 0194 0195 /// This function provides an access to the test unit currently being executed. 0196 0197 /// The difference with current_test_case is about the time between a test-suite 0198 /// is being set up or torn down (fixtures) and when the test-cases of that suite start. 0199 0200 /// This function is only valid during test execution phase. 0201 /// @see current_test_case_id, current_test_case 0202 BOOST_TEST_DECL test_unit const& current_test_unit(); 0203 0204 /// This function provides an access to the test case currently being executed. 0205 0206 /// This function is only valid during test execution phase. 0207 /// @see current_test_case_id 0208 BOOST_TEST_DECL test_case const& current_test_case(); 0209 0210 /// This function provides an access to an id of the test case currently being executed. 0211 0212 /// This function safer than current_test_case, cause if wont throw if no test case is being executed. 0213 /// @see current_test_case 0214 BOOST_TEST_DECL test_unit_id current_test_case_id(); /* safe version of above */ 0215 0216 /// This function provides access to a test unit by id and type combination. It will throw if no test unit located. 0217 /// @param[in] tu_id id of a test unit to locate 0218 /// @param[in] tu_type type of a test unit to locate 0219 /// @returns located test unit 0220 BOOST_TEST_DECL test_unit& get( test_unit_id tu_id, test_unit_type tu_type ); 0221 0222 /// This function template provides access to a typed test unit by id 0223 0224 /// It will throw if you specify incorrect test unit type 0225 /// @tparam UnitType compile time type of test unit to get (test_suite or test_case) 0226 /// @param id id of test unit to get 0227 template<typename UnitType> 0228 inline UnitType& get( test_unit_id id ) 0229 { 0230 return static_cast<UnitType&>( get( id, static_cast<test_unit_type>(UnitType::type) ) ); 0231 } 0232 ///@} 0233 0234 /// @name Test initiation interface 0235 /// @{ 0236 0237 /// Initiates test execution 0238 0239 /// This function is used to start the test execution from a specific "root" test unit. 0240 /// If no root provided, test is started from master test suite. This second argument facilitates an ability of the test cases to 0241 /// start some other test units (primarily used internally for self testing). 0242 /// @param[in] tu Optional id of the test unit or test unit itself from which the test is started. If absent, master test suite is used 0243 /// @param[in] continue_test true == continue test if it was already started, false == restart the test from scratch regardless 0244 BOOST_TEST_DECL void run( test_unit_id tu = INV_TEST_UNIT_ID, bool continue_test = true ); 0245 /// Initiates test execution. Same as other overload 0246 BOOST_TEST_DECL void run( test_unit const* tu, bool continue_test = true ); 0247 /// @} 0248 0249 /// @name Test events dispatchers 0250 /// @{ 0251 /// Reports results of assertion to all test observers 0252 BOOST_TEST_DECL void assertion_result( unit_test::assertion_result ar ); 0253 /// Reports uncaught exception to all test observers 0254 BOOST_TEST_DECL void exception_caught( execution_exception const& ); 0255 /// Reports aborted test unit to all test observers 0256 BOOST_TEST_DECL void test_unit_aborted( test_unit const& ); 0257 /// Reports aborted test module to all test observers 0258 BOOST_TEST_DECL void test_aborted( ); 0259 /// @} 0260 0261 namespace impl { 0262 // exclusively for self test 0263 BOOST_TEST_DECL void setup_for_execution( test_unit const& ); 0264 BOOST_TEST_DECL void setup_loggers( ); 0265 0266 // Helper for setting the name of the master test suite globally 0267 struct BOOST_TEST_DECL master_test_suite_name_setter { 0268 master_test_suite_name_setter( const_string name ); 0269 }; 0270 0271 } // namespace impl 0272 0273 // ************************************************************************** // 0274 // ************** framework errors ************** // 0275 // ************************************************************************** // 0276 0277 /// This exception type is used to report internal Boost.Test framework errors. 0278 struct BOOST_TEST_DECL internal_error : public std::runtime_error { 0279 internal_error( const_string m ) : std::runtime_error( std::string( m.begin(), m.size() ) ) {} 0280 }; 0281 0282 //____________________________________________________________________________// 0283 0284 /// This exception type is used to report test module setup errors. 0285 struct BOOST_TEST_DECL setup_error : public std::runtime_error { 0286 setup_error( const_string m ) : std::runtime_error( std::string( m.begin(), m.size() ) ) {} 0287 }; 0288 0289 #define BOOST_TEST_SETUP_ASSERT( cond, msg ) BOOST_TEST_I_ASSRT( cond, unit_test::framework::setup_error( msg ) ) 0290 0291 //____________________________________________________________________________// 0292 0293 struct nothing_to_test { 0294 explicit nothing_to_test( int rc ) : m_result_code( rc ) {} 0295 0296 int m_result_code; 0297 }; 0298 0299 //____________________________________________________________________________// 0300 0301 } // namespace framework 0302 } // unit_test 0303 } // namespace boost 0304 0305 #include <boost/test/detail/enable_warnings.hpp> 0306 0307 #endif // BOOST_TEST_FRAMEWORK_HPP_020805GER
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|