1 /*
2  *  Created by Martin Hořeňovský on 03/04/2017.
3  *
4  * Distributed under the Boost Software License, Version 1.0. (See accompanying
5  * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  */
7 #ifndef TWOBLUECUBES_CATCH_MATCHERS_GENERIC_HPP_INCLUDED
8 #define TWOBLUECUBES_CATCH_MATCHERS_GENERIC_HPP_INCLUDED
9 
10 #include "catch_common.h"
11 #include "catch_matchers.h"
12 
13 #include <functional>
14 #include <string>
15 
16 namespace Catch {
17 namespace Matchers {
18 namespace Generic {
19 
20 namespace Detail {
21     std::string finalizeDescription(const std::string& desc);
22 }
23 
24 template <typename T>
25 class PredicateMatcher : public MatcherBase<T> {
26     std::function<bool(T const&)> m_predicate;
27     std::string m_description;
28 public:
29 
PredicateMatcher(std::function<bool (T const &)> const & elem,std::string const & descr)30     PredicateMatcher(std::function<bool(T const&)> const& elem, std::string const& descr)
31         :m_predicate(std::move(elem)),
32         m_description(Detail::finalizeDescription(descr))
33     {}
34 
match(T const & item) const35     bool match( T const& item ) const override {
36         return m_predicate(item);
37     }
38 
describe() const39     std::string describe() const override {
40         return m_description;
41     }
42 };
43 
44 } // namespace Generic
45 
46     // The following functions create the actual matcher objects.
47     // The user has to explicitly specify type to the function, because
48     // inferring std::function<bool(T const&)> is hard (but possible) and
49     // requires a lot of TMP.
50     template<typename T>
Predicate(std::function<bool (T const &)> const & predicate,std::string const & description="")51     Generic::PredicateMatcher<T> Predicate(std::function<bool(T const&)> const& predicate, std::string const& description = "") {
52         return Generic::PredicateMatcher<T>(predicate, description);
53     }
54 
55 } // namespace Matchers
56 } // namespace Catch
57 
58 #endif // TWOBLUECUBES_CATCH_MATCHERS_GENERIC_HPP_INCLUDED
59