Changed MOCK_DESTRUCTOR and MOCK_*CONVERSION_OPERATOR to support different calling conventions

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@707 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2014-02-07 14:26:21 +00:00
parent a6c6b5dd82
commit 53e67c0a81
5 changed files with 300 additions and 87 deletions

View file

@ -157,7 +157,7 @@ struct base_class
MOCK_BASE_CLASS( mock_class, base_class )
{
MOCK_METHOD( method, 1, void( float ), method ) // this generates both const and non-const versions
MOCK_METHOD( method, 1, void( float ) ) // this generates both const and non-const versions
};
//]
}
@ -231,6 +231,28 @@ MOCK_CLASS( mock_class )
//]
}
#ifdef BOOST_MSVC
namespace member_function_example_9
{
//[ member_function_example_9
MOCK_CLASS( mock_class )
{
MOCK_METHOD( __stdcall method, 0, void(), method ) // all parameters must be provided when specifying a different calling convention
};
//]
}
#elif defined( BOOST_GCC )
namespace member_function_example_10
{
//[ member_function_example_10
MOCK_CLASS( mock_class )
{
MOCK_METHOD( __attribute((stdcall)) method, 0, void(), method ) // all parameters must be provided when specifying a different calling convention
};
//]
}
#endif
namespace static_member_function_example_1
{
//[ static_member_function_example_1
@ -252,6 +274,28 @@ MOCK_CLASS( mock_class )
//]
}
#ifdef BOOST_MSVC
namespace static_member_function_example_3
{
//[ static_member_function_example_3
MOCK_CLASS( mock_class )
{
MOCK_STATIC_METHOD( __stdcall method, 0, void(), method ) // all parameters must be provided when specifying a different calling convention
};
//]
}
#elif defined( BOOST_GCC )
namespace static_member_function_example_4
{
//[ static_member_function_example_4
MOCK_CLASS( mock_class )
{
MOCK_STATIC_METHOD( __attribute((stdcall)) method, 0, void(), method ) // all parameters must be provided when specifying a different calling convention
};
//]
}
#endif
namespace constructor_example_1
{
//[ constructor_example_1
@ -274,23 +318,67 @@ MOCK_CLASS( mock_class )
//]
}
#ifdef BOOST_MSVC
namespace constructor_example_3
{
//[ constructor_example_3
MOCK_CLASS( mock_class )
{
MOCK_CONSTRUCTOR( __stdcall mock_class, 0, (), constructor )
};
//]
}
#elif defined( BOOST_GCC )
namespace constructor_example_4
{
//[ constructor_example_4
MOCK_CLASS( mock_class )
{
MOCK_CONSTRUCTOR( __attribute((stdcall)) mock_class, 0, (), constructor )
};
//]
}
#endif
namespace destructor_example_1
{
//[ destructor_example_1
MOCK_CLASS( mock_class )
{
MOCK_DESTRUCTOR( mock_class, destructor )
MOCK_DESTRUCTOR( ~mock_class, destructor )
};
//]
}
#ifdef BOOST_MSVC
namespace destructor_example_2
{
//[ destructor_example_2
MOCK_CLASS( mock_class )
{
MOCK_DESTRUCTOR( __stdcall ~mock_class, destructor )
};
//]
}
#elif defined( BOOST_GCC )
namespace destructor_example_3
{
//[ destructor_example_3
MOCK_CLASS( mock_class )
{
MOCK_DESTRUCTOR( __attribute((stdcall)) ~mock_class, destructor )
};
//]
}
#endif
namespace conversion_operator_example_1
{
//[ conversion_operator_example_1
MOCK_CLASS( mock_class )
{
MOCK_CONVERSION_OPERATOR( int, conversion_to_int )
MOCK_CONST_CONVERSION_OPERATOR( const std::string&, conversion_to_string )
MOCK_CONVERSION_OPERATOR( operator, int, conversion_to_int )
MOCK_CONST_CONVERSION_OPERATOR( operator, const std::string&, conversion_to_string )
};
//]
}
@ -301,13 +389,66 @@ namespace conversion_operator_example_2
template< typename T >
MOCK_CLASS( mock_class )
{
MOCK_CONVERSION_OPERATOR_TPL( T, conversion_to_T ) // the _TPL variants must be used if the signature includes a template parameter of the class
MOCK_CONST_CONVERSION_OPERATOR( const std::string&, const_conversion_to_string )
MOCK_NON_CONST_CONVERSION_OPERATOR( const std::string&, non_const_conversion_to_string )
MOCK_CONVERSION_OPERATOR_TPL( operator, T, conversion_to_T ) // the _TPL variants must be used if the signature includes a template parameter of the class
MOCK_CONST_CONVERSION_OPERATOR( operator, const std::string&, const_conversion_to_string )
MOCK_NON_CONST_CONVERSION_OPERATOR( operator, const std::string&, non_const_conversion_to_string )
};
//]
}
#ifdef BOOST_MSVC
namespace conversion_operator_example_3
{
//[ conversion_operator_example_3
MOCK_CLASS( mock_class )
{
MOCK_CONVERSION_OPERATOR( __stdcall operator, int, conversion_to_int )
};
//]
}
#elif defined( BOOST_GCC )
namespace conversion_operator_example_4
{
//[ conversion_operator_example_4
MOCK_CLASS( mock_class )
{
MOCK_CONVERSION_OPERATOR( __attribute((stdcall)) operator, int, conversion_to_int )
};
//]
}
#endif
namespace function_example_1
{
//[ function_example_1
MOCK_FUNCTION( f, 1, float( int ) )
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_mock_function )
{
f( 3 );
}
//]
}
#ifdef BOOST_MSVC
namespace function_example_2
{
//[ function_example_2
MOCK_FUNCTION( __stdcall f, 0, void(), f ) // all parameters must be provided when specifying a different calling convention
//]
}
#elif defined( BOOST_GCC )
namespace function_example_3
{
//[ function_example_3
MOCK_CLASS( mock_class )
{
MOCK_FUNCTION( __attribute((stdcall)) f, 0, void() )
};
//]
}
#endif
namespace functor_example_1
{
//[ functor_example_1
@ -336,18 +477,6 @@ BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_mock_functor )
//]
}
namespace function_example_1
{
//[ function_example_1
MOCK_FUNCTION( f, 1, float( int ) )
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_mock_function )
{
f( 3 );
}
//]
}
namespace expectation_example_1
{
//[ expectation_example_1

View file

@ -80,13 +80,13 @@ Deriving from mock::object is optional but provides the additional following ben
Synopsis :
MOCK_METHOD( name, arity[, signature[, identifier]] ) // generates both const and non-const methods
MOCK_CONST_METHOD( name, arity[, signature[, identifier]] ) // generates only the const version of the method
MOCK_NON_CONST_METHOD( name, arity[, signature[, identifier]] ) // generates only the non-const version of the method
MOCK_METHOD( [calling convention] name, arity[, signature[, identifier]] ) // generates both const and non-const methods
MOCK_CONST_METHOD( [calling convention] name, arity[, signature[, identifier]] ) // generates only the const version of the method
MOCK_NON_CONST_METHOD( [calling convention] name, arity[, signature[, identifier]] ) // generates only the non-const version of the method
MOCK_METHOD_TPL( name, arity[, signature[, identifier]] ) // must be used if the signature uses a template parameter of the class, generates both const and non-const methods
MOCK_CONST_METHOD_TPL( name, arity[, signature[, identifier]] ) // must be used if the signature uses a template parameter of the class, generates only the const version of the method
MOCK_NON_CONST_METHOD_TPL( name, arity[, signature[, identifier]] ) // must be used if the signature uses a template parameter of the class, generates only the non-const version of the method
MOCK_METHOD_TPL( [calling convention] name, arity[, signature[, identifier]] ) // must be used if the signature uses a template parameter of the class, generates both const and non-const methods
MOCK_CONST_METHOD_TPL( [calling convention] name, arity[, signature[, identifier]] ) // must be used if the signature uses a template parameter of the class, generates only the const version of the method
MOCK_NON_CONST_METHOD_TPL( [calling convention] name, arity[, signature[, identifier]] ) // must be used if the signature uses a template parameter of the class, generates only the non-const version of the method
[note If the identifier is omitted it will default to the method name.]
@ -96,19 +96,19 @@ Synopsis :
[note [link turtle.reference.creation.constructor Constructors], [link turtle.reference.creation.destructor destructors] and [link turtle.reference.creation.conversion_operator conversion operators] require special care.]
For compilers without support for variadic macros the signature and the identifier cannot be specified, thus in case of ambiguity another set of macros must be used.
[note In case of a calling convention specified, all four parameters must be provided.]
[warning For compilers without support for variadic macros the MOCK_METHOD_EXT familly set of macros must be used.]
Synopsis :
MOCK_METHOD_EXT( name, arity, signature, identifier ) // generates both const and non-const methods
MOCK_CONST_METHOD_EXT( name, arity, signature, identifier ) // generates only the const version of the method
MOCK_NON_CONST_METHOD_EXT( name, arity, signature, identifier ) // generates only the non-const version of the method
MOCK_METHOD_EXT( [calling convention] name, arity, signature, identifier ) // generates both const and non-const methods
MOCK_CONST_METHOD_EXT( [calling convention] name, arity, signature, identifier ) // generates only the const version of the method
MOCK_NON_CONST_METHOD_EXT( [calling convention] name, arity, signature, identifier ) // generates only the non-const version of the method
MOCK_METHOD_EXT_TPL( name, arity, signature, identifier ) // must be used if the signature uses a template parameter of the class, generates both const and non-const methods
MOCK_CONST_METHOD_EXT_TPL( name, arity, signature, identifier ) // must be used if the signature uses a template parameter of the class, generates only the const version of the method
MOCK_NON_CONST_METHOD_EXT_TPL( name, arity, signature, identifier ) // must be used if the signature uses a template parameter of the class, generates only the non-const version of the method
Of course those macros are also available for compilers which support variadic macros.
MOCK_METHOD_EXT_TPL( [calling convention] name, arity, signature, identifier ) // must be used if the signature uses a template parameter of the class, generates both const and non-const methods
MOCK_CONST_METHOD_EXT_TPL( [calling convention] name, arity, signature, identifier ) // must be used if the signature uses a template parameter of the class, generates only the const version of the method
MOCK_NON_CONST_METHOD_EXT_TPL( [calling convention] name, arity, signature, identifier ) // must be used if the signature uses a template parameter of the class, generates only the non-const version of the method
Example :
@ -138,18 +138,28 @@ Example :
[member_function_example_8]
Example for msvc :
[member_function_example_9]
Example for gcc :
[member_function_example_10]
[endsect]
[section Static member function]
Synopsis :
MOCK_STATIC_METHOD( name, arity, signature[, identifier] ) // if 'identifier' is omitted it will default to 'name'
MOCK_STATIC_METHOD( [calling convention] name, arity, signature[, identifier] ) // if 'identifier' is omitted it will default to 'name'
MOCK_STATIC_METHOD_TPL( name, arity, signature[, identifier] ) // must be used if the signature uses a template parameter of the class, if 'identifier' is omitted it will default to 'name'
MOCK_STATIC_METHOD_TPL( [calling convention] name, arity, signature[, identifier] ) // must be used if the signature uses a template parameter of the class, if 'identifier' is omitted it will default to 'name'
[note A static object is used behind the scene in order to keep track of the expectations of a mock static method, therefore to ensure all tests run in isolation it is strongly suggested to manually [link turtle.reference.verification verify] and [link turtle.reference.reset reset] the static method at the end of each test.]
[note In case of a calling convention specified, all four parameters must be provided.]
[warning For compilers without support for variadic macros the identifier cannot be omitted and must be given explicitly.]
Example :
@ -160,15 +170,23 @@ Example :
[static_member_function_example_2]
Example for msvc :
[static_member_function_example_3]
Example for gcc :
[static_member_function_example_4]
[endsect]
[section Constructor]
Synopsis :
MOCK_CONSTRUCTOR( name, arity, parameters, identifier )
MOCK_CONSTRUCTOR( [calling convention] name, arity, parameters, identifier )
MOCK_CONSTRUCTOR_TPL( name, arity, parameters, identifier ) // must be used if the signature uses a template parameter of the class
MOCK_CONSTRUCTOR_TPL( [calling convention] name, arity, parameters, identifier ) // must be used if the signature uses a template parameter of the class
[note As constructors do not have a return type, the usual signature gets restricted here to just the parameters.]
@ -180,13 +198,21 @@ Example :
[constructor_example_2]
Example for msvc :
[constructor_example_3]
Example for gcc :
[constructor_example_4]
[endsect]
[section Destructor]
Synopsis :
MOCK_DESTRUCTOR( name, identifier )
MOCK_DESTRUCTOR( [calling convention] ~name, identifier )
[note When mocking a destructor it is strongly suggested to manually [link turtle.reference.verification verify] the expectation at the end of the test, because the automatic verification will not be triggered if the mock object is not destroyed.]
@ -194,19 +220,27 @@ Example :
[destructor_example_1]
Example for msvc :
[destructor_example_2]
Example for gcc :
[destructor_example_3]
[endsect]
[section Conversion operator]
Synopsis :
MOCK_CONVERSION_OPERATOR( type, identifier ) // generates both const and non-const operators
MOCK_CONST_CONVERSION_OPERATOR( type, identifier ) // generates only a const operator
MOCK_NON_CONST_CONVERSION_OPERATOR( type, identifier ) // generates only a non-const operator
MOCK_CONVERSION_OPERATOR( [calling convention] name, type, identifier ) // generates both const and non-const operators
MOCK_CONST_CONVERSION_OPERATOR( [calling convention] name, type, identifier ) // generates only a const operator
MOCK_NON_CONST_CONVERSION_OPERATOR( [calling convention] name, type, identifier ) // generates only a non-const operator
MOCK_CONVERSION_OPERATOR_TPL( type, identifier ) // must be used if the signature uses a template parameter of the class
MOCK_CONST_CONVERSION_OPERATOR_TPL( type, identifier ) // must be used if the signature uses a template parameter of the class
MOCK_NON_CONST_CONVERSION_OPERATOR_TPL( type, identifier ) // must be used if the signature uses a template parameter of the class
MOCK_CONVERSION_OPERATOR_TPL( [calling convention] name, type, identifier ) // must be used if the signature uses a template parameter of the class
MOCK_CONST_CONVERSION_OPERATOR_TPL( [calling convention] name, type, identifier ) // must be used if the signature uses a template parameter of the class
MOCK_NON_CONST_CONVERSION_OPERATOR_TPL( [calling convention] name, type, identifier ) // must be used if the signature uses a template parameter of the class
Example :
@ -216,15 +250,49 @@ Example :
[conversion_operator_example_2]
Example for msvc :
[conversion_operator_example_3]
Example for gcc :
[conversion_operator_example_4]
[endsect]
[section Function]
Synopsis :
MOCK_FUNCTION( [calling convention] name, arity, signature[, identifier] ) // if 'identifier' is omitted it will default to 'name'
[note A static object is used behind the scene in order to keep track of the expectations of a mock function, therefore to ensure all tests run in isolation it is strongly suggested to manually [link turtle.reference.verification verify] and [link turtle.reference.reset reset] the mock function at the end of each test.]
[note In case of a calling convention specified, all four parameters must be provided.]
[warning For compilers without support for variadic macros the identifier cannot be omitted and must be given explicitly.]
Example :
[function_example_1]
Example for msvc :
[function_example_2]
Example for gcc :
[function_example_3]
[endsect]
[section Functor]
Synopsis :
MOCK_FUNCTOR( name, signature );
MOCK_FUNCTOR( [calling convention] name, signature );
MOCK_FUNCTOR_TPL( name, signature ); // must be used if the signature uses a template parameter
MOCK_FUNCTOR_TPL( [calling convention] name, signature ); // must be used if the signature uses a template parameter
Example :
@ -236,22 +304,6 @@ Example :
[endsect]
[section Function]
Synopsis :
MOCK_FUNCTION( name, arity, signature[, identifier] ) // if 'identifier' is omitted it will default to 'name'
[note A static object is used behind the scene in order to keep track of the expectations of a mock function, therefore to ensure all tests run in isolation it is strongly suggested to manually [link turtle.reference.verification verify] and [link turtle.reference.reset reset] the mock function at the end of each test.]
[warning For compilers without support for variadic macros the identifier cannot be omitted and must be given explicitly.]
Example :
[function_example_1]
[endsect]
[endsect]
[section Expectation]

View file

@ -321,7 +321,7 @@ namespace
{
MOCK_CLASS( my_destroyed_class )
{
MOCK_DESTRUCTOR( my_destroyed_class, destructor )
MOCK_DESTRUCTOR( ~my_destroyed_class, destructor )
};
}

View file

@ -54,7 +54,7 @@ namespace
{
MOCK_CLASS( mock_class_with_conversion_operator )
{
MOCK_CONVERSION_OPERATOR( int, conversion )
MOCK_CONVERSION_OPERATOR( operator, int, conversion )
};
}
@ -71,7 +71,7 @@ namespace
template< typename T >
MOCK_CLASS( mock_template_class_with_conversion_operator )
{
MOCK_CONVERSION_OPERATOR_TPL( T, conversion )
MOCK_CONVERSION_OPERATOR_TPL( operator, T, conversion )
};
}
@ -87,7 +87,7 @@ namespace
{
MOCK_CLASS( mock_class_with_const_conversion_operator )
{
MOCK_CONST_CONVERSION_OPERATOR( int, conversion )
MOCK_CONST_CONVERSION_OPERATOR( operator, int, conversion )
};
}
@ -104,7 +104,7 @@ namespace
{
MOCK_CLASS( mock_class_with_non_const_conversion_operator )
{
MOCK_CONST_CONVERSION_OPERATOR( int, conversion )
MOCK_CONST_CONVERSION_OPERATOR( operator, int, conversion )
};
}
@ -122,7 +122,7 @@ namespace
template< typename T >
MOCK_CLASS( mock_template_class_with_const_conversion_operator )
{
MOCK_CONST_CONVERSION_OPERATOR_TPL( T, conversion )
MOCK_CONST_CONVERSION_OPERATOR_TPL( operator, T, conversion )
};
}
@ -139,7 +139,7 @@ namespace
template< typename T >
MOCK_CLASS( mock_template_class_with_non_const_conversion_operator )
{
MOCK_NON_CONST_CONVERSION_OPERATOR_TPL( T, conversion )
MOCK_NON_CONST_CONVERSION_OPERATOR_TPL( operator, T, conversion )
};
}
@ -401,3 +401,35 @@ namespace
}
#endif // MOCK_VARIADIC_MACROS
#ifdef BOOST_MSVC
# define MOCK_STDCALL __stdcall
#elif defined( BOOST_GCC )
# define MOCK_STDCALL __attribute((stdcall))
#else
# define MOCK_STDCALL
#endif // BOOST_GCC
namespace stdcall
{
struct base
{
virtual void MOCK_STDCALL m1() = 0;
};
MOCK_BASE_CLASS( derived, base )
{
MOCK_CONSTRUCTOR( MOCK_STDCALL derived, 0, (), derived )
MOCK_DESTRUCTOR( MOCK_STDCALL ~derived, derived )
MOCK_CONVERSION_OPERATOR( MOCK_STDCALL operator, int, to_int )
MOCK_METHOD_EXT( MOCK_STDCALL m1, 0, void(), m1 )
MOCK_METHOD_EXT( MOCK_STDCALL m2, 0, void(), m2 )
#ifdef MOCK_VARIADIC_MACROS
MOCK_METHOD( MOCK_STDCALL m3, 0, void(), m3 )
#endif
MOCK_STATIC_METHOD( MOCK_STDCALL m4, 0, void(), m4 )
};
MOCK_FUNCTOR( MOCK_STDCALL f, void() )
MOCK_FUNCTION( MOCK_STDCALL f, 0, void(), f )
}

