Refactoring

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@135 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2010-03-02 22:13:59 +00:00
parent dad638f4ab
commit 3109a228f0
5 changed files with 76 additions and 80 deletions

View file

@ -24,19 +24,21 @@ namespace mock
{ {
namespace detail namespace detail
{ {
template< typename Constraint, typename Actual > template< typename Functor, typename Actual >
struct ConstraintCompatible : private boost::noncopyable struct FunctorCompatible : private boost::noncopyable
{ {
public: public:
BOOST_CONCEPT_USAGE( ConstraintCompatible ) BOOST_CONCEPT_USAGE( FunctorCompatible )
{ {
boost::require_boolean_expr(
// if an error is generated by the line below it means an argument // if an error is generated by the line below it means an argument
// passed to 'with' was of the wrong type. // passed to 'with' was of the wrong type.
constraint_accepts( actual_argument_type ); functor_accepts( actual_argument_type )
);
} }
private: private:
ConstraintCompatible( int ) {} FunctorCompatible( int ) {}
Constraint constraint_accepts; Functor functor_accepts;
Actual actual_argument_type; Actual actual_argument_type;
}; };
@ -61,20 +63,17 @@ namespace detail
template< typename Actual > template< typename Actual >
class check class check
{ {
typedef BOOST_DEDUCED_TYPENAME
boost::function< bool( Actual ) > constraint_type;
public: public:
template< typename Constraint > template< typename Functor >
explicit check( const Constraint& constraint, explicit check( const Functor& f,
BOOST_DEDUCED_TYPENAME boost::enable_if< BOOST_DEDUCED_TYPENAME boost::enable_if<
BOOST_DEDUCED_TYPENAME detail::is_functor< Constraint > BOOST_DEDUCED_TYPENAME detail::is_functor< Functor >
>::type* = 0 ) >::type* = 0 )
: desc_( "?" ) : desc_( "?" )
{ {
BOOST_CONCEPT_ASSERT(( ConstraintCompatible< Constraint, Actual > )); BOOST_CONCEPT_ASSERT(( FunctorCompatible< Functor, Actual > ));
constraint_ = constraint; f_ = f;
if( !constraint_ ) if( ! f_ )
std::invalid_argument( "invalid constraint" ); std::invalid_argument( "invalid constraint" );
} }
template< typename Expected > template< typename Expected >
@ -85,23 +84,23 @@ namespace detail
: desc_( format( expected ) ) : desc_( format( expected ) )
{ {
BOOST_CONCEPT_ASSERT(( EqualityComparable< Expected, Actual > )); BOOST_CONCEPT_ASSERT(( EqualityComparable< Expected, Actual > ));
constraint_ = mock::equal( expected ).constraint_; f_ = mock::equal( expected ).f_;
if( !constraint_ ) if( ! f_ )
std::invalid_argument( "invalid constraint" ); std::invalid_argument( "invalid constraint" );
} }
template< typename Constraint > template< typename Functor >
explicit check( const placeholder< Constraint >& ph ) explicit check( const placeholder< Functor >& ph )
: desc_( ph.desc_ ) : desc_( ph.desc_ )
{ {
BOOST_CONCEPT_ASSERT(( ConstraintCompatible< Constraint, Actual > )); BOOST_CONCEPT_ASSERT(( FunctorCompatible< Functor, Actual > ));
constraint_ = ph.constraint_; f_ = ph.f_;
if( !constraint_ ) if( ! f_ )
std::invalid_argument( "invalid constraint" ); std::invalid_argument( "invalid constraint" );
} }
bool operator()( Actual actual ) const bool operator()( Actual actual ) const
{ {
return constraint_( actual ); return f_( actual );
} }
friend std::ostream& operator<<( std::ostream& s, const check& c ) friend std::ostream& operator<<( std::ostream& s, const check& c )
@ -110,7 +109,7 @@ namespace detail
} }
private: private:
constraint_type constraint_; boost::function< bool( Actual ) > f_;
std::string desc_; std::string desc_;
}; };
} }

View file

@ -46,7 +46,7 @@ namespace detail
placeholder() placeholder()
: desc_( "any" ) : desc_( "any" )
{} {}
any constraint_; any f_;
std::string desc_; std::string desc_;
}; };
template<> template<>
@ -55,7 +55,7 @@ namespace detail
placeholder() placeholder()
: desc_( "negate" ) : desc_( "negate" )
{} {}
negate constraint_; negate f_;
std::string desc_; std::string desc_;
}; };
template<> template<>
@ -64,7 +64,7 @@ namespace detail
placeholder() placeholder()
: desc_( "evaluate" ) : desc_( "evaluate" )
{} {}
evaluate constraint_; evaluate f_;
std::string desc_; std::string desc_;
}; };
} }
@ -102,7 +102,7 @@ namespace detail
detail::or_< detail::less< T >, detail::equal< T > > > detail::or_< detail::less< T >, detail::equal< T > > >
less_equal( T t ) less_equal( T t )
{ {
return constraint( (less( t ) || equal( t )).constraint_, return constraint( (less( t ) || equal( t )).f_,
"less_equal", t ); "less_equal", t );
} }
@ -111,7 +111,7 @@ namespace detail
detail::or_< detail::greater< T >, detail::equal< T > > > detail::or_< detail::greater< T >, detail::equal< T > > >
greater_equal( T t ) greater_equal( T t )
{ {
return constraint( (greater( t ) || equal( t )).constraint_, return constraint( (greater( t ) || equal( t )).f_,
"greater_equal", t ); "greater_equal", t );
} }

