unit_test_suite.hpp 19 KB

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