1 parent
f0825fbd1a
commit
42268c6c35
45 files changed
+2732
-262
No files matched your search
@@ -1,25 +1,279 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <string>
|
||||
|
||||
#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; };
|
||||
template<typename T> class EqMatcher : public Matcher<T> { T expected_; public: explicit EqMatcher(T expected) : expected_(std::move(expected)) {} bool matches(const T& v) const override { return v == expected_; } std::string describe() const override { return std::format("eq({})", Void::Testing::Detail::repr(expected_)); } };
|
||||
template<typename T> class AnyMatcher : public Matcher<T> { public: bool matches(const T&) const override { return true; } std::string describe() const override { return "_"; } };
|
||||
namespace Void::TestingMock
|
||||
{
|
||||
template <typename T> class Matcher
|
||||
{
|
||||
public:
|
||||
|
||||
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>>(); }
|
||||
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(); }
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class MatcherHolder {
|
||||
std::unique_ptr<Matcher<T>> matcher_;
|
||||
public:
|
||||
MatcherHolder() : matcher_(std::make_unique<AnyMatcher<T>>()) {}
|
||||
MatcherHolder(std::unique_ptr<Matcher<T>> m) : matcher_(std::move(m)) {}
|
||||
MatcherHolder(T exact_value) : matcher_(std::make_unique<EqMatcher<T>>(std::move(exact_value))) {}
|
||||
bool matches(const T& v) const { return matcher_->matches(v); }
|
||||
std::string describe() const { return matcher_->describe(); }
|
||||
};
|
||||
} // namespace Void::TestingMock
|
||||
Reference in new issue
Block a user