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)
6 // See http://www.boost.org/libs/test for the library home page.
9 /// Defines fixture interface and object makers
10 // ***************************************************************************
12 #ifndef BOOST_TEST_TREE_FIXTURE_HPP_100311GER
13 #define BOOST_TEST_TREE_FIXTURE_HPP_100311GER
16 #include <boost/test/detail/config.hpp>
19 #include <boost/shared_ptr.hpp>
20 #include <boost/scoped_ptr.hpp>
21 #include <boost/function/function0.hpp>
22 #include <boost/utility/declval.hpp>
24 #include <boost/test/detail/suppress_warnings.hpp>
26 //____________________________________________________________________________//
31 // ************************************************************************** //
32 // ************** test_unit_fixture ************** //
33 // ************************************************************************** //
35 class BOOST_TEST_DECL test_unit_fixture {
37 virtual ~test_unit_fixture() {}
40 virtual void setup() = 0;
41 virtual void teardown() = 0;
44 typedef shared_ptr<test_unit_fixture> test_unit_fixture_ptr;
46 // ************************************************************************** //
47 // ************** fixture helper functions ************** //
48 // ************************************************************************** //
50 namespace impl_fixture {
52 #if defined(BOOST_NO_CXX11_DECLTYPE) || defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES)
54 template<typename U, void (U::*)()> struct fixture_detect {};
59 template<typename U> static char Test(fixture_detect<U, &U::setup>*);
60 template<typename U> static int Test(...);
62 static const bool value = sizeof(Test<T>(0)) == sizeof(char);
68 template<typename U> static char Test(fixture_detect<U, &U::teardown>*);
69 template<typename U> static int Test(...);
71 static const bool value = sizeof(Test<T>(0)) == sizeof(char);
76 template<typename U> struct fixture_detect { typedef char type; };
80 template<typename U> static auto Test(U*) -> typename fixture_detect<decltype(boost::declval<U>().setup())>::type;
81 template<typename U> static int Test(...);
83 static const bool value = sizeof(Test<T>(0)) == sizeof(char);
89 template<typename U> static auto Test(U*) -> typename fixture_detect<decltype(boost::declval<U>().teardown())>::type;
90 template<typename U> static int Test(...);
92 static const bool value = sizeof(Test<T>(0)) == sizeof(char);
97 template <bool has_setup = false>
98 struct call_setup { template <class U> void operator()(U& ) { } };
101 struct call_setup<true> { template <class U> void operator()(U& u) { u.setup(); } };
103 template <bool has_teardown = false>
104 struct call_teardown { template <class U> void operator()(U& ) { } };
107 struct call_teardown<true> { template <class U> void operator()(U& u) { u.teardown(); } };
110 //! Calls the fixture "setup" if detected by the compiler, otherwise does nothing.
112 void setup_conditional(U& u) {
113 return impl_fixture::call_setup<impl_fixture::has_setup<U>::value>()(u);
116 //! Calls the fixture "teardown" if detected by the compiler, otherwise does nothing.
118 void teardown_conditional(U& u) {
119 return impl_fixture::call_teardown<impl_fixture::has_teardown<U>::value>()(u);
123 // ************************************************************************** //
124 // ************** class_based_fixture ************** //
125 // ************************************************************************** //
127 template<typename F, typename Arg=void>
128 class class_based_fixture : public test_unit_fixture {
131 explicit class_based_fixture( Arg const& arg ) : m_inst(), m_arg( arg ) {}
135 virtual void setup() { m_inst.reset( new F( m_arg ) ); setup_conditional(*m_inst); }
136 virtual void teardown() { teardown_conditional(*m_inst); m_inst.reset(); }
139 scoped_ptr<F> m_inst;
143 //____________________________________________________________________________//
146 class class_based_fixture<F,void> : public test_unit_fixture {
149 class_based_fixture() : m_inst( 0 ) {}
153 virtual void setup() { m_inst.reset( new F ); setup_conditional(*m_inst); }
154 virtual void teardown() { teardown_conditional(*m_inst); m_inst.reset(); }
157 scoped_ptr<F> m_inst;
160 //____________________________________________________________________________//
162 // ************************************************************************** //
163 // ************** function_based_fixture ************** //
164 // ************************************************************************** //
166 class function_based_fixture : public test_unit_fixture {
169 function_based_fixture( boost::function<void ()> const& setup_, boost::function<void ()> const& teardown_ )
171 , m_teardown( teardown_ )
177 virtual void setup() { if( m_setup ) m_setup(); }
178 virtual void teardown() { if( m_teardown ) m_teardown(); }
181 boost::function<void ()> m_setup;
182 boost::function<void ()> m_teardown;
185 } // namespace unit_test
188 #include <boost/test/detail/enable_warnings.hpp>
190 #endif // BOOST_TEST_TREE_FIXTURE_HPP_100311GER