Adress review comments

This commit is contained in:
Alexander Grund 2020-09-24 22:50:22 +02:00
parent 279bb2c767
commit c7873cde4b
No known key found for this signature in database
GPG key ID: AA48A0760367A42B
13 changed files with 36 additions and 48 deletions

View file

@ -126,7 +126,7 @@ BOOST_AUTO_TEST_CASE( forty_one_plus_one_is_forty_two_plus_or_minus_one_near_cre
{ {
mock_view v; mock_view v;
calculator c( v ); calculator c( v );
int expected, threshold; int expected = 0, threshold = 0;
MOCK_EXPECT( v.display ).with( near( std::cref( expected ), std::cref( threshold ) ) ); MOCK_EXPECT( v.display ).with( near( std::cref( expected ), std::cref( threshold ) ) );
expected = 42; expected = 42;
threshold = 1; threshold = 1;

View file

@ -52,5 +52,7 @@ BOOST_AUTO_TEST_CASE(check_method_stub_is_called)
{ {
mock_base b; mock_base b;
MOCK_EXPECT(b.method).once().with(1); MOCK_EXPECT(b.method).once().with(1);
static_cast<base*>(&b)->method(1); // Example user code taking a base* (or smart pointer variant)
auto callMethod = [](base* bPtr){ bPtr->method(1); };
callMethod(&b);
} }

View file

@ -29,5 +29,5 @@ BOOST_AUTO_TEST_CASE(method_not_called_through_base)
{ {
mock_base b; mock_base b;
MOCK_EXPECT(b.method).never(); MOCK_EXPECT(b.method).never();
static_cast<base*>(&b)->method(); // Doesn't call the mocked method static_cast<base*>(&b)->method(); // Doesn't call the mocked method as asserted above
} }

View file

@ -90,8 +90,8 @@ std::string mock_concept::create< std::string >()
BOOST_AUTO_TEST_CASE(dispatch_methods_are_called) BOOST_AUTO_TEST_CASE(dispatch_methods_are_called)
{ {
mock_concept b; mock_concept b;
MOCK_EXPECT(b.create_int).once().returns(int{}); MOCK_EXPECT(b.create_int).once().returns(0);
MOCK_EXPECT(b.create_string).once().returns(std::string{}); MOCK_EXPECT(b.create_string).once().returns("");
function_under_test(b); function_under_test(b);
} }
} }

View file

@ -31,8 +31,8 @@ namespace mock_test
my_class::my_class( base_class& b): b(b){} my_class::my_class( base_class& b): b(b){}
void my_class::flush() void my_class::flush()
{ {
static int secret_value = 7; static int counter = 7;
if(--secret_value == 0) if(--counter == 0)
b.method(); b.method();
} }
} }
@ -59,15 +59,16 @@ namespace mock_test
{ {
MOCK_METHOD( method, 0 ) MOCK_METHOD( method, 0 )
}; };
}
BOOST_AUTO_TEST_CASE( method_is_called ) BOOST_AUTO_TEST_CASE( method_is_called )
{ {
using namespace mock_test;
mock_base_class m; mock_base_class m;
my_class c( m ); my_class c( m );
bool done = false; bool done = false;
MOCK_EXPECT( m.method ).once().calls( [&done](){ done = true; } ); MOCK_EXPECT( m.method ).once().calls( [&done](){ done = true; } );
check( done, [&c](){ c.flush(); } ); // just wait on done, flushing from time to time check( done, [&c](){ c.flush(); } ); // just wait on done, flushing from time to time
} }
}
//] //]

View file

@ -21,14 +21,14 @@
namespace namespace
{ {
int receivedValue = 0; int receivedValue = 0;
void setx(int newValue) void setValue(int newValue)
{ {
receivedValue = newValue; receivedValue = newValue;
} }
} }
void function( base_class& c) void function( base_class& c)
{ {
c.method(setx); c.method(setValue);
} }
//[ invoke_functor_solution //[ invoke_functor_solution
@ -48,6 +48,6 @@ BOOST_AUTO_TEST_CASE( how_to_invoke_a_functor_passed_as_parameter_of_a_mock_meth
mock_class mock; mock_class mock;
MOCK_EXPECT( mock.method ).calls( [](const auto &functor){ functor(42); } ); // whenever 'method' is called, invoke the functor with 42 MOCK_EXPECT( mock.method ).calls( [](const auto &functor){ functor(42); } ); // whenever 'method' is called, invoke the functor with 42
function( mock ); function( mock );
BOOST_CHECK(receivedValue == 42); BOOST_CHECK(receivedValue == 42); // functor was called and received the value 42
} }
//] //]

View file

