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<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. But, this is just one option. If you have some very specific needs, go and create your matcher from scratch.
[](const auto& target, const auto& element)
{
return std::ranges::find(target, element) != std::ranges::end(target);
},
"contains element {}",
"contains not element {}",
std::tuple{42}};
and expect::arg<0>(containsMatcher);
std::vector collection{42, 1337};
mock(collection);
◆ 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<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.