An expectation is a functor

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@18 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2009-09-01 12:25:40 +00:00
parent 135846946f
commit 60d5c263a6
2 changed files with 174 additions and 111 deletions

View file

@ -32,32 +32,87 @@ namespace mock
typename ErrorPolicy = boost_test_error_policy<
BOOST_DEDUCED_TYPENAME boost::function<
Signature >::result_type > > // $$$$ MAT : concept check Signature is actually a signature
class expectation : private verifiable
class expectation
{
public:
typedef BOOST_DEDUCED_TYPENAME
boost::function< Signature >::result_type result_type;
private:
typedef detail::matcher< result_type,
Signature,
ErrorPolicy,
boost::function< Signature >::arity >
matcher_type;
public:
expectation( node& parent = root, const std::string& name = "?" )
: impl_( new expectation_impl( parent, name ) )
{}
expectation( const std::string& name )
: impl_( new expectation_impl( root, name ) )
{}
expectation& set_name( const std::string& name )
{
impl_->set_name( name );
return *this;
}
expectation& set_parent( node& parent )
{
impl_->set_parent( parent );
return *this;
}
bool verify()
{
return impl_->verify();
}
void reset()
{
impl_->reset();
}
matcher_type& expect( const std::string& file, int line )
{
return impl_->expect( file, line );
}
matcher_type& expect()
{
return impl_->expect();
}
result_type operator()() const
{
return (*impl_)();
}
#define MOCK_EXPECTATION_OPERATOR(z, n, d) \
template< BOOST_PP_ENUM_PARAMS(n, typename A) > \
result_type operator()( BOOST_PP_ENUM_BINARY_PARAMS(n, const A, & a) ) const \
{ \
return (*impl_)( BOOST_PP_ENUM_PARAMS(n, a) ); \
}
BOOST_PP_REPEAT_FROM_TO(1, MOCK_MAX_ARGS, MOCK_EXPECTATION_OPERATOR, BOOST_PP_EMPTY)
#undef MOCK_EXPECTATION_OPERATOR
friend std::ostream& operator<<( std::ostream& s, const expectation& e )
{
return s << *e.impl_;
}
private:
class expectation_impl : private verifiable
{
public:
expectation_impl( node& parent, const std::string& name )
: name_( name )
, parent_( &parent )
, valid_( true )
{
parent_->add( *this );
}
expectation( const std::string& name )
: name_( name )
, parent_( &root )
, valid_( true )
{
parent_->add( *this );
}
virtual ~expectation()
virtual ~expectation_impl()
{
parent_->remove( *this );
if( ! std::uncaught_exception() )
@ -68,17 +123,15 @@ namespace mock
context(), it->file(), it->line() );
}
expectation& set_name( const std::string& name )
void set_name( const std::string& name )
{
name_ = name;
return *this;
}
expectation& set_parent( node& parent )
void set_parent( node& parent )
{
parent_->remove( *this );
parent.add( *this );
parent_ = &parent;
return *this;
}
virtual bool verify()
@ -158,7 +211,7 @@ namespace mock
#undef MOCK_EXPECTATION_DETAIL
#undef MOCK_EXPECTATION_OPERATOR
friend std::ostream& operator<<( std::ostream& s, const expectation& e )
friend std::ostream& operator<<( std::ostream& s, const expectation_impl& e )
{
return s << e.context();
}
@ -199,6 +252,10 @@ namespace mock
mutable bool valid_;
matchers_type matchers_;
};
private:
boost::shared_ptr< expectation_impl > impl_;
};
}
#endif // #ifndef MOCK_EXPECTATION_HPP_INCLUDED

View file

@ -26,6 +26,12 @@ namespace
// functor
BOOST_AUTO_TEST_CASE( an_expectation_can_be_passed_as_functor )
{
mock::expectation< void() > e;
boost::function< void() > f = e;
}
BOOST_AUTO_TEST_CASE( an_expectation_can_be_passed_as_functor_using_boost_bind_and_boost_ref )
{
mock::expectation< void() > e;