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

View file

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

View file

@ -15,67 +15,67 @@ namespace mock
{
namespace detail
{
template< typename Constraint >
template< typename Functor >
struct placeholder
{
placeholder( const Constraint& c, const std::string& desc )
: constraint_( c )
, desc_ ( desc )
placeholder( const Functor& f, const std::string& desc )
: f_( f )
, desc_( desc )
{}
Constraint constraint_;
Functor f_;
std::string desc_;
};
template< typename Constraint1, typename Constraint2 >
template< typename Functor1, typename Functor2 >
class and_
{
public:
and_( const Constraint1& c1, const Constraint2& c2 )
: c1_( c1 )
, c2_( c2 )
and_( const Functor1& f1, const Functor2& f2 )
: f1_( f1 )
, f2_( f2 )
{}
template< typename Actual >
bool operator()( const Actual& actual ) const
{
return c1_( actual ) && c2_( actual );
return f1_( actual ) && f2_( actual );
}
private:
Constraint1 c1_;
Constraint2 c2_;
Functor1 f1_;
Functor2 f2_;
};
template< typename Constraint1, typename Constraint2 >
template< typename Functor1, typename Functor2 >
class or_
{
public:
or_( const Constraint1& c1, const Constraint2& c2 )
: c1_( c1 )
, c2_( c2 )
or_( const Functor1& f1, const Functor2& f2 )
: f1_( f1 )
, f2_( f2 )
{}
template< typename Actual >
bool operator()( const Actual& actual ) const
{
return c1_( actual ) || c2_( actual );
return f1_( actual ) || f2_( actual );
}
private:
Constraint1 c1_;
Constraint2 c2_;
Functor1 f1_;
Functor2 f2_;
};
template< typename Constraint >
template< typename Functor >
class not_
{
public:
explicit not_( const Constraint& c )
: c_( c )
explicit not_( const Functor& f )
: f_( f )
{}
template< typename Actual >
bool operator()( const Actual& actual ) const
{
return ! c_( actual );
return ! f_( actual );
}
private:
Constraint c_;
Functor f_;
};
template< typename F1, typename F2 >
@ -84,7 +84,7 @@ namespace detail
const placeholder< F2 >& rhs )
{
return placeholder< or_< F1, F2 > >(
or_< F1, F2 >( lhs.constraint_, rhs.constraint_ ),
or_< F1, F2 >( lhs.f_, rhs.f_ ),
"(" + lhs.desc_ + " || " + rhs.desc_ + ")" );
}
@ -94,7 +94,7 @@ namespace detail
const placeholder< F2 >& rhs )
{
return placeholder< and_< F1, F2 > >(
and_< F1, F2 >( lhs.constraint_, rhs.constraint_ ),
and_< F1, F2 >( lhs.f_, rhs.f_ ),
"(" + lhs.desc_ + " && " + rhs.desc_ + ")" );
}
@ -103,7 +103,7 @@ namespace detail
operator!( const placeholder< F >& ph )
{
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 j = 0;
BOOST_CHECK_EQUAL( i, j );
BOOST_CHECK( ! mock::same( i ).constraint_( j ) );
BOOST_CHECK( mock::same( i ).constraint_( i ) );
BOOST_CHECK( ! mock::same( i ).f_( j ) );
BOOST_CHECK( mock::same( i ).f_( i ) );
}
BOOST_AUTO_TEST_CASE( assign )
{
{
int i = 0;
BOOST_CHECK( mock::assign( 3 ).constraint_( i ) );
BOOST_CHECK( mock::assign( 3 ).f_( i ) );
BOOST_CHECK_EQUAL( 3, i );
}
{
const int* i = 0;
const int j = 1;
BOOST_CHECK( mock::assign( &j ).constraint_( i ) );
BOOST_CHECK( mock::assign( &j ).f_( i ) );
BOOST_CHECK_EQUAL( i, &j );
}
}
@ -72,49 +72,49 @@ BOOST_AUTO_TEST_CASE( retrieve )
{
int i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) );
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, j );
}
{
int* i = 0;
int j = 1;
BOOST_CHECK( mock::retrieve( i ).constraint_( &j ) );
BOOST_CHECK( mock::retrieve( i ).f_( &j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
const int* i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) );
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
const int* i = 0;
int j = 1;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) );
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
int* i = 0;
int j = 1;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) );
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
const int* i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) );
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
int** i = 0;
int* j = 0;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) );
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
{
const int** i = 0;
const int* j = 0;
BOOST_CHECK( mock::retrieve( i ).constraint_( j ) );
BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
}
@ -137,15 +137,15 @@ BOOST_AUTO_TEST_CASE( retrieve_uses_assignment_operator )
{
B b;
const A a = A();
mock::retrieve( b ).constraint_( a );
mock::retrieve( b ).f_( a );
}
BOOST_AUTO_TEST_CASE( negate )
{
int* i = 0;
int j;
BOOST_CHECK( mock::negate.constraint_( i ) );
BOOST_CHECK( ! mock::negate.constraint_( &j ) );
BOOST_CHECK( mock::negate.f_( i ) );
BOOST_CHECK( ! mock::negate.f_( &j ) );
}
namespace
@ -162,12 +162,12 @@ namespace
BOOST_AUTO_TEST_CASE( evaluate )
{
BOOST_CHECK( mock::evaluate.constraint_( &return_true ) );
BOOST_CHECK( ! mock::evaluate.constraint_( &return_false ) );
BOOST_CHECK( mock::evaluate.f_( &return_true ) );
BOOST_CHECK( ! mock::evaluate.f_( &return_false ) );
}
BOOST_AUTO_TEST_CASE( contain )
{
BOOST_CHECK( mock::contain( "string" ).constraint_( "this is a string" ) );
BOOST_CHECK( ! mock::contain( "not found" ).constraint_( "this is a string" ) );
BOOST_CHECK( mock::contain( "string" ).f_( "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 ) );
mock::verify();
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 );
BOOST_CHECK_EQUAL( 7, m.my_method( 42 ) );
mock::verify();