1 //  (C) Copyright Gennadiy Rozental 2011-2014.
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)
5 
6 //  See http://www.boost.org/libs/test for the library home page.
7 //
8 //  File        : $RCSfile$
9 //
10 //  Version     : $Revision: 74640 $
11 //
12 //  Description : defines fixture interface and object makers
13 // ***************************************************************************
14 
15 #ifndef BOOST_TEST_TREE_FIXTURE_HPP_100311GER
16 #define BOOST_TEST_TREE_FIXTURE_HPP_100311GER
17 
18 // Boost.Test
19 #include <boost/test/detail/config.hpp>
20 
21 // Boost
22 #include <boost/shared_ptr.hpp>
23 #include <boost/scoped_ptr.hpp>
24 #include <boost/function/function0.hpp>
25 
26 #include <boost/test/detail/suppress_warnings.hpp>
27 
28 //____________________________________________________________________________//
29 
30 namespace boost {
31 namespace unit_test {
32 
33 // ************************************************************************** //
34 // **************               test_unit_fixture              ************** //
35 // ************************************************************************** //
36 
37 class BOOST_TEST_DECL test_unit_fixture {
38 public:
~test_unit_fixture()39     virtual ~test_unit_fixture() {}
40 
41     // Fixture interface
42     virtual void    setup() = 0;
43     virtual void    teardown() = 0;
44 };
45 
46 typedef shared_ptr<test_unit_fixture> test_unit_fixture_ptr;
47 
48 // ************************************************************************** //
49 // **************              class_based_fixture             ************** //
50 // ************************************************************************** //
51 
52 template<typename F, typename Arg=void>
53 class class_based_fixture : public test_unit_fixture {
54 public:
55     // Constructor
class_based_fixture(Arg const & arg)56     explicit class_based_fixture( Arg const& arg ) : m_inst(), m_arg( arg ) {}
57 
58 private:
59     // Fixture interface
setup()60     virtual void    setup()         { m_inst.reset( new F( m_arg ) ); }
teardown()61     virtual void    teardown()      { m_inst.reset(); }
62 
63     // Data members
64     scoped_ptr<F>   m_inst;
65     Arg             m_arg;
66 };
67 
68 //____________________________________________________________________________//
69 
70 template<typename F>
71 class class_based_fixture<F,void> : public test_unit_fixture {
72 public:
73     // Constructor
class_based_fixture()74     class_based_fixture() : m_inst( 0 ) {}
75 
76 private:
77     // Fixture interface
setup()78     virtual void    setup()         { m_inst.reset( new F ); }
teardown()79     virtual void    teardown()      { m_inst.reset(); }
80 
81     // Data members
82     scoped_ptr<F>   m_inst;
83 };
84 
85 //____________________________________________________________________________//
86 
87 // ************************************************************************** //
88 // **************            function_based_fixture            ************** //
89 // ************************************************************************** //
90 
91 class function_based_fixture : public test_unit_fixture {
92 public:
93     // Constructor
function_based_fixture(boost::function<void ()> const & setup_,boost::function<void ()> const & teardown_)94     function_based_fixture( boost::function<void ()> const& setup_, boost::function<void ()> const& teardown_ )
95     : m_setup( setup_ )
96     , m_teardown( teardown_ )
97     {
98     }
99 
100 private:
101     // Fixture interface
setup()102     virtual void                setup()     { if( m_setup ) m_setup(); }
teardown()103     virtual void                teardown()  { if( m_teardown ) m_teardown(); }
104 
105     // Data members
106     boost::function<void ()>    m_setup;
107     boost::function<void ()>    m_teardown;
108 };
109 
110 } // namespace unit_test
111 } // namespace boost
112 
113 #include <boost/test/detail/enable_warnings.hpp>
114 
115 #endif // BOOST_TEST_TREE_FIXTURE_HPP_100311GER
116 
117