Files
Library/include/Void/TestingMock/Matcher.hpp
T

624 lines
15 KiB
C++

#pragma once
#include <Void/Testing/Assert.hpp>
#include <concepts>
#include <cstddef>
#include <format>
#include <functional>
#include <memory>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
namespace Void::TestingMock
{
inline constexpr std::size_t kMaxMatcherDepth = 32;
template <typename T> class Matcher
{
public:
virtual ~Matcher() = default;
virtual bool matches(const T& value) const = 0;
virtual std::string describe() const = 0;
[[nodiscard]] virtual std::size_t depth() const noexcept
{
return 1;
}
};
// ---------------------------------------------------------------------
// Built-in matchers
// ---------------------------------------------------------------------
template <typename T> class EqMatcher : public Matcher<T>
{
T m_expected;
public:
explicit EqMatcher(T expected) : m_expected(std::move(expected)) {}
bool matches(const T& v) const override { return v == m_expected; }
std::string describe() const override
{
return std::format("Eq({})",
Void::Testing::Detail::repr(m_expected));
}
};
template <typename T> class NeMatcher : public Matcher<T>
{
T m_unexpected;
public:
explicit NeMatcher(T unexpected)
: m_unexpected(std::move(unexpected))
{
}
bool matches(const T& v) const override
{
return v != m_unexpected;
}
std::string describe() const override
{
return std::format("Ne({})",
Void::Testing::Detail::repr(m_unexpected));
}
};
template <typename T> class AnyMatcher : public Matcher<T>
{
public:
bool matches(const T&) const override { return true; }
std::string describe() const override { return "_"; }
};
template <typename T> class LtMatcher : public Matcher<T>
{
T m_bound;
public:
explicit LtMatcher(T bound) : m_bound(std::move(bound)) {}
bool matches(const T& v) const override { return v < m_bound; }
std::string describe() const override
{
return std::format("Lt({})",
Void::Testing::Detail::repr(m_bound));
}
};
template <typename T> class LeMatcher : public Matcher<T>
{
T m_bound;
public:
explicit LeMatcher(T bound) : m_bound(std::move(bound)) {}
bool matches(const T& v) const override { return v <= m_bound; }
std::string describe() const override
{
return std::format("Le({})",
Void::Testing::Detail::repr(m_bound));
}
};
template <typename T> class GtMatcher : public Matcher<T>
{
T m_bound;
public:
explicit GtMatcher(T bound) : m_bound(std::move(bound)) {}
bool matches(const T& v) const override { return v > m_bound; }
std::string describe() const override
{
return std::format("Gt({})",
Void::Testing::Detail::repr(m_bound));
}
};
template <typename T> class GeMatcher : public Matcher<T>
{
T m_bound;
public:
explicit GeMatcher(T bound) : m_bound(std::move(bound)) {}
bool matches(const T& v) const override { return v >= m_bound; }
std::string describe() const override
{
return std::format("Ge({})",
Void::Testing::Detail::repr(m_bound));
}
};
template <typename T> class RangeMatcher : public Matcher<T>
{
T m_min;
T m_max;
public:
RangeMatcher(T min, T max)
: m_min(std::move(min)), m_max(std::move(max))
{
}
bool matches(const T& v) const override
{
return v >= m_min && v <= m_max;
}
std::string describe() const override
{
return std::format("Range({}, {})",
Void::Testing::Detail::repr(m_min),
Void::Testing::Detail::repr(m_max));
}
};
template <typename T, typename Fn> class PredMatcher : public Matcher<T>
{
Fn m_pred;
public:
explicit PredMatcher(Fn p) : m_pred(std::move(p)) {}
bool matches(const T& v) const override { return m_pred(v); }
std::string describe() const override { return "Pred(...)"; }
};
template <typename T> class StartsWithMatcher : public Matcher<T>
{
std::string m_prefix;
public:
explicit StartsWithMatcher(std::string prefix)
: m_prefix(std::move(prefix))
{
}
bool matches(const T& v) const override
{
const std::string_view sv{v};
return sv.starts_with(m_prefix);
}
std::string describe() const override
{
return std::format("StartsWith(\"{}\")", m_prefix);
}
};
template <typename T> class EndsWithMatcher : public Matcher<T>
{
std::string m_suffix;
public:
explicit EndsWithMatcher(std::string suffix)
: m_suffix(std::move(suffix))
{
}
bool matches(const T& v) const override
{
const std::string_view sv{v};
return sv.ends_with(m_suffix);
}
std::string describe() const override
{
return std::format("EndsWith(\"{}\")", m_suffix);
}
};
template <typename T> class ContainsMatcher : public Matcher<T>
{
std::string m_needle;
public:
explicit ContainsMatcher(std::string needle)
: m_needle(std::move(needle))
{
}
bool matches(const T& v) const override
{
const std::string_view sv{v};
return sv.find(m_needle) != std::string_view::npos;
}
std::string describe() const override
{
return std::format("Contains(\"{}\")", m_needle);
}
};
template <typename T> class IsNullMatcher : public Matcher<T>
{
public:
bool matches(const T& v) const override { return v == nullptr; }
std::string describe() const override { return "IsNull()"; }
};
template <typename T> class NotNullMatcher : public Matcher<T>
{
public:
bool matches(const T& v) const override { return v != nullptr; }
std::string describe() const override { return "NotNull()"; }
};
template <typename T> class NotMatcher : public Matcher<T>
{
std::shared_ptr<const Matcher<T>> m_inner;
std::size_t m_depth{2};
public:
explicit NotMatcher(std::shared_ptr<const Matcher<T>> inner)
: m_inner(std::move(inner)),
m_depth(m_inner ? (m_inner->depth() + 1U) : 1U)
{
}
bool matches(const T& v) const override
{
return m_inner ? !m_inner->matches(v) : false;
}
std::string describe() const override
{
if (m_depth > kMaxMatcherDepth) {
return "Not(<depth-limit>)";
}
return std::format("Not({})",
m_inner ? m_inner->describe() : "<null>");
}
[[nodiscard]] std::size_t depth() const noexcept override
{
return m_depth;
}
};
template <typename T> class AllOfMatcher : public Matcher<T>
{
std::vector<std::shared_ptr<const Matcher<T>>> m_matchers;
std::size_t m_depth{2};
public:
explicit AllOfMatcher(
std::vector<std::shared_ptr<const Matcher<T>>> matchers)
: m_matchers(std::move(matchers))
{
std::size_t max_child = 0;
for (const auto& m : m_matchers) {
if (m && m->depth() > max_child) {
max_child = m->depth();
}
}
m_depth = max_child + 1U;
}
bool matches(const T& v) const override
{
for (const auto& m : m_matchers) {
if (!m || !m->matches(v)) {
return false;
}
}
return true;
}
std::string describe() const override
{
if (m_depth > kMaxMatcherDepth) {
return "AllOf(<depth-limit>)";
}
std::string out{"AllOf("};
for (std::size_t i = 0; i < m_matchers.size(); ++i) {
if (i > 0) out += ", ";
out += m_matchers[i] ? m_matchers[i]->describe() : "<null>";
}
out += ")";
return out;
}
[[nodiscard]] std::size_t depth() const noexcept override
{
return m_depth;
}
};
template <typename T> class AnyOfMatcher : public Matcher<T>
{
std::vector<std::shared_ptr<const Matcher<T>>> m_matchers;
std::size_t m_depth{2};
public:
explicit AnyOfMatcher(
std::vector<std::shared_ptr<const Matcher<T>>> matchers)
: m_matchers(std::move(matchers))
{
std::size_t max_child = 0;
for (const auto& m : m_matchers) {
if (m && m->depth() > max_child) {
max_child = m->depth();
}
}
m_depth = max_child + 1U;
}
bool matches(const T& v) const override
{
for (const auto& m : m_matchers) {
if (m && m->matches(v)) {
return true;
}
}
return false;
}
std::string describe() const override
{
if (m_depth > kMaxMatcherDepth) {
return "AnyOf(<depth-limit>)";
}
std::string out{"AnyOf("};
for (std::size_t i = 0; i < m_matchers.size(); ++i) {
if (i > 0) out += ", ";
out += m_matchers[i] ? m_matchers[i]->describe() : "<null>";
}
out += ")";
return out;
}
[[nodiscard]] std::size_t depth() const noexcept override
{
return m_depth;
}
};
// ---------------------------------------------------------------------
// Factory functions
// ---------------------------------------------------------------------
template <typename T> auto Eq(T v)
{
return std::make_unique<EqMatcher<T>>(std::move(v));
}
template <typename T> auto Ne(T v)
{
return std::make_unique<NeMatcher<T>>(std::move(v));
}
template <typename T> auto _() { return std::make_unique<AnyMatcher<T>>(); }
template <typename T> auto Lt(T v)
{
return std::make_unique<LtMatcher<T>>(std::move(v));
}
template <typename T> auto Le(T v)
{
return std::make_unique<LeMatcher<T>>(std::move(v));
}
template <typename T> auto Gt(T v)
{
return std::make_unique<GtMatcher<T>>(std::move(v));
}
template <typename T> auto Ge(T v)
{
return std::make_unique<GeMatcher<T>>(std::move(v));
}
template <typename T> auto Range(T min, T max)
{
return std::make_unique<RangeMatcher<T>>(std::move(min),
std::move(max));
}
template <typename T, typename Fn>
requires std::predicate<Fn, const T&>
auto Pred(Fn p)
{
return std::make_unique<PredMatcher<T, Fn>>(std::move(p));
}
template <typename T = std::string> auto StartsWith(std::string prefix)
{
return std::make_unique<StartsWithMatcher<T>>(std::move(prefix));
}
template <typename T = std::string> auto EndsWith(std::string suffix)
{
return std::make_unique<EndsWithMatcher<T>>(std::move(suffix));
}
template <typename T = std::string> auto Contains(std::string needle)
{
return std::make_unique<ContainsMatcher<T>>(std::move(needle));
}
template <typename T> auto IsNull()
{
return std::make_unique<IsNullMatcher<T>>();
}
template <typename T> auto NotNull()
{
return std::make_unique<NotNullMatcher<T>>();
}
template <typename T, typename Derived>
requires std::derived_from<Derived, Matcher<T>>
std::unique_ptr<Matcher<T>> Not(std::unique_ptr<Derived> inner)
{
return std::make_unique<NotMatcher<T>>(
std::shared_ptr<const Matcher<T>>(std::move(inner)));
}
template <typename Derived>
auto Not(std::unique_ptr<Derived> inner)
{
// Extract T from Matcher<T> via helper overload below if needed.
return std::unique_ptr<Derived>{std::move(inner)};
}
namespace Detail
{
template <typename T> struct MatcherValueType;
template <typename T> struct MatcherValueType<Matcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<EqMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<NeMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<AnyMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<LtMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<LeMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<GtMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<GeMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<RangeMatcher<T>> {
using type = T;
};
template <typename T, typename Fn>
struct MatcherValueType<PredMatcher<T, Fn>> {
using type = T;
};
template <typename T> struct MatcherValueType<StartsWithMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<EndsWithMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<ContainsMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<IsNullMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<NotNullMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<NotMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<AllOfMatcher<T>> {
using type = T;
};
template <typename T> struct MatcherValueType<AnyOfMatcher<T>> {
using type = T;
};
} // namespace Detail
template <typename M>
requires requires { typename Detail::MatcherValueType<M>::type; }
auto NotOf(std::unique_ptr<M> inner)
{
using T = typename Detail::MatcherValueType<M>::type;
return std::make_unique<NotMatcher<T>>(
std::shared_ptr<const Matcher<T>>(std::move(inner)));
}
template <typename T, typename... Ms>
std::unique_ptr<Matcher<T>> AllOf(std::unique_ptr<Ms>... matchers)
{
std::vector<std::shared_ptr<const Matcher<T>>> vec;
vec.reserve(sizeof...(Ms));
(vec.emplace_back(std::shared_ptr<const Matcher<T>>(std::move(matchers))),
...);
return std::make_unique<AllOfMatcher<T>>(std::move(vec));
}
template <typename T, typename... Ms>
std::unique_ptr<Matcher<T>> AnyOf(std::unique_ptr<Ms>... matchers)
{
std::vector<std::shared_ptr<const Matcher<T>>> vec;
vec.reserve(sizeof...(Ms));
(vec.emplace_back(std::shared_ptr<const Matcher<T>>(std::move(matchers))),
...);
return std::make_unique<AnyOfMatcher<T>>(std::move(vec));
}
// ---------------------------------------------------------------------
// MatcherHolder — owns a shared immutable Matcher<T> or exact Eq value
// ---------------------------------------------------------------------
template <typename T> class MatcherHolder
{
std::shared_ptr<const Matcher<T>> m_matcher;
public:
MatcherHolder() : m_matcher(std::make_shared<AnyMatcher<T>>()) {}
template <typename Derived>
requires std::derived_from<Derived, Matcher<T>>
MatcherHolder(std::unique_ptr<Derived> m)
: m_matcher(std::shared_ptr<const Matcher<T>>(std::move(m)))
{
}
MatcherHolder(std::shared_ptr<const Matcher<T>> m)
: m_matcher(std::move(m))
{
}
template <typename U = T>
requires(std::same_as<std::decay_t<U>, T> &&
std::equality_comparable<T> && std::movable<T>)
MatcherHolder(T exact_value)
: m_matcher(
std::make_shared<EqMatcher<T>>(std::move(exact_value)))
{
}
bool matches(const T& v) const { return m_matcher->matches(v); }
std::string describe() const { return m_matcher->describe(); }
[[nodiscard]] std::size_t depth() const noexcept
{
return m_matcher ? m_matcher->depth() : 0;
}
};
} // namespace Void::TestingMock