diff --git a/doc/example/patterns_invoke_functor.cpp b/doc/example/patterns_invoke_functor.cpp index 01f7809..a66e471 100644 --- a/doc/example/patterns_invoke_functor.cpp +++ b/doc/example/patterns_invoke_functor.cpp @@ -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 + +static void someFunctor(int newValue); //[ invoke_functor_problem #include @@ -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 @@ -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 } //]