| 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) |
| 5 | |
| 6 | // See http://www.boost.org/libs/test for the library home page. |
| 7 | // |
| 8 | // Description : unit test decorators implementation |
| 9 | // *************************************************************************** |
| 10 | |
| 11 | #ifndef BOOST_TEST_TREE_DECORATOR_IPP_091911GER |
| 12 | #define BOOST_TEST_TREE_DECORATOR_IPP_091911GER |
| 13 | |
| 14 | // Boost.Test |
| 15 | #include <boost/test/tree/decorator.hpp> |
| 16 | #include <boost/test/tree/test_unit.hpp> |
| 17 | |
| 18 | #include <boost/test/framework.hpp> |
| 19 | #if BOOST_TEST_SUPPORT_TOKEN_ITERATOR |
| 20 | #include <boost/test/utils/iterator/token_iterator.hpp> |
| 21 | #endif |
| 22 | |
| 23 | #include <boost/test/detail/throw_exception.hpp> |
| 24 | |
| 25 | #include <boost/test/detail/suppress_warnings.hpp> |
| 26 | |
| 27 | //____________________________________________________________________________// |
| 28 | |
| 29 | namespace boost { |
| 30 | namespace unit_test { |
| 31 | namespace decorator { |
| 32 | |
| 33 | // ************************************************************************** // |
| 34 | // ************** decorator::collector_t ************** // |
| 35 | // ************************************************************************** // |
| 36 | |
| 37 | // singleton pattern |
| 38 | BOOST_TEST_SINGLETON_CONS_IMPL(collector_t) |
| 39 | |
| 40 | |
| 41 | collector_t& |
| 42 | collector_t::operator*( base const& d ) |
| 43 | { |
| 44 | m_tu_decorators_stack.begin()->push_back( x: d.clone() ); |
| 45 | |
| 46 | return *this; |
| 47 | } |
| 48 | |
| 49 | //____________________________________________________________________________// |
| 50 | |
| 51 | void |
| 52 | collector_t::store_in( test_unit& tu ) |
| 53 | { |
| 54 | tu.p_decorators.value.insert( |
| 55 | position: tu.p_decorators.value.end(), |
| 56 | first: m_tu_decorators_stack.begin()->begin(), |
| 57 | last: m_tu_decorators_stack.begin()->end() ); |
| 58 | } |
| 59 | |
| 60 | //____________________________________________________________________________// |
| 61 | |
| 62 | void |
| 63 | collector_t::reset() |
| 64 | { |
| 65 | if(m_tu_decorators_stack.size() > 1) { |
| 66 | m_tu_decorators_stack.erase(position: m_tu_decorators_stack.begin()); |
| 67 | } |
| 68 | else { |
| 69 | assert(m_tu_decorators_stack.size() == 1); |
| 70 | m_tu_decorators_stack.begin()->clear(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | collector_t::stack() |
| 76 | { |
| 77 | assert(m_tu_decorators_stack.size() >= 1); |
| 78 | m_tu_decorators_stack.insert(position: m_tu_decorators_stack.begin(), x: std::vector<base_ptr>()); |
| 79 | } |
| 80 | |
| 81 | //____________________________________________________________________________// |
| 82 | |
| 83 | std::vector<base_ptr> |
| 84 | collector_t::get_lazy_decorators() const |
| 85 | { |
| 86 | return *m_tu_decorators_stack.begin(); |
| 87 | } |
| 88 | |
| 89 | //____________________________________________________________________________// |
| 90 | |
| 91 | // ************************************************************************** // |
| 92 | // ************** decorator::base ************** // |
| 93 | // ************************************************************************** // |
| 94 | |
| 95 | collector_t& |
| 96 | base::operator*() const |
| 97 | { |
| 98 | return collector_t::instance() * *this; |
| 99 | } |
| 100 | |
| 101 | // ************************************************************************** // |
| 102 | // ************** decorator::stack_decorator ************** // |
| 103 | // ************************************************************************** // |
| 104 | |
| 105 | collector_t& |
| 106 | stack_decorator::operator*() const |
| 107 | { |
| 108 | collector_t& instance = collector_t::instance(); |
| 109 | instance.stack(); |
| 110 | return instance * *this; |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | stack_decorator::apply( test_unit& /*tu*/ ) |
| 115 | { |
| 116 | // does nothing by definition |
| 117 | } |
| 118 | |
| 119 | // ************************************************************************** // |
| 120 | // ************** decorator::label ************** // |
| 121 | // ************************************************************************** // |
| 122 | |
| 123 | void |
| 124 | label::apply( test_unit& tu ) |
| 125 | { |
| 126 | tu.add_label( l: m_label ); |
| 127 | } |
| 128 | |
| 129 | //____________________________________________________________________________// |
| 130 | |
| 131 | // ************************************************************************** // |
| 132 | // ************** decorator::expected_failures ************** // |
| 133 | // ************************************************************************** // |
| 134 | |
| 135 | void |
| 136 | expected_failures::apply( test_unit& tu ) |
| 137 | { |
| 138 | tu.increase_exp_fail( num: m_exp_fail ); |
| 139 | } |
| 140 | |
| 141 | //____________________________________________________________________________// |
| 142 | |
| 143 | // ************************************************************************** // |
| 144 | // ************** decorator::timeout ************** // |
| 145 | // ************************************************************************** // |
| 146 | |
| 147 | void |
| 148 | timeout::apply( test_unit& tu ) |
| 149 | { |
| 150 | tu.p_timeout.value = m_timeout; |
| 151 | } |
| 152 | |
| 153 | //____________________________________________________________________________// |
| 154 | |
| 155 | // ************************************************************************** // |
| 156 | // ************** decorator::description ************** // |
| 157 | // ************************************************************************** // |
| 158 | |
| 159 | void |
| 160 | description::apply( test_unit& tu ) |
| 161 | { |
| 162 | tu.p_description.value += m_description; |
| 163 | } |
| 164 | |
| 165 | //____________________________________________________________________________// |
| 166 | |
| 167 | // ************************************************************************** // |
| 168 | // ************** decorator::depends_on ************** // |
| 169 | // ************************************************************************** // |
| 170 | |
| 171 | void |
| 172 | depends_on::apply( test_unit& tu ) |
| 173 | { |
| 174 | #if !BOOST_TEST_SUPPORT_TOKEN_ITERATOR |
| 175 | BOOST_TEST_SETUP_ASSERT( false, "depends_on decorator is not supported on this platform" ); |
| 176 | #else |
| 177 | utils::string_token_iterator tit( m_dependency, (utils::dropped_delimeters = "/" , utils::kept_delimeters = utils::dt_none) ); |
| 178 | |
| 179 | test_unit* dep = &framework::master_test_suite(); |
| 180 | while( tit != utils::string_token_iterator() ) { |
| 181 | BOOST_TEST_SETUP_ASSERT( dep->p_type == TUT_SUITE, std::string( "incorrect dependency specification " ) + m_dependency ); |
| 182 | |
| 183 | test_unit_id next_id = static_cast<test_suite*>(dep)->get( tu_name: *tit ); |
| 184 | |
| 185 | BOOST_TEST_SETUP_ASSERT( next_id != INV_TEST_UNIT_ID, |
| 186 | std::string( "incorrect dependency specification " ) + m_dependency ); |
| 187 | |
| 188 | dep = &framework::get( tu_id: next_id, tu_type: TUT_ANY ); |
| 189 | ++tit; |
| 190 | } |
| 191 | |
| 192 | tu.depends_on( tu: dep ); |
| 193 | #endif |
| 194 | } |
| 195 | |
| 196 | //____________________________________________________________________________// |
| 197 | |
| 198 | // ************************************************************************** // |
| 199 | // ************** decorator::enable_if/enabled/disabled ************** // |
| 200 | // ************************************************************************** // |
| 201 | |
| 202 | void |
| 203 | enable_if_impl::apply_impl( test_unit& tu, bool condition ) |
| 204 | { |
| 205 | BOOST_TEST_SETUP_ASSERT(tu.p_default_status == test_unit::RS_INHERIT, |
| 206 | "Can't apply multiple enabled/disabled decorators " |
| 207 | "to the same test unit " + tu.full_name()); |
| 208 | |
| 209 | tu.p_default_status.value = condition ? test_unit::RS_ENABLED : test_unit::RS_DISABLED; |
| 210 | } |
| 211 | |
| 212 | //____________________________________________________________________________// |
| 213 | |
| 214 | // ************************************************************************** // |
| 215 | // ************** decorator::fixture ************** // |
| 216 | // ************************************************************************** // |
| 217 | |
| 218 | void |
| 219 | fixture_t::apply( test_unit& tu ) |
| 220 | { |
| 221 | tu.p_fixtures.value.push_back( x: m_impl ); |
| 222 | } |
| 223 | |
| 224 | //____________________________________________________________________________// |
| 225 | |
| 226 | // ************************************************************************** // |
| 227 | // ************** decorator::depends_on ************** // |
| 228 | // ************************************************************************** // |
| 229 | |
| 230 | void |
| 231 | precondition::apply( test_unit& tu ) |
| 232 | { |
| 233 | tu.add_precondition( m_precondition ); |
| 234 | } |
| 235 | |
| 236 | //____________________________________________________________________________// |
| 237 | |
| 238 | } // namespace decorator |
| 239 | } // namespace unit_test |
| 240 | } // namespace boost |
| 241 | |
| 242 | #include <boost/test/detail/enable_warnings.hpp> |
| 243 | |
| 244 | #endif // BOOST_TEST_TREE_DECORATOR_IPP_091911GER |
| 245 | |