catch_matchers_generic.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include "catch_common.h"
  10. #include "catch_matchers.h"
  11. #include <functional>
  12. #include <string>
  13. namespace Catch {
  14. namespace Matchers {
  15. namespace Generic {
  16. namespace Detail {
  17. std::string finalizeDescription(const std::string& desc);
  18. }
  19. template <typename T>
  20. class PredicateMatcher : public MatcherBase<T> {
  21. std::function<bool(T const&)> m_predicate;
  22. std::string m_description;
  23. public:
  24. PredicateMatcher(std::function<bool(T const&)> const& elem, std::string const& descr)
  25. :m_predicate(std::move(elem)),
  26. m_description(Detail::finalizeDescription(descr))
  27. {}
  28. bool match( T const& item ) const override {
  29. return m_predicate(item);
  30. }
  31. std::string describe() const override {
  32. return m_description;
  33. }
  34. };
  35. } // namespace Generic
  36. // The following functions create the actual matcher objects.
  37. // The user has to explicitly specify type to the function, because
  38. // inferring std::function<bool(T const&)> is hard (but possible) and
  39. // requires a lot of TMP.
  40. template<typename T>
  41. Generic::PredicateMatcher<T> Predicate(std::function<bool(T const&)> const& predicate, std::string const& description = "") {
  42. return Generic::PredicateMatcher<T>(predicate, description);
  43. }
  44. } // namespace Matchers
  45. } // namespace Catch
  46. #endif // TWOBLUECUBES_CATCH_MATCHERS_GENERIC_HPP_INCLUDED