Files
Library/include/Void/TestingMock/Matcher.hpp
T
2026-05-03 18:09:46 +03:00

280 lines
6.2 KiB
C++

#pragma once
#include <Void/Testing/Assert.hpp>
#include <concepts>
#include <format>
#include <functional>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
namespace Void::TestingMock
{
template <typename T> class Matcher
{
public:
virtual ~Matcher() = default;
virtual bool matches(const T& value) const = 0;
virtual std::string describe() const = 0;
};
// ---------------------------------------------------------------------
// 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 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 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);
}
};
// ---------------------------------------------------------------------
// Factory functions
// ---------------------------------------------------------------------
template <typename T> auto Eq(T v)
{
return std::make_unique<EqMatcher<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 Contains(std::string needle)
{
return std::make_unique<ContainsMatcher<T>>(std::move(needle));
}
// ---------------------------------------------------------------------
// MatcherHolder — owns a Matcher<T> or wraps an exact value as Eq
// ---------------------------------------------------------------------
template <typename T> class MatcherHolder
{
std::unique_ptr<Matcher<T>> m_matcher;
public:
MatcherHolder() : m_matcher(std::make_unique<AnyMatcher<T>>()) {}
MatcherHolder(std::unique_ptr<Matcher<T>> m)
: m_matcher(std::move(m))
{
}
MatcherHolder(T exact_value)
: m_matcher(
std::make_unique<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(); }
};
} // namespace Void::TestingMock