Moved examples to source code

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@643 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2013-05-12 08:58:40 +00:00
parent ef9a3e2c4c
commit d2fbf4167b
4 changed files with 914 additions and 492 deletions

View file

@ -13,19 +13,19 @@ using doxygen ;
xml mock : mock.qbk ; xml mock : mock.qbk ;
doxygen reference #doxygen reference
: # :
[ glob ../../../boost/mock/*.hpp ] # [ glob ../../../boost/mock/*.hpp ]
: # :
<doxygen:param>EXPAND_ONLY_PREDEF=YES # <doxygen:param>EXPAND_ONLY_PREDEF=YES
<doxygen:param>HIDE_UNDOC_MEMBERS=NO # <doxygen:param>HIDE_UNDOC_MEMBERS=NO
<doxygen:param>QUIET=YES # <doxygen:param>QUIET=YES
<doxygen:param>WARN_IF_UNDOCUMENTED=NO # <doxygen:param>WARN_IF_UNDOCUMENTED=NO
<doxygen:param>EXTRACT_PRIVATE=NO # <doxygen:param>EXTRACT_PRIVATE=NO
<doxygen:param>ENABLE_PREPROCESSING=YES # <doxygen:param>ENABLE_PREPROCESSING=YES
<doxygen:param>MACRO_EXPANSION=YES # <doxygen:param>MACRO_EXPANSION=YES
<doxygen:param>SEARCH_INCLUDES=NO # <doxygen:param>SEARCH_INCLUDES=NO
; #;
boostbook standalone boostbook standalone
: :
@ -51,3 +51,4 @@ compile example/patterns_async_call.cpp ;
compile example/patterns_retrieve_cref.cpp ; compile example/patterns_retrieve_cref.cpp ;
compile example/patterns_invoke_functor.cpp ; compile example/patterns_invoke_functor.cpp ;
compile example/patterns_quick_constraint.cpp ; compile example/patterns_quick_constraint.cpp ;
compile example/reference.cpp ;

View file

@ -0,0 +1,773 @@
#define BOOST_AUTO_TEST_MAIN
#include <boost/test/auto_unit_test.hpp>
#include <turtle/mock.hpp>
namespace class_example_1
{
//[ class_example_1
MOCK_CLASS( mock_class )
{};
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_mock_class )
{
mock_class c;
}
//]
}
namespace class_example_2
{
//[ class_example_2
template< typename T >
MOCK_CLASS( mock_class )
{};
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_template_mock_class )
{
mock_class< int > c;
}
//]
}
namespace class_example_3
{
//[ class_example_3
struct base_class
{};
MOCK_BASE_CLASS( mock_class, base_class )
{};
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_derived_mock_class )
{
mock_class c;
}
//]
}
namespace class_example_4
{
//[ class_example_4
template< typename T >
struct base_class
{};
template< typename T >
MOCK_BASE_CLASS( mock_class, base_class< T > )
{};
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_template_derived_mock_class )
{
mock_class< int > c;
}
//]
}
namespace class_example_5
{
//[ class_example_5
struct name : mock::object // equivalent to using MOCK_CLASS
{};
//]
}
namespace class_example_6
{
//[ class_example_6
template< typename T >
struct name : mock::object // equivalent to using MOCK_CLASS
{};
//]
}
namespace class_example_7
{
//[ class_example_7
class base
{};
struct name : base, mock::object // equivalent to using MOCK_BASE_CLASS
{
typedef base base_type; // this is required for MOCK_METHOD to work when not using MOCK_BASE_CLASS
};
//]
}
namespace class_example_8
{
//[ class_example_8
template< typename T >
struct base
{};
template< typename T >
struct mock : base< T >, mock::object
{
typedef typename base< T > base_type;
};
//]
}
namespace member_function_example_1
{
//[ member_function_example_1
struct base_class
{
virtual ~base_class()
{}
virtual void method( int ) = 0;
};
MOCK_BASE_CLASS( mock_class, base_class )
{
MOCK_METHOD( method, 1 ) // only possible when referring unambiguously to a base class method
};
//]
}
namespace member_function_example_2
{
//[ member_function_example_2
struct base_class
{
virtual ~base_class()
{}
virtual void method( int, const std::string& ) = 0;
virtual void method( float ) = 0;
};
MOCK_BASE_CLASS( mock_class, base_class )
{
MOCK_METHOD( method, 2, void( int, const std::string& ), identifier_1 ) // MOCK_METHOD cannot be used because of overloading
MOCK_METHOD( method, 1, void( float ), identifier_2 ) // the identifier must differ from the previous one in order to fully disambiguate methods
};
//]
}
namespace member_function_example_3
{
//[ member_function_example_3
struct base_class
{
virtual ~base_class()
{}
virtual void method( float ) = 0;
virtual void method( float ) const = 0;
};
MOCK_BASE_CLASS( mock_class, base_class )
{
MOCK_METHOD( method, 1, void( float ), method ) // this generates both const and non-const versions
};
//]
}
namespace member_function_example_4
{
//[ member_function_example_4
struct base_class
{
virtual ~base_class()
{}
virtual void method( float ) = 0;
virtual void method( float ) const = 0;
};
MOCK_BASE_CLASS( mock_class, base_class )
{
MOCK_CONST_METHOD( method, 1, void( float ), identifier_1 ) // this generates only the const version
MOCK_NON_CONST_METHOD( method, 1, void( float ), identifier_2 ) // this generates only the non-const version, with a different identifier
};
//]
}
namespace member_function_example_5
{
//[ member_function_example_5
struct base_class
{
virtual ~base_class()
{}
virtual void method( float ) = 0;
};
struct mock_class : base_class
{
typedef base_class base_type; // this is required for MOCK_METHOD to work when not using MOCK_BASE_CLASS
MOCK_METHOD( method, 1 )
};
//]
}
namespace member_function_example_6
{
//[ member_function_example_6
MOCK_CLASS( mock_class )
{
MOCK_NON_CONST_METHOD( operator=, 1, mock_class&( const mock_class& ), assignment ) // operators require a custom identifier
};
//]
}
namespace member_function_example_7
{
//[ member_function_example_7
template< typename T >
MOCK_CLASS( mock_class )
{
MOCK_METHOD_TPL( method, 1, void( const T& ) ) // the _TPL variants must be used if the signature includes a template parameter of the class
};
//]
}
namespace member_function_example_8
{
//[ member_function_example_8
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 0, (std::map< int, int >()) ) // the signature must be surrounded with round parenthesis if the return type contains a comma
};
//]
}
namespace static_member_function_example_1
{
//[ static_member_function_example_1
MOCK_CLASS( mock_class )
{
MOCK_STATIC_METHOD( method, 1, float( int ) )
};
//]
}
namespace static_member_function_example_2
{
//[ static_member_function_example_2
template< typename T >
MOCK_CLASS( mock_class )
{
MOCK_STATIC_METHOD_TPL( method, 1, void( T ) )
};
//]
}
namespace constructor_example_1
{
//[ constructor_example_1
MOCK_CLASS( mock_class )
{
MOCK_CONSTRUCTOR( mock_class, 2, ( int, const std::string& ), identifier )
};
//]
}
namespace constructor_example_2
{
//[ constructor_example_2
template< typename T >
MOCK_CLASS( mock_class )
{
MOCK_CONSTRUCTOR( mock_class, 2, ( int, const std::string& ), identifier )
MOCK_CONSTRUCTOR_TPL( mock_class, 2, ( T, const std::string& ), identifier )
};
//]
}
namespace destructor_example_1
{
//[ destructor_example_1
MOCK_CLASS( mock_class )
{
MOCK_DESTRUCTOR( mock_class, destructor )
};
//]
}
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 )
};
//]
}
namespace conversion_operator_example_2
{
//[ 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 )
};
//]
}
namespace functor_example_1
{
//[ functor_example_1
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_mock_functor )
{
MOCK_FUNCTOR( f, void( int ) );
f( 3 );
}
//]
}
namespace functor_example_2
{
//[ functor_example_2
template< typename T >
struct mock_class
{
MOCK_FUNCTOR_TPL( f, void( T ) );
};
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_mock_functor )
{
mock_class< int > c;
c.f( 3 );
}
//]
}
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
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 1, int( int ), method )
MOCK_METHOD( method, 2, void( const std::string&, float ), method2 )
};
BOOST_AUTO_TEST_CASE( demonstrates_configuring_mock_objects )
{
mock_class c;
mock::sequence s;
MOCK_EXPECT( c.method ).once().with( 0 ).in( s ).returns( 42 );
MOCK_EXPECT( c.method2 ).never().with( "ok", mock::any );
MOCK_EXPECT( c.method2 ).at_least( 2 ).in( s ).throws( std::runtime_error( "error !" ) );
}
//]
}
namespace invocation_example_1
{
//[ invocation_example_1
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 2, void( int, const std::string& ) )
};
BOOST_AUTO_TEST_CASE( demonstrates_setting_up_invocations_on_a_mock_method )
{
mock_class c;
MOCK_EXPECT( c.method ).once(); // can only be called once
MOCK_EXPECT( c.method ); // can be called an unlimited number of times
}
//]
}
namespace invocation_example_2
{
//[ invocation_example_2
BOOST_AUTO_TEST_CASE( demonstrates_setting_up_an_invocation_on_a_mock_functor )
{
MOCK_FUNCTOR( f, void( int, const std::string& ) );
MOCK_EXPECT( f ).once();
}
//]
}
namespace invocation_example_3
{
//[ invocation_example_3
MOCK_FUNCTION( f, 1, float( int ) )
BOOST_AUTO_TEST_CASE( demonstrates_setting_up_an_invocation_on_a_mock_function )
{
MOCK_EXPECT( f ).once();
}
//]
}
namespace invocation_example_4
{
//[ invocation_example_4
MOCK_CLASS( mock_class )
{
MOCK_STATIC_METHOD( method, 1, float( int ) )
};
BOOST_AUTO_TEST_CASE( demonstrates_setting_up_an_invocation_on_a_mock_static_method )
{
mock_class c;
MOCK_EXPECT( c.method ).once();
MOCK_EXPECT( mock_class::method ).once(); // does the same
}
//]
}
namespace constraints_example_1
{
//[ constraints_example_1
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 2, void( int, const std::string& ) )
};
BOOST_AUTO_TEST_CASE( demonstrates_adding_builtin_constraints )
{
mock_class c;
MOCK_EXPECT( c.method ).with( mock::equal( 3 ), mock::equal( "some string" ) );
MOCK_EXPECT( c.method ).with( 3, "some string" ); // equivalent to the previous one using short-cuts
}
//]
}
namespace constraints_example_2
{
//[ constraints_example_2
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 1, void( int ) )
};
bool custom_constraint( int actual )
{
return actual == 42;
}
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_a_free_function )
{
mock_class c;
MOCK_EXPECT( c.method ).with( &custom_constraint );
}
//]
}
namespace constraints_example_3
{
//[ constraints_example_3
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 1, void( int ) )
};
bool custom_constraint( int expected, int actual )
{
return expected == actual;
}
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_a_standard_library_functor )
{
mock_class c;
MOCK_EXPECT( c.method ).with( std::bind1st( std::ptr_fun( &custom_constraint ), 42 ) ); // std::ptr_fun creates an std::unary_function
}
//]
}
namespace constraints_example_4
{
//[ constraints_example_4
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 1, void( int ) )
};
bool custom_constraint( int expected, int actual )
{
return expected == actual;
}
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_boost_bind )
{
mock_class c;
MOCK_EXPECT( c.method ).with( boost::bind( &custom_constraint, 42, _1 ) );
}
//]
}
#include <boost/lambda/lambda.hpp>
namespace constraints_example_5
{
//[ constraints_example_5
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 1, void( int ) )
};
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_boost_lambda )
{
mock_class c;
MOCK_EXPECT( c.method ).with( boost::lambda::_1 == 42 );
}
//]
}
#include <boost/phoenix/phoenix.hpp>
namespace constraints_example_6
{
//[ constraints_example_6
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 1, void( int ) )
};
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_boost_phoenix )
{
mock_class c;
MOCK_EXPECT( c.method ).with( boost::phoenix::arg_names::arg1 == 42 );
MOCK_EXPECT( c.method ).with( boost::phoenix::arg_names::_1 == 42 );
}
//]
}
#if !defined(BOOST_NO_CXX11_LAMBDAS) && !defined(BOOST_NO_LAMBDAS)
namespace constraints_example_7
{
//[ constraints_example_7
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 1, void( int ) )
};
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_constraint_with_cxx11_lambda )
{
mock_class c;
MOCK_EXPECT( c.method ).with( []( int actual ) { return 42 == actual; } );
}
//]
}
#endif
namespace constraints_example_8
{
//[ constraints_example_8
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 2, void( int, const std::string& ) )
};
BOOST_AUTO_TEST_CASE( demonstrates_combining_constraints )
{
mock_class c;
MOCK_EXPECT( c.method ).with( mock::less( 4 ) && mock::greater( 2 ), ! mock::equal( "" ) );
}
//]
}
namespace sequence_example_1
{
//[ sequence_example_1
MOCK_CLASS( mock_class_1 )
{
MOCK_METHOD( method_1, 0, void() )
};
MOCK_CLASS( mock_class_2 )
{
MOCK_METHOD( method_2, 0, void() )
};
MOCK_CLASS( mock_class_3 )
{
MOCK_METHOD( method_3, 0, void() )
};
BOOST_AUTO_TEST_CASE( demonstrates_enforcing_several_expectation_orders )
{
mock_class_1 c_1;
mock_class_2 c_2;
mock_class_3 c_3;
mock::sequence s_1, s_2;
MOCK_EXPECT( c_1.method_1 ).in( s_1 );
MOCK_EXPECT( c_2.method_2 ).in( s_2 ); // c_1.method_1 and c_2.method_2 are in different sequences and can be called in any order
MOCK_EXPECT( c_3.method_3 ).in( s_1, s_2 ); // c_3.method_3 must be called after both c_1.method_1 and c_2.method_2
}
//]
}
namespace action_example_1
{
//[ action_example_1
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 1, int( int ) )
};
int function( int i )
{
return i;
}
BOOST_AUTO_TEST_CASE( demonstrates_configuring_actions )
{
mock_class c;
MOCK_EXPECT( c.method ).returns( 42 );
MOCK_EXPECT( c.method ).throws( std::runtime_error( "error !" ) );
MOCK_EXPECT( c.method ).calls( &function ); // forwards 'method' parameter to 'function'
MOCK_EXPECT( c.method ).calls( boost::bind( &function, 42 ) ); // drops 'method' parameter and binds 42 as parameter to 'function'
MOCK_EXPECT( c.method ).calls( []( int i ) { return i; } ); // uses a C++11 lambda
}
//]
}
namespace verification_example_1
{
//[ verification_example_1
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 0, void() )
};
BOOST_AUTO_TEST_CASE( demonstrates_verifying_a_mock_method )
{
mock_class c;
MOCK_VERIFY( c.method ); // logs an error and returns false if not all expectations are met
mock::verify( c ); // verifies all expectations set for all methods of 'c'
mock::verify(); // verifies all existing mock objects, functions and functors
}
//]
}
namespace verification_example_2
{
//[ verification_example_2
BOOST_AUTO_TEST_CASE( demonstrates_verifying_a_mock_functor )
{
MOCK_FUNCTOR( f, void( int ) );
MOCK_VERIFY( f ); // logs an error and returns false if not all expectations are met
mock::verify( f ); // behaves the same as MOCK_VERIFY
mock::verify(); // verifies all existing mock objects, functions and functors
}
//]
}
namespace verification_example_3
{
//[ verification_example_3
MOCK_FUNCTION( f, 1, void( int ) )
BOOST_AUTO_TEST_CASE( demonstrates_verifying_a_mock_function )
{
MOCK_VERIFY( f ); // logs an error and returns false if not all expectations are met
mock::verify(); // verifies all existing mock objects, functions and functors
}
//]
}
namespace verification_example_4
{
//[ verification_example_4
MOCK_CLASS( mock_class )
{
MOCK_STATIC_METHOD( method, 0, void() )
};
BOOST_AUTO_TEST_CASE( demonstrates_verifying_a_static_mock_method )
{
mock_class c;
MOCK_VERIFY( c.method ); // logs an error and returns false if not all expectations are met
MOCK_VERIFY( mock_class::method ); // does the same
mock::verify(); // verifies all existing mock objects, functions and functors
}
//]
}
namespace reset_example_1
{
//[ reset_example_1
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 0, void() )
};
BOOST_AUTO_TEST_CASE( demonstrates_resetting_a_mock_method )
{
mock_class c;
MOCK_RESET( c.method ); // resets all expectations set for 'c.method'
mock::reset( c ); // resets all expectations set on 'c'
mock::reset(); // resets all existing mock objects, functions and functors
}
//]
}
namespace reset_example_2
{
//[ reset_example_2
BOOST_AUTO_TEST_CASE( demonstrates_resetting_a_mock_functor )
{
MOCK_FUNCTOR( f, void( int ) );
MOCK_RESET( f ); // resets all expectations set for 'f'
mock::reset( f ); // behaves the same as MOCK_RESET
mock::reset(); // resets all existing mock objects, functions and functors
}
//]
}
namespace reset_example_3
{
//[ reset_example_3
MOCK_FUNCTION( f, 1, void( int ) )
BOOST_AUTO_TEST_CASE( demonstrates_resetting_a_mock_function )
{
MOCK_RESET( f ); // resets all expectations set for 'f'
mock::reset(); // resets all existing mock objects, functions and functors
}
//]
}
namespace reset_example_4
{
//[ reset_example_4
MOCK_CLASS( mock_class )
{
MOCK_STATIC_METHOD( method, 0, void() )
};
BOOST_AUTO_TEST_CASE( demonstrates_resetting_a_static_mock_method )
{
mock_class c;
MOCK_RESET( c.method ); // resets all expectations set for 'c::method'
MOCK_RESET( mock_class::method ); // resets all expectations set for 'c::method'
mock::reset(); // resets all existing mock objects, functions and functors
}
//]
}
namespace helpers_example_1
{
//[ helpers_example_1
MOCK_UNARY_CONSTRAINT( any, true ) // this is (almost) how mock::any is defined
MOCK_UNARY_CONSTRAINT( forty_two, actual == 42 ) // this defines a 'forty_two' constraint
//]
}
namespace helpers_example_2
{
//[ helpers_example_2
MOCK_BINARY_CONSTRAINT( equal, actual == expected ) // this is how mock::equal is defined
MOCK_BINARY_CONSTRAINT( near, std::abs( actual - expected ) < 0.01 ) // this defines a 'near' constraint which can be used as 'near( 42 )'
//]
}

View file

@ -179,7 +179,7 @@ Finally another workaround would be to not use the macro at all :
struct my_mock : my_base_type< T1, T2 >, mock::object struct my_mock : my_base_type< T1, T2 >, mock::object
{}; {};
Note that [@www.boost.org/libs/utility/identity_type/doc/html/index.html Boost.IdentityType] is of little help here because the type is by essence very often abstract, which doesn't work well for some compilers (e.g. gcc)]. Note that [@www.boost.org/libs/utility/identity_type/doc/html/index.html Boost.IdentityType] is of little help here because the type is by essence very often abstract, which doesn't work well for some compilers (e.g. gcc).
[endsect] [endsect]

View file

@ -1,12 +1,11 @@
[section Reference] [section Reference]
[import example/reference.cpp]
This section describes the library syntax exhaustively. This section describes the library syntax exhaustively.
All source code snippets assume the following prerequisite : All source code snippets assume the following prerequisite :
#define BOOST_AUTO_TEST_MAIN [prerequisite]
#include <boost/test/auto_unit_test.hpp>
#include <turtle/mock.hpp>
[section Creation] [section Creation]
@ -31,215 +30,98 @@ Most of the time the identifier will be identical to the object name, but in cas
[section Class] [section Class]
The prefered form for defining a mock class is with using MOCK_CLASS and MOCK_BASE_CLASS, however equivalent alternatives exist without the macros.
Synopsis : Synopsis :
MOCK_CLASS( name ) MOCK_CLASS( name ) // defines a class
{ MOCK_BASE_CLASS( name, base ) // defines a class deriving from a base class
};
Example :
template< typename T >
MOCK_CLASS( name ) [class_example_1]
{
}; Example :
MOCK_BASE_CLASS( name, base ) [class_example_2]
{
}; Example :
template< typename T > [class_example_3]
MOCK_BASE_CLASS( name, base< T > )
{ Example :
};
[class_example_4]
struct name : mock::object // equivalent to using MOCK_CLASS
{ The preferred form for defining a mock class is with using MOCK_CLASS and MOCK_BASE_CLASS, however equivalent alternatives exist without the macros.
};
Example :
template< typename T >
struct name : mock::object // equivalent to using MOCK_CLASS [class_example_5]
{
}; Example :
struct name : base, mock::object // equivalent to using MOCK_BASE_CLASS [class_example_6]
{
typedef base base_type; Example :
};
[class_example_7]
template< typename T >
struct mock : base< T >, mock::object Example :
{
typedef typename base< T > base_type; [class_example_8]
};
Deriving from mock::object is optional but provides the additional following benefits : Deriving from mock::object is optional but provides the additional following benefits :
* the object acts as a composite to [link turtle.reference.verification verify] and [link turtle.reference.reset reset] all the expectations for all its methods at once * the object acts as a composite to [link turtle.reference.verification verify] and [link turtle.reference.reset reset] all the expectations for all its methods at once
* logs involving the object are enhanced because configuring an expectation for a method will set the class name for all the other methods as well * logs involving the object are enhanced because configuring an expectation for a method will set the class name for all the other methods as well
Example :
MOCK_CLASS( mock_class )
{
};
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_mock_class )
{
mock_class c;
}
Example :
template< typename T >
MOCK_CLASS( mock_class )
{
};
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_template_mock_class )
{
mock_class< int > c;
}
Example :
class base_class
{
};
MOCK_BASE_CLASS( mock_class, base_class )
{
};
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_derived_mock_class )
{
mock_class c;
}
Example :
template< typename T >
class base_class
{
};
template< typename T >
MOCK_BASE_CLASS( mock_class, base_class< T > )
{
};
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_template_derived_mock_class )
{
mock_class< int > c;
}
[endsect] [endsect]
[section Member function] [section Member function]
Synopsis : Synopsis :
MOCK_METHOD( name, arity ) // generates both const and non-const methods, only works in the context of a derived MOCK_BASE_CLASS or base_type typedef MOCK_METHOD( name, arity ) // generates both const and non-const methods, only works in the context of a derived MOCK_BASE_CLASS or base_type typedef
MOCK_METHOD( name, arity, signature[, identifier] ) // generates both const and non-const methods, if 'identifier' is omitted it will default to 'name' MOCK_METHOD( name, arity, signature[, identifier] ) // generates both const and non-const methods, if 'identifier' is omitted it will default to 'name'
MOCK_CONST_METHOD( name, arity, signature[, identifier] ) // generates only the const version of the method, if 'identifier' is omitted it will default to 'name' MOCK_CONST_METHOD( name, arity, signature[, identifier] ) // generates only the const version of the method, if 'identifier' is omitted it will default to 'name'
MOCK_NON_CONST_METHOD( name, arity, signature[, identifier] ) // generates only the non-const version of the method, if 'identifier' is omitted it will default to 'name' MOCK_NON_CONST_METHOD( name, arity, signature[, identifier] ) // generates only the non-const version of the method, if 'identifier' is omitted it will default to 'name'
MOCK_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_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_CONST_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_CONST_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_NON_CONST_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_NON_CONST_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'
[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] [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]
Example : [note The signature must be surrounded with round parenthesis if the return type contains a comma]
class base_class
{
virtual void method( int ) = 0;
};
MOCK_BASE_CLASS( mock_class, base_class )
{
MOCK_METHOD( method, 1 ) // only possible when referring unambiguously to a base class method
};
Example : Example :
class base_class [member_function_example_1]
{
virtual void method( int, const std::string& ) = 0; Example :
virtual void method( float ) = 0;
}; [member_function_example_2]
MOCK_BASE_CLASS( mock_class, base_class )
{
MOCK_METHOD( method, 2, void( int, const std::string& ), identifier_1 ) // MOCK_METHOD cannot be used because of overloading
MOCK_METHOD( method, 1, void( float ), identifier_2 ) // the identifier must differ from the previous one in order to fully disambiguate methods
};
[note For a compiler not supporting variadic macros MOCK_METHOD_EXT must be used instead] [note For a compiler not supporting variadic macros MOCK_METHOD_EXT must be used instead]
Example : Example :
class base_class [member_function_example_3]
{
virtual void method( float ) = 0;
virtual void method( float ) const = 0;
};
MOCK_BASE_CLASS( mock_class, base_class )
{
MOCK_METHOD( method, 1 ) // this generates both const and non-const versions
};
Example : Example :
class base_class [member_function_example_4]
{
virtual void method( float ) = 0;
virtual void method( float ) const = 0;
};
MOCK_BASE_CLASS( mock_class, base_class )
{
MOCK_CONST_METHOD( method, 1, void( float ), identifier_1 ) // this generates only the const version
MOCK_NON_CONST_METHOD( method, 1, void( float ), identifier_2 ) // this generates only the non-const version, with a different identifier
};
Example : Example :
class base_class [member_function_example_6]
{
virtual void method( float ) = 0;
};
struct mock_class : base_class
{
typedef base_class base_type; // this is required for MOCK_METHOD to work when not using MOCK_BASE_CLASS
MOCK_METHOD( method, 1 )
};
Example : Example :
MOCK_CLASS( mock_class ) [member_function_example_7]
{
MOCK_NON_CONST_METHOD( operator=, 1, mock_class&( const mock_class& ), assignment ) // operators need a custom identifier
};
Example : Example :
template< typename T > [member_function_example_8]
MOCK_CLASS( mock_class )
{
MOCK_METHOD_TPL( method, 1, void( const T& ) ) // the _TPL variants must be used if the signature includes a template parameter of the class
};
If 'signature' has a return type which contains a comma, for instance std::map< int, int >(), then 'signature' must be surrounded with round parenthesis, for instance :
struct mock_class
{
MOCK_METHOD( method, 0, (std::map< int, int >()) )
};
[endsect] [endsect]
@ -248,16 +130,18 @@ If 'signature' has a return type which contains a comma, for instance std::map<
Synopsis : Synopsis :
MOCK_STATIC_METHOD( name, arity, signature[, identifier] ) // if 'identifier' is omitted it will default to 'name' MOCK_STATIC_METHOD( 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( 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]
Example : Example :
MOCK_CLASS( mock_class ) [static_member_function_example_1]
{
MOCK_STATIC_METHOD( method, 1, float( int ) )
};
[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] Example :
[static_member_function_example_2]
[endsect] [endsect]
@ -266,25 +150,18 @@ Example :
Synopsis : Synopsis :
MOCK_CONSTRUCTOR( name, arity, parameters, identifier ) MOCK_CONSTRUCTOR( 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( 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] [note As constructors do not have a return type, the usual signature gets restricted here to just the parameters]
Example : Example :
MOCK_CLASS( mock_class ) [constructor_example_1]
{
MOCK_CONSTRUCTOR( mock_class, 2, ( int, const std::string& ), identifier )
};
Example : Example :
template< typename T > [constructor_example_2]
MOCK_CLASS( mock_class )
{
MOCK_CONSTRUCTOR( mock_class, 2, ( int, const std::string& ), identifier )
MOCK_CONSTRUCTOR_TPL( mock_class, 2, ( T, const std::string& ), identifier )
};
[endsect] [endsect]
@ -294,14 +171,11 @@ Synopsis :
MOCK_DESTRUCTOR( name, identifier ) MOCK_DESTRUCTOR( 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]
Example : Example :
MOCK_CLASS( mock_class ) [destructor_example_1]
{
MOCK_DESTRUCTOR( mock_class, destructor )
};
[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]
[endsect] [endsect]
@ -309,9 +183,9 @@ Example :
Synopsis : Synopsis :
MOCK_CONVERSION_OPERATOR( type, identifier ) // this generates both const and non-const operators MOCK_CONVERSION_OPERATOR( type, identifier ) // generates both const and non-const operators
MOCK_CONST_CONVERSION_OPERATOR( type, identifier ) MOCK_CONST_CONVERSION_OPERATOR( type, identifier ) // generates only a const operator
MOCK_NON_CONST_CONVERSION_OPERATOR( type, identifier ) MOCK_NON_CONST_CONVERSION_OPERATOR( 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_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_CONST_CONVERSION_OPERATOR_TPL( type, identifier ) // must be used if the signature uses a template parameter of the class
@ -319,21 +193,11 @@ Synopsis :
Example : Example :
MOCK_CLASS( mock_class ) [conversion_operator_example_1]
{
MOCK_CONVERSION_OPERATOR( int, conversion_to_int )
MOCK_CONST_CONVERSION_OPERATOR( const std::string&, conversion_to_string )
};
Example : Example :
template< typename T > [conversion_operator_example_2]
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&, const_conversion_to_string )
};
[endsect] [endsect]
@ -347,27 +211,11 @@ Synopsis :
Example : Example :
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_mock_functor ) [functor_example_1]
{
MOCK_FUNCTOR( f, void( int ) );
}
Example : Example :
namespace [functor_example_2]
{
template< typename T >
struct template_class
{
MOCK_FUNCTOR_TPL( f, void( T ) );
};
}
BOOST_AUTO_TEST_CASE( demonstrates_instantiating_a_mock_template_functor )
{
template_class< int > c;
c.f( 3 );
}
[endsect] [endsect]
@ -377,11 +225,11 @@ Synopsis :
MOCK_FUNCTION( name, arity, signature[, identifier] ) // if 'identifier' is omitted it will default to 'name' 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 free 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 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]
Example : Example :
MOCK_FUNCTION( mock_function, 1, float( int ) ) [function_example_1]
[endsect] [endsect]
@ -399,23 +247,12 @@ Synopsis :
Example : Example :
MOCK_CLASS( mock_class ) [expectation_example_1]
{
MOCK_METHOD( method, 0, int( int ), method )
MOCK_METHOD( method, 0, void( const std::string&, float ), method2 )
};
BOOST_AUTO_TEST_CASE( demonstrates_configuring_mock_objects )
{
mock_class c;
mock::sequence s;
MOCK_EXPECT( c.method ).once().with( 0 ).in( s ).returns( 42 );
MOCK_EXPECT( c.method2 ).never().with( "ok", mock::any );
MOCK_EXPECT( c.method2 ).at_least( 2 ).in( s ).throws( std::runtime_error() );
}
[section Invocation] [section Invocation]
An invocation defines how many times a mock object is to be exercised.
Synopsis : Synopsis :
MOCK_EXPECT( identifier ); // any number of times including never MOCK_EXPECT( identifier ); // any number of times including never
@ -428,51 +265,26 @@ Synopsis :
Example : Example :
MOCK_CLASS( mock_class ) [invocation_example_1]
{
MOCK_METHOD( method, 2, void( int, const std::string& ) )
};
BOOST_AUTO_TEST_CASE( demonstrates_setting_up_invocations_on_a_mock_method )
{
mock_class c;
MOCK_EXPECT( c.method ).once(); // can only be called once
MOCK_EXPECT( c.method ); // can be called an unlimited number of times
}
Example : Example :
BOOST_AUTO_TEST_CASE( demonstrates_setting_up_an_invocation_on_a_mock_functor ) [invocation_example_2]
{
MOCK_FUNCTOR( f, void( int, const std::string& ) );
MOCK_EXPECT( f );
}
Example : Example :
MOCK_FUNCTION( free_function, 1, float( int ) ) [invocation_example_3]
BOOST_AUTO_TEST_CASE( demonstrates_setting_up_an_invocation_on_a_mock_function )
{
MOCK_EXPECT( free_function ).once();
}
Example : Example :
MOCK_CLASS( mock_class ) [invocation_example_4]
{
MOCK_STATIC_METHOD( method, 1, float( int ) )
};
BOOST_AUTO_TEST_CASE( demonstrates_setting_up_an_invocation_on_a_mock_static_method )
{
MOCK_EXPECT( mock_class::method ).once();
}
[endsect] [endsect]
[section Constraints] [section Constraints]
A constraint validates the actual parameter value of a call to a mock object.
Synopsis : Synopsis :
MOCK_EXPECT( identifier ).with( constraint_1, constraint_2, ... ); MOCK_EXPECT( identifier ).with( constraint_1, constraint_2, ... );
@ -522,170 +334,72 @@ Constraints :
Example : Example :
MOCK_CLASS( mock_class ) [constraints_example_1]
{
MOCK_METHOD( method, 2, void( int, const std::string& ) )
};
BOOST_AUTO_TEST_CASE( demonstrates_adding_builtin_constraints )
{
mock_class c;
MOCK_EXPECT( c.method ).with( mock::equal( 3 ), mock::equal( "some string" ) );
MOCK_EXPECT( c.method ).with( 3, "some string" ); // equivalent to the previous one using short-cuts
}
Example using a function pointer : Example using a function pointer :
bool custom_constraint( int actual ) [constraints_example_2]
{
return actual == 42;
}
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_a_free_function )
{
mock_class c;
MOCK_EXPECT( c.method ).with( &custom_constraint );
}
Example using a standard library functor : Example using a standard library functor :
bool custom_constraint( int expected, int actual ) [constraints_example_3]
{
return expected == actual;
}
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_a_standard_library_functor )
{
mock_class c;
MOCK_EXPECT( c.method ).with( std::bind1st( std::ptr_fun( &custom_constraint ), 42 ) ); // std::ptr_fun creates an std::unary_function
}
Example using [@http://www.boost.org/libs/bind Boost.Bind] : Example using [@http://www.boost.org/libs/bind Boost.Bind] :
bool custom_constraint( int expected, int actual ) [constraints_example_4]
{
return expected == actual;
}
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_boost_bind )
{
mock_class c;
MOCK_EXPECT( c.method ).with( boost::bind( &custom_constraint, 42, _1 ) ) );
}
Example using [@http://www.boost.org/libs/lambda Boost.Lambda] : Example using [@http://www.boost.org/libs/lambda Boost.Lambda] :
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_boost_lambda ) [constraints_example_5]
{
mock_class c;
MOCK_EXPECT( c.method ).with( boost::lambda::_1 == 42 );
}
Example using [@http://www.boost.org/libs/phoenix Boost.Phoenix] : Example using [@http://www.boost.org/libs/phoenix Boost.Phoenix] :
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_custom_constraint_with_boost_phoenix ) [constraints_example_6]
{
mock_class c;
MOCK_EXPECT( c.method ).with( boost::phoenix::arg_names::arg1 == 42 );
MOCK_EXPECT( c.method ).with( boost::phoenix::arg_names::_1 == 42 );
}
Example using C++11 lambdas : Example using C++11 lambdas :
BOOST_AUTO_TEST_CASE( demonstrates_adding_a_constraint_with_cxx11_lambda ) [constraints_example_7]
{
mock_class c;
MOCK_EXPECT( c.method ).with( []( int actual ) { return 42 == actual; } );
}
Example using &&, || and ! : Example using &&, || and ! :
BOOST_AUTO_TEST_CASE( demonstrates_combining_constraints ) [constraints_example_8]
{
mock_class c;
MOCK_EXPECT( c.method ).with( mock::less( 4 ) && mock::greater( 2 ), ! mock::equal( "" ) );
}
[endsect] [endsect]
[section Sequence] [section Sequence]
A sequence enforces a given order between two or more expectations.
Synopsis : Synopsis :
mock::sequence s_1, s_2; MOCK_EXPECT( identifier_1 ).in( sequence_1 [, sequence_2 [, ...]] );
MOCK_EXPECT( identifier_1 ).in( s_1 );
MOCK_EXPECT( identifier_2 ).in( s_1, s_2 ); Each sequence is an instance of mock::sequence.
The maximum number of sequences that can be set is MOCK_MAX_SEQUENCES which defaults to 10. If needed the value can be increased before including the library :
#define MOCK_MAX_SEQUENCES 12
#include <turtle/mock.hpp>
Example : Example :
MOCK_CLASS( mock_class_1 ) [sequence_example_1]
{
MOCK_METHOD( method_1, 0, void() )
};
MOCK_CLASS( mock_class_2 )
{
MOCK_METHOD( method_2, 0, void() )
};
BOOST_AUTO_TEST_CASE( demonstrates_enforcing_expectations_order )
{
mock_class_1 c_1;
mock_class_2 c_2;
mock::sequence s;
MOCK_EXPECT( c_1.method_1 ).in( s );
MOCK_EXPECT( c_2.method_2 ).in( s );
}
Example of setting several sequences :
MOCK_CLASS( mock_class )
{
MOCK_METHOD( method, 0, void() )
};
BOOST_AUTO_TEST_CASE( demonstrates_enforcing_several_expectation_orders )
{
mock_class c;
mock::sequence s_1, s_2;
MOCK_EXPECT( c.method ).in( s_1, s_2 );
}
The maximum number of sequences that can be passed to ['in] is MOCK_MAX_SEQUENCES which defaults to 5. If needed the value can be increased before including the library :
#define MOCK_MAX_SEQUENCES 7
#include <mock/turtle.hpp>
[endsect] [endsect]
[section Actions] [section Actions]
An action performs additional treatments after an expectation has been deemed valid.
Synopsis : Synopsis :
MOCK_EXPECT( identifier ).returns( value ); MOCK_EXPECT( identifier ).returns( value );
MOCK_EXPECT( identifier ).throws( exception ); MOCK_EXPECT( identifier ).throws( exception );
MOCK_EXPECT( identifier ).calls( functor ); // throws std::invalid_argument if ! ['functor] MOCK_EXPECT( identifier ).calls( functor ); // gets assigned to a boost::function and throws std::invalid_argument if empty
Example : Example :
MOCK_CLASS( mock_class ) [action_example_1]
{
MOCK_METHOD( method, 0, int( int ) )
};
int function( int i )
{
return i;
}
BOOST_AUTO_TEST_CASE( demonstrates_configuring_actions )
{
mock_class c;
MOCK_EXPECT( c.method ).returns( 42 );
MOCK_EXPECT( c.method ).throws( std::runtime_error( "error !" ) );
MOCK_EXPECT( c.method ).calls( &function ); // forwards 'method' parameter to 'function'
MOCK_EXPECT( c.method ).calls( boost::bind( &function, 42 ) ); // drops 'method' parameter and binds 42 as parameter to 'function'
MOCK_EXPECT( c.method ).calls( []( int i ) { return i; } ); // uses a C++11 lambda
}
[endsect] [endsect]
@ -696,9 +410,9 @@ Example :
Synopsis : Synopsis :
MOCK_VERIFY( identifier ); MOCK_VERIFY( identifier );
mock::verify( object ); // verifies all expectations set for all methods of 'object' which must be an instance of a class created using MOCK_CLASS or MOCK_BASE_CLASS, or inherit mock::object mock::verify( object ); // verifies all expectations for all methods of 'object' which must be an instance of a class created using MOCK_CLASS or MOCK_BASE_CLASS, or inherit mock::object
mock::verify( functor ); // verifies all expectations set for 'functor' which must be an instance of a functor created using MOCK_FUNCTOR mock::verify( functor ); // verifies all expectations for 'functor' which must be an instance of a functor created using MOCK_FUNCTOR
mock::verify(); // verifies all existing mock objects, functions and functors mock::verify(); // verifies all expectations for all mock objects, functions and functors
[note These calls all return a boolean indicating whether the verification was successful or not, however usually simply calling them is enough because a failing verification will be logged to the test framework.] [note These calls all return a boolean indicating whether the verification was successful or not, however usually simply calling them is enough because a failing verification will be logged to the test framework.]
@ -708,52 +422,19 @@ Synopsis :
Example : Example :
MOCK_CLASS( mock_class ) [verification_example_1]
{
MOCK_METHOD( method, 0, void() )
};
BOOST_AUTO_TEST_CASE( demonstrates_verifying_a_mock_method )
{
mock_class c;
MOCK_VERIFY( c.method ); // logs an error and returns false if not all expectations are met
mock::verify( c ); // verifies all expectations set for all methods of 'c'
mock::verify(); // verifies all existing mock objects, functions and functors
}
Example : Example :
BOOST_AUTO_TEST_CASE( demonstrates_verifying_a_mock_functor ) [verification_example_2]
{
MOCK_FUNCTOR( f, void( int ) );
MOCK_VERIFY( f ); // logs an error and returns false if not all expectations are met
mock::verify( f ); // behaves the same as MOCK_VERIFY
mock::verify(); // verifies all existing mock objects, functions and functors
}
Example : Example :
MOCK_FUNCTION( f, 1, void( int ) ) [verification_example_3]
BOOST_AUTO_TEST_CASE( demonstrates_verifying_a_mock_function )
{
MOCK_VERIFY( f ); // logs an error and returns false if not all expectations are met
mock::verify(); // verifies all existing mock objects, functions and functors
}
Example : Example :
MOCK_CLASS( mock_class ) [verification_example_4]
{
MOCK_STATIC_METHOD( method, 0, void() )
};
BOOST_AUTO_TEST_CASE( demonstrates_verifying_a_static_mock_method )
{
mock_class c;
MOCK_VERIFY( c::method ); // logs an error and returns false if not all expectations are met
mock::verify(); // verifies all existing mock objects, functions and functors
}
[endsect] [endsect]
@ -762,56 +443,25 @@ Example :
Synopsis : Synopsis :
MOCK_RESET( identifier ); MOCK_RESET( identifier );
mock::reset( object ); // resets all expectations set for all methods of 'object' which must be an instance of a class created using MOCK_CLASS or MOCK_BASE_CLASS, or inherit mock::object mock::reset( object ); // resets all expectations for all methods of 'object' which must be an instance of a class created using MOCK_CLASS or MOCK_BASE_CLASS, or inherit mock::object
mock::reset(); // resets all existing mock objects, functions and functors mock::reset( functor ); // resets all expectations for 'functor' which must be an instance of a functor created using MOCK_FUNCTOR
mock::reset(); // resets all expectations for all mock objects, functions and functors
Example : Example :
MOCK_CLASS( mock_class ) [reset_example_1]
{
MOCK_METHOD( method, 0, void() )
};
BOOST_AUTO_TEST_CASE( demonstrates_resetting_a_mock_method )
{
mock_class c;
MOCK_RESET( c.method ); // resets all expectations set for 'c.method'
mock::reset( c ); // resets all expectations set on 'c'
mock::reset(); // resets all existing mock objects, functions and functors
}
Example : Example :
BOOST_AUTO_TEST_CASE( demonstrates_resetting_a_mock_functor ) [reset_example_2]
{
MOCK_FUNCTOR( f, void( int ) );
MOCK_RESET( f ); // resets all expectations set for 'f'
mock::reset(); // resets all existing mock objects, functions and functors
}
Example : Example :
MOCK_FUNCTION( f, 1, void( int ) ) [reset_example_3]
BOOST_AUTO_TEST_CASE( demonstrates_resetting_a_mock_function )
{
MOCK_RESET( f ); // resets all expectations set for 'f'
mock::reset(); // resets all existing mock objects, functions and functors
}
Example : Example :
MOCK_CLASS( mock_class ) [reset_example_4]
{
MOCK_STATIC_METHOD( method, 0, void() )
};
BOOST_AUTO_TEST_CASE( demonstrates_resetting_a_static_mock_method )
{
mock_class c;
MOCK_RESET( c::method ); // resets all expectations set for 'c::method'
mock::reset(); // resets all existing mock objects, functions and functors
}
[endsect] [endsect]
@ -829,8 +479,7 @@ The expression manipulates the received parameter 'actual' in order to implement
Example : Example :
MOCK_UNARY_CONSTRAINT( any, true ) // this is (almost) how mock::any is defined [helpers_example_1]
MOCK_UNARY_CONSTRAINT( forty_two, actual == 42 ) // this defines a 'forty_two' constraint
[endsect] [endsect]
@ -846,8 +495,7 @@ The expression manipulates the received parameter 'actual' as well as the passed
Example : Example :
MOCK_BINARY_CONSTRAINT( equal, actual == expected ) // this is how mock::equal is defined [helpers_example_2]
MOCK_BINARY_CONSTRAINT( near, std::abs( actual - expected ) < 0.01 ) // this defines a 'near' constraint which can be used as 'near( 42 )'
[endsect] [endsect]