// (C) Copyright Gennadiy Rozental 2001. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // See http://www.boost.org/libs/test for the library home page. // /// @file /// Defines fixture interface and object makers // *************************************************************************** #ifndef BOOST_TEST_TREE_FIXTURE_HPP_100311GER #define BOOST_TEST_TREE_FIXTURE_HPP_100311GER // Boost.Test #include // Boost #include #include #include #include #include //____________________________________________________________________________// namespace boost { namespace unit_test { // ************************************************************************** // // ************** test_unit_fixture ************** // // ************************************************************************** // class BOOST_TEST_DECL test_unit_fixture { public: virtual ~test_unit_fixture() {} // Fixture interface virtual void setup() = 0; virtual void teardown() = 0; }; typedef shared_ptr test_unit_fixture_ptr; // ************************************************************************** // // ************** fixture helper functions ************** // // ************************************************************************** // namespace impl_fixture { #if defined(BOOST_NO_CXX11_DECLTYPE) || defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) template struct fixture_detect {}; template struct has_setup { private: template static char Test(fixture_detect*); template static int Test(...); public: static const bool value = sizeof(Test(0)) == sizeof(char); }; template struct has_teardown { private: template static char Test(fixture_detect*); template static int Test(...); public: static const bool value = sizeof(Test(0)) == sizeof(char); }; #else template struct fixture_detect { typedef char type; }; template struct has_setup { private: template static auto Test(U*) -> typename fixture_detect().setup())>::type; template static int Test(...); public: static const bool value = sizeof(Test(0)) == sizeof(char); }; template struct has_teardown { private: template static auto Test(U*) -> typename fixture_detect().teardown())>::type; template static int Test(...); public: static const bool value = sizeof(Test(0)) == sizeof(char); }; #endif template struct call_setup { template void operator()(U& ) { } }; template <> struct call_setup { template void operator()(U& u) { u.setup(); } }; template struct call_teardown { template void operator()(U& ) { } }; template <> struct call_teardown { template void operator()(U& u) { u.teardown(); } }; } //! Calls the fixture "setup" if detected by the compiler, otherwise does nothing. template void setup_conditional(U& u) { return impl_fixture::call_setup::value>()(u); } //! Calls the fixture "teardown" if detected by the compiler, otherwise does nothing. template void teardown_conditional(U& u) { return impl_fixture::call_teardown::value>()(u); } // ************************************************************************** // // ************** class_based_fixture ************** // // ************************************************************************** // template class class_based_fixture : public test_unit_fixture { public: // Constructor explicit class_based_fixture( Arg const& arg ) : m_inst(), m_arg( arg ) {} private: // Fixture interface 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 m_inst; Arg m_arg; }; //____________________________________________________________________________// template class class_based_fixture : public test_unit_fixture { public: // Constructor class_based_fixture() : m_inst( 0 ) {} private: // Fixture interface 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 m_inst; }; //____________________________________________________________________________// // ************************************************************************** // // ************** function_based_fixture ************** // // ************************************************************************** // class function_based_fixture : public test_unit_fixture { public: // Constructor function_based_fixture( boost::function const& setup_, boost::function const& teardown_ ) : m_setup( setup_ ) , m_teardown( teardown_ ) { } private: // Fixture interface virtual void setup() { if( m_setup ) m_setup(); } virtual void teardown() { if( m_teardown ) m_teardown(); } // Data members boost::function m_setup; boost::function m_teardown; }; } // namespace unit_test } // namespace boost #include #endif // BOOST_TEST_TREE_FIXTURE_HPP_100311GER