mirror of
https://github.com/mat007/turtle.git
synced 2026-06-22 12:13:43 +00:00
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:
parent
728dfd06eb
commit
e43059423e
1 changed files with 20 additions and 11 deletions
|
|
@ -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
|
||||
{
|
||||
int receivedValue = 0;
|
||||
void setValue(int newValue)
|
||||
// Some test-only code to verify what is described
|
||||
static int receivedValue = 0;
|
||||
static void someFunctor(int newValue)
|
||||
{
|
||||
receivedValue = newValue;
|
||||
}
|
||||
}
|
||||
void function( base_class& c)
|
||||
// Check that the functor was called with 42
|
||||
struct CheckReceivedValue
|
||||
{
|
||||
c.method(setValue);
|
||||
void teardown()
|
||||
{
|
||||
BOOST_CHECK(receivedValue == 42); // functor was called and received the value 42
|
||||
}
|
||||
};
|
||||
// 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
|
||||
}
|
||||
//]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue