File indexing completed on 2025-01-18 09:54:07
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef CATCH_MATCHERS_CONTAINS_HPP_INCLUDED
0009 #define CATCH_MATCHERS_CONTAINS_HPP_INCLUDED
0010
0011 #include <catch2/matchers/catch_matchers_templated.hpp>
0012 #include <catch2/internal/catch_move_and_forward.hpp>
0013
0014 #include <algorithm>
0015 #include <functional>
0016
0017 namespace Catch {
0018 namespace Matchers {
0019
0020 template <typename T, typename Equality>
0021 class ContainsElementMatcher final : public MatcherGenericBase {
0022 T m_desired;
0023 Equality m_eq;
0024 public:
0025 template <typename T2, typename Equality2>
0026 ContainsElementMatcher(T2&& target, Equality2&& predicate):
0027 m_desired(CATCH_FORWARD(target)),
0028 m_eq(CATCH_FORWARD(predicate))
0029 {}
0030
0031 std::string describe() const override {
0032 return "contains element " + Catch::Detail::stringify(m_desired);
0033 }
0034
0035 template <typename RangeLike>
0036 bool match( RangeLike&& rng ) const {
0037 for ( auto&& elem : rng ) {
0038 if ( m_eq( elem, m_desired ) ) { return true; }
0039 }
0040 return false;
0041 }
0042 };
0043
0044
0045 template <typename Matcher>
0046 class ContainsMatcherMatcher final : public MatcherGenericBase {
0047 Matcher m_matcher;
0048 public:
0049
0050
0051
0052 ContainsMatcherMatcher(Matcher matcher):
0053 m_matcher(CATCH_MOVE(matcher))
0054 {}
0055
0056 template <typename RangeLike>
0057 bool match(RangeLike&& rng) const {
0058 for (auto&& elem : rng) {
0059 if (m_matcher.match(elem)) {
0060 return true;
0061 }
0062 }
0063 return false;
0064 }
0065
0066 std::string describe() const override {
0067 return "contains element matching " + m_matcher.describe();
0068 }
0069 };
0070
0071
0072
0073
0074
0075
0076 template <typename T>
0077 std::enable_if_t<!Detail::is_matcher<T>::value,
0078 ContainsElementMatcher<T, std::equal_to<>>> Contains(T&& elem) {
0079 return { CATCH_FORWARD(elem), std::equal_to<>{} };
0080 }
0081
0082
0083 template <typename Matcher>
0084 std::enable_if_t<Detail::is_matcher<Matcher>::value,
0085 ContainsMatcherMatcher<Matcher>> Contains(Matcher&& matcher) {
0086 return { CATCH_FORWARD(matcher) };
0087 }
0088
0089
0090
0091
0092
0093
0094 template <typename T, typename Equality>
0095 ContainsElementMatcher<T, Equality> Contains(T&& elem, Equality&& eq) {
0096 return { CATCH_FORWARD(elem), CATCH_FORWARD(eq) };
0097 }
0098
0099 }
0100 }
0101
0102 #endif