View file

@ -114,26 +114,26 @@
MOCK_METHOD_AUX(M, n, S, t,, BOOST_DEDUCED_TYPENAME) \
MOCK_METHOD_HELPER(S, t, BOOST_DEDUCED_TYPENAME)
#define MOCK_CONST_CONVERSION_OPERATOR(T, t) \
operator T() const { return MOCK_ANONYMOUS_HELPER(t)(); } \
#define MOCK_CONVERSION_OPERATOR(M, T, t) \
M T() const { return MOCK_ANONYMOUS_HELPER(t)(); } \
M T() { return MOCK_ANONYMOUS_HELPER(t)(); } \
MOCK_METHOD_HELPER(T(), t,)
#define MOCK_NON_CONST_CONVERSION_OPERATOR(T, t) \
operator T() { return MOCK_ANONYMOUS_HELPER(t)(); } \
#define MOCK_CONST_CONVERSION_OPERATOR(M, T, t) \
M T() const { return MOCK_ANONYMOUS_HELPER(t)(); } \
MOCK_METHOD_HELPER(T(), t,)
#define MOCK_CONVERSION_OPERATOR(T, t) \
operator T() const { return MOCK_ANONYMOUS_HELPER(t)(); } \
operator T() { return MOCK_ANONYMOUS_HELPER(t)(); } \
#define MOCK_NON_CONST_CONVERSION_OPERATOR(M, T, t) \
M T() { return MOCK_ANONYMOUS_HELPER(t)(); } \
MOCK_METHOD_HELPER(T(), t,)
#define MOCK_CONST_CONVERSION_OPERATOR_TPL(T, t) \
operator T() const { return MOCK_ANONYMOUS_HELPER(t)(); } \
#define MOCK_CONVERSION_OPERATOR_TPL(M, T, t) \
M T() const { return MOCK_ANONYMOUS_HELPER(t)(); } \
M T() { return MOCK_ANONYMOUS_HELPER(t)(); } \
MOCK_METHOD_HELPER(T(), t, BOOST_DEDUCED_TYPENAME)
#define MOCK_NON_CONST_CONVERSION_OPERATOR_TPL(T, t) \
operator T() { return MOCK_ANONYMOUS_HELPER(t)(); } \
#define MOCK_CONST_CONVERSION_OPERATOR_TPL(M, T, t) \
M T() const { return MOCK_ANONYMOUS_HELPER(t)(); } \
MOCK_METHOD_HELPER(T(), t, BOOST_DEDUCED_TYPENAME)
#define MOCK_CONVERSION_OPERATOR_TPL(T, t) \
operator T() const { return MOCK_ANONYMOUS_HELPER(t)(); } \
operator T() { return MOCK_ANONYMOUS_HELPER(t)(); } \
#define MOCK_NON_CONST_CONVERSION_OPERATOR_TPL(M, T, t) \
M T() { return MOCK_ANONYMOUS_HELPER(t)(); } \
MOCK_METHOD_HELPER(T(), t, BOOST_DEDUCED_TYPENAME)
#define MOCK_FUNCTION_HELPER(S, t, s, tpn) \
@ -146,11 +146,11 @@
}
#define MOCK_CONSTRUCTOR_AUX(T, n, A, t, tpn) \
MOCK_FUNCTION_HELPER(void A, t, static, tpn) \
T( MOCK_PARAMS(n, void A, tpn) ) \
{ \
MOCK_HELPER(t)( BOOST_PP_ENUM_PARAMS(n, p) ); \
}
} \
MOCK_FUNCTION_HELPER(void A, t, static, tpn)
#define MOCK_CONSTRUCTOR(T, n, A, t) \
MOCK_CONSTRUCTOR_AUX(T, n, A, t,)
@ -158,7 +158,7 @@
MOCK_CONSTRUCTOR_AUX(T, n, A, t, BOOST_DEDUCED_TYPENAME)
#define MOCK_DESTRUCTOR(T, t) \
~T() { try { MOCK_ANONYMOUS_HELPER(t)(); } catch( ... ) {} } \
T() { try { MOCK_ANONYMOUS_HELPER(t)(); } catch( ... ) {} } \
MOCK_METHOD_HELPER(void(), t,)
#define MOCK_FUNCTION_AUX(F, n, S, t, s, tpn) \