Fixed action for pointer to const data as return type

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@770 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2015-03-01 11:28:47 +00:00
parent 88451b7c02
commit c37537401c
2 changed files with 78 additions and 72 deletions

View file

@ -437,6 +437,15 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_the_set_value, mock_e
BOOST_CHECK( ! f() ); BOOST_CHECK( ! f() );
CHECK_CALLS( 1 ); CHECK_CALLS( 1 );
} }
#ifdef BOOST_MSVC // this produces a warning with gcc
{
mock::detail::function< int* const() > f;
int i = 0;
f.expect().returns( &i );
BOOST_CHECK_EQUAL( &i, f() );
CHECK_CALLS( 1 );
}
#endif
{ {
mock::detail::function< int() > f; mock::detail::function< int() > f;
f.expect().returns( 0 ); f.expect().returns( 0 );
@ -570,20 +579,36 @@ BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_moves_the_set_unique_ptr_lval
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_moves_the_set_unique_ptr_rvalue, mock_error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_moves_the_set_unique_ptr_rvalue, mock_error_fixture )
{ {
{
mock::detail::function< std::unique_ptr< int >() > f; mock::detail::function< std::unique_ptr< int >() > f;
f.expect().moves( std::unique_ptr< int >( new int ) ); f.expect().moves( std::unique_ptr< int >( new int ) );
BOOST_CHECK_NO_THROW( f() ); BOOST_CHECK_NO_THROW( f() );
CHECK_CALLS( 1 ); CHECK_CALLS( 1 );
}
} }
#endif // MOCK_SMART_PTR #endif // MOCK_SMART_PTR
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_the_set_shared_ptr_value, mock_error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_the_set_shared_ptr_value, mock_error_fixture )
{ {
{
mock::detail::function< boost::shared_ptr< base >() > f; mock::detail::function< boost::shared_ptr< base >() > f;
f.expect().returns( new derived ); f.expect().returns( new derived );
BOOST_CHECK_NO_THROW( f() ); BOOST_CHECK_NO_THROW( f() );
CHECK_CALLS( 1 ); CHECK_CALLS( 1 );
}
{
mock::detail::function< const boost::shared_ptr< base >&() > f;
f.expect().returns( new derived );
BOOST_CHECK_NO_THROW( f() );
CHECK_CALLS( 1 );
}
{
mock::detail::function< boost::shared_ptr< base >&() > f;
f.expect().returns( new derived );
BOOST_CHECK_NO_THROW( f() );
CHECK_CALLS( 1 );
}
} }
BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_by_reference, mock_error_fixture ) BOOST_FIXTURE_TEST_CASE( triggering_an_expectation_returns_by_reference, mock_error_fixture )

View file

@ -91,16 +91,7 @@ namespace detail
template< typename Value > template< typename Value >
void returns( const Value& v ) void returns( const Value& v )
{ {
returns( boost::ref( v_.store( v ) ) ); this->set( boost::ref( store( v ) ) );
}
template< typename Value >
void returns( Value* v )
{
typedef typename
boost::remove_reference<
typename boost::remove_const< Result >::type
>::type result_type;
returns( result_type( v ) );
} }
template< typename Y > template< typename Y >
void returns( const boost::reference_wrapper< Y >& r ) void returns( const boost::reference_wrapper< Y >& r )
@ -114,68 +105,58 @@ namespace detail
this->set( this->set(
boost::bind( boost::bind(
&boost::move< BOOST_RV_REF( Value ) >, &boost::move< BOOST_RV_REF( Value ) >,
boost::ref( v_.store( boost::move( v ) ) ) ) ); boost::ref( store( boost::move( v ) ) ) ) );
} }
private: private:
struct value struct value : boost::noncopyable
{ {
struct holder : boost::noncopyable virtual ~value()
{
virtual ~holder()
{} {}
}; };
template< typename T > template< typename T >
struct holder_imp : holder struct value_imp : value
{ {
holder_imp( BOOST_RV_REF( T ) t ) typedef
typename boost::remove_const<
typename boost::remove_reference<
T
>::type
>::type value_type;
value_imp( BOOST_RV_REF( value_type ) t )
: t_( boost::move( t ) ) : t_( boost::move( t ) )
{} {}
holder_imp( const T& t ) value_imp( const T& t )
: t_( t ) : t_( t )
{} {}
T t_; template< typename Y >
value_imp( Y* y )
: t_( y )
{}
value_type t_;
}; };
template< typename T > template< typename T >
T& store( BOOST_RV_REF( T ) t ) T& store( BOOST_RV_REF( T ) t )
{ {
h_.reset( new holder_imp< T >( boost::move( t ) ) ); v_.reset( new value_imp< T >( boost::move( t ) ) );
return static_cast< holder_imp< T >& >( *h_ ).t_; return static_cast< value_imp< T >& >( *v_ ).t_;
} }
template< typename T > template< typename T >
T& store( const T& t ) T& store( const T& t )
{ {
h_.reset( new holder_imp< T >( t ) ); v_.reset( new value_imp< T >( t ) );
return static_cast< holder_imp< T >& >( *h_ ).t_; return static_cast< value_imp< T >& >( *v_ ).t_;
} }
boost::shared_ptr< holder > h_;
};
value v_;
};
template< typename Result, typename Signature >
class action< Result*, Signature >
: public action_base< Result*, Signature >
{
public:
void returns( Result* r )
{
this->set( boost::bind( &do_val< Result* >, r ) );
}
template< typename Y >
void returns( const boost::reference_wrapper< Y >& r )
{
this->set( r );
}
private:
template< typename T > template< typename T >
static T do_val( T t ) Result& store( T* t )
{ {
return t; v_.reset( new value_imp< Result >( t ) );
return static_cast< value_imp< Result >& >( *v_ ).t_;
} }
boost::shared_ptr< value > v_;
}; };
template< typename Signature > template< typename Signature >
@ -210,13 +191,13 @@ namespace detail
void returns( Y* r ) void returns( Y* r )
{ {
v_.reset( r ); v_.reset( r );
returns( boost::ref( v_ ) ); this->set( boost::ref( v_ ) );
} }
template< typename Y > template< typename Y >
void returns( std::auto_ptr< Y > r ) void returns( std::auto_ptr< Y > r )
{ {
v_ = r; v_ = r;
returns( boost::ref( v_ ) ); this->set( boost::ref( v_ ) );
} }
template< typename Y > template< typename Y >
void returns( const boost::reference_wrapper< Y >& r ) void returns( const boost::reference_wrapper< Y >& r )