@ -17,7 +17,6 @@ namespace
class my_class class my_class
{ {
base_class& b;
public: public:
explicit my_class( base_class& ); explicit my_class( base_class& );
@ -28,12 +27,13 @@ namespace
namespace namespace
{ {
my_class::my_class( base_class& b): b(b){} static base_class* global_b = nullptr;
my_class::my_class( base_class& b){ global_b = &b; }
void my_class::process() void my_class::process()
{ {
int secret_value = 42; int secret_value = 42;
b.method(secret_value); global_b->method(secret_value);
b.method(secret_value); global_b->method(secret_value);
} }
} }

View file

@ -438,7 +438,7 @@ BOOST_AUTO_TEST_CASE( demonstrates_configuring_mock_objects )
MOCK_EXPECT( c.method ).once().with( 0 ).in( s ).returns( 42 ); MOCK_EXPECT( c.method ).once().with( 0 ).in( s ).returns( 42 );
MOCK_EXPECT( c.method2 ).never().with( "ok", mock::any ); MOCK_EXPECT( c.method2 ).never().with( "ok", mock::any );
MOCK_EXPECT( c.method2 ).at_least( 2 ).in( s ).throws( std::runtime_error( "error !" ) ); MOCK_EXPECT( c.method2 ).at_least( 2 ).in( s ).throws( std::runtime_error( "error !" ) );
BOOST_CHECK(c.method(0) == 42); BOOST_TEST(c.method(0) == 42);
BOOST_CHECK_THROW(c.method("not ok", 1.f), std::runtime_error); BOOST_CHECK_THROW(c.method("not ok", 1.f), std::runtime_error);
BOOST_CHECK_THROW(c.method("not ok", 2.f), std::runtime_error); BOOST_CHECK_THROW(c.method("not ok", 2.f), std::runtime_error);
} }
@ -505,7 +505,7 @@ BOOST_AUTO_TEST_CASE( demonstrates_setting_up_an_invocation_on_a_mock_static_met
{ {
mock_class c; mock_class c;
MOCK_EXPECT( c.method1 ).once(); MOCK_EXPECT( c.method1 ).once();
MOCK_EXPECT( mock_class::method2 ).once(); // does the same MOCK_EXPECT( mock_class::method2 ).once(); // does the same (but for the other method)
c.method1(42); c.method1(42);
c.method2(42); c.method2(42);
} }
@ -525,7 +525,7 @@ BOOST_AUTO_TEST_CASE( demonstrates_adding_builtin_constraints )
{ {
mock_class c; mock_class c;
MOCK_EXPECT( c.method1 ).with( mock::equal( 3 ), mock::equal( "some string" ) ); MOCK_EXPECT( c.method1 ).with( mock::equal( 3 ), mock::equal( "some string" ) );
MOCK_EXPECT( c.method2 ).with( 3, "some string" ); // equivalent to the previous one using short-cuts MOCK_EXPECT( c.method2 ).with( 3, "some string" ); // similar to the previous one using short-cuts
c.method1(3, "some string"); c.method1(3, "some string");
c.method2(3, "some string"); c.method2(3, "some string");
} }
@ -756,12 +756,12 @@ BOOST_AUTO_TEST_CASE( demonstrates_configuring_actions )
MOCK_EXPECT( c.method ).once().calls( &function ); // forwards 'method' parameter to 'function' MOCK_EXPECT( c.method ).once().calls( &function ); // forwards 'method' parameter to 'function'
MOCK_EXPECT( c.method ).once().calls( std::bind( &function, 42 ) ); // drops 'method' parameter and binds 42 as parameter to 'function' MOCK_EXPECT( c.method ).once().calls( std::bind( &function, 42 ) ); // drops 'method' parameter and binds 42 as parameter to 'function'
MOCK_EXPECT( c.method ).once().calls( []( int i ) { return i; } ); // uses a C++11 lambda MOCK_EXPECT( c.method ).once().calls( []( int i ) { return i; } ); // uses a C++11 lambda
BOOST_CHECK(c.method(0) == 42); BOOST_TEST(c.method(0) == 42);
BOOST_CHECK(c.method(1) == 42); BOOST_TEST(c.method(1) == 42);
BOOST_CHECK_THROW(c.method(0), std::runtime_error); BOOST_CHECK_THROW(c.method(0), std::runtime_error);
BOOST_CHECK(c.method(2) == 2); BOOST_TEST(c.method(2) == 2);
BOOST_CHECK(c.method(3) == 42); BOOST_TEST(c.method(3) == 42);
BOOST_CHECK(c.method(4) == 4); BOOST_TEST(c.method(4) == 4);
} }
//] //]
} }

View file

