Warning: This is an old version. The latest stable version is Version 7.1.1.
In header: #include <stub/function.hpp>
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 <typename R, typename... Args>
class function< R(Args...)>
struct | expectation |
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) |
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
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
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.
- Returns:
- The number of times the call operator has been invoked
- Returns:
- True if no calls have been made otherwise false
- Returns:
- The arguments passed to the n’th call
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
Removes all calls from the function object and reset the return handler.
Clear the calls.
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
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