mirror of
https://github.com/mat007/turtle.git
synced 2026-06-22 12:13:43 +00:00
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:
parent
135846946f
commit
60d5c263a6
2 changed files with 174 additions and 111 deletions
|
|
@ -32,32 +32,87 @@ namespace mock
|
||||||
typename ErrorPolicy = boost_test_error_policy<
|
typename ErrorPolicy = boost_test_error_policy<
|
||||||
BOOST_DEDUCED_TYPENAME boost::function<
|
BOOST_DEDUCED_TYPENAME boost::function<
|
||||||
Signature >::result_type > > // $$$$ MAT : concept check Signature is actually a signature
|
Signature >::result_type > > // $$$$ MAT : concept check Signature is actually a signature
|
||||||
class expectation : private verifiable
|
class expectation
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef BOOST_DEDUCED_TYPENAME
|
typedef BOOST_DEDUCED_TYPENAME
|
||||||
boost::function< Signature >::result_type result_type;
|
boost::function< Signature >::result_type result_type;
|
||||||
|
|
||||||
|
private:
|
||||||
typedef detail::matcher< result_type,
|
typedef detail::matcher< result_type,
|
||||||
Signature,
|
Signature,
|
||||||
ErrorPolicy,
|
ErrorPolicy,
|
||||||
boost::function< Signature >::arity >
|
boost::function< Signature >::arity >
|
||||||
matcher_type;
|
matcher_type;
|
||||||
|
|
||||||
|
public:
|
||||||
expectation( node& parent = root, const std::string& name = "?" )
|
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 )
|
: name_( name )
|
||||||
, parent_( &parent )
|
, parent_( &parent )
|
||||||
, valid_( true )
|
, valid_( true )
|
||||||
{
|
{
|
||||||
parent_->add( *this );
|
parent_->add( *this );
|
||||||
}
|
}
|
||||||
expectation( const std::string& name )
|
virtual ~expectation_impl()
|
||||||
: name_( name )
|
|
||||||
, parent_( &root )
|
|
||||||
, valid_( true )
|
|
||||||
{
|
|
||||||
parent_->add( *this );
|
|
||||||
}
|
|
||||||
virtual ~expectation()
|
|
||||||
{
|
{
|
||||||
parent_->remove( *this );
|
parent_->remove( *this );
|
||||||
if( ! std::uncaught_exception() )
|
if( ! std::uncaught_exception() )
|
||||||
|
|
@ -68,17 +123,15 @@ namespace mock
|
||||||
context(), it->file(), it->line() );
|
context(), it->file(), it->line() );
|
||||||
}
|
}
|
||||||
|
|
||||||
expectation& set_name( const std::string& name )
|
void set_name( const std::string& name )
|
||||||
{
|
{
|
||||||
name_ = name;
|
name_ = name;
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
expectation& set_parent( node& parent )
|
void set_parent( node& parent )
|
||||||
{
|
{
|
||||||
parent_->remove( *this );
|
parent_->remove( *this );
|
||||||
parent.add( *this );
|
parent.add( *this );
|
||||||
parent_ = &parent;
|
parent_ = &parent;
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool verify()
|
virtual bool verify()
|
||||||
|
|
@ -158,7 +211,7 @@ namespace mock
|
||||||
#undef MOCK_EXPECTATION_DETAIL
|
#undef MOCK_EXPECTATION_DETAIL
|
||||||
#undef MOCK_EXPECTATION_OPERATOR
|
#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();
|
return s << e.context();
|
||||||
}
|
}
|
||||||
|
|
@ -199,6 +252,10 @@ namespace mock
|
||||||
mutable bool valid_;
|
mutable bool valid_;
|
||||||
matchers_type matchers_;
|
matchers_type matchers_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
boost::shared_ptr< expectation_impl > impl_;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // #ifndef MOCK_EXPECTATION_HPP_INCLUDED
|
#endif // #ifndef MOCK_EXPECTATION_HPP_INCLUDED
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,12 @@ namespace
|
||||||
|
|
||||||
// functor
|
// 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 )
|
BOOST_AUTO_TEST_CASE( an_expectation_can_be_passed_as_functor_using_boost_bind_and_boost_ref )
|
||||||
{
|
{
|
||||||
mock::expectation< void() > e;
|
mock::expectation< void() > e;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue