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

struct stub::compare_call

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

Template parameters

template <class... Args>
struct compare_call

Member functions (public)

  compare_call (WithArgs &&… expected)
bool compare (const arguments< Args… > & actual) const

Description

This stores a tuple of types that is to, at some point, be compared with a different tuple of arguments.

The basic usage is as follows:

auto expectation = std::make_tuple(5, true, 4.0);
compare_call<int, bool, double> expect(expectation);
auto actual = std::make_tuple(4, true, 4.0);

Now compare our expectation with the actual values:

bool result = expect.compare(actual);
assert(result == false);

The compare_call::compare (…) function uses compare_arguments(…) to compare the elements of the two tuples element-wise using compare_argument(…). In the case above the two tuples contain the same types. So every thing is pretty straight-forward.

However, in some cases we want to customize the comparison of two element. This could be that we simply don’t care about a specific value or we want to use a user-define comparison function.

To support this use-case we allow special values to be used in the comparison tuple. The two tuples can therefore have different types, making the implementation a bit more involved and the reason for the type-erausre implemented in this class.

Lets see how this would work in practice.

Notice how we can pass ignore() as the second argument to our expectation tuple:

auto expectation = std::make_tuple(5, ignore(), 4.0);
compare_call<int, bool, double> expect(expectation);
auto actual = std::make_tuple(5, true, 4.0);

Now compare our expectation with the actual values:

bool result = expect.compare(actual);
assert(result == true);

The reason this works when comparing the two tuples element-wise is that the compare_argument(…) function has an overload that accepts ignore as the second argument. By specializing compare_argument(…) we can extend support for more special values to support custom behaviour.

Member Function Descriptions

template <class… WithArgs>
compare_call (WithArgs &&… expected)
Construct a new call comparison.

bool compare (const arguments< Args… > & actual) const
Returns:
Compare the values of the passed tuple with those of the expectation.
Versions
7.1.1
7.1.0
7.0.0
Development
latest