class function< R(Args…)>

Scope: stub

In header: #include <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.

Member types (public)

struct expectation

Member functions (public)

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

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: stub::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:

stub::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 return_handler.hpp

Member Function Description

const arguments< Args… > & call_arguments (uint32_t index)

Returns:
The arguments passed to the n’th call

uint32_t calls ()

Returns:
The number of times the call operator has been invoked

void clear ()

Removes all calls from the function object and reset the return handler.

void clear_calls ()

Clear the calls.

expectation expect_calls ()

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

bool no_calls ()

Returns:
True if no calls have been made otherwise false

R operator() (Args… args)

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

void print (std::ostream & out)

Prints the status of the function object to the std::ostream. Example (using the output operator): stub::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 stub::function status should be

return_handler set_return (Returns &&… return_value)