unit_test_suite.hpp 19 KB

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