unit_test_suite.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // (C) Copyright Gennadiy Rozental 2001.
  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. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. /// @file
  8. /// @brief Defines Unit Test Framework public API
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_UNIT_TEST_SUITE_HPP_071894GER
  11. #define BOOST_TEST_UNIT_TEST_SUITE_HPP_071894GER
  12. // Boost.Test
  13. #include <boost/test/framework.hpp>
  14. #include <boost/test/tree/auto_registration.hpp>
  15. #include <boost/test/tree/test_case_template.hpp>
  16. #include <boost/test/tree/global_fixture.hpp>
  17. #include <boost/test/detail/suppress_warnings.hpp>
  18. #include <boost/test/detail/pp_variadic.hpp>
  19. //____________________________________________________________________________//
  20. // ************************************************************************** //
  21. // ************** Non-auto (explicit) test case interface ************** //
  22. // ************************************************************************** //
  23. #define BOOST_TEST_CASE( test_function ) \
  24. boost::unit_test::make_test_case( boost::function<void ()>(test_function), \
  25. BOOST_TEST_STRINGIZE( test_function ), \
  26. __FILE__, __LINE__ )
  27. #define BOOST_CLASS_TEST_CASE( test_function, tc_instance ) \
  28. boost::unit_test::make_test_case( (test_function), \
  29. BOOST_TEST_STRINGIZE( test_function ), \
  30. __FILE__, __LINE__, tc_instance )
  31. // ************************************************************************** //
  32. // ************** BOOST_TEST_SUITE ************** //
  33. // ************************************************************************** //
  34. #define BOOST_TEST_SUITE( testsuite_name ) \
  35. ( new boost::unit_test::test_suite( testsuite_name, __FILE__, __LINE__ ) )
  36. // ************************************************************************** //
  37. // ************** BOOST_AUTO_TEST_SUITE ************** //
  38. // ************************************************************************** //
  39. #define BOOST_AUTO_TEST_SUITE_WITH_DECOR( suite_name, decorators ) \
  40. namespace suite_name { \
  41. BOOST_AUTO_TU_REGISTRAR( suite_name )( \
  42. BOOST_STRINGIZE( suite_name ), \
  43. __FILE__, __LINE__, \
  44. decorators ); \
  45. /**/
  46. #define BOOST_AUTO_TEST_SUITE_NO_DECOR( suite_name ) \
  47. BOOST_AUTO_TEST_SUITE_WITH_DECOR( \
  48. suite_name, \
  49. boost::unit_test::decorator::collector::instance() ) \
  50. /**/
  51. #if BOOST_PP_VARIADICS
  52. #define BOOST_AUTO_TEST_SUITE( ... ) \
  53. BOOST_TEST_INVOKE_IF_N_ARGS( 1, \
  54. BOOST_AUTO_TEST_SUITE_NO_DECOR, \
  55. BOOST_AUTO_TEST_SUITE_WITH_DECOR, \
  56. __VA_ARGS__) \
  57. /**/
  58. #else /* BOOST_PP_VARIADICS */
  59. #define BOOST_AUTO_TEST_SUITE( suite_name ) \
  60. BOOST_AUTO_TEST_SUITE_NO_DECOR( suite_name ) \
  61. /**/
  62. #endif /* BOOST_PP_VARIADICS */
  63. // ************************************************************************** //
  64. // ************** BOOST_FIXTURE_TEST_SUITE ************** //
  65. // ************************************************************************** //
  66. #define BOOST_FIXTURE_TEST_SUITE_WITH_DECOR(suite_name, F, decorators) \
  67. BOOST_AUTO_TEST_SUITE_WITH_DECOR( suite_name, decorators ) \
  68. typedef F BOOST_AUTO_TEST_CASE_FIXTURE; \
  69. /**/
  70. #define BOOST_FIXTURE_TEST_SUITE_NO_DECOR( suite_name, F ) \
  71. BOOST_AUTO_TEST_SUITE_NO_DECOR( suite_name ) \
  72. typedef F BOOST_AUTO_TEST_CASE_FIXTURE; \
  73. /**/
  74. #if BOOST_PP_VARIADICS
  75. #define BOOST_FIXTURE_TEST_SUITE( ... ) \
  76. BOOST_TEST_INVOKE_IF_N_ARGS( 2, \
  77. BOOST_FIXTURE_TEST_SUITE_NO_DECOR, \
  78. BOOST_FIXTURE_TEST_SUITE_WITH_DECOR, \
  79. __VA_ARGS__) \
  80. /**/
  81. #else /* BOOST_PP_VARIADICS */
  82. #define BOOST_FIXTURE_TEST_SUITE( suite_name, F ) \
  83. BOOST_FIXTURE_TEST_SUITE_NO_DECOR( suite_name, F ) \
  84. /**/
  85. #endif /* BOOST_PP_VARIADICS */
  86. // ************************************************************************** //
  87. // ************** BOOST_AUTO_TEST_SUITE_END ************** //
  88. // ************************************************************************** //
  89. #define BOOST_AUTO_TEST_SUITE_END() \
  90. BOOST_AUTO_TU_REGISTRAR( end_suite )( 1 ); \
  91. } \
  92. /**/
  93. // ************************************************************************** //
  94. // ************** BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES ************** //
  95. // ************************************************************************** //
  96. /// @deprecated use decorator instead
  97. #define BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES( test_name, n ) \
  98. BOOST_TEST_DECORATOR( * boost::unit_test::expected_failures( n ) ) \
  99. /**/
  100. // ************************************************************************** //
  101. // ************** BOOST_FIXTURE_TEST_CASE ************** //
  102. // ************************************************************************** //
  103. #define BOOST_FIXTURE_TEST_CASE_WITH_DECOR( test_name, F, decorators ) \
  104. struct test_name : public F { void test_method(); }; \
  105. \
  106. static void BOOST_AUTO_TC_INVOKER( test_name )() \
  107. { \
  108. BOOST_TEST_CHECKPOINT('"' << #test_name << "\" fixture ctor"); \
  109. test_name t; \
  110. BOOST_TEST_CHECKPOINT('"' << #test_name << "\" fixture setup"); \
  111. boost::unit_test::setup_conditional(t); \
  112. BOOST_TEST_CHECKPOINT('"' << #test_name << "\" test entry"); \
  113. t.test_method(); \
  114. BOOST_TEST_CHECKPOINT('"' << #test_name << "\" fixture teardown"); \
  115. boost::unit_test::teardown_conditional(t); \
  116. BOOST_TEST_CHECKPOINT('"' << #test_name << "\" fixture dtor"); \
  117. } \
  118. \
  119. struct BOOST_AUTO_TC_UNIQUE_ID( test_name ) {}; \
  120. \
  121. BOOST_AUTO_TU_REGISTRAR( test_name )( \
  122. boost::unit_test::make_test_case( \
  123. &BOOST_AUTO_TC_INVOKER( test_name ), \
  124. #test_name, __FILE__, __LINE__ ), \
  125. decorators ); \
  126. \
  127. void test_name::test_method() \
  128. /**/
  129. #define BOOST_FIXTURE_TEST_CASE_NO_DECOR( test_name, F ) \
  130. BOOST_FIXTURE_TEST_CASE_WITH_DECOR( test_name, F, \
  131. boost::unit_test::decorator::collector::instance() ) \
  132. /**/
  133. #if BOOST_PP_VARIADICS
  134. #define BOOST_FIXTURE_TEST_CASE( ... ) \
  135. BOOST_TEST_INVOKE_IF_N_ARGS( 2, \
  136. BOOST_FIXTURE_TEST_CASE_NO_DECOR, \
  137. BOOST_FIXTURE_TEST_CASE_WITH_DECOR, \
  138. __VA_ARGS__) \
  139. /**/
  140. #else /* BOOST_PP_VARIADICS */
  141. #define BOOST_FIXTURE_TEST_CASE( test_name, F ) \
  142. BOOST_FIXTURE_TEST_CASE_NO_DECOR(test_name, F) \
  143. /**/
  144. #endif /* BOOST_PP_VARIADICS */
  145. // ************************************************************************** //
  146. // ************** BOOST_AUTO_TEST_CASE ************** //
  147. // ************************************************************************** //
  148. #define BOOST_AUTO_TEST_CASE_NO_DECOR( test_name ) \
  149. BOOST_FIXTURE_TEST_CASE_NO_DECOR( test_name, \
  150. BOOST_AUTO_TEST_CASE_FIXTURE ) \
  151. /**/
  152. #define BOOST_AUTO_TEST_CASE_WITH_DECOR( test_name, decorators ) \
  153. BOOST_FIXTURE_TEST_CASE_WITH_DECOR( test_name, \
  154. BOOST_AUTO_TEST_CASE_FIXTURE, decorators ) \
  155. /**/
  156. #if BOOST_PP_VARIADICS
  157. #define BOOST_AUTO_TEST_CASE( ... ) \
  158. BOOST_TEST_INVOKE_IF_N_ARGS( 1, \
  159. BOOST_AUTO_TEST_CASE_NO_DECOR, \
  160. BOOST_AUTO_TEST_CASE_WITH_DECOR, \
  161. __VA_ARGS__) \
  162. /**/
  163. #else /* BOOST_PP_VARIADICS */
  164. #define BOOST_AUTO_TEST_CASE( test_name ) \
  165. BOOST_AUTO_TEST_CASE_NO_DECOR( test_name ) \
  166. /**/
  167. #endif /* BOOST_PP_VARIADICS */
  168. // ************************************************************************** //
  169. // ************** BOOST_FIXTURE_TEST_CASE_TEMPLATE ************** //
  170. // ************************************************************************** //
  171. #define BOOST_FIXTURE_TEST_CASE_TEMPLATE( test_name, type_name, TL, F ) \
  172. template<typename type_name> \
  173. struct test_name : public F \
  174. { void test_method(); }; \
  175. \
  176. struct BOOST_AUTO_TC_INVOKER( test_name ) { \
  177. template<typename TestType> \
  178. static void run( boost::type<TestType>* = 0 ) \
  179. { \
  180. BOOST_TEST_CHECKPOINT('"' << #test_name <<"\" fixture entry."); \
  181. test_name<TestType> t; boost::unit_test::setup_conditional(t); \
  182. BOOST_TEST_CHECKPOINT('"' << #test_name << "\" entry."); \
  183. t.test_method(); \
  184. BOOST_TEST_CHECKPOINT('"' << #test_name << "\" exit."); \
  185. boost::unit_test::teardown_conditional(t); \
  186. } \
  187. }; \
  188. \
  189. BOOST_AUTO_TU_REGISTRAR( test_name )( \
  190. boost::unit_test::ut_detail::template_test_case_gen< \
  191. BOOST_AUTO_TC_INVOKER( test_name ),TL >( \
  192. BOOST_STRINGIZE( test_name ), __FILE__, __LINE__ ), \
  193. boost::unit_test::decorator::collector::instance() ); \
  194. \
  195. template<typename type_name> \
  196. void test_name<type_name>::test_method() \
  197. /**/
  198. // ************************************************************************** //
  199. // ************** BOOST_AUTO_TEST_CASE_TEMPLATE ************** //
  200. // ************************************************************************** //
  201. #define BOOST_AUTO_TEST_CASE_TEMPLATE( test_name, type_name, TL ) \
  202. BOOST_FIXTURE_TEST_CASE_TEMPLATE( test_name, type_name, TL, \
  203. BOOST_AUTO_TEST_CASE_FIXTURE ) \
  204. /**/
  205. // ************************************************************************** //
  206. // ************** BOOST_TEST_CASE_TEMPLATE ************** //
  207. // ************************************************************************** //
  208. #define BOOST_TEST_CASE_TEMPLATE( name, typelist ) \
  209. boost::unit_test::ut_detail::template_test_case_gen<name,typelist>( \
  210. BOOST_TEST_STRINGIZE( name ), __FILE__, __LINE__ ) \
  211. /**/
  212. // ************************************************************************** //
  213. // ************** BOOST_TEST_CASE_TEMPLATE_FUNCTION ************** //
  214. // ************************************************************************** //
  215. #define BOOST_TEST_CASE_TEMPLATE_FUNCTION( name, type_name ) \
  216. template<typename type_name> \
  217. void BOOST_JOIN( name, _impl )( boost::type<type_name>* ); \
  218. \
  219. struct name { \
  220. template<typename TestType> \
  221. static void run( boost::type<TestType>* frwrd = 0 ) \
  222. { \
  223. BOOST_JOIN( name, _impl )( frwrd ); \
  224. } \
  225. }; \
  226. \
  227. template<typename type_name> \
  228. void BOOST_JOIN( name, _impl )( boost::type<type_name>* ) \
  229. /**/
  230. // ************************************************************************** //
  231. // ************** BOOST_GLOBAL_FIXTURE ************** //
  232. // ************************************************************************** //
  233. #define BOOST_GLOBAL_FIXTURE( F ) \
  234. static boost::unit_test::ut_detail::global_configuration_impl<F> BOOST_JOIN( gf_, F ) \
  235. /**/
  236. // ************************************************************************** //
  237. // ************** BOOST_TEST_GLOBAL_CONFIGURATION ************** //
  238. // ************************************************************************** //
  239. #define BOOST_TEST_GLOBAL_CONFIGURATION( F ) \
  240. static boost::unit_test::ut_detail::global_configuration_impl<F> BOOST_JOIN( gf_, F ) \
  241. /**/
  242. // ************************************************************************** //
  243. // ************** BOOST_TEST_GLOBAL_FIXTURE ************** //
  244. // ************************************************************************** //
  245. #define BOOST_TEST_GLOBAL_FIXTURE( F ) \
  246. static boost::unit_test::ut_detail::global_fixture_impl<F> BOOST_JOIN( gf_, F ) \
  247. /**/
  248. // ************************************************************************** //
  249. // ************** BOOST_TEST_DECORATOR ************** //
  250. // ************************************************************************** //
  251. #define BOOST_TEST_DECORATOR( D ) \
  252. static boost::unit_test::decorator::collector const& \
  253. BOOST_TEST_APPEND_UNIQUE_ID(decorator_collector) = D; \
  254. /**/
  255. // ************************************************************************** //
  256. // ************** BOOST_AUTO_TEST_CASE_FIXTURE ************** //
  257. // ************************************************************************** //
  258. namespace boost { namespace unit_test { namespace ut_detail {
  259. struct nil_t {};
  260. } // namespace ut_detail
  261. } // unit_test
  262. } // namespace boost
  263. // Intentionally is in global namespace, so that FIXTURE_TEST_SUITE can reset it in user code.
  264. typedef ::boost::unit_test::ut_detail::nil_t BOOST_AUTO_TEST_CASE_FIXTURE;
  265. // ************************************************************************** //
  266. // ************** Auto registration facility helper macros ************** //
  267. // ************************************************************************** //
  268. // Facility for having a unique name based on __LINE__ and __COUNTER__ (later if available)
  269. #if defined(__COUNTER__)
  270. #define BOOST_TEST_INTERNAL_HAS_COUNTER
  271. #endif
  272. #if defined(BOOST_TEST_INTERNAL_HAS_COUNTER)
  273. #define BOOST_TEST_APPEND_UNIQUE_ID( name ) \
  274. BOOST_JOIN( BOOST_JOIN( name, __LINE__ ), __COUNTER__)
  275. /**/
  276. #else
  277. #define BOOST_TEST_APPEND_UNIQUE_ID( name ) \
  278. BOOST_JOIN( name, __LINE__ )
  279. /**/
  280. #endif
  281. /**/
  282. #define BOOST_AUTO_TU_REGISTRAR( test_name ) \
  283. static boost::unit_test::ut_detail::auto_test_unit_registrar \
  284. BOOST_TEST_APPEND_UNIQUE_ID( BOOST_JOIN( test_name, _registrar ) ) \
  285. /**/
  286. #define BOOST_AUTO_TC_INVOKER( test_name ) BOOST_JOIN( test_name, _invoker )
  287. #define BOOST_AUTO_TC_UNIQUE_ID( test_name ) BOOST_JOIN( test_name, _id )
  288. // ************************************************************************** //
  289. // ************** BOOST_TEST_MAIN ************** //
  290. // ************************************************************************** //
  291. #if defined(BOOST_TEST_MAIN)
  292. #ifdef BOOST_TEST_ALTERNATIVE_INIT_API
  293. bool init_unit_test() {
  294. #else
  295. ::boost::unit_test::test_suite*
  296. init_unit_test_suite( int, char* [] ) {
  297. #endif
  298. #ifdef BOOST_TEST_MODULE
  299. using namespace ::boost::unit_test;
  300. assign_op( framework::master_test_suite().p_name.value, BOOST_TEST_STRINGIZE( BOOST_TEST_MODULE ).trim( "\"" ), 0 );
  301. #endif
  302. #ifdef BOOST_TEST_ALTERNATIVE_INIT_API
  303. return true;
  304. }
  305. #else
  306. return 0;
  307. }
  308. #endif
  309. #endif
  310. //____________________________________________________________________________//
  311. #include <boost/test/detail/enable_warnings.hpp>
  312. #endif // BOOST_TEST_UNIT_TEST_SUITE_HPP_071894GER