Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:07

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
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         //! Matcher for checking that an element in range is equal to specific element
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         //! Meta-matcher for checking that an element in a range matches a specific matcher
0045         template <typename Matcher>
0046         class ContainsMatcherMatcher final : public MatcherGenericBase {
0047             Matcher m_matcher;
0048         public:
0049             // Note that we do a copy+move to avoid having to SFINAE this
0050             // constructor (and also avoid some perfect forwarding failure
0051             // cases)
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          * Creates a matcher that checks whether a range contains a specific element.
0073          *
0074          * Uses `std::equal_to` to do the comparison
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         //! Creates a matcher that checks whether a range contains element matching a matcher
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          * Creates a matcher that checks whether a range contains a specific element.
0091          *
0092          * Uses `eq` to do the comparisons, the element is provided on the rhs
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 // CATCH_MATCHERS_CONTAINS_HPP_INCLUDED