View file

@ -15,67 +15,67 @@ namespace mock
{ {
namespace detail namespace detail
{ {
template< typename Constraint > template< typename Functor >
struct placeholder struct placeholder
{ {
placeholder( const Constraint& c, const std::string& desc ) placeholder( const Functor& f, const std::string& desc )
: constraint_( c ) : f_( f )
, desc_( desc ) , desc_( desc )
{} {}
Constraint constraint_; Functor f_;
std::string desc_; std::string desc_;
}; };
template< typename Constraint1, typename Constraint2 > template< typename Functor1, typename Functor2 >
class and_ class and_
{ {
public: public:
and_( const Constraint1& c1, const Constraint2& c2 ) and_( const Functor1& f1, const Functor2& f2 )
: c1_( c1 ) : f1_( f1 )
, c2_( c2 ) , f2_( f2 )
{} {}
template< typename Actual > template< typename Actual >
bool operator()( const Actual& actual ) const bool operator()( const Actual& actual ) const
{ {
return c1_( actual ) && c2_( actual ); return f1_( actual ) && f2_( actual );
} }
private: private:
Constraint1 c1_; Functor1 f1_;
Constraint2 c2_; Functor2 f2_;
}; };
template< typename Constraint1, typename Constraint2 > template< typename Functor1, typename Functor2 >
class or_ class or_
{ {
public: public:
or_( const Constraint1& c1, const Constraint2& c2 ) or_( const Functor1& f1, const Functor2& f2 )
: c1_( c1 ) : f1_( f1 )
, c2_( c2 ) , f2_( f2 )
{} {}
template< typename Actual > template< typename Actual >
bool operator()( const Actual& actual ) const bool operator()( const Actual& actual ) const
{ {
return c1_( actual ) || c2_( actual ); return f1_( actual ) || f2_( actual );
} }
private: private:
Constraint1 c1_; Functor1 f1_;
Constraint2 c2_; Functor2 f2_;
}; };
template< typename Constraint > template< typename Functor >
class not_ class not_
{ {
public: public:
explicit not_( const Constraint& c ) explicit not_( const Functor& f )
: c_( c ) : f_( f )
{} {}
template< typename Actual > template< typename Actual >
bool operator()( const Actual& actual ) const bool operator()( const Actual& actual ) const
{ {
return ! c_( actual ); return ! f_( actual );
} }
private: private:
Constraint c_; Functor f_;
}; };
template< typename F1, typename F2 > template< typename F1, typename F2 >
@ -84,7 +84,7 @@ namespace detail
const placeholder< F2 >& rhs ) const placeholder< F2 >& rhs )
{ {
return placeholder< or_< F1, F2 > >( return placeholder< or_< F1, F2 > >(
or_< F1, F2 >( lhs.constraint_, rhs.constraint_ ), or_< F1, F2 >( lhs.f_, rhs.f_ ),
"(" + lhs.desc_ + " || " + rhs.desc_ + ")" ); "(" + lhs.desc_ + " || " + rhs.desc_ + ")" );
} }
@ -94,7 +94,7 @@ namespace detail
const placeholder< F2 >& rhs ) const placeholder< F2 >& rhs )
{ {
return placeholder< and_< F1, F2 > >( return placeholder< and_< F1, F2 > >(
and_< F1, F2 >( lhs.constraint_, rhs.constraint_ ), and_< F1, F2 >( lhs.f_, rhs.f_ ),
"(" + lhs.desc_ + " && " + rhs.desc_ + ")" ); "(" + lhs.desc_ + " && " + rhs.desc_ + ")" );
} }
@ -103,7 +103,7 @@ namespace detail
operator!( const placeholder< F >& ph ) operator!( const placeholder< F >& ph )
{ {
return placeholder< not_< F > >( return placeholder< not_< F > >(
not_< F >( ph.constraint_ ), "! " + ph.desc_ ); not_< F >( ph.f_ ), "! " + ph.desc_ );
} }
} }
} }

View file

