Added support for boost::cref and boost::ref for mock::assign and mock::retrieve for uniformity

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@286 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2011-04-07 22:46:29 +00:00
parent f238f63086
commit f1b8550c84
2 changed files with 74 additions and 19 deletions

View file

@ -90,7 +90,7 @@ namespace detail
struct same struct same
{ {
explicit same( const Expected& expected ) explicit same( const Expected& expected )
: expected_( &expected ) : expected_( &boost::unwrap_ref( expected ) )
{} {}
template< typename Actual > template< typename Actual >
bool operator()( const Actual& actual ) const bool operator()( const Actual& actual ) const
@ -101,7 +101,8 @@ namespace detail
{ {
return os << "same( " << mock::format( *s.expected_ ) << " )"; return os << "same( " << mock::format( *s.expected_ ) << " )";
} }
const Expected* expected_; const BOOST_DEDUCED_TYPENAME
boost::unwrap_reference< Expected >::type* expected_;
}; };
template< typename Expected > template< typename Expected >
@ -111,10 +112,7 @@ namespace detail
: expected_( expected ) : expected_( expected )
{} {}
template< typename Actual > template< typename Actual >
bool operator()( Actual& actual, bool operator()( Actual& actual ) const
BOOST_DEDUCED_TYPENAME boost::disable_if<
boost::is_convertible< Expected*, Actual >
>::type* = 0 ) const
{ {
actual = boost::unwrap_ref( expected_ ); actual = boost::unwrap_ref( expected_ );
return true; return true;
@ -122,7 +120,11 @@ namespace detail
template< typename Actual > template< typename Actual >
bool operator()( Actual* actual, bool operator()( Actual* actual,
BOOST_DEDUCED_TYPENAME boost::enable_if< BOOST_DEDUCED_TYPENAME boost::enable_if<
boost::is_convertible< Expected, Actual > boost::is_convertible<
BOOST_DEDUCED_TYPENAME
boost::unwrap_reference< Expected >::type,
Actual
>
>::type* = 0 ) const >::type* = 0 ) const
{ {
*actual = boost::unwrap_ref( expected_ ); *actual = boost::unwrap_ref( expected_ );
@ -139,12 +141,16 @@ namespace detail
struct retrieve struct retrieve
{ {
explicit retrieve( Expected& expected ) explicit retrieve( Expected& expected )
: expected_( &expected ) : expected_( &boost::unwrap_ref( expected ) )
{} {}
template< typename Actual > template< typename Actual >
bool operator()( const Actual& actual, bool operator()( const Actual& actual,
BOOST_DEDUCED_TYPENAME boost::disable_if< BOOST_DEDUCED_TYPENAME boost::disable_if<
boost::is_convertible< const Actual*, Expected > boost::is_convertible<
const Actual*,
BOOST_DEDUCED_TYPENAME
boost::unwrap_reference< Expected >::type
>
>::type* = 0 ) const >::type* = 0 ) const
{ {
*expected_ = actual; *expected_ = actual;
@ -153,7 +159,10 @@ namespace detail
template< typename Actual > template< typename Actual >
bool operator()( Actual& actual, bool operator()( Actual& actual,
BOOST_DEDUCED_TYPENAME boost::enable_if< BOOST_DEDUCED_TYPENAME boost::enable_if<
boost::is_convertible< Actual*, Expected > boost::is_convertible< Actual*,
BOOST_DEDUCED_TYPENAME
boost::unwrap_reference< Expected >::type
>
>::type* = 0 ) const >::type* = 0 ) const
{ {
*expected_ = &actual; *expected_ = &actual;
@ -163,7 +172,8 @@ namespace detail
{ {
return s << "retrieve( " << mock::format( *r.expected_ ) << " )"; return s << "retrieve( " << mock::format( *r.expected_ ) << " )";
} }
Expected* expected_; BOOST_DEDUCED_TYPENAME
boost::unwrap_reference< Expected >::type* expected_;
}; };
template< typename Expected > template< typename Expected >

View file

@ -62,11 +62,25 @@ BOOST_AUTO_TEST_CASE( equal )
BOOST_AUTO_TEST_CASE( same ) 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 ).f_( j ) ); BOOST_CHECK( ! mock::same( i ).f_( j ) );
BOOST_CHECK( mock::same( i ).f_( i ) ); BOOST_CHECK( mock::same( i ).f_( i ) );
}
{
int i = 0;
int j = 0;
BOOST_CHECK_EQUAL( i, j );
mock::constraint<
mock::detail::same<
const boost::reference_wrapper< const int >
>
> c = mock::same( boost::cref( i ) );
BOOST_CHECK( ! c.f_( j ) );
BOOST_CHECK( c.f_( i ) );
}
} }
BOOST_AUTO_TEST_CASE( assign ) BOOST_AUTO_TEST_CASE( assign )
@ -85,32 +99,51 @@ BOOST_AUTO_TEST_CASE( assign )
const int* i = 0; const int* i = 0;
const int j = 1; const int j = 1;
BOOST_CHECK( mock::assign( &j ).f_( i ) ); BOOST_CHECK( mock::assign( &j ).f_( i ) );
BOOST_CHECK_EQUAL( i, &j ); BOOST_CHECK_EQUAL( &j, i );
} }
{ {
int i = 0; int i = 0;
int j = 0; int j = 1;
mock::constraint< mock::constraint<
mock::detail::assign< mock::detail::assign<
boost::reference_wrapper< const int > boost::reference_wrapper< const int >
> >
> c = mock::assign( boost::cref( j ) ); > c = mock::assign( boost::cref( j ) );
BOOST_CHECK( c.f_( i ) );
BOOST_CHECK_EQUAL( 1, i );
j = 3; j = 3;
BOOST_CHECK( c.f_( i ) ); BOOST_CHECK( c.f_( i ) );
BOOST_CHECK_EQUAL( 3, i ); BOOST_CHECK_EQUAL( 3, i );
} }
{ {
int i = 0; int i = 0;
int j = 0; int j = 1;
mock::constraint< mock::constraint<
mock::detail::assign< mock::detail::assign<
boost::reference_wrapper< const int > boost::reference_wrapper< const int >
> >
> c = mock::assign( boost::cref( j ) ); > c = mock::assign( boost::cref( j ) );
BOOST_CHECK( c.f_( &i ) );
BOOST_CHECK_EQUAL( 1, i );
j = 3; j = 3;
BOOST_CHECK( c.f_( &i ) ); BOOST_CHECK( c.f_( &i ) );
BOOST_CHECK_EQUAL( 3, i ); BOOST_CHECK_EQUAL( 3, i );
} }
{
const int* i = 0;
int k = 1;
int* j = &k;
mock::constraint<
mock::detail::assign<
boost::reference_wrapper< int* const >
>
> c = mock::assign( boost::cref( j ) );
BOOST_CHECK( c.f_( i ) );
BOOST_CHECK_EQUAL( j, i );
j = 0;
BOOST_CHECK( c.f_( i ) );
BOOST_CHECK_EQUAL( j, i );
}
} }
BOOST_AUTO_TEST_CASE( retrieve ) BOOST_AUTO_TEST_CASE( retrieve )
@ -163,6 +196,18 @@ BOOST_AUTO_TEST_CASE( retrieve )
BOOST_CHECK( mock::retrieve( i ).f_( j ) ); BOOST_CHECK( mock::retrieve( i ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j ); BOOST_CHECK_EQUAL( i, &j );
} }
{
int i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( boost::ref( i ) ).f_( j ) );
BOOST_CHECK_EQUAL( i, j );
}
{
const int* i = 0;
const int j = 1;
BOOST_CHECK( mock::retrieve( boost::ref( i ) ).f_( j ) );
BOOST_CHECK_EQUAL( i, &j );
}
} }
namespace namespace