Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMartin Hořeňovský2023-02-10 23:25:41 +0100
committerMartin Hořeňovský2023-02-10 23:25:45 +0100
commite8ba329b6c66b06c4afd07953020b74c4b0f1a75 (patch)
tree10d2a26eeb666669705601f1b090925bb3b248d1
parent4aa88299af4d4d7dec18a1b6cf314a4b03d7567c (diff)
downloadcatch3-e8ba329b6c66b06c4afd07953020b74c4b0f1a75.tar.gz
Add support for iterator+sentinel pairs in Contains matcher
-rw-r--r--src/catch2/matchers/catch_matchers_contains.hpp14
-rw-r--r--tests/SelfTest/UsageTests/MatchersRanges.tests.cpp10
2 files changed, 16 insertions, 8 deletions
diff --git a/src/catch2/matchers/catch_matchers_contains.hpp b/src/catch2/matchers/catch_matchers_contains.hpp
index e298cab7..877d6924 100644
--- a/src/catch2/matchers/catch_matchers_contains.hpp
+++ b/src/catch2/matchers/catch_matchers_contains.hpp
@@ -33,13 +33,11 @@ namespace Catch {
}
template <typename RangeLike>
- bool match(RangeLike&& rng) const {
- using std::begin; using std::end;
-
- return end(rng) != std::find_if(begin(rng), end(rng),
- [&](auto const& elem) {
- return m_eq(elem, m_desired);
- });
+ bool match( RangeLike&& rng ) const {
+ for ( auto&& elem : rng ) {
+ if ( m_eq( elem, m_desired ) ) { return true; }
+ }
+ return false;
}
};
@@ -91,7 +89,7 @@ namespace Catch {
/**
* Creates a matcher that checks whether a range contains a specific element.
*
- * Uses `eq` to do the comparisons
+ * 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) {
diff --git a/tests/SelfTest/UsageTests/MatchersRanges.tests.cpp b/tests/SelfTest/UsageTests/MatchersRanges.tests.cpp
index 8b1420c9..05e11b0c 100644
--- a/tests/SelfTest/UsageTests/MatchersRanges.tests.cpp
+++ b/tests/SelfTest/UsageTests/MatchersRanges.tests.cpp
@@ -646,6 +646,16 @@ TEST_CASE( "RangeEquals supports ranges with different types returned from begin
REQUIRE_THAT( diff_types, UnorderedRangeEquals( diff_types ) );
}
+TEST_CASE( "RangeContains supports ranges with different types returned from "
+ "begin and end",
+ "[matchers][templated][range][approvals]" ) {
+ using Catch::Matchers::Contains;
+
+ has_different_begin_end_types<size_t> diff_types{ 1, 2, 3, 4, 5 };
+ REQUIRE_THAT( diff_types, Contains( size_t( 3 ) ) );
+ REQUIRE_THAT( diff_types, Contains( LessThanMatcher( size_t( 4 ) ) ) );
+}
+
#endif
TEST_CASE( "Usage of RangeEquals range matcher", "[matchers][templated][quantifiers]" ) {