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_PREDICATE_HPP_INCLUDED
0009 #define CATCH_MATCHERS_PREDICATE_HPP_INCLUDED
0010 
0011 #include <catch2/matchers/catch_matchers.hpp>
0012 #include <catch2/internal/catch_meta.hpp>
0013 #include <catch2/internal/catch_move_and_forward.hpp>
0014 
0015 #include <string>
0016 
0017 namespace Catch {
0018 namespace Matchers {
0019 
0020 namespace Detail {
0021     std::string finalizeDescription(const std::string& desc);
0022 } // namespace Detail
0023 
0024 template <typename T, typename Predicate>
0025 class PredicateMatcher final : public MatcherBase<T> {
0026     Predicate m_predicate;
0027     std::string m_description;
0028 public:
0029 
0030     PredicateMatcher(Predicate&& elem, std::string const& descr)
0031         :m_predicate(CATCH_FORWARD(elem)),
0032         m_description(Detail::finalizeDescription(descr))
0033     {}
0034 
0035     bool match( T const& item ) const override {
0036         return m_predicate(item);
0037     }
0038 
0039     std::string describe() const override {
0040         return m_description;
0041     }
0042 };
0043 
0044     /**
0045      * Creates a matcher that calls delegates `match` to the provided predicate.
0046      *
0047      * The user has to explicitly specify the argument type to the matcher
0048      */
0049     template<typename T, typename Pred>
0050     PredicateMatcher<T, Pred> Predicate(Pred&& predicate, std::string const& description = "") {
0051         static_assert(is_callable<Pred(T)>::value, "Predicate not callable with argument T");
0052         static_assert(std::is_same<bool, FunctionReturnType<Pred, T>>::value, "Predicate does not return bool");
0053         return PredicateMatcher<T, Pred>(CATCH_FORWARD(predicate), description);
0054     }
0055 
0056 } // namespace Matchers
0057 } // namespace Catch
0058 
0059 #endif // CATCH_MATCHERS_PREDICATE_HPP_INCLUDED