1 // (C) Copyright Gennadiy Rozental 2001.
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)
6 // See http://www.boost.org/libs/test for the library home page.
10 // Version : $Revision: 62016 $
12 // Description : defines decorators to be using with auto registered test units
13 // ***************************************************************************
15 #ifndef BOOST_TEST_TREE_DECORATOR_HPP_091911GER
16 #define BOOST_TEST_TREE_DECORATOR_HPP_091911GER
19 #include <boost/test/detail/config.hpp>
20 #include <boost/test/detail/global_typedef.hpp>
22 #include <boost/test/tree/fixture.hpp>
24 #include <boost/test/tools/assertion_result.hpp>
26 #include <boost/test/utils/basic_cstring/basic_cstring.hpp>
27 #include <boost/test/utils/trivial_singleton.hpp>
30 #include <boost/shared_ptr.hpp>
31 #include <boost/function/function0.hpp>
32 #include <boost/function/function1.hpp>
34 #include <boost/test/detail/suppress_warnings.hpp>
39 //____________________________________________________________________________//
48 // ************************************************************************** //
49 // ************** decorator::collector ************** //
50 // ************************************************************************** //
53 typedef boost::shared_ptr<base> base_ptr;
55 class BOOST_TEST_DECL collector : public singleton<collector> {
57 collector& operator*( base const& d );
59 void store_in( test_unit& tu );
64 BOOST_TEST_SINGLETON_CONS( collector )
67 std::vector<base_ptr> m_tu_decorators;
70 // ************************************************************************** //
71 // ************** decorator::base ************** //
72 // ************************************************************************** //
74 class BOOST_TEST_DECL base {
76 // composition interface
77 collector& operator*() const;
79 // application interface
80 virtual void apply( test_unit& tu ) = 0;
82 // deep cloning interface
83 virtual base_ptr clone() const = 0;
89 // ************************************************************************** //
90 // ************** decorator::label ************** //
91 // ************************************************************************** //
93 class BOOST_TEST_DECL label : public decorator::base {
95 explicit label( const_string l ) : m_label( l ) {}
98 // decorator::base interface
99 virtual void apply( test_unit& tu );
100 virtual base_ptr clone() const { return base_ptr(new label( m_label )); }
103 const_string m_label;
106 // ************************************************************************** //
107 // ************** decorator::expected_failures ************** //
108 // ************************************************************************** //
110 class BOOST_TEST_DECL expected_failures : public decorator::base {
112 explicit expected_failures( counter_t ef ) : m_exp_fail( ef ) {}
115 // decorator::base interface
116 virtual void apply( test_unit& tu );
117 virtual base_ptr clone() const { return base_ptr(new expected_failures( m_exp_fail )); }
120 counter_t m_exp_fail;
123 // ************************************************************************** //
124 // ************** decorator::timeout ************** //
125 // ************************************************************************** //
127 class BOOST_TEST_DECL timeout : public decorator::base {
129 explicit timeout( unsigned t ) : m_timeout( t ) {}
132 // decorator::base interface
133 virtual void apply( test_unit& tu );
134 virtual base_ptr clone() const { return base_ptr(new timeout( m_timeout )); }
140 // ************************************************************************** //
141 // ************** decorator::description ************** //
142 // ************************************************************************** //
144 class BOOST_TEST_DECL description : public decorator::base {
146 explicit description( const_string descr ) : m_description( descr ) {}
149 // decorator::base interface
150 virtual void apply( test_unit& tu );
151 virtual base_ptr clone() const { return base_ptr(new description( m_description )); }
154 const_string m_description;
157 // ************************************************************************** //
158 // ************** decorator::depends_on ************** //
159 // ************************************************************************** //
161 class BOOST_TEST_DECL depends_on : public decorator::base {
163 explicit depends_on( const_string dependency ) : m_dependency( dependency ) {}
166 // decorator::base interface
167 virtual void apply( test_unit& tu );
168 virtual base_ptr clone() const { return base_ptr(new depends_on( m_dependency )); }
171 const_string m_dependency;
174 // ************************************************************************** //
175 // ************** decorator::enable_if/enabled/disabled ************** //
176 // ************************************************************************** //
178 class BOOST_TEST_DECL enable_if_impl : public decorator::base {
180 void apply_impl( test_unit& tu, bool condition );
183 template<bool condition>
184 class enable_if : public enable_if_impl {
186 // decorator::base interface
187 virtual void apply( test_unit& tu ) { this->apply_impl( tu, condition ); }
188 virtual base_ptr clone() const { return base_ptr(new enable_if<condition>()); }
191 typedef enable_if<true> enabled;
192 typedef enable_if<false> disabled;
194 // ************************************************************************** //
195 // ************** decorator::fixture ************** //
196 // ************************************************************************** //
198 class BOOST_TEST_DECL fixture_t : public decorator::base {
201 explicit fixture_t( test_unit_fixture_ptr impl ) : m_impl( impl ) {}
204 // decorator::base interface
205 virtual void apply( test_unit& tu );
206 virtual base_ptr clone() const { return base_ptr(new fixture_t( m_impl )); }
209 test_unit_fixture_ptr m_impl;
212 //____________________________________________________________________________//
218 return fixture_t( test_unit_fixture_ptr( new unit_test::class_based_fixture<F>() ) );
221 //____________________________________________________________________________//
223 template<typename F, typename Arg>
225 fixture( Arg const& arg )
227 return fixture_t( test_unit_fixture_ptr( new unit_test::class_based_fixture<F,Arg>( arg ) ) );
230 //____________________________________________________________________________//
233 fixture( boost::function<void()> const& setup, boost::function<void()> const& teardown = boost::function<void()>() )
235 return fixture_t( test_unit_fixture_ptr( new unit_test::function_based_fixture( setup, teardown ) ) );
238 //____________________________________________________________________________//
240 // ************************************************************************** //
241 // ************** decorator::depends_on ************** //
242 // ************************************************************************** //
244 class BOOST_TEST_DECL precondition : public decorator::base {
246 typedef boost::function<test_tools::assertion_result (test_unit_id)> predicate_t;
248 explicit precondition( predicate_t p ) : m_precondition( p ) {}
251 // decorator::base interface
252 virtual void apply( test_unit& tu );
253 virtual base_ptr clone() const { return base_ptr(new precondition( m_precondition )); }
256 predicate_t m_precondition;
259 } // namespace decorator
261 using decorator::label;
262 using decorator::expected_failures;
263 using decorator::timeout;
264 using decorator::description;
265 using decorator::depends_on;
266 using decorator::enable_if;
267 using decorator::enabled;
268 using decorator::disabled;
269 using decorator::fixture;
270 using decorator::precondition;
272 } // namespace unit_test
275 #include <boost/test/detail/enable_warnings.hpp>
277 #endif // BOOST_TEST_TREE_DECORATOR_HPP_091911GER