File indexing completed on 2025-01-18 09:54:07
0001
0002
0003
0004
0005
0006
0007
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 }
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
0046
0047
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 }
0057 }
0058
0059 #endif