From bc524d70253a4ab2fe40c3ca3e5666e267c0a4d1 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sun, 29 Jun 2025 19:25:29 +0100 Subject: import catch2 3.8.1 --- src/catch2/matchers/catch_matchers_contains.hpp | 102 ++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/catch2/matchers/catch_matchers_contains.hpp (limited to 'src/catch2/matchers/catch_matchers_contains.hpp') diff --git a/src/catch2/matchers/catch_matchers_contains.hpp b/src/catch2/matchers/catch_matchers_contains.hpp new file mode 100644 index 0000000..877d692 --- /dev/null +++ b/src/catch2/matchers/catch_matchers_contains.hpp @@ -0,0 +1,102 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 +#ifndef CATCH_MATCHERS_CONTAINS_HPP_INCLUDED +#define CATCH_MATCHERS_CONTAINS_HPP_INCLUDED + +#include +#include + +#include +#include + +namespace Catch { + namespace Matchers { + //! Matcher for checking that an element in range is equal to specific element + template + class ContainsElementMatcher final : public MatcherGenericBase { + T m_desired; + Equality m_eq; + public: + template + ContainsElementMatcher(T2&& target, Equality2&& predicate): + m_desired(CATCH_FORWARD(target)), + m_eq(CATCH_FORWARD(predicate)) + {} + + std::string describe() const override { + return "contains element " + Catch::Detail::stringify(m_desired); + } + + template + bool match( RangeLike&& rng ) const { + for ( auto&& elem : rng ) { + if ( m_eq( elem, m_desired ) ) { return true; } + } + return false; + } + }; + + //! Meta-matcher for checking that an element in a range matches a specific matcher + template + class ContainsMatcherMatcher final : public MatcherGenericBase { + Matcher m_matcher; + public: + // 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): + m_matcher(CATCH_MOVE(matcher)) + {} + + template + bool match(RangeLike&& rng) const { + for (auto&& elem : rng) { + if (m_matcher.match(elem)) { + return true; + } + } + return false; + } + + std::string describe() const override { + return "contains element matching " + m_matcher.describe(); + } + }; + + /** + * Creates a matcher that checks whether a range contains a specific element. + * + * Uses `std::equal_to` to do the comparison + */ + template + std::enable_if_t::value, + ContainsElementMatcher>> Contains(T&& elem) { + return { CATCH_FORWARD(elem), std::equal_to<>{} }; + } + + //! Creates a matcher that checks whether a range contains element matching a matcher + template + std::enable_if_t::value, + ContainsMatcherMatcher> Contains(Matcher&& matcher) { + return { CATCH_FORWARD(matcher) }; + } + + /** + * Creates a matcher that checks whether a range contains a specific element. + * + * Uses `eq` to do the comparisons, the element is provided on the rhs + */ + template + ContainsElementMatcher Contains(T&& elem, Equality&& eq) { + return { CATCH_FORWARD(elem), CATCH_FORWARD(eq) }; + } + + } +} + +#endif // CATCH_MATCHERS_CONTAINS_HPP_INCLUDED -- cgit v1.2.3