Remove test-artifact from user visible part of how_to_invoke_a_functor_passed_as_parameter_of_a_mock_method

Only show what is required
This commit is contained in:
Alexander Grund 2022-01-06 15:10:27 +01:00
parent 728dfd06eb
commit e43059423e
No known key found for this signature in database
GPG key ID: AA48A0760367A42B

View file

@ -6,6 +6,10 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Intentionally duplicate to have complete examples and minimal user visible, yet tested test code
#include <boost/test/unit_test.hpp>
static void someFunctor(int newValue);
//[ invoke_functor_problem
#include <functional>
@ -15,21 +19,27 @@
virtual void method( const std::function< void( int ) >& functor ) = 0;
};
void function( base_class& ); // the function will call 'method' with a functor to be applied
// the function will call 'method' with a functor to be applied
void function(base_class& c) { c.method(someFunctor); }
//]
namespace
// Some test-only code to verify what is described
static int receivedValue = 0;
static void someFunctor(int newValue)
{
int receivedValue = 0;
void setValue(int newValue)
receivedValue = newValue;
}
// Check that the functor was called with 42
struct CheckReceivedValue
{
void teardown()
{
receivedValue = newValue;
BOOST_CHECK(receivedValue == 42); // functor was called and received the value 42
}
}
void function( base_class& c)
{
c.method(setValue);
}
};
// And force using it w/o showing that in the docs
#undef BOOST_AUTO_TEST_CASE
#define BOOST_AUTO_TEST_CASE(name) BOOST_FIXTURE_TEST_CASE(name, CheckReceivedValue)
//[ invoke_functor_solution
#include <boost/test/unit_test.hpp>
@ -48,6 +58,5 @@ BOOST_AUTO_TEST_CASE( how_to_invoke_a_functor_passed_as_parameter_of_a_mock_meth
mock_class mock;
MOCK_EXPECT( mock.method ).calls( [](const auto &functor){ functor(42); } ); // whenever 'method' is called, invoke the functor with 42
function( mock );
BOOST_CHECK(receivedValue == 42); // functor was called and received the value 42
}
//]