diff options
Diffstat (limited to 'boost/test/tree')
-rw-r--r-- | boost/test/tree/fixture.hpp | 93 | ||||
-rw-r--r-- | boost/test/tree/global_fixture.hpp | 70 | ||||
-rw-r--r-- | boost/test/tree/observer.hpp | 6 |
3 files changed, 151 insertions, 18 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; diff --git a/boost/test/tree/global_fixture.hpp b/boost/test/tree/global_fixture.hpp index 2114595929..7c96d34e89 100644 --- a/boost/test/tree/global_fixture.hpp +++ b/boost/test/tree/global_fixture.hpp @@ -17,6 +17,7 @@ #include <boost/test/detail/global_typedef.hpp> #include <boost/test/tree/observer.hpp> +#include <boost/test/tree/fixture.hpp> #include <boost/test/detail/suppress_warnings.hpp> @@ -27,13 +28,36 @@ namespace boost { namespace unit_test { // ************************************************************************** // +// ************** global_configuration ************** // +// ************************************************************************** // + +class BOOST_TEST_DECL global_configuration : public test_observer { + +public: + // Constructor + global_configuration(); + + // Dtor + virtual ~global_configuration(); + + // Happens after the framework global observer init has been done + virtual int priority() { return 1; } +}; + + + +// ************************************************************************** // // ************** global_fixture ************** // // ************************************************************************** // -class BOOST_TEST_DECL global_fixture : public test_observer { +class BOOST_TEST_DECL global_fixture : public test_unit_fixture { + public: // Constructor global_fixture(); + + // Dtor + virtual ~global_fixture(); }; //____________________________________________________________________________// @@ -41,14 +65,48 @@ public: namespace ut_detail { template<typename F> -struct global_fixture_impl : public global_fixture { +struct global_configuration_impl : public global_configuration { // Constructor - global_fixture_impl() : m_fixture( 0 ) {} + global_configuration_impl() : m_configuration_observer( 0 ) { + } // test observer interface - virtual void test_start( counter_t ) { m_fixture = new F; } - virtual void test_finish() { delete m_fixture; m_fixture = 0; } - virtual void test_aborted() { delete m_fixture; m_fixture = 0; } + virtual void test_start( counter_t ) { + m_configuration_observer = new F; + } + + // test observer interface + virtual void test_finish() { + if(m_configuration_observer) { + delete m_configuration_observer; + m_configuration_observer = 0; + } + } +private: + // Data members + F* m_configuration_observer; +}; + +template<typename F> +struct global_fixture_impl : public global_fixture { + // Constructor + global_fixture_impl() : m_fixture( 0 ) { + } + + // test fixture interface + virtual void setup() { + m_fixture = new F; + setup_conditional(*m_fixture); + } + + // test fixture interface + virtual void teardown() { + if(m_fixture) { + teardown_conditional(*m_fixture); + } + delete m_fixture; + m_fixture = 0; + } private: // Data members diff --git a/boost/test/tree/observer.hpp b/boost/test/tree/observer.hpp index 4db930fb07..bd6fc9bff5 100644 --- a/boost/test/tree/observer.hpp +++ b/boost/test/tree/observer.hpp @@ -34,7 +34,7 @@ namespace unit_test { /// Boost.Test framework on the current execution state. /// /// Several observers can be running at the same time, and it is not unusual to -/// have interactions among them. The test_observer#priority member function allows the specification +/// have interactions among them. The @ref test_observer::priority member function allows the specification /// of a particular order among them (lowest priority executed first, except specified otherwise). /// class BOOST_TEST_DECL test_observer { @@ -44,10 +44,8 @@ public: //! //! @param[in] number_of_test_cases indicates the number of test cases. Only active //! test cases are taken into account. - //! virtual void test_start( counter_t /* number_of_test_cases */ ) {} - //! Called after the framework ends executing the test cases //! //! @note The call is made with a reversed priority order. @@ -98,6 +96,8 @@ public: //! additional data about the exception. virtual void exception_caught( execution_exception const& ) {} + //! The priority indicates the order at which this observer is initialized + //! and tore down in the UTF framework. The order is lowest to highest priority. virtual int priority() { return 0; } protected: |