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        : $RCSfile$
0009 //
0010 //  Version     : $Revision: 62016 $
0011 //
0012 //  Description : defines decorators to be using with auto registered test units
0013 // ***************************************************************************
0014 
0015 #ifndef BOOST_TEST_TREE_DECORATOR_HPP_091911GER
0016 #define BOOST_TEST_TREE_DECORATOR_HPP_091911GER
0017 
0018 // Boost.Test
0019 #include <boost/test/detail/config.hpp>
0020 #include <boost/test/detail/global_typedef.hpp>
0021 
0022 #include <boost/test/tree/fixture.hpp>
0023 
0024 #include <boost/test/tools/assertion_result.hpp>
0025 #include <boost/test/utils/basic_cstring/basic_cstring.hpp>
0026 
0027 // Boost
0028 #include <boost/shared_ptr.hpp>
0029 #include <boost/function/function0.hpp>
0030 #include <boost/function/function1.hpp>
0031 
0032 #include <boost/test/detail/suppress_warnings.hpp>
0033 
0034 // STL
0035 #include <vector>
0036 
0037 //____________________________________________________________________________//
0038 
0039 namespace boost {
0040 namespace unit_test {
0041 
0042 class test_unit;
0043 
0044 namespace decorator {
0045 
0046 // ************************************************************************** //
0047 // **************             decorator::collector_t             ************** //
0048 // ************************************************************************** //
0049 
0050 class base;
0051 typedef boost::shared_ptr<base> base_ptr;
0052 
0053 class BOOST_TEST_DECL collector_t {
0054 
0055 public:
0056     collector_t&            operator*( base const& d );
0057 
0058     void                    store_in( test_unit& tu );
0059 
0060     void                    reset();
0061 
0062     void                    stack();
0063 
0064     std::vector<base_ptr>   get_lazy_decorators() const;
0065 
0066     // singleton pattern without ctor
0067     BOOST_TEST_SINGLETON_CONS_NO_CTOR( collector_t )
0068 
0069 private:
0070     // Class invariant: minimal size is 1.
0071     collector_t() : m_tu_decorators_stack(1) {}
0072 
0073     // Data members
0074     std::vector< std::vector<base_ptr> >   m_tu_decorators_stack;
0075 };
0076 
0077 
0078 // ************************************************************************** //
0079 // **************              decorator::base                 ************** //
0080 // ************************************************************************** //
0081 
0082 class BOOST_TEST_DECL base {
0083 public:
0084     // composition interface
0085     virtual collector_t&    operator*() const;
0086 
0087     // application interface
0088     virtual void            apply( test_unit& tu ) = 0;
0089 
0090     // deep cloning interface
0091     virtual base_ptr        clone() const = 0;
0092 
0093 protected:
0094     virtual ~base() {}
0095 };
0096 
0097 // ************************************************************************** //
0098 // **************         decorator::stack_decorator           ************** //
0099 // ************************************************************************** //
0100 
0101 //!@ A decorator that creates a new stack in the collector
0102 //!
0103 //! This decorator may be used in places where the currently accumulated decorators
0104 //! in the collector should be applied to lower levels of the hierarchy rather
0105 //! than the current one. This is for instance for dataset test cases, where the
0106 //! macro does not let the user specify decorators for the underlying generated tests
0107 //! (but rather on the main generator function), applying the stack_decorator at the
0108 //! parent level lets us consume the decorator at the underlying test cases level.
0109 class BOOST_TEST_DECL stack_decorator : public decorator::base {
0110 public:
0111     explicit                stack_decorator() {}
0112 
0113     collector_t&    operator*() const BOOST_OVERRIDE;
0114 
0115 private:
0116     // decorator::base interface
0117     void            apply( test_unit& tu ) BOOST_OVERRIDE;
0118     base_ptr        clone() const BOOST_OVERRIDE { return base_ptr(new stack_decorator()); }
0119 };
0120 
0121 // ************************************************************************** //
0122 // **************               decorator::label               ************** //
0123 // ************************************************************************** //
0124 
0125 class BOOST_TEST_DECL label : public decorator::base {
0126 public:
0127     explicit                label( const_string l ) : m_label( l ) {}
0128 
0129 private:
0130     // decorator::base interface
0131     void            apply( test_unit& tu ) BOOST_OVERRIDE;
0132     base_ptr        clone() const BOOST_OVERRIDE { return base_ptr(new label( m_label )); }
0133 
0134     // Data members
0135     const_string            m_label;
0136 };
0137 
0138 // ************************************************************************** //
0139 // **************         decorator::expected_failures         ************** //
0140 // ************************************************************************** //
0141 
0142 class BOOST_TEST_DECL expected_failures : public decorator::base {
0143 public:
0144     explicit                expected_failures( counter_t ef ) : m_exp_fail( ef ) {}
0145 
0146 private:
0147     // decorator::base interface
0148     void            apply( test_unit& tu ) BOOST_OVERRIDE;
0149     base_ptr        clone() const BOOST_OVERRIDE { return base_ptr(new expected_failures( m_exp_fail )); }
0150 
0151     // Data members
0152     counter_t               m_exp_fail;
0153 };
0154 
0155 // ************************************************************************** //
0156 // **************              decorator::timeout              ************** //
0157 // ************************************************************************** //
0158 
0159 class BOOST_TEST_DECL timeout : public decorator::base {
0160 public:
0161     explicit                timeout( unsigned t ) : m_timeout( t ) {}
0162 
0163 private:
0164     // decorator::base interface
0165     void            apply( test_unit& tu ) BOOST_OVERRIDE;
0166     base_ptr        clone() const BOOST_OVERRIDE { return base_ptr(new timeout( m_timeout )); }
0167 
0168     // Data members
0169     unsigned                m_timeout;
0170 };
0171 
0172 // ************************************************************************** //
0173 // **************            decorator::description            ************** //
0174 // ************************************************************************** //
0175 
0176 class BOOST_TEST_DECL description : public decorator::base {
0177 public:
0178     explicit                description( const_string descr ) : m_description( descr ) {}
0179 
0180 private:
0181     // decorator::base interface
0182     void            apply( test_unit& tu ) BOOST_OVERRIDE;
0183     base_ptr        clone() const BOOST_OVERRIDE { return base_ptr(new description( m_description )); }
0184 
0185     // Data members
0186     const_string            m_description;
0187 };
0188 
0189 // ************************************************************************** //
0190 // **************            decorator::depends_on             ************** //
0191 // ************************************************************************** //
0192 
0193 class BOOST_TEST_DECL depends_on : public decorator::base {
0194 public:
0195     explicit                depends_on( const_string dependency ) : m_dependency( dependency ) {}
0196 
0197 private:
0198     // decorator::base interface
0199     void            apply( test_unit& tu ) BOOST_OVERRIDE;
0200     base_ptr        clone() const BOOST_OVERRIDE { return base_ptr(new depends_on( m_dependency )); }
0201 
0202     // Data members
0203     const_string            m_dependency;
0204 };
0205 
0206 // ************************************************************************** //
0207 // **************    decorator::enable_if/enabled/disabled     ************** //
0208 // ************************************************************************** //
0209 
0210 class BOOST_TEST_DECL enable_if_impl : public decorator::base {
0211 protected:
0212     void                    apply_impl( test_unit& tu, bool condition );
0213 };
0214 
0215 template<bool condition>
0216 class enable_if : public enable_if_impl {
0217 private:
0218     // decorator::base interface
0219     void            apply( test_unit& tu ) BOOST_OVERRIDE   { this->apply_impl( tu, condition ); }
0220     base_ptr        clone() const BOOST_OVERRIDE            { return base_ptr(new enable_if<condition>()); }
0221 };
0222 
0223 typedef enable_if<true> enabled;
0224 typedef enable_if<false> disabled;
0225 
0226 // ************************************************************************** //
0227 // **************              decorator::fixture              ************** //
0228 // ************************************************************************** //
0229 
0230 class BOOST_TEST_DECL fixture_t : public decorator::base {
0231 public:
0232     // Constructor
0233     explicit                fixture_t( test_unit_fixture_ptr impl ) : m_impl( impl ) {}
0234 
0235 private:
0236     // decorator::base interface
0237     void            apply( test_unit& tu ) BOOST_OVERRIDE;
0238     base_ptr        clone() const BOOST_OVERRIDE { return base_ptr(new fixture_t( m_impl )); }
0239 
0240     // Data members
0241     test_unit_fixture_ptr m_impl;
0242 };
0243 
0244 //____________________________________________________________________________//
0245 
0246 template<typename F>
0247 inline fixture_t
0248 fixture()
0249 {
0250     return fixture_t( test_unit_fixture_ptr( new unit_test::class_based_fixture<F>() ) );
0251 }
0252 
0253 //____________________________________________________________________________//
0254 
0255 template<typename F, typename Arg>
0256 inline fixture_t
0257 fixture( Arg const& arg )
0258 {
0259     return fixture_t( test_unit_fixture_ptr( new unit_test::class_based_fixture<F,Arg>( arg ) ) );
0260 }
0261 
0262 //____________________________________________________________________________//
0263 
0264 inline fixture_t
0265 fixture( boost::function<void()> const& setup, boost::function<void()> const& teardown = boost::function<void()>() )
0266 {
0267     return fixture_t( test_unit_fixture_ptr( new unit_test::function_based_fixture( setup, teardown ) ) );
0268 }
0269 
0270 //____________________________________________________________________________//
0271 
0272 // ************************************************************************** //
0273 // **************            decorator::depends_on             ************** //
0274 // ************************************************************************** //
0275 
0276 class BOOST_TEST_DECL precondition : public decorator::base {
0277 public:
0278     typedef boost::function<test_tools::assertion_result (test_unit_id)>   predicate_t;
0279 
0280     explicit                precondition( predicate_t p ) : m_precondition( p ) {}
0281 
0282 private:
0283     // decorator::base interface
0284     void            apply( test_unit& tu ) BOOST_OVERRIDE;
0285     base_ptr        clone() const BOOST_OVERRIDE { return base_ptr(new precondition( m_precondition )); }
0286 
0287     // Data members
0288     predicate_t             m_precondition;
0289 };
0290 
0291 } // namespace decorator
0292 
0293 using decorator::label;
0294 using decorator::expected_failures;
0295 using decorator::timeout;
0296 using decorator::description;
0297 using decorator::depends_on;
0298 using decorator::enable_if;
0299 using decorator::enabled;
0300 using decorator::disabled;
0301 using decorator::fixture;
0302 using decorator::precondition;
0303 
0304 } // namespace unit_test
0305 } // namespace boost
0306 
0307 #include <boost/test/detail/enable_warnings.hpp>
0308 
0309 #endif // BOOST_TEST_TREE_DECORATOR_HPP_091911GER