diff options
| author | Stefan Haller | 2024-10-12 17:18:59 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2024-10-14 21:02:03 +0200 |
| commit | 69d62abc9a48033177b99ae79c13a6f1b280e94c (patch) | |
| tree | 16ca0b61e7a9ecb540ff157c66c373f5df763547 /src | |
| parent | 1e0ccb1b2150c031a5b601f25cb7fc86dacff9f7 (diff) | |
| download | catch3-69d62abc9a48033177b99ae79c13a6f1b280e94c.tar.gz | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/catch2/matchers/catch_matchers_range_equals.hpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/catch2/matchers/catch_matchers_range_equals.hpp b/src/catch2/matchers/catch_matchers_range_equals.hpp index 49ba19b5..8130f604 100644 --- a/src/catch2/matchers/catch_matchers_range_equals.hpp +++ b/src/catch2/matchers/catch_matchers_range_equals.hpp @@ -110,6 +110,22 @@ namespace Catch { /** * 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. * * Uses the provided predicate `predicate` to do the comparisons @@ -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 |