2018-04-03 23:28:14 +02:00
|
|
|
/*
|
|
|
|
* Created by Martin Hořeňovský on 03/04/2017.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_MATCHERS_GENERIC_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_MATCHERS_GENERIC_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "catch_common.h"
|
|
|
|
#include "catch_matchers.h"
|
2019-10-28 15:28:19 +01:00
|
|
|
#include "catch_meta.hpp"
|
2018-04-03 23:28:14 +02:00
|
|
|
|
|
|
|
#include <string>
|
2019-10-28 15:28:19 +01:00
|
|
|
#include <utility>
|
2018-04-03 23:28:14 +02:00
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
namespace Matchers {
|
|
|
|
namespace Generic {
|
|
|
|
|
|
|
|
namespace Detail {
|
|
|
|
std::string finalizeDescription(const std::string& desc);
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:28:19 +01:00
|
|
|
template <typename T, typename Predicate>
|
2018-04-03 23:28:14 +02:00
|
|
|
class PredicateMatcher : public MatcherBase<T> {
|
2019-10-28 15:28:19 +01:00
|
|
|
Predicate m_predicate;
|
2018-04-03 23:28:14 +02:00
|
|
|
std::string m_description;
|
|
|
|
public:
|
|
|
|
|
2019-10-28 15:28:19 +01:00
|
|
|
PredicateMatcher(Predicate&& elem, std::string const& descr)
|
|
|
|
:m_predicate(std::forward<Predicate>(elem)),
|
2018-04-03 23:28:14 +02:00
|
|
|
m_description(Detail::finalizeDescription(descr))
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool match( T const& item ) const override {
|
|
|
|
return m_predicate(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string describe() const override {
|
|
|
|
return m_description;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Generic
|
|
|
|
|
|
|
|
// The following functions create the actual matcher objects.
|
|
|
|
// The user has to explicitly specify type to the function, because
|
2019-04-08 23:30:28 +02:00
|
|
|
// inferring std::function<bool(T const&)> is hard (but possible) and
|
2018-04-03 23:28:14 +02:00
|
|
|
// requires a lot of TMP.
|
2019-10-28 15:28:19 +01:00
|
|
|
template<typename T, typename Pred>
|
|
|
|
Generic::PredicateMatcher<T, Pred> Predicate(Pred&& predicate, std::string const& description = "") {
|
|
|
|
static_assert(is_callable<Pred(T)>::value, "Predicate not callable with argument T");
|
|
|
|
static_assert(std::is_same<bool, FunctionReturnType<Pred, T>>::value, "Predicate does not return bool");
|
|
|
|
return Generic::PredicateMatcher<T, Pred>(std::forward<Pred>(predicate), description);
|
2018-04-03 23:28:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Matchers
|
|
|
|
} // namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_MATCHERS_GENERIC_HPP_INCLUDED
|