#pragma once #include #include #include #include #include #include #include #include #include #include #include namespace Void::TestingMock { inline constexpr std::size_t kMaxMatcherDepth = 32; template 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 class EqMatcher : public Matcher { 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 class NeMatcher : public Matcher { 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 class AnyMatcher : public Matcher { public: bool matches(const T&) const override { return true; } std::string describe() const override { return "_"; } }; template class LtMatcher : public Matcher { 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 class LeMatcher : public Matcher { 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 class GtMatcher : public Matcher { 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 class GeMatcher : public Matcher { 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 class RangeMatcher : public Matcher { 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 class PredMatcher : public Matcher { 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 class StartsWithMatcher : public Matcher { 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 class EndsWithMatcher : public Matcher { 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 class ContainsMatcher : public Matcher { 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 class IsNullMatcher : public Matcher { public: bool matches(const T& v) const override { return v == nullptr; } std::string describe() const override { return "IsNull()"; } }; template class NotNullMatcher : public Matcher { public: bool matches(const T& v) const override { return v != nullptr; } std::string describe() const override { return "NotNull()"; } }; template class NotMatcher : public Matcher { std::shared_ptr> m_inner; std::size_t m_depth{2}; public: explicit NotMatcher(std::shared_ptr> 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()"; } return std::format("Not({})", m_inner ? m_inner->describe() : ""); } [[nodiscard]] std::size_t depth() const noexcept override { return m_depth; } }; template class AllOfMatcher : public Matcher { std::vector>> m_matchers; std::size_t m_depth{2}; public: explicit AllOfMatcher( std::vector>> 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()"; } 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() : ""; } out += ")"; return out; } [[nodiscard]] std::size_t depth() const noexcept override { return m_depth; } }; template class AnyOfMatcher : public Matcher { std::vector>> m_matchers; std::size_t m_depth{2}; public: explicit AnyOfMatcher( std::vector>> 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()"; } 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() : ""; } out += ")"; return out; } [[nodiscard]] std::size_t depth() const noexcept override { return m_depth; } }; // --------------------------------------------------------------------- // Factory functions // --------------------------------------------------------------------- template auto Eq(T v) { return std::make_unique>(std::move(v)); } template auto Ne(T v) { return std::make_unique>(std::move(v)); } template auto _() { return std::make_unique>(); } template auto Lt(T v) { return std::make_unique>(std::move(v)); } template auto Le(T v) { return std::make_unique>(std::move(v)); } template auto Gt(T v) { return std::make_unique>(std::move(v)); } template auto Ge(T v) { return std::make_unique>(std::move(v)); } template auto Range(T min, T max) { return std::make_unique>(std::move(min), std::move(max)); } template requires std::predicate auto Pred(Fn p) { return std::make_unique>(std::move(p)); } template auto StartsWith(std::string prefix) { return std::make_unique>(std::move(prefix)); } template auto EndsWith(std::string suffix) { return std::make_unique>(std::move(suffix)); } template auto Contains(std::string needle) { return std::make_unique>(std::move(needle)); } template auto IsNull() { return std::make_unique>(); } template auto NotNull() { return std::make_unique>(); } template requires std::derived_from> std::unique_ptr> Not(std::unique_ptr inner) { return std::make_unique>( std::shared_ptr>(std::move(inner))); } template auto Not(std::unique_ptr inner) { // Extract T from Matcher via helper overload below if needed. return std::unique_ptr{std::move(inner)}; } namespace Detail { template struct MatcherValueType; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; template struct MatcherValueType> { using type = T; }; } // namespace Detail template requires requires { typename Detail::MatcherValueType::type; } auto NotOf(std::unique_ptr inner) { using T = typename Detail::MatcherValueType::type; return std::make_unique>( std::shared_ptr>(std::move(inner))); } template std::unique_ptr> AllOf(std::unique_ptr... matchers) { std::vector>> vec; vec.reserve(sizeof...(Ms)); (vec.emplace_back(std::shared_ptr>(std::move(matchers))), ...); return std::make_unique>(std::move(vec)); } template std::unique_ptr> AnyOf(std::unique_ptr... matchers) { std::vector>> vec; vec.reserve(sizeof...(Ms)); (vec.emplace_back(std::shared_ptr>(std::move(matchers))), ...); return std::make_unique>(std::move(vec)); } // --------------------------------------------------------------------- // MatcherHolder — owns a shared immutable Matcher or exact Eq value // --------------------------------------------------------------------- template class MatcherHolder { std::shared_ptr> m_matcher; public: MatcherHolder() : m_matcher(std::make_shared>()) {} template requires std::derived_from> MatcherHolder(std::unique_ptr m) : m_matcher(std::shared_ptr>(std::move(m))) { } MatcherHolder(std::shared_ptr> m) : m_matcher(std::move(m)) { } template requires(std::same_as, T> && std::equality_comparable && std::movable) MatcherHolder(T exact_value) : m_matcher( std::make_shared>(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