Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:52:41

0001 //  (C) Copyright Gennadiy Rozental 2001.
0002 //  Distributed under the Boost Software License, Version 1.0.
0003 //  (See accompanying file LICENSE_1_0.txt or copy at
0004 //  http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 //  See http://www.boost.org/libs/test for the library home page.
0007 //
0008 /// @file
0009 /// Defines fixture interface and object makers
0010 // ***************************************************************************
0011 
0012 #ifndef BOOST_TEST_TREE_FIXTURE_HPP_100311GER
0013 #define BOOST_TEST_TREE_FIXTURE_HPP_100311GER
0014 
0015 // Boost.Test
0016 #include <boost/test/detail/config.hpp>
0017 
0018 // Boost
0019 #include <boost/shared_ptr.hpp>
0020 #include <boost/scoped_ptr.hpp>
0021 #include <boost/function/function0.hpp>
0022 #include <boost/utility/declval.hpp>
0023 
0024 #include <boost/test/detail/suppress_warnings.hpp>
0025 
0026 //____________________________________________________________________________//
0027 
0028 namespace boost {
0029 namespace unit_test {
0030 
0031 // ************************************************************************** //
0032 // **************               test_unit_fixture              ************** //
0033 // ************************************************************************** //
0034 
0035 class BOOST_TEST_DECL test_unit_fixture {
0036 public:
0037     virtual ~test_unit_fixture() {}
0038 
0039     // Fixture interface
0040     virtual void    setup() = 0;
0041     virtual void    teardown() = 0;
0042 };
0043 
0044 typedef shared_ptr<test_unit_fixture> test_unit_fixture_ptr;
0045 
0046 // ************************************************************************** //
0047 // **************               fixture helper functions       ************** //
0048 // ************************************************************************** //
0049 
0050 namespace impl_fixture {
0051 
0052 #if defined(BOOST_NO_CXX11_DECLTYPE) || defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES)
0053 
0054     template<typename U, void (U::*)()> struct fixture_detect {};
0055 
0056     template<typename T>
0057     struct has_setup {
0058     private:
0059         template<typename U> static char Test(fixture_detect<U, &U::setup>*);
0060         template<typename U> static int Test(...);
0061     public:
0062         static const bool value = sizeof(Test<T>(0)) == sizeof(char);
0063     };
0064 
0065     template<typename T>
0066     struct has_teardown {
0067     private:
0068         template<typename U> static char Test(fixture_detect<U, &U::teardown>*);
0069         template<typename U> static int Test(...);
0070     public:
0071         static const bool value = sizeof(Test<T>(0)) == sizeof(char);
0072     };
0073 
0074 #else
0075 
0076     template<typename U> struct fixture_detect { typedef char type; };
0077     template<typename T>
0078     struct has_setup {
0079     private:
0080         template<typename U> static auto Test(U*) -> typename fixture_detect<decltype(boost::declval<U>().setup())>::type;
0081         template<typename U> static int Test(...);
0082     public:
0083         static const bool value = sizeof(Test<T>(0)) == sizeof(char);
0084     };
0085 
0086     template<typename T>
0087     struct has_teardown {
0088     private:
0089         template<typename U> static auto Test(U*) -> typename fixture_detect<decltype(boost::declval<U>().teardown())>::type;
0090         template<typename U> static int Test(...);
0091     public:
0092         static const bool value = sizeof(Test<T>(0)) == sizeof(char);
0093     };
0094 
0095 #endif
0096 
0097     template <bool has_setup = false>
0098     struct call_setup          { template <class U> void operator()(U& ) { }                };
0099 
0100     template <>
0101     struct call_setup<true>    { template <class U> void operator()(U& u) { u.setup(); }    };
0102 
0103     template <bool has_teardown = false>
0104     struct call_teardown       { template <class U> void operator()(U& ) { }                };
0105 
0106     template <>
0107     struct call_teardown<true> { template <class U> void operator()(U& u) { u.teardown(); } };
0108 }
0109 
0110 //! Calls the fixture "setup" if detected by the compiler, otherwise does nothing.
0111 template <class U>
0112 void setup_conditional(U& u) {
0113     return impl_fixture::call_setup<impl_fixture::has_setup<U>::value>()(u);
0114 }
0115 
0116 //! Calls the fixture "teardown" if detected by the compiler, otherwise does nothing.
0117 template <class U>
0118 void teardown_conditional(U& u) {
0119     return impl_fixture::call_teardown<impl_fixture::has_teardown<U>::value>()(u);
0120 }
0121 
0122 
0123 // ************************************************************************** //
0124 // **************              class_based_fixture             ************** //
0125 // ************************************************************************** //
0126 
0127 template<typename F, typename Arg=void>
0128 class class_based_fixture : public test_unit_fixture {
0129 public:
0130     // Constructor
0131     explicit class_based_fixture( Arg const& arg ) : m_inst(), m_arg( arg ) {}
0132 
0133 private:
0134     // Fixture interface
0135     void    setup() BOOST_OVERRIDE         { m_inst.reset( new F( m_arg ) ); setup_conditional(*m_inst); }
0136     void    teardown() BOOST_OVERRIDE      { teardown_conditional(*m_inst); m_inst.reset(); }
0137 
0138     // Data members
0139     scoped_ptr<F>   m_inst;
0140     Arg             m_arg;
0141 };
0142 
0143 //____________________________________________________________________________//
0144 
0145 template<typename F>
0146 class class_based_fixture<F,void> : public test_unit_fixture {
0147 public:
0148     // Constructor
0149     class_based_fixture() : m_inst( 0 ) {}
0150 
0151 private:
0152     // Fixture interface
0153     void    setup() BOOST_OVERRIDE         { m_inst.reset( new F ); setup_conditional(*m_inst); }
0154     void    teardown() BOOST_OVERRIDE      { teardown_conditional(*m_inst); m_inst.reset(); }
0155 
0156     // Data members
0157     scoped_ptr<F>   m_inst;
0158 };
0159 
0160 //____________________________________________________________________________//
0161 
0162 // ************************************************************************** //
0163 // **************            function_based_fixture            ************** //
0164 // ************************************************************************** //
0165 
0166 class function_based_fixture : public test_unit_fixture {
0167 public:
0168     // Constructor
0169     function_based_fixture( boost::function<void ()> const& setup_, boost::function<void ()> const& teardown_ )
0170     : m_setup( setup_ )
0171     , m_teardown( teardown_ )
0172     {
0173     }
0174 
0175 private:
0176     // Fixture interface
0177     void                setup() BOOST_OVERRIDE     { if( m_setup ) m_setup(); }
0178     void                teardown() BOOST_OVERRIDE  { if( m_teardown ) m_teardown(); }
0179 
0180     // Data members
0181     boost::function<void ()>    m_setup;
0182     boost::function<void ()>    m_teardown;
0183 };
0184 
0185 } // namespace unit_test
0186 } // namespace boost
0187 
0188 #include <boost/test/detail/enable_warnings.hpp>
0189 
0190 #endif // BOOST_TEST_TREE_FIXTURE_HPP_100311GER
0191