feat(testing-mock): complete P2 M-TESTINGMOCK-01..04 sessions, action-outside-lock, move-only, combinators
This commit is contained in:
1 parent
86d777eae6
commit
658c21a2fa
8 files changed
+1702
-30
No files matched your search
@@ -3,15 +3,20 @@
|
||||
#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:
|
||||
@@ -19,6 +24,10 @@ namespace Void::TestingMock
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
@@ -42,6 +51,29 @@ namespace Void::TestingMock
|
||||
}
|
||||
};
|
||||
|
||||
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:
|
||||
@@ -171,6 +203,29 @@ namespace Void::TestingMock
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
@@ -194,6 +249,155 @@ namespace Void::TestingMock
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
// ---------------------------------------------------------------------
|
||||
@@ -203,6 +407,11 @@ namespace Void::TestingMock
|
||||
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)
|
||||
@@ -243,37 +452,172 @@ namespace Void::TestingMock
|
||||
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 Matcher<T> or wraps an exact value as Eq
|
||||
// MatcherHolder — owns a shared immutable Matcher<T> or exact Eq value
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
template <typename T> class MatcherHolder
|
||||
{
|
||||
std::unique_ptr<Matcher<T>> m_matcher;
|
||||
std::shared_ptr<const Matcher<T>> m_matcher;
|
||||
|
||||
public:
|
||||
|
||||
MatcherHolder() : m_matcher(std::make_unique<AnyMatcher<T>>()) {}
|
||||
MatcherHolder() : m_matcher(std::make_shared<AnyMatcher<T>>()) {}
|
||||
|
||||
MatcherHolder(std::unique_ptr<Matcher<T>> m)
|
||||
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_unique<EqMatcher<T>>(std::move(exact_value)))
|
||||
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
|
||||
Reference in new issue
Block a user