@ -48,21 +48,21 @@ BOOST_AUTO_TEST_CASE( same )
int i = 0; int i = 0;
int j = 0; int j = 0;
BOOST_CHECK_EQUAL( i, j ); BOOST_CHECK_EQUAL( i, j );
BOOST_CHECK( ! mock::same( i ).constraint_( j ) ); BOOST_CHECK( ! mock::same( i ).f_( j ) );
BOOST_CHECK( mock::same( i ).constraint_( i ) ); BOOST_CHECK( mock::same( i ).f_( i ) );
} }
BOOST_AUTO_TEST_CASE( assign ) BOOST_AUTO_TEST_CASE( assign )
{ {
{ {
int i = 0; int i = 0;
BOOST_CHECK( mock::assign( 3 ).constraint_( i ) ); BOOST_CHECK( mock::assign( 3 ).f_( i ) );
BOOST_CHECK_EQUAL( 3, i ); BOOST_CHECK_EQUAL( 3, i );
} }
{ {
const int* i = 0; const int* i = 0;
const int j = 1; const int j = 1;
BOOST_CHECK( mock::assign( &j ).constraint_( i ) ); BOOST_CHECK( mock::assign( &j ).f_( i ) );
BOOST_CHECK_EQUAL( i, &j ); BOOST_CHECK_EQUAL( i, &j );
} }
} }
@ -72,49 +72,49 @@ BOOST_AUTO_TEST_CASE( retrieve )
{ {
int i = 0; int i = 0;
const int j = 1; const int j = 1;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) ); BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, j ); BOOST_CHECK_EQUAL( i, j );
} }
{ {
int* i = 0; int* i = 0;
int j = 1; int j = 1;
BOOST_CHECK( mock::retrieve( i ).constraint_( &j ) ); BOOST_CHECK( mock::retrieve( i ).f_( &j ) );
BOOST_CHECK_EQUAL( i, &j ); BOOST_CHECK_EQUAL( i, &j );
} }
{ {
const int* i = 0; const int* i = 0;
const int j = 1; const int j = 1;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) ); BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j ); BOOST_CHECK_EQUAL( i, &j );
} }
{ {
const int* i = 0; const int* i = 0;
int j = 1; int j = 1;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) ); BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j ); BOOST_CHECK_EQUAL( i, &j );
} }
{ {
int* i = 0; int* i = 0;
int j = 1; int j = 1;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) ); BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j ); BOOST_CHECK_EQUAL( i, &j );
} }
{ {
const int* i = 0; const int* i = 0;
const int j = 1; const int j = 1;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) ); BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j ); BOOST_CHECK_EQUAL( i, &j );
} }
{ {
int** i = 0; int** i = 0;
int* j = 0; int* j = 0;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) ); BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j ); BOOST_CHECK_EQUAL( i, &j );
} }
{ {
const int** i = 0; const int** i = 0;
const int* j = 0; const int* j = 0;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) ); BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j ); BOOST_CHECK_EQUAL( i, &j );
} }
} }
@ -137,15 +137,15 @@ BOOST_AUTO_TEST_CASE( retrieve_uses_assignment_operator )
{ {
B b; B b;
const A a = A(); const A a = A();
mock::retrieve( b ).constraint_( a ); mock::retrieve( b ).f_( a );
} }
BOOST_AUTO_TEST_CASE( negate ) BOOST_AUTO_TEST_CASE( negate )
{ {
int* i = 0; int* i = 0;
int j; int j;
BOOST_CHECK( mock::negate.constraint_( i ) ); BOOST_CHECK( mock::negate.f_( i ) );
BOOST_CHECK( ! mock::negate.constraint_( &j ) ); BOOST_CHECK( ! mock::negate.f_( &j ) );
} }
namespace namespace
@ -162,12 +162,12 @@ namespace
BOOST_AUTO_TEST_CASE( evaluate ) BOOST_AUTO_TEST_CASE( evaluate )
{ {
BOOST_CHECK( mock::evaluate.constraint_( &return_true ) ); BOOST_CHECK( mock::evaluate.f_( &return_true ) );
BOOST_CHECK( ! mock::evaluate.constraint_( &return_false ) ); BOOST_CHECK( ! mock::evaluate.f_( &return_false ) );
} }
BOOST_AUTO_TEST_CASE( contain ) BOOST_AUTO_TEST_CASE( contain )
{ {
BOOST_CHECK( mock::contain( "string" ).constraint_( "this is a string" ) ); BOOST_CHECK( mock::contain( "string" ).f_( "this is a string" ) );
BOOST_CHECK( ! mock::contain( "not found" ).constraint_( "this is a string" ) ); BOOST_CHECK( ! mock::contain( "not found" ).f_( "this is a string" ) );
} }

View file

@ -66,9 +66,6 @@ BOOST_AUTO_TEST_CASE( basic_mock_object_usage )
BOOST_CHECK_EQUAL( 0, m.my_method( 13 ) ); BOOST_CHECK_EQUAL( 0, m.my_method( 13 ) );
mock::verify(); mock::verify();
mock::reset(); mock::reset();
// MOCK_EXPECT( m, my_method ).once().with( &f ).returns( 7 );
// MOCK_EXPECT( m, my_method ).once().with( mock::equal( "" ) ).returns( 7 );
// MOCK_EXPECT( m, my_method ).once().with( "" ).returns( 7 );
MOCK_EXPECT( m, my_method ).once().with( 42 ).returns( 7 ); MOCK_EXPECT( m, my_method ).once().with( 42 ).returns( 7 );
BOOST_CHECK_EQUAL( 7, m.my_method( 42 ) ); BOOST_CHECK_EQUAL( 7, m.my_method( 42 ) );
mock::verify(); mock::verify();