Refactoring

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@133 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2010-03-01 23:13:43 +00:00
parent a21b3d2c2f
commit 9a6c43ca6b
3 changed files with 54 additions and 52 deletions

View file

@ -28,15 +28,15 @@ namespace mock
{ {
namespace detail namespace detail
{ {
class matcher_base : private orderable class expectation_base : private orderable
{ {
public: public:
matcher_base() expectation_base()
: i_( new detail::unlimited() ) : i_( new detail::unlimited() )
, file_( "unknown location" ) , file_( "unknown location" )
, line_( 0 ) , line_( 0 )
{} {}
virtual ~matcher_base() virtual ~expectation_base()
{ {
for( sequences_cit it = sequences_.begin(); for( sequences_cit it = sequences_.begin();
it != sequences_.end(); ++it ) it != sequences_.end(); ++it )
@ -149,7 +149,7 @@ namespace detail
template< typename Result, typename Signature > template< typename Result, typename Signature >
class expectation< Result, Signature, 0 > class expectation< Result, Signature, 0 >
: public matcher_base, public action< Result, Signature > : public expectation_base, public action< Result, Signature >
{ {
public: public:
bool is_valid() const bool is_valid() const
@ -182,7 +182,7 @@ namespace detail
#define MOCK_EXPECTATION(z, n, d) \ #define MOCK_EXPECTATION(z, n, d) \
template< typename Result, typename Signature > \ template< typename Result, typename Signature > \
class expectation< Result, Signature, n > \ class expectation< Result, Signature, n > \
: public matcher_base, public action< Result, Signature > \ : public expectation_base, public action< Result, Signature > \
{ \ { \
BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_TYPEDEF, BOOST_PP_EMPTY) \ BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_TYPEDEF, BOOST_PP_EMPTY) \
public: \ public: \

View file

@ -51,7 +51,8 @@ namespace mock
}; };
private: private:
typedef detail::expectation< result_type, Signature, arity::value > matcher_type; typedef detail::expectation< result_type, Signature, arity::value >
expectation_type;
public: public:
struct expectation_tag struct expectation_tag
@ -84,11 +85,11 @@ namespace mock
impl_->reset(); impl_->reset();
} }
matcher_type& expect( const std::string& file, int line ) expectation_type& expect( const std::string& file, int line )
{ {
return impl_->expect( file, line ); return impl_->expect( file, line );
} }
matcher_type& expect() expectation_type& expect()
{ {
return impl_->expect(); return impl_->expect();
} }
@ -131,8 +132,8 @@ namespace mock
{ {
parent_->remove( *this ); parent_->remove( *this );
if( ! std::uncaught_exception() ) if( ! std::uncaught_exception() )
for( matchers_cit it = matchers_.begin(); for( expectations_cit it = expectations_.begin();
it != matchers_.end(); ++it ) it != expectations_.end(); ++it )
if( valid_ && ! it->verify() ) if( valid_ && ! it->verify() )
ErrorPolicy::untriggered_expectation( ErrorPolicy::untriggered_expectation(
context(), it->file(), it->line() ); context(), it->file(), it->line() );
@ -155,8 +156,8 @@ namespace mock
virtual bool verify() const virtual bool verify() const
{ {
for( matchers_cit it = matchers_.begin(); for( expectations_cit it = expectations_.begin();
it != matchers_.end(); ++it ) it != expectations_.end(); ++it )
if( !it->verify() ) if( !it->verify() )
{ {
valid_ = false; valid_ = false;
@ -168,21 +169,21 @@ namespace mock
virtual void reset() virtual void reset()
{ {
valid_ = true; valid_ = true;
matchers_.clear(); expectations_.clear();
} }
matcher_type& expect( const std::string& file, int line ) expectation_type& expect( const std::string& file, int line )
{ {
matchers_.push_back( matcher_type() ); expectations_.push_back( expectation_type() );
matchers_.back().set_location( file, line ); expectations_.back().set_location( file, line );
valid_ = true; valid_ = true;
return matchers_.back(); return expectations_.back();
} }
matcher_type& expect() expectation_type& expect()
{ {
matchers_.push_back( matcher_type() ); expectations_.push_back( expectation_type() );
valid_ = true; valid_ = true;
return matchers_.back(); return expectations_.back();
} }
struct no_throw_abort struct no_throw_abort
@ -199,13 +200,14 @@ namespace mock
return invoke< ErrorPolicy >(); return invoke< ErrorPolicy >();
} }
#define MOCK_EXPECTATION_FORMAT(z, n, d) BOOST_PP_IF(n, + ", " +,) format( p##n ) #define MOCK_EXPECTATION_FORMAT(z, n, d) \
BOOST_PP_IF(n, + ", " +,) format( p##n )
#define MOCK_EXPECTATION_CONTEXT(n) \ #define MOCK_EXPECTATION_CONTEXT(n) \
context( BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_FORMAT, BOOST_PP_EMPTY) ) context( BOOST_PP_REPEAT_FROM_TO(0, n, MOCK_EXPECTATION_FORMAT, BOOST_PP_EMPTY) )
#define MOCK_EXPECTATION_OPERATOR(z, n, d) \ #define MOCK_EXPECTATION_OPERATOR(z, n, d) \
MOCK_DECL(operator(), n, Signature, const, BOOST_DEDUCED_TYPENAME) \ MOCK_DECL(operator(), n, Signature, const, BOOST_DEDUCED_TYPENAME) \
{ \ { \
for( matchers_cit it = matchers_.begin(); it != matchers_.end(); ++it ) \ for( expectations_cit it = expectations_.begin(); it != expectations_.end(); ++it ) \
if( it->is_valid( BOOST_PP_ENUM_PARAMS(n, p) ) ) \ if( it->is_valid( BOOST_PP_ENUM_PARAMS(n, p) ) ) \
{ \ { \
if( ! it->invoke() ) \ if( ! it->invoke() ) \
@ -239,8 +241,8 @@ namespace mock
template< typename T > template< typename T >
result_type invoke() const result_type invoke() const
{ {
for( matchers_cit it = matchers_.begin(); for( expectations_cit it = expectations_.begin();
it != matchers_.end(); ++it ) it != expectations_.end(); ++it )
if( it->is_valid() ) if( it->is_valid() )
{ {
if( ! it->invoke() ) if( ! it->invoke() )
@ -264,14 +266,14 @@ namespace mock
} }
private: private:
typedef std::list< matcher_type > matchers_type; typedef std::list< expectation_type > expectations_type;
typedef BOOST_DEDUCED_TYPENAME typedef BOOST_DEDUCED_TYPENAME
matchers_type::const_iterator matchers_cit; expectations_type::const_iterator expectations_cit;
void serialize( std::ostream& s ) const void serialize( std::ostream& s ) const
{ {
for( matchers_cit it = matchers_.begin(); for( expectations_cit it = expectations_.begin();
it != matchers_.end(); ++it ) it != expectations_.end(); ++it )
s << std::endl << *it; s << std::endl << *it;
} }
@ -297,7 +299,7 @@ namespace mock
std::string name_; std::string name_;
node* parent_; node* parent_;
mutable bool valid_; mutable bool valid_;
matchers_type matchers_; expectations_type expectations_;
}; };
private: private:

View file

@ -530,7 +530,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_throws_the_set_exception, err
BOOST_FAIL( "should have thrown" ); BOOST_FAIL( "should have thrown" );
} }
// multiple matchers // multiple expectations
BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_twice, error_fixture ) BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_twice, error_fixture )
{ {
@ -552,7 +552,7 @@ BOOST_FIXTURE_TEST_CASE( expecting_twice_a_single_expectation_makes_it_callable_
} }
} }
BOOST_FIXTURE_TEST_CASE( best_matcher_is_selected_first, error_fixture ) BOOST_FIXTURE_TEST_CASE( best_expectation_is_selected_first, error_fixture )
{ {
{ {
mock::function< void( int ) > e; mock::function< void( int ) > e;
@ -704,7 +704,7 @@ BOOST_FIXTURE_TEST_CASE( triggering_no_match_call_disables_the_automatic_verific
CHECK_ERROR( e(), no_match ); CHECK_ERROR( e(), no_match );
} }
BOOST_FIXTURE_TEST_CASE( adding_a_matcher_reactivates_the_verification_upon_destruction, error_fixture ) BOOST_FIXTURE_TEST_CASE( adding_a_expectation_reactivates_the_verification_upon_destruction, error_fixture )
{ {
std::auto_ptr< mock::function< void() > > e( new mock::function< void() > ); std::auto_ptr< mock::function< void() > > e( new mock::function< void() > );
CHECK_ERROR( (*e)(), no_match ); CHECK_ERROR( (*e)(), no_match );