test_unit.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. /// Defines @ref boost::unit_test::test_unit "test_unit", @ref boost::unit_test::test_case "test_case",
  9. /// @ref boost::unit_test::test_suite "test_suite" and @ref boost::unit_test::master_test_suite_t "master_test_suite_t"
  10. // ***************************************************************************
  11. #ifndef BOOST_TEST_TREE_TEST_UNIT_HPP_100211GER
  12. #define BOOST_TEST_TREE_TEST_UNIT_HPP_100211GER
  13. // Boost.Test
  14. #include <boost/test/detail/config.hpp>
  15. #include <boost/test/detail/global_typedef.hpp>
  16. #include <boost/test/detail/fwd_decl.hpp>
  17. #include <boost/test/tree/decorator.hpp>
  18. #include <boost/test/tree/fixture.hpp>
  19. #include <boost/test/framework.hpp>
  20. #include <boost/test/tools/assertion_result.hpp>
  21. #include <boost/test/utils/class_properties.hpp>
  22. // Boost
  23. #include <boost/function/function0.hpp>
  24. #include <boost/function/function1.hpp>
  25. // STL
  26. #include <vector>
  27. #include <string>
  28. #include <map>
  29. #include <boost/test/detail/suppress_warnings.hpp>
  30. //____________________________________________________________________________//
  31. namespace boost {
  32. namespace unit_test {
  33. namespace framework {
  34. class state;
  35. }
  36. // ************************************************************************** //
  37. // ************** test_unit ************** //
  38. // ************************************************************************** //
  39. typedef std::vector<test_unit_id> test_unit_id_list;
  40. class BOOST_TEST_DECL test_unit {
  41. public:
  42. enum { type = TUT_ANY };
  43. enum run_status { RS_DISABLED, RS_ENABLED, RS_INHERIT, RS_INVALID };
  44. typedef std::vector<test_unit_id> id_list;
  45. typedef std::vector<test_unit_fixture_ptr> fixture_list_t;
  46. typedef BOOST_READONLY_PROPERTY(test_unit_id,(framework::state)) id_t;
  47. typedef BOOST_READONLY_PROPERTY(test_unit_id,(test_suite)) parent_id_t;
  48. typedef BOOST_READONLY_PROPERTY(id_list,(test_unit)) id_list_t;
  49. typedef std::vector<decorator::base_ptr> decor_list_t;
  50. typedef BOOST_READONLY_PROPERTY(std::vector<std::string>,(test_unit)) label_list_t;
  51. typedef boost::function<test_tools::assertion_result (test_unit_id)> precondition_t;
  52. typedef BOOST_READONLY_PROPERTY(std::vector<precondition_t>,(test_unit)) precond_list_t;
  53. // preconditions management
  54. void depends_on( test_unit* tu );
  55. void add_precondition( precondition_t const& );
  56. test_tools::assertion_result check_preconditions() const;
  57. // labels management
  58. void add_label( const_string l );
  59. bool has_label( const_string l ) const;
  60. // helper access methods
  61. void increase_exp_fail( counter_t num );
  62. bool is_enabled() const { return p_run_status == RS_ENABLED; }
  63. std::string full_name() const;
  64. // Public r/o properties
  65. test_unit_type const p_type; ///< type for this test unit
  66. const_string const p_type_name; ///< "case"/"suite"/"module"
  67. const_string const p_file_name;
  68. std::size_t const p_line_num;
  69. id_t p_id; ///< unique id for this test unit
  70. parent_id_t p_parent_id; ///< parent test suite id
  71. label_list_t p_labels; ///< list of labels associated with this test unit
  72. id_list_t p_dependencies; ///< list of test units this one depends on
  73. precond_list_t p_preconditions; ///< user supplied preconditions for this test unit;
  74. // Public r/w properties
  75. readwrite_property<std::string> p_name; ///< name for this test unit
  76. readwrite_property<std::string> p_description; ///< description for this test unit
  77. readwrite_property<unsigned> p_timeout; ///< timeout for the test unit execution in seconds
  78. readwrite_property<counter_t> p_expected_failures; ///< number of expected failures in this test unit
  79. readwrite_property<run_status> p_default_status; ///< run status obtained by this unit during setup phase
  80. readwrite_property<run_status> p_run_status; ///< run status assigned to this unit before execution phase after applying all filters
  81. readwrite_property<counter_t> p_sibling_rank; ///< rank of this test unit amoung siblings of the same parent
  82. readwrite_property<decor_list_t> p_decorators; ///< automatically assigned decorators; execution is delayed till framework::finalize_setup_phase function
  83. readwrite_property<fixture_list_t> p_fixtures; ///< fixtures associated with this test unit
  84. protected:
  85. ~test_unit();
  86. // Constructor
  87. test_unit( const_string tu_name, const_string tc_file, std::size_t tc_line, test_unit_type t );
  88. // Master test suite constructor
  89. explicit test_unit( const_string module_name );
  90. };
  91. // ************************************************************************** //
  92. // ************** test_unit_generator ************** //
  93. // ************************************************************************** //
  94. class BOOST_TEST_DECL test_unit_generator {
  95. public:
  96. virtual test_unit* next() const = 0;
  97. protected:
  98. BOOST_TEST_PROTECTED_VIRTUAL ~test_unit_generator() {}
  99. };
  100. // ************************************************************************** //
  101. // ************** test_case ************** //
  102. // ************************************************************************** //
  103. class BOOST_TEST_DECL test_case : public test_unit {
  104. public:
  105. enum { type = TUT_CASE };
  106. // Constructor
  107. test_case( const_string tc_name, boost::function<void ()> const& test_func );
  108. test_case( const_string tc_name, const_string tc_file, std::size_t tc_line, boost::function<void ()> const& test_func );
  109. // Public property
  110. typedef BOOST_READONLY_PROPERTY(boost::function<void ()>,(test_case)) test_func;
  111. test_func p_test_func;
  112. private:
  113. friend class framework::state;
  114. ~test_case() {}
  115. };
  116. // ************************************************************************** //
  117. // ************** test_suite ************** //
  118. // ************************************************************************** //
  119. //! Class representing test suites
  120. class BOOST_TEST_DECL test_suite : public test_unit {
  121. public:
  122. enum { type = TUT_SUITE };
  123. // Constructor
  124. explicit test_suite( const_string ts_name, const_string ts_file, std::size_t ts_line );
  125. // test unit list management
  126. /*!@brief Adds a test unit to a test suite.
  127. *
  128. * It is possible to specify the timeout and the expected failures.
  129. */
  130. void add( test_unit* tu, counter_t expected_failures = 0, unsigned timeout = 0 );
  131. /// @overload
  132. void add( test_unit_generator const& gen, unsigned timeout = 0 );
  133. /// @overload
  134. void add( test_unit_generator const& gen, decorator::collector_t& decorators );
  135. /// @overload
  136. void add( boost::shared_ptr<test_unit_generator> gen_ptr, decorator::collector_t& decorators );
  137. //! Removes a test from the test suite.
  138. void remove( test_unit_id id );
  139. //! Generates all the delayed test_units from the generators
  140. void generate( );
  141. //! Check for duplicates name in test cases
  142. //!
  143. //! Raises a setup_error if there are duplicates
  144. void check_for_duplicate_test_cases();
  145. // access methods
  146. test_unit_id get( const_string tu_name ) const;
  147. std::size_t size() const { return m_children.size(); }
  148. protected:
  149. // Master test suite constructor
  150. explicit test_suite( const_string module_name );
  151. friend BOOST_TEST_DECL
  152. void traverse_test_tree( test_suite const&, test_tree_visitor&, bool );
  153. friend class framework::state;
  154. virtual ~test_suite() {}
  155. typedef std::multimap<counter_t,test_unit_id> children_per_rank;
  156. // Data members
  157. test_unit_id_list m_children;
  158. children_per_rank m_ranked_children; ///< maps child sibling rank to list of children with that rank
  159. std::vector< std::pair<boost::shared_ptr<test_unit_generator>, std::vector<decorator::base_ptr> > > m_generators; /// lazy evaluation
  160. };
  161. // ************************************************************************** //
  162. // ************** master_test_suite ************** //
  163. // ************************************************************************** //
  164. class BOOST_TEST_DECL master_test_suite_t : public test_suite {
  165. private:
  166. master_test_suite_t();
  167. master_test_suite_t(const master_test_suite_t&); // undefined
  168. master_test_suite_t& operator=(master_test_suite_t const &); // undefined
  169. public:
  170. // Data members
  171. int argc;
  172. char** argv;
  173. friend BOOST_TEST_DECL master_test_suite_t& boost::unit_test::framework::master_test_suite();
  174. };
  175. // ************************************************************************** //
  176. // ************** user_tc_method_invoker ************** //
  177. // ************************************************************************** //
  178. namespace ut_detail {
  179. BOOST_TEST_DECL std::string normalize_test_case_name( const_string tu_name );
  180. //____________________________________________________________________________//
  181. template<typename InstanceType,typename UserTestCase>
  182. struct user_tc_method_invoker {
  183. typedef void (UserTestCase::*TestMethod )();
  184. user_tc_method_invoker( shared_ptr<InstanceType> inst, TestMethod test_method )
  185. : m_inst( inst ), m_test_method( test_method ) {}
  186. void operator()() { ((*m_inst).*m_test_method)(); }
  187. shared_ptr<InstanceType> m_inst;
  188. TestMethod m_test_method;
  189. };
  190. } // namespace ut_detail
  191. // ************************************************************************** //
  192. // ************** make_test_case ************** //
  193. // ************************************************************************** //
  194. inline test_case*
  195. make_test_case( boost::function<void ()> const& test_func, const_string tc_name, const_string tc_file, std::size_t tc_line )
  196. {
  197. return new test_case( ut_detail::normalize_test_case_name( tc_name ), tc_file, tc_line, test_func );
  198. }
  199. //____________________________________________________________________________//
  200. template<typename UserTestCase, typename InstanceType>
  201. inline test_case*
  202. make_test_case( void (UserTestCase::* test_method )(),
  203. const_string tc_name,
  204. const_string tc_file,
  205. std::size_t tc_line,
  206. boost::shared_ptr<InstanceType> user_test_case )
  207. {
  208. return new test_case( ut_detail::normalize_test_case_name( tc_name ),
  209. tc_file,
  210. tc_line,
  211. ut_detail::user_tc_method_invoker<InstanceType,UserTestCase>( user_test_case, test_method ) );
  212. }
  213. //____________________________________________________________________________//
  214. } // namespace unit_test
  215. } // namespace boost
  216. #include <boost/test/detail/enable_warnings.hpp>
  217. #endif // BOOST_TEST_TREE_TEST_UNIT_HPP_100211GER