turtle/doc/example/patterns_invoke_functor.cpp
Alexander Grund 5ef17d0e33
Include examples in CI tests
Also fix and update examples and documentation where required
This allows to make sure examples are actually runnable avoiding them to
become outdated
2022-01-07 17:08:13 +01:00

53 lines
1.2 KiB
C++

// http://turtle.sourceforge.net
//
// Copyright Mathieu Champlon 2014
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//[ invoke_functor_problem
#include <functional>
class base_class
{
public:
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
//]
namespace
{
int receivedValue = 0;
void setx(int newValue)
{
receivedValue = newValue;
}
}
void function( base_class& c)
{
c.method(setx);
}
//[ invoke_functor_solution
#include <boost/test/auto_unit_test.hpp>
#include <turtle/mock.hpp>
namespace
{
MOCK_BASE_CLASS( mock_class, base_class )
{
MOCK_METHOD( method, 1 )
};
}
BOOST_AUTO_TEST_CASE( how_to_invoke_a_functor_passed_as_parameter_of_a_mock_method )
{
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);
}
//]