diff options
Diffstat (limited to 'boost/test/tree/fixture.hpp')
-rw-r--r-- | boost/test/tree/fixture.hpp | 93 |
1 files changed, 84 insertions, 9 deletions
diff --git a/boost/test/tree/fixture.hpp b/boost/test/tree/fixture.hpp index 7bca5a8de3..8e07b2aa1d 100644 --- a/boost/test/tree/fixture.hpp +++ b/boost/test/tree/fixture.hpp @@ -5,11 +5,8 @@ // See http://www.boost.org/libs/test for the library home page. // -// File : $RCSfile$ -// -// Version : $Revision: 74640 $ -// -// Description : defines fixture interface and object makers +/// @file +/// Defines fixture interface and object makers // *************************************************************************** #ifndef BOOST_TEST_TREE_FIXTURE_HPP_100311GER @@ -22,6 +19,7 @@ #include <boost/shared_ptr.hpp> #include <boost/scoped_ptr.hpp> #include <boost/function/function0.hpp> +#include <boost/utility/declval.hpp> #include <boost/test/detail/suppress_warnings.hpp> @@ -46,6 +44,83 @@ public: typedef shared_ptr<test_unit_fixture> test_unit_fixture_ptr; // ************************************************************************** // +// ************** fixture helper functions ************** // +// ************************************************************************** // + +namespace impl_fixture { + +#if defined(BOOST_NO_CXX11_DECLTYPE) || defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) + + template<typename U, void (U::*)()> struct fixture_detect {}; + + template<typename T> + struct has_setup { + private: + template<typename U> static char Test(fixture_detect<U, &U::setup>*); + template<typename U> static int Test(...); + public: + static const bool value = sizeof(Test<T>(0)) == sizeof(char); + }; + + template<typename T> + struct has_teardown { + private: + template<typename U> static char Test(fixture_detect<U, &U::teardown>*); + template<typename U> static int Test(...); + public: + static const bool value = sizeof(Test<T>(0)) == sizeof(char); + }; + +#else + + template<typename U> struct fixture_detect { typedef char type; }; + template<typename T> + struct has_setup { + private: + template<typename U> static auto Test(U*) -> typename fixture_detect<decltype(boost::declval<U>().setup())>::type; + template<typename U> static int Test(...); + public: + static const bool value = sizeof(Test<T>(0)) == sizeof(char); + }; + + template<typename T> + struct has_teardown { + private: + template<typename U> static auto Test(U*) -> typename fixture_detect<decltype(boost::declval<U>().teardown())>::type; + template<typename U> static int Test(...); + public: + static const bool value = sizeof(Test<T>(0)) == sizeof(char); + }; + +#endif + + template <bool has_setup = false> + struct call_setup { template <class U> void operator()(U& ) { } }; + + template <> + struct call_setup<true> { template <class U> void operator()(U& u) { u.setup(); } }; + + template <bool has_teardown = false> + struct call_teardown { template <class U> void operator()(U& ) { } }; + + template <> + struct call_teardown<true> { template <class U> void operator()(U& u) { u.teardown(); } }; +} + +//! Calls the fixture "setup" if detected by the compiler, otherwise does nothing. +template <class U> +void setup_conditional(U& u) { + return impl_fixture::call_setup<impl_fixture::has_setup<U>::value>()(u); +} + +//! Calls the fixture "teardown" if detected by the compiler, otherwise does nothing. +template <class U> +void teardown_conditional(U& u) { + return impl_fixture::call_teardown<impl_fixture::has_teardown<U>::value>()(u); +} + + +// ************************************************************************** // // ************** class_based_fixture ************** // // ************************************************************************** // @@ -57,8 +132,8 @@ public: private: // Fixture interface - virtual void setup() { m_inst.reset( new F( m_arg ) ); } - virtual void teardown() { m_inst.reset(); } + virtual void setup() { m_inst.reset( new F( m_arg ) ); setup_conditional(*m_inst); } + virtual void teardown() { teardown_conditional(*m_inst); m_inst.reset(); } // Data members scoped_ptr<F> m_inst; @@ -75,8 +150,8 @@ public: private: // Fixture interface - virtual void setup() { m_inst.reset( new F ); } - virtual void teardown() { m_inst.reset(); } + virtual void setup() { m_inst.reset( new F ); setup_conditional(*m_inst); } + virtual void teardown() { teardown_conditional(*m_inst); m_inst.reset(); } // Data members scoped_ptr<F> m_inst; |