forked from catchorg/Catch2
Constexpr matching support in generic Contains matchers
This commit is contained in:
@@ -502,6 +502,8 @@ matching. Currently, this includes:
|
||||
* `AnyMatch(Matcher element_matcher)`
|
||||
* `NoneMatch(Matcher element_matcher)`
|
||||
* `AllTrue()`, `AnyTrue()`, `NoneTrue()`
|
||||
* `Contains(T&& target_element, Comparator = std::equal_to<>{})`
|
||||
* `Contains(Matcher element_matcher)`
|
||||
|
||||
|
||||
---
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Catch {
|
||||
Equality m_eq;
|
||||
public:
|
||||
template <typename T2, typename Equality2>
|
||||
ContainsElementMatcher(T2&& target, Equality2&& predicate):
|
||||
constexpr ContainsElementMatcher(T2&& target, Equality2&& predicate):
|
||||
m_desired(CATCH_FORWARD(target)),
|
||||
m_eq(CATCH_FORWARD(predicate))
|
||||
{}
|
||||
@@ -33,7 +33,7 @@ namespace Catch {
|
||||
}
|
||||
|
||||
template <typename RangeLike>
|
||||
bool match( RangeLike&& rng ) const {
|
||||
constexpr bool match( RangeLike&& rng ) const {
|
||||
for ( auto&& elem : rng ) {
|
||||
if ( m_eq( elem, m_desired ) ) { return true; }
|
||||
}
|
||||
@@ -49,12 +49,12 @@ namespace Catch {
|
||||
// Note that we do a copy+move to avoid having to SFINAE this
|
||||
// constructor (and also avoid some perfect forwarding failure
|
||||
// cases)
|
||||
ContainsMatcherMatcher(Matcher matcher):
|
||||
constexpr ContainsMatcherMatcher(Matcher matcher):
|
||||
m_matcher(CATCH_MOVE(matcher))
|
||||
{}
|
||||
|
||||
template <typename RangeLike>
|
||||
bool match(RangeLike&& rng) const {
|
||||
constexpr bool match(RangeLike&& rng) const {
|
||||
for (auto&& elem : rng) {
|
||||
if (m_matcher.match(elem)) {
|
||||
return true;
|
||||
@@ -74,14 +74,14 @@ namespace Catch {
|
||||
* Uses `std::equal_to` to do the comparison
|
||||
*/
|
||||
template <typename T>
|
||||
std::enable_if_t<!Detail::is_matcher_v<T>,
|
||||
constexpr std::enable_if_t<!Detail::is_matcher_v<T>,
|
||||
ContainsElementMatcher<T, std::equal_to<>>> Contains(T&& elem) {
|
||||
return { CATCH_FORWARD(elem), std::equal_to<>{} };
|
||||
}
|
||||
|
||||
//! Creates a matcher that checks whether a range contains element matching a matcher
|
||||
template <typename Matcher>
|
||||
std::enable_if_t<Detail::is_matcher_v<Matcher>,
|
||||
constexpr std::enable_if_t<Detail::is_matcher_v<Matcher>,
|
||||
ContainsMatcherMatcher<Matcher>> Contains(Matcher&& matcher) {
|
||||
return { CATCH_FORWARD(matcher) };
|
||||
}
|
||||
@@ -92,7 +92,7 @@ namespace Catch {
|
||||
* Uses `eq` to do the comparisons, the element is provided on the rhs
|
||||
*/
|
||||
template <typename T, typename Equality>
|
||||
ContainsElementMatcher<T, Equality> Contains(T&& elem, Equality&& eq) {
|
||||
constexpr ContainsElementMatcher<T, Equality> Contains(T&& elem, Equality&& eq) {
|
||||
return { CATCH_FORWARD(elem), CATCH_FORWARD(eq) };
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <catch2/matchers/catch_matchers_templated.hpp>
|
||||
#include <catch2/matchers/catch_matchers_container_properties.hpp>
|
||||
#include <catch2/matchers/catch_matchers_quantifiers.hpp>
|
||||
#include <catch2/matchers/catch_matchers_contains.hpp>
|
||||
|
||||
#include <array>
|
||||
|
||||
@@ -47,6 +48,18 @@ namespace {
|
||||
|
||||
constexpr MatchTrueMatcher MatchTrue() { return MatchTrueMatcher(); }
|
||||
|
||||
constexpr std::array<bool, 3> compute_bools( int type ) {
|
||||
switch ( type ) {
|
||||
case 0:
|
||||
return { true, true, true };
|
||||
case 1:
|
||||
return { false, true, false };
|
||||
case 2:
|
||||
return { false, false, false };
|
||||
default:
|
||||
return { false, false, false };
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -65,19 +78,6 @@ TEST_CASE( "IsEmpty and HasSize matchers can be used in constexpr contexts",
|
||||
STATIC_REQUIRE_THAT( arr, SizeIs( MatchAll() ) );
|
||||
}
|
||||
|
||||
constexpr std::array<bool, 3> compute_bools( int type ) {
|
||||
switch ( type ) {
|
||||
case 0:
|
||||
return { true, true, true };
|
||||
case 1:
|
||||
return { false, true, false };
|
||||
case 2:
|
||||
return { false, false, false };
|
||||
default:
|
||||
return { false, false, false };
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE( "Quantifier matchers can be used in constexpr contexts",
|
||||
"[constexpr][matchers][approvals]" ) {
|
||||
using namespace Catch::Matchers;
|
||||
@@ -90,6 +90,14 @@ TEST_CASE( "Quantifier matchers can be used in constexpr contexts",
|
||||
STATIC_REQUIRE_THAT( compute_bools( 2 ), NoneMatch( MatchTrue() ) );
|
||||
}
|
||||
|
||||
TEST_CASE( "Generic Equals matchers can be used in constexpr contexts",
|
||||
"[constexpr][matchers][approvals]" ) {
|
||||
using Catch::Matchers::Contains;
|
||||
STATIC_REQUIRE_THAT( compute_bools( 0 ), Contains( true ) );
|
||||
STATIC_REQUIRE_THAT( compute_bools( 1 ), Contains( MatchTrue() ) );
|
||||
STATIC_REQUIRE_THAT( compute_bools( 2 ), Contains( true, std::not_equal_to<>{} ) );
|
||||
}
|
||||
|
||||
// Combining matchers needs C++26 and P2738, so they are in separate preprocessor block
|
||||
# if __cpp_constexpr >= 202306L
|
||||
|
||||
|
||||
Reference in New Issue
Block a user