Warning: This is a development version. The latest stable version is Version 7.1.1.

class stub::function< R(Args…)>

In header: #include <stub/function.hpp>

Brief description

The function object act like a “sink” for function calls i.e. we can define a function object to accept any type of function call and it will simply store the arguments for later inspection.

Template parameters

template <typename R, typename... Args>
class function< R(Args...)>

Member types (public)

struct expectation

Member functions (public)

R operator() (Args… args) const
return_handler< R > & set_return (Returns &&… return_value)
uint32_t calls () const
bool no_calls () const
const arguments< Args… > & call_arguments (uint32_t index) const
expectation expect_calls () const
void clear ()
void clear_calls ()
void print (std::ostream & out) const
void add_side_effect (std::function< void()> side_effect)

Description

The typical use-case for the function object is when testing that some code invokes a specific set of functions with a specific set of arguments.

Example:

function<void(uint32_t)> some_function;

The above function takes an uint32_t and returns nothing, lets invoke it:

some_function(3);
some_function(4);

Now we may check how the function was called:

bool called_once = some_function.expect_calls().with(3U);
assert(called_once == false);

bool called_with = some_function.expect_calls().with(4U);
assert(called_with == true);

We can also define a function which returns a value:

function<bool(uint32_t)> another_function;

Here we have to specify what return value we expect:

another_function.set_return(true);

bool a = another_function(23);
bool b = another_function(13);

assert(a == true);
assert(b == true);

For more information on the options for return values see the src/stub/return_handler.hpp

Member Function Descriptions

R operator() (Args… args) const

The call operator to “simulate” performing a function call.

Parameter args:
The arguments that should be stored
Returns:
The return value generated by the return_handler

template <class… Returns>
return_handler< R > & set_return (Returns &&… return_value)

Initializes the return_handler with the return values to use. Calling this function will also reset the return_handler state. So any previously specified returns values will be removed etc.

Parameter return_value:
The list of return values to use
Returns:
Reference to the return handler, this allows the caller to perform additional customization to the return handler such as turn on or off repeat.

uint32_t calls () const
Returns:
The number of times the call operator has been invoked

bool no_calls () const
Returns:
True if no calls have been made otherwise false

const arguments< Args… > & call_arguments (uint32_t index) const
Returns:
The arguments passed to the n’th call

expectation expect_calls () const

Used when we want to check whether the function object is in a certain state. See examples usage in the expectation struct member functions.

Returns:
An expectation object

void clear ()
Removes all calls from the function object and reset the return handler.

void clear_calls ()
Clear the calls.

void print (std::ostream & out) const

Prints the status of the function object to the std::ostream.

Example (using the output operator):

function<void(uint32_t)> my_func;

my_func(4U);
my_func(5U);

// Print the current status of the function object,
std::cout << my_func << std::endl;
Parameter out:
The ostream where the function status should be

void add_side_effect (std::function< void()> side_effect)

Add a side effect to the function object. A side effect is simply a function which is invoked every time the function object is invoked.

Example:

function<void(uint32_t)> my_func;

my_func.add_side_effect([]()
{
    std::cout << "Side effect" << std::endl;
});

my_func(4U);
my_func(5U);

// Prints:
// Side effect
// Side effect
Parameter side_effect:
The side effect to add
Versions
7.1.1
7.1.0
7.0.0
Development
latest