@ -24,7 +24,7 @@
<< ')' \ << ')' \
<< lazy_expectations( this ) << lazy_expectations( this )
#define MOCK_MOVE(z, n, d) \ #define MOCK_FORWARD(z, n, d) \
std::forward< T##n >( t##n ) std::forward< T##n >( t##n )
namespace mock namespace mock
@ -229,7 +229,7 @@ namespace detail
for( const auto& expectation: expectations_ ) for( const auto& expectation: expectations_ )
{ {
if( expectation.is_valid( if( expectation.is_valid(
BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_MOVE, _) ) ) BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_FORWARD, _) ) )
{ {
if( ! expectation.invoke() ) if( ! expectation.invoke() )
{ {
@ -247,7 +247,7 @@ namespace detail
error_type::call( MOCK_FUNCTION_CONTEXT, expectation.file(), expectation.line() ); error_type::call( MOCK_FUNCTION_CONTEXT, expectation.file(), expectation.line() );
if( expectation.functor() ) if( expectation.functor() )
return expectation.functor()( return expectation.functor()(
BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_MOVE, _) ); BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_FORWARD, _) );
return expectation.trigger(); return expectation.trigger();
} }
} }
@ -314,4 +314,4 @@ namespace detail
#undef MOCK_FUNCTION_FORMAT #undef MOCK_FUNCTION_FORMAT
#undef MOCK_FUNCTION_CONTEXT #undef MOCK_FUNCTION_CONTEXT
#undef MOCK_MOVE #undef MOCK_FORWARD

View file

@ -8,7 +8,7 @@
#include "function_impl_template.hpp" #include "function_impl_template.hpp"
#define MOCK_MOVE(z, n, d) \ #define MOCK_FORWARD(z, n, d) \
std::forward< T##n >( t##n ) std::forward< T##n >( t##n )
namespace mock namespace mock
@ -65,7 +65,7 @@ namespace detail
R operator()( R operator()(
BOOST_PP_ENUM_BINARY_PARAMS(MOCK_NUM_ARGS, T, t) ) const BOOST_PP_ENUM_BINARY_PARAMS(MOCK_NUM_ARGS, T, t) ) const
{ {
return (*impl_)( BOOST_PP_ENUM(MOCK_NUM_ARGS, MOCK_MOVE, _) ); 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 )
@ -94,4 +94,4 @@ namespace detail
} }
} // mock } // mock
#undef MOCK_MOVE #undef MOCK_FORWARD

View file

@ -47,8 +47,7 @@
#define MOCK_FUNCTOR(f, ...) \ #define MOCK_FUNCTOR(f, ...) \
mock::detail::functor< MOCK_FUNCTION_TYPE(__VA_ARGS__) > f, f##_mock mock::detail::functor< MOCK_FUNCTION_TYPE(__VA_ARGS__) > f, f##_mock
/// MOCK_FUNCTOR_TPL( name, signature ) /// MOCK_FUNCTOR_TPL( name, signature )
/// Deprecated. Same as MOCK_FUNCTOR #define MOCK_FUNCTOR_TPL(f, ...) static_assert(false, "MOCK_FUNCTOR_TPL has been replaced by MOCK_FUNCTOR")
#define MOCK_FUNCTOR_TPL(f, ...) MOCK_FUNCTOR(f, __VA_ARGS__)
#define MOCK_HELPER(t) \ #define MOCK_HELPER(t) \
t##_mock( mock::detail::root, BOOST_PP_STRINGIZE(t) ) t##_mock( mock::detail::root, BOOST_PP_STRINGIZE(t) )

View file

@ -16,7 +16,7 @@ namespace
std::string to_string( const T& t) std::string to_string( const T& t)
{ {
std::ostringstream s; std::ostringstream s;
s << mock::detail::make_type_name(t); // Typename can be streamed s << mock::detail::make_type_name(t);
return s.str(); return s.str();
} }
} }

View file

@ -22,17 +22,3 @@ BOOST_AUTO_TEST_CASE( mock_constraint_is_supported_by_compilers_with_variadic_ma
BOOST_CHECK( constraint_1( 0 ).c_( 0 ) ); BOOST_CHECK( constraint_1( 0 ).c_( 0 ) );
BOOST_CHECK( constraint_2( 0, 0 ).c_( 0 ) ); BOOST_CHECK( constraint_2( 0, 0 ).c_( 0 ) );
} }
namespace
{
MOCK_CONSTRAINT_EXT( constraint_0_ext, 0,, actual == 0 )
MOCK_CONSTRAINT_EXT( constraint_1_ext, 1, ( expected ), actual == expected )
MOCK_CONSTRAINT_EXT( constraint_2_ext, 2, ( expected_0, expected_1 ), actual == expected_0 || actual == expected_1 )
}
BOOST_AUTO_TEST_CASE( mock_constraint_ext_is_supported_by_all_compilers )
{
BOOST_CHECK( constraint_0_ext.c_( 0 ) );
BOOST_CHECK( constraint_1_ext( 0 ).c_( 0 ) );
BOOST_CHECK( constraint_2_ext( 0, 0 ).c_( 0 ) );
}