Fixed MOCK_MAX_ARGS usage to increase to maximum number of arguments of mocked methods

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@169 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2010-07-23 13:17:18 +00:00
parent 542984c2b6
commit d63d5d2c1f
5 changed files with 71 additions and 28 deletions

View file

@ -10,6 +10,9 @@
#define MOCK_ACTION_HPP_INCLUDED
#include "config.hpp"
#include <boost/spirit/home/phoenix/bind/bind_function.hpp>
#include <boost/spirit/home/phoenix/statement/sequence.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/ref.hpp>
#include <memory>
@ -41,7 +44,7 @@ namespace detail
template< typename Exception >
void throws( Exception e )
{
f_ = boost::bind( &throw_exception< Exception >, e );
f_ = boost::phoenix::bind( &throw_exception< Exception >, e );
}
const functor_type& functor() const
@ -52,15 +55,17 @@ namespace detail
private:
void set( Result r )
{
f_ = boost::bind( &return_value, r );
f_ = (boost::phoenix::bind( &nothing ), boost::bind( &identity, r ));
}
template< typename Y >
void set( const boost::reference_wrapper< Y >& r )
{
f_ = boost::bind( &return_value, r );
f_ = boost::phoenix::val( r );
}
static Result return_value( Result r )
static void nothing()
{}
static Result identity( Result r )
{
return r;
}
@ -83,12 +88,12 @@ namespace detail
public:
void returns( Result* r )
{
f_ = boost::bind( &return_value, r );
f_ = boost::phoenix::val( r );
}
template< typename Y >
void returns( const boost::reference_wrapper< Y >& r )
{
f_ = boost::bind( &return_value, r );
f_ = boost::phoenix::val( r );
}
void calls( const functor_type& f )
@ -101,7 +106,7 @@ namespace detail
template< typename Exception >
void throws( Exception e )
{
f_ = boost::bind( &throw_exception< Exception >, e );
f_ = boost::phoenix::bind( &throw_exception< Exception >, e );
}
const functor_type& functor() const
@ -110,11 +115,6 @@ namespace detail
}
private:
static Result* return_value( Result* r )
{
return r;
}
template< typename Exception >
static Result* throw_exception( const Exception& e )
{
@ -132,7 +132,7 @@ namespace detail
public:
action()
: f_( boost::bind( &nothing ) )
: f_( boost::phoenix::bind( &nothing ) )
{}
void calls( const functor_type& f )
@ -145,7 +145,7 @@ namespace detail
template< typename Exception >
void throws( Exception e )
{
f_ = boost::bind( &throw_exception< Exception >, e );
f_ = boost::phoenix::bind( &throw_exception< Exception >, e );
}
const functor_type& functor() const
@ -179,7 +179,7 @@ namespace detail
{}
action( const action& rhs )
: r_( const_cast< action& >( rhs ).r_.release() )
, f_( r_.get() ? boost::bind( &return_value, boost::ref( r_ ) ) : rhs.f_ )
, f_( r_.get() ? boost::phoenix::val( boost::ref( r_ ) ) : rhs.f_ )
{}
template< typename Value >
@ -198,7 +198,7 @@ namespace detail
template< typename Exception >
void throws( Exception e )
{
f_ = boost::bind( &throw_exception< Exception >, e );
f_ = boost::phoenix::bind( &throw_exception< Exception >, e );
r_.reset();
}
@ -212,24 +212,19 @@ namespace detail
void set( std::auto_ptr< Y > r )
{
r_ = r;
f_ = boost::bind( &return_value, boost::ref( r_ ) );
f_ = boost::phoenix::val( boost::ref( r_ ) );
}
template< typename Y >
void set( const boost::reference_wrapper< Y >& r )
{
f_ = boost::bind( &return_value, r );
f_ = boost::phoenix::val( r );
r_.reset();
}
template< typename Y >
void set( Y* r )
{
r_.reset( r );
f_ = boost::bind( &return_value, boost::ref( r_ ) );
}
static std::auto_ptr< Result > return_value( std::auto_ptr< Result > r )
{
return r;
f_ = boost::phoenix::val( boost::ref( r_ ) );
}
template< typename Exception >

View file

@ -9,17 +9,24 @@
#ifndef MOCK_CONFIG_HPP_INCLUDED
#define MOCK_CONFIG_HPP_INCLUDED
#include <boost/preprocessor/comparison/less_equal.hpp>
#include <boost/preprocessor/debug/assert.hpp>
#include <boost/preprocessor/inc.hpp>
#include <boost/function.hpp>
#ifndef MOCK_MAX_ARGS
# define MOCK_MAX_ARGS 10
#endif
#define MOCK_NUM_ARGS BOOST_PP_INC(MOCK_MAX_ARGS)
BOOST_PP_ASSERT( BOOST_PP_LESS_EQUAL(MOCK_MAX_ARGS, BOOST_FUNCTION_MAX_ARGS) )
#ifndef PHOENIX_LIMIT
# define PHOENIX_LIMIT MOCK_MAX_ARGS
#elif (PHOENIX_LIMIT < MOCK_MAX_ARGS)
# error "PHOENIX_LIMIT is set too low"
#endif
#ifndef BOOST_FUNCTION_MAX_ARGS
# define BOOST_FUNCTION_MAX_ARGS MOCK_MAX_ARGS
#elif (BOOST_FUNCTION_MAX_ARGS < MOCK_MAX_ARGS)
# error "BOOST_FUNCTION_MAX_ARGS is set too low"
#endif
#ifdef BOOST_TEST_DECL
# define MOCK_USE_BOOST_TEST

View file

@ -9,6 +9,7 @@
#ifndef MOCK_MOCK_HPP_INCLUDED
#define MOCK_MOCK_HPP_INCLUDED
#include "config.hpp"
#include "error.hpp"
#include "object.hpp"
#include "function.hpp"

View file

@ -0,0 +1,36 @@
//
// Copyright Mathieu Champlon 2008
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include "silent_error.hpp"
#define MOCK_MAX_ARGS 20
#define MOCK_ERROR_POLICY silent_error
#include <turtle/mock.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework
#include <boost/config/auto_link.hpp>
#define TEXT(z, n, text) text
namespace
{
struct my_custom_mock
{
MOCK_METHOD_EXT( my_method_with_max_number_of_args, MOCK_MAX_ARGS, void( BOOST_PP_ENUM(MOCK_MAX_ARGS, TEXT, int) ), my_method_with_max_number_of_args )
};
}
BOOST_AUTO_TEST_CASE( call_mock_method_with_max_number_of_args )
{
my_custom_mock m;
MOCK_EXPECT( m, my_method_with_max_number_of_args ).once().with( BOOST_PP_ENUM(MOCK_MAX_ARGS, TEXT, 0) );
m.my_method_with_max_number_of_args( BOOST_PP_ENUM(MOCK_MAX_ARGS, TEXT, 0) );
}