Fixed move-only type argument in actions

Because boost::function does not move the parameters it receives we need to use std::function instead.
This commit is contained in:
Mathieu Champlon 2018-01-15 06:23:25 +01:00
parent 58b5e55bb5
commit d3a5d3010c
8 changed files with 40 additions and 22 deletions

View file

@ -13,6 +13,7 @@ Not yet released
* Fixed mocking of a function returning a reference for gcc 4.1 * Fixed mocking of a function returning a reference for gcc 4.1
* Added MOCK_NO_AUTO_PTR to deactivate std::auto_ptr support * Added MOCK_NO_AUTO_PTR to deactivate std::auto_ptr support
* Added [@https://github.com/philsquared/Catch Catch] integration * Added [@https://github.com/philsquared/Catch Catch] integration
* Fixed move-only type argument in actions
[endsect] [endsect]

View file

@ -27,8 +27,8 @@ namespace detail
class action_base class action_base
{ {
private: private:
typedef boost::function< Signature > functor_type; typedef std::function< Signature > functor_type;
typedef boost::function< Result() > action_type; typedef std::function< Result() > action_type;
public: public:
const functor_type& functor() const const functor_type& functor() const

View file

@ -23,6 +23,9 @@
#define MOCK_EXPECTATION_SERIALIZE_ANY(z, n, d) \ #define MOCK_EXPECTATION_SERIALIZE_ANY(z, n, d) \
BOOST_PP_IF(n, << ", " <<,) "any" BOOST_PP_IF(n, << ", " <<,) "any"
#define MOCK_CALL_PARAM_TYPE(z, n, d) \
typename boost::call_traits< T##n >::param_type
namespace mock namespace mock
{ {
namespace detail namespace detail
@ -36,7 +39,7 @@ namespace detail
{ {
private: private:
virtual bool operator()( virtual bool operator()(
BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, T) ) BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_CALL_PARAM_TYPE, _) )
{ {
return true; return true;
} }
@ -70,7 +73,7 @@ namespace detail
private: private:
virtual bool operator()( virtual bool operator()(
BOOST_PP_ENUM_BINARY_PARAMS(MOCK_NUM_ARGS, T, a) ) BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_CALL_PARAM, _) )
{ {
return BOOST_PP_REPEAT(MOCK_NUM_ARGS, return BOOST_PP_REPEAT(MOCK_NUM_ARGS,
MOCK_EXPECTATION_IS_VALID, _); MOCK_EXPECTATION_IS_VALID, _);
@ -100,7 +103,7 @@ namespace detail
private: private:
virtual bool operator()( virtual bool operator()(
BOOST_PP_ENUM_BINARY_PARAMS( MOCK_NUM_ARGS, T, a ) ) BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_CALL_PARAM, _) )
{ {
return f_( BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, a) ); return f_( BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, a) );
} }
@ -198,7 +201,7 @@ namespace detail
} }
bool is_valid( bool is_valid(
BOOST_PP_ENUM_BINARY_PARAMS(MOCK_NUM_ARGS, T, a) ) const BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_CALL_PARAM, _) ) const
{ {
return !invocation_->exhausted() return !invocation_->exhausted()
&& (*matcher_)( BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, a) ); && (*matcher_)( BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, a) );
@ -256,6 +259,7 @@ namespace detail
} }
} // mock } // mock
#undef MOCK_CALL_PARAM_TYPE
#undef MOCK_EXPECTATION_INITIALIZE #undef MOCK_EXPECTATION_INITIALIZE
#undef MOCK_EXPECTATION_MEMBER #undef MOCK_EXPECTATION_MEMBER
#undef MOCK_EXPECTATION_IS_VALID #undef MOCK_EXPECTATION_IS_VALID

View file

@ -33,6 +33,7 @@
#include <boost/call_traits.hpp> #include <boost/call_traits.hpp>
#include <boost/make_shared.hpp> #include <boost/make_shared.hpp>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <boost/move/move.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <ostream> #include <ostream>
#include <vector> #include <vector>

View file

