Matchers check various argument properties.
More...
|
| consteval auto | mimicpp::matches::NaN () noexcept |
| | Tests, whether the floating-point target is NaN.
|
| |
| constexpr auto | mimicpp::matches::approx_abs (const std::floating_point auto value, const std::floating_point auto epsilon) |
| | Tests, whether the floating-point target is approximately equal to value.
|
| |
| constexpr auto | mimicpp::matches::approx_rel (const std::floating_point auto value, const std::floating_point auto relEpsilon) |
| | Tests, whether the floating-point target is approximately equal to value.
|
| |
| template<std::floating_point Float> |
| constexpr auto | mimicpp::matches::approx_rel (const Float value) |
| | Tests, whether the floating-point target is approximately equal to value.
|
| |
| template<typename T> |
| constexpr auto | mimicpp::matches::eq (T &&value) |
| | Tests, whether the target compares equal to the expected value.
|
| |
| template<typename T> |
| constexpr auto | mimicpp::matches::ne (T &&value) |
| | Tests, whether the target compares not equal to the expected value.
|
| |
| template<typename T> |
| constexpr auto | mimicpp::matches::lt (T &&value) |
| | Tests, whether the target is less than the expected value.
|
| |
| template<typename T> |
| constexpr auto | mimicpp::matches::le (T &&value) |
| | Tests, whether the target is less than or equal to the expected value.
|
| |
| template<typename T> |
| constexpr auto | mimicpp::matches::gt (T &&value) |
| | Tests, whether the target is greater than the expected value.
|
| |
| template<typename T> |
| constexpr auto | mimicpp::matches::ge (T &&value) |
| | Tests, whether the target is greater than or equal to the expected value.
|
| |
| template<typename UnaryPredicate> |
| constexpr auto | mimicpp::matches::predicate (UnaryPredicate &&predicate, StringViewT description="passes predicate", StringViewT invertedDescription="fails predicate") |
| | Tests, whether the target fulfills the given predicate.
|
| |
| template<util::satisfies< std::is_lvalue_reference > T> |
| constexpr auto | mimicpp::matches::instance (T &&instance) |
| | Tests, whether the target is the expected instance.
|
| |
Matchers check various argument properties.
Matchers can be used to check various argument properties and are highly customizable. In general, they simply compare their arguments with a pre-defined predicate, but also provide a meaningful description.
- Attention
- Matchers receive their arguments as possibly non-const, which is due to workaround some restrictions on const qualified views. Either way, matchers should never modify any of their arguments.
Matching arguments
In general matchers can be applied via the expect::arg<n> factory, but they can also be directly used at the expect statement.
and expect::arg<0>(matches::gt(42))
and expect::arg<0>(matches::lt(1338));
mock(1337);
For equality testing, there exists an even shorter syntax.
Most of the built-in matchers support the inversion operator (operator !), which then tests for the opposite condition.
and expect::arg<0>(!matches::le(42));
mock(1337);
Custom Matcher
Matchers are highly customizable. In fact, any type which satisfies matcher_for concept can be used. There exists no base or interface type, but the PredicateMatcher servers as a convenient generic type, which simply contains a predicate, a format string and optional additional arguments.
A very straight-forward custom matcher may look like this:
[[nodiscard]]
constexpr auto Contains(int expectedElement)
{
[](auto const& argument, auto const& element) {
return std::ranges::find(argument, element) != std::ranges::end(argument);
},
"contains element {}",
"contains not element {}",
std::make_tuple(expectedElement)};
}
std::vector const collection{42, 1337};
mock(collection);
mock(collection);
In fact, the PredicateMatcher is very flexible and can most likely tailored to your needs. For example, you can store any additional data. In this case the internal formatter requires the raw-pattern string, but the actual predicate needs a std::regex.
[[nodiscard]]
auto MatchesRegex(std::string pattern)
{
[](std::ranges::range auto&& input, [[maybe_unused]] std::string const& patternString, std::regex const& regex) {
return std::regex_match(
std::ranges::begin(input),
std::ranges::end(input),
regex);
},
"matches regex {}",
"does not match regex {}",
std::make_tuple(pattern, std::regex{pattern})};
}
SCOPED_EXP mock.expect_call(MatchesRegex(R
"(\d{4})"));
mock("1337");
SCOPED_EXP mock.expect_call(!MatchesRegex(R
"(\d*)"));
mock("Hello, World!");
Variadic matchers are also directly supported. In this case, the matcher requires two inputs and checks whether the sum of both matches the specified value.
[[nodiscard]]
constexpr auto MatchesSum(int const expectedSum)
{
[](int const firstArg, int const secondArg, int const expected) {
return firstArg + secondArg == expected;
},
"matches sum {}",
"does not match sum {}",
std::make_tuple(expectedSum)};
}
using matches::_;
and expect::all_args(MatchesSum(1337));
mock(42, 1295);
When there are very special needs, users can also just define their own matcher type without any base-class.
class IsEvenMatcher
{
public:
[[nodiscard]]
bool matches(int const input) const
{
return 0 == input % 2;
}
[[nodiscard]]
std::string_view describe() const
{
return "is an even number.";
}
};
◆ approx_abs()
| auto mimicpp::matches::approx_abs |
( |
const std::floating_point auto | value, |
|
|
const std::floating_point auto | epsilon ) |
|
nodiscardconstexpr |
Tests, whether the floating-point target is approximately equal to value.
- Parameters
-
| value | The value to compare to. |
| epsilon | The maximum absolute difference. |
- Exceptions
-
| std::runtime_error | When value is NaN or infinity. |
| std::runtime_error | When epsilon is NaN, infinity or non-positive. |
- Returns
- The newly created matcher.
◆ approx_rel() [1/2]
template<std::floating_point Float>
| auto mimicpp::matches::approx_rel |
( |
const Float | value | ) |
|
|
nodiscardconstexpr |
◆ approx_rel() [2/2]
| auto mimicpp::matches::approx_rel |
( |
const std::floating_point auto | value, |
|
|
const std::floating_point auto | relEpsilon ) |
|
nodiscardconstexpr |
Tests, whether the floating-point target is approximately equal to value.
- Parameters
-
| value | The value to compare against. |
| relEpsilon | The maximum relative difference. |
- Returns
- The newly created matcher.
- Exceptions
-
| std::runtime_error | When value is NaN or infinity. |
| std::runtime_error | When relEpsilon is NaN, infinity or non-positive. |
This functions compares both floating-point values with a scaled epsilon. In fact:
|a-b| <= eps * max(|a|, |b|),
where a and b are the operands and eps denotes the factor of the maximum input by which a and b may differ.
- Note
- This algorithm was published by Donald Knuth in his book “The Art of Computer Programming, Volume II: Seminumerical Algorithms (Addison-Wesley, 1969)”.
◆ eq()
template<typename T>
| auto mimicpp::matches::eq |
( |
T && | value | ) |
|
|
nodiscardconstexpr |
Tests, whether the target compares equal to the expected value.
- Template Parameters
-
- Parameters
-
◆ ge()
template<typename T>
| auto mimicpp::matches::ge |
( |
T && | value | ) |
|
|
nodiscardconstexpr |
Tests, whether the target is greater than or equal to the expected value.
- Template Parameters
-
- Parameters
-
◆ gt()
template<typename T>
| auto mimicpp::matches::gt |
( |
T && | value | ) |
|
|
nodiscardconstexpr |
Tests, whether the target is greater than the expected value.
- Template Parameters
-
- Parameters
-
◆ instance()
template<util::satisfies< std::is_lvalue_reference > T>
| auto mimicpp::matches::instance |
( |
T && | instance | ) |
|
|
nodiscardconstexpr |
Tests, whether the target is the expected instance.
- Template Parameters
-
- Parameters
-
| instance | The instance to be compared to.
int myInt{};
SCOPED_EXP mock.expect_call(matches::instance(myInt));
mock(myInt);
|
◆ le()
template<typename T>
| auto mimicpp::matches::le |
( |
T && | value | ) |
|
|
nodiscardconstexpr |
Tests, whether the target is less than or equal to the expected value.
- Template Parameters
-
- Parameters
-
◆ lt()
template<typename T>
| auto mimicpp::matches::lt |
( |
T && | value | ) |
|
|
nodiscardconstexpr |
Tests, whether the target is less than the expected value.
- Template Parameters
-
- Parameters
-
◆ NaN()
| auto mimicpp::matches::NaN |
( |
| ) |
|
|
nodiscardconstevalnoexcept |
Tests, whether the floating-point target is NaN.
◆ ne()
template<typename T>
| auto mimicpp::matches::ne |
( |
T && | value | ) |
|
|
nodiscardconstexpr |
Tests, whether the target compares not equal to the expected value.
- Template Parameters
-
- Parameters
-
◆ predicate()
template<typename UnaryPredicate>
| auto mimicpp::matches::predicate |
( |
UnaryPredicate && | predicate, |
|
|
StringViewT | description = "passes predicate", |
|
|
StringViewT | invertedDescription = "fails predicate" ) |
|
nodiscardconstexpr |
Tests, whether the target fulfills the given predicate.
- Template Parameters
-
| UnaryPredicate | Predicate type. |
- Parameters
-
| predicate | The predicate to test. |
| description | The formatting string. |
| invertedDescription | The formatting string for the inversion.
constexpr auto isOdd = [](int val) { return 0 != val % 2; };
and expect::arg<0>(matches::predicate(isOdd));
mock(1337);
|
The wildcard matcher, always matching.
◆ type
Matcher, which can be used as a last resort to disambiguate similar overloads.
- Template Parameters
-
| T | The exact argument type.
SECTION("Selecting void(int&&)")
{
mock(42);
}
SECTION("Selecting void(int const&)")
{
int constexpr i{42};
mock(i);
}
|