Provide overloads for {Unordered}RangeEquals taking a std::initializer_list

This allows writing something like

  const auto v = calculateSomeVectorOfInts();
  CHECK_THAT(v, RangeEquals({1, 2, 3}));

Fixes #2915.
This commit is contained in:
Stefan Haller
2024-10-12 17:18:59 +02:00
committed by Martin Hořeňovský
parent 1e0ccb1b21
commit 69d62abc9a
15 changed files with 238 additions and 11 deletions

View File

@@ -108,6 +108,22 @@ namespace Catch {
return { CATCH_FORWARD( range ), CATCH_FORWARD( predicate ) };
}
/**
* Creates a matcher that checks if all elements in a range are equal
* to all elements in an initializer list.
*
* Uses the provided predicate `predicate` to do the comparisons
* (defaulting to `std::equal_to`)
*/
template <typename T,
typename Equality = decltype( std::equal_to<>{} )>
constexpr
RangeEqualsMatcher<std::initializer_list<T>, Equality>
RangeEquals( std::initializer_list<T> range,
Equality&& predicate = std::equal_to<>{} ) {
return { range, CATCH_FORWARD( predicate ) };
}
/**
* Creates a matcher that checks if all elements in a range are equal
* to all elements in another range, in some permutation.
@@ -123,6 +139,22 @@ namespace Catch {
Equality&& predicate = std::equal_to<>{} ) {
return { CATCH_FORWARD( range ), CATCH_FORWARD( predicate ) };
}
/**
* Creates a matcher that checks if all elements in a range are equal
* to all elements in an initializer list, in some permutation.
*
* Uses the provided predicate `predicate` to do the comparisons
* (defaulting to `std::equal_to`)
*/
template <typename T,
typename Equality = decltype( std::equal_to<>{} )>
constexpr
UnorderedRangeEqualsMatcher<std::initializer_list<T>, Equality>
UnorderedRangeEquals( std::initializer_list<T> range,
Equality&& predicate = std::equal_to<>{} ) {
return { range, CATCH_FORWARD( predicate ) };
}
} // namespace Matchers
} // namespace Catch