@ -235,7 +235,7 @@ namespace detail
MOCK_FUNCTION_CONTEXT, it->file(), it->line() ); MOCK_FUNCTION_CONTEXT, it->file(), it->line() );
if( it->functor() ) if( it->functor() )
return it->functor()( return it->functor()(
BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, t) ); BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_FORWARD, _) );
return it->trigger(); return it->trigger();
} }
error_type::fail( "unexpected call", MOCK_FUNCTION_CONTEXT ); error_type::fail( "unexpected call", MOCK_FUNCTION_CONTEXT );

View file

@ -8,12 +8,8 @@
#include "function_impl_template.hpp" #include "function_impl_template.hpp"
#define MOCK_FUNCTION_CALL(z, n, d ) \ #define MOCK_FORWARD(z, n, d) \
BOOST_PP_COMMA_IF(n) typename \ boost::forward< T##n >( t##n )
boost::call_traits< T##n >::param_type
#define MOCK_FUNCTION_PARAM(z, n, d) \
MOCK_FUNCTION_CALL(z, n, d) t##n
namespace mock namespace mock
{ {
@ -36,7 +32,7 @@ namespace detail
private: private:
typedef function_impl< typedef function_impl<
R ( BOOST_PP_REPEAT(MOCK_NUM_ARGS, MOCK_FUNCTION_CALL, _) ) R ( BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, T) )
> impl_type; > impl_type;
typedef typename impl_type::wrapper_type expectation_type; typedef typename impl_type::wrapper_type expectation_type;
typedef typename impl_type::error_type error_type; typedef typename impl_type::error_type error_type;
@ -76,9 +72,9 @@ namespace detail
} }
R operator()( R operator()(
BOOST_PP_REPEAT(MOCK_NUM_ARGS, MOCK_FUNCTION_PARAM, _) ) const BOOST_PP_ENUM_BINARY_PARAMS(MOCK_NUM_ARGS, T, t) ) const
{ {
return (*impl_)( BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, t) ); return (*impl_)( BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_FORWARD, _) );
} }
friend std::ostream& operator<<( std::ostream& s, const function& f ) friend std::ostream& operator<<( std::ostream& s, const function& f )
@ -106,6 +102,3 @@ namespace detail
}; };
} }
} // mock } // mock
#undef MOCK_FUNCTION_CALL
#undef MOCK_FUNCTION_PARAM

View file

@ -6,6 +6,9 @@
// (See accompanying file LICENSE_1_0.txt or copy at // (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
#define MOCK_CALL_PARAM(z, n, d) \
typename boost::call_traits< T##n >::param_type a##n
namespace mock namespace mock
{ {
namespace detail namespace detail
@ -13,15 +16,15 @@ namespace detail
template< typename Signature > class matcher_base; template< typename Signature > class matcher_base;
template< template<
BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, typename Actual_) > BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, typename T) >
class matcher_base< void( BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, Actual_) ) > class matcher_base< void( BOOST_PP_ENUM_PARAMS(MOCK_NUM_ARGS, T) ) >
: boost::noncopyable : boost::noncopyable
{ {
public: public:
virtual ~matcher_base() {} virtual ~matcher_base() {}
virtual bool operator()( virtual bool operator()(
BOOST_PP_ENUM_BINARY_PARAMS(MOCK_NUM_ARGS, Actual_, actual_) ) = 0; BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_CALL_PARAM, _) ) = 0;
friend std::ostream& operator<<( friend std::ostream& operator<<(
std::ostream& s, const matcher_base& m ) std::ostream& s, const matcher_base& m )

View file

@ -690,3 +690,19 @@ BOOST_FIXTURE_TEST_CASE( mock_method_accepts_polymorphic_multi_constraint, mock_
m.m2( 1, 2 ); m.m2( 1, 2 );
CHECK_CALLS( 1 ); CHECK_CALLS( 1 );
} }
#ifdef MOCK_SMART_PTR
BOOST_FIXTURE_TEST_CASE( std_unique_ptr_argument_is_supported_in_action, mock_error_fixture )
{
MOCK_FUNCTOR( f, void( std::unique_ptr< int > ) );
std::unique_ptr< int > p;
MOCK_EXPECT( f ).once().calls(
[]( std::unique_ptr< int > )
{
} );
f( std::unique_ptr< int >( new int( 7 ) ) );
CHECK_CALLS( 1 );
}
#endif