fixture.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 fixture interface and object makers
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_TREE_FIXTURE_HPP_100311GER
  11. #define BOOST_TEST_TREE_FIXTURE_HPP_100311GER
  12. // Boost.Test
  13. #include <boost/test/detail/config.hpp>
  14. // Boost
  15. #include <boost/shared_ptr.hpp>
  16. #include <boost/scoped_ptr.hpp>
  17. #include <boost/function/function0.hpp>
  18. #include <boost/utility/declval.hpp>
  19. #include <boost/test/detail/suppress_warnings.hpp>
  20. //____________________________________________________________________________//
  21. namespace boost {
  22. namespace unit_test {
  23. // ************************************************************************** //
  24. // ************** test_unit_fixture ************** //
  25. // ************************************************************************** //
  26. class BOOST_TEST_DECL test_unit_fixture {
  27. public:
  28. virtual ~test_unit_fixture() {}
  29. // Fixture interface
  30. virtual void setup() = 0;
  31. virtual void teardown() = 0;
  32. };
  33. typedef shared_ptr<test_unit_fixture> test_unit_fixture_ptr;
  34. // ************************************************************************** //
  35. // ************** fixture helper functions ************** //
  36. // ************************************************************************** //
  37. namespace impl_fixture {
  38. #if defined(BOOST_NO_CXX11_DECLTYPE) || defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES)
  39. template<typename U, void (U::*)()> struct fixture_detect {};
  40. template<typename T>
  41. struct has_setup {
  42. private:
  43. template<typename U> static char Test(fixture_detect<U, &U::setup>*);
  44. template<typename U> static int Test(...);
  45. public:
  46. static const bool value = sizeof(Test<T>(0)) == sizeof(char);
  47. };
  48. template<typename T>
  49. struct has_teardown {
  50. private:
  51. template<typename U> static char Test(fixture_detect<U, &U::teardown>*);
  52. template<typename U> static int Test(...);
  53. public:
  54. static const bool value = sizeof(Test<T>(0)) == sizeof(char);
  55. };
  56. #else
  57. template<typename U> struct fixture_detect { typedef char type; };
  58. template<typename T>
  59. struct has_setup {
  60. private:
  61. template<typename U> static auto Test(U*) -> typename fixture_detect<decltype(boost::declval<U>().setup())>::type;
  62. template<typename U> static int Test(...);
  63. public:
  64. static const bool value = sizeof(Test<T>(0)) == sizeof(char);
  65. };
  66. template<typename T>
  67. struct has_teardown {
  68. private:
  69. template<typename U> static auto Test(U*) -> typename fixture_detect<decltype(boost::declval<U>().teardown())>::type;
  70. template<typename U> static int Test(...);
  71. public:
  72. static const bool value = sizeof(Test<T>(0)) == sizeof(char);
  73. };
  74. #endif
  75. template <bool has_setup = false>
  76. struct call_setup { template <class U> void operator()(U& ) { } };
  77. template <>
  78. struct call_setup<true> { template <class U> void operator()(U& u) { u.setup(); } };
  79. template <bool has_teardown = false>
  80. struct call_teardown { template <class U> void operator()(U& ) { } };
  81. template <>
  82. struct call_teardown<true> { template <class U> void operator()(U& u) { u.teardown(); } };
  83. }
  84. //! Calls the fixture "setup" if detected by the compiler, otherwise does nothing.
  85. template <class U>
  86. void setup_conditional(U& u) {
  87. return impl_fixture::call_setup<impl_fixture::has_setup<U>::value>()(u);
  88. }
  89. //! Calls the fixture "teardown" if detected by the compiler, otherwise does nothing.
  90. template <class U>
  91. void teardown_conditional(U& u) {
  92. return impl_fixture::call_teardown<impl_fixture::has_teardown<U>::value>()(u);
  93. }
  94. // ************************************************************************** //
  95. // ************** class_based_fixture ************** //
  96. // ************************************************************************** //
  97. template<typename F, typename Arg=void>
  98. class class_based_fixture : public test_unit_fixture {
  99. public:
  100. // Constructor
  101. explicit class_based_fixture( Arg const& arg ) : m_inst(), m_arg( arg ) {}
  102. private:
  103. // Fixture interface
  104. virtual void setup() { m_inst.reset( new F( m_arg ) ); setup_conditional(*m_inst); }
  105. virtual void teardown() { teardown_conditional(*m_inst); m_inst.reset(); }
  106. // Data members
  107. scoped_ptr<F> m_inst;
  108. Arg m_arg;
  109. };
  110. //____________________________________________________________________________//
  111. template<typename F>
  112. class class_based_fixture<F,void> : public test_unit_fixture {
  113. public:
  114. // Constructor
  115. class_based_fixture() : m_inst( 0 ) {}
  116. private:
  117. // Fixture interface
  118. virtual void setup() { m_inst.reset( new F ); setup_conditional(*m_inst); }
  119. virtual void teardown() { teardown_conditional(*m_inst); m_inst.reset(); }
  120. // Data members
  121. scoped_ptr<F> m_inst;
  122. };
  123. //____________________________________________________________________________//
  124. // ************************************************************************** //
  125. // ************** function_based_fixture ************** //
  126. // ************************************************************************** //
  127. class function_based_fixture : public test_unit_fixture {
  128. public:
  129. // Constructor
  130. function_based_fixture( boost::function<void ()> const& setup_, boost::function<void ()> const& teardown_ )
  131. : m_setup( setup_ )
  132. , m_teardown( teardown_ )
  133. {
  134. }
  135. private:
  136. // Fixture interface
  137. virtual void setup() { if( m_setup ) m_setup(); }
  138. virtual void teardown() { if( m_teardown ) m_teardown(); }
  139. // Data members
  140. boost::function<void ()> m_setup;
  141. boost::function<void ()> m_teardown;
  142. };
  143. } // namespace unit_test
  144. } // namespace boost
  145. #include <boost/test/detail/enable_warnings.hpp>
  146. #endif // BOOST_TEST_TREE_FIXTURE_HPP_100311GER