1 parent
f0825fbd1a
commit
42268c6c35
45 files changed
+2732
-262
No files matched your search
@@ -1,33 +1,151 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/TestingMock/Matcher.hpp>
|
||||
#include <source_location>
|
||||
|
||||
#include <concepts>
|
||||
#include <cstddef>
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::TestingMock::Detail {
|
||||
namespace Void::TestingMock::Detail
|
||||
{
|
||||
|
||||
template<typename> class Expectation;
|
||||
template <typename> class Expectation;
|
||||
|
||||
template <typename R, typename... Args> class Expectation<R(Args...)>
|
||||
{
|
||||
public:
|
||||
|
||||
using MatcherTuple =
|
||||
std::tuple<MatcherHolder<std::decay_t<Args>>...>;
|
||||
/// Move-only function — supports unique_ptr-capturing lambdas
|
||||
/// and other move-only callables.
|
||||
using Action = std::move_only_function<R(Args...)>;
|
||||
|
||||
Expectation(MatcherTuple matchers, std::source_location loc)
|
||||
: m_matchers(std::move(matchers)), m_location(loc)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename U = R>
|
||||
requires(!std::is_void_v<U> && std::same_as<U, R>)
|
||||
Expectation& returns(U value)
|
||||
{
|
||||
m_action = [val = std::move(value)](Args...) mutable -> R {
|
||||
return val;
|
||||
};
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
requires std::invocable<F&, Args...>
|
||||
Expectation& invokes(F action)
|
||||
{
|
||||
m_action = std::move(action);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Expectation& times(int n)
|
||||
{
|
||||
m_min_calls = n;
|
||||
m_max_calls = n;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Expectation& once() { return times(1); }
|
||||
|
||||
bool matches(Args... args) const
|
||||
{
|
||||
return match_impl(args..., std::index_sequence_for<Args...>{});
|
||||
}
|
||||
|
||||
R execute(Args... args)
|
||||
{
|
||||
if (!m_action) {
|
||||
if constexpr (std::is_same_v<R, void>) {
|
||||
return;
|
||||
} else {
|
||||
return R{};
|
||||
}
|
||||
}
|
||||
return m_action(args...);
|
||||
}
|
||||
|
||||
void record_call() { ++m_call_count; }
|
||||
|
||||
bool is_saturated() const { return m_call_count >= m_max_calls; }
|
||||
|
||||
void verify(std::string_view func_name)
|
||||
{
|
||||
if (m_call_count < m_min_calls) {
|
||||
Void::Testing::fail(std::format(
|
||||
"unsatisfied expectation on {}: {} — {}/{} calls",
|
||||
func_name, describe_matchers(), m_call_count,
|
||||
m_min_calls));
|
||||
}
|
||||
}
|
||||
|
||||
// -------- accessors used by failure-message construction --------
|
||||
|
||||
[[nodiscard]] int call_count() const noexcept
|
||||
{
|
||||
return m_call_count;
|
||||
}
|
||||
|
||||
[[nodiscard]] int max_calls() const noexcept { return m_max_calls; }
|
||||
|
||||
[[nodiscard]] std::source_location location() const noexcept
|
||||
{
|
||||
return m_location;
|
||||
}
|
||||
|
||||
/// Render the matcher tuple as e.g. "(Eq(7), _, Gt(3))".
|
||||
[[nodiscard]] std::string describe_matchers() const
|
||||
{
|
||||
return describe_impl(std::index_sequence_for<Args...>{});
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
MatcherTuple m_matchers;
|
||||
Action m_action;
|
||||
std::source_location m_location;
|
||||
int m_min_calls{1};
|
||||
int m_max_calls{1};
|
||||
int m_call_count{0};
|
||||
|
||||
template <std::size_t... Is>
|
||||
bool match_impl(Args... args, std::index_sequence<Is...>) const
|
||||
{
|
||||
return (std::get<Is>(m_matchers).matches(args) && ...);
|
||||
}
|
||||
|
||||
template <std::size_t... Is>
|
||||
std::string describe_impl(std::index_sequence<Is...>) const
|
||||
{
|
||||
if constexpr (sizeof...(Is) == 0) {
|
||||
return "()";
|
||||
} else {
|
||||
std::string out{"("};
|
||||
bool first = true;
|
||||
auto append = [&](const auto& m) {
|
||||
if (!first) {
|
||||
out += ", ";
|
||||
}
|
||||
out += m.describe();
|
||||
first = false;
|
||||
};
|
||||
(append(std::get<Is>(m_matchers)), ...);
|
||||
out += ")";
|
||||
return out;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R, typename... Args>
|
||||
class Expectation<R(Args...)> {
|
||||
public:
|
||||
using MatcherTuple = std::tuple<MatcherHolder<std::decay_t<Args>>...>;
|
||||
using Action = std::function<R(Args...)>;
|
||||
Expectation(MatcherTuple matchers, std::source_location loc) : matchers_(std::move(matchers)), location_(loc) {}
|
||||
Expectation& returns(R value) { action_ = [val = std::move(value)](Args...) mutable -> R { return val; }; return *this; }
|
||||
Expectation& invokes(Action action) { action_ = std::move(action); return *this; }
|
||||
Expectation& times(int n) { min_calls_ = n; max_calls_ = n; return *this; }
|
||||
Expectation& once() { return times(1); }
|
||||
bool matches(Args... args) const { return match_impl(args..., std::index_sequence_for<Args...>{}); }
|
||||
R execute(Args... args) const { if(!action_) return R{}; return action_(args...); }
|
||||
void record_call() { call_count_++; }
|
||||
bool is_saturated() const { return call_count_ >= max_calls_; }
|
||||
void verify(std::string_view func_name) const {
|
||||
if(call_count_ < min_calls_) { Void::Testing::fail(std::format("Unsatisfied expectation on {}", func_name)); }
|
||||
}
|
||||
private:
|
||||
MatcherTuple matchers_; Action action_; std::source_location location_; int min_calls_{1}; int max_calls_{1}; int call_count_{0};
|
||||
template<size_t... Is> bool match_impl(Args... args, std::index_sequence<Is...>) const { return (std::get<Is>(matchers_).matches(args) && ...); }
|
||||
};
|
||||
} // namespace Void::TestingMock::Detail
|
||||
@@ -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
|
||||
@@ -1,10 +1,80 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
namespace Void::TestingMock {
|
||||
template<typename Derived>
|
||||
class Mock {
|
||||
public:
|
||||
Mock() = default; virtual ~Mock() = default;
|
||||
void verify_all() {}
|
||||
};
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace Void::TestingMock
|
||||
{
|
||||
namespace Detail
|
||||
{
|
||||
/// Type-erased base for MockFunction<Sig>. The aggregate
|
||||
/// MockBase::verify_all() walks a vector of these and calls
|
||||
/// verify() on each.
|
||||
class MockFunctionBase
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~MockFunctionBase() = default;
|
||||
|
||||
/// Verify all expectations were satisfied. Implementations
|
||||
/// should be idempotent — verify_all() may be called
|
||||
/// before MockFunction's destructor runs the same logic.
|
||||
virtual void verify() = 0;
|
||||
};
|
||||
} // namespace Detail
|
||||
|
||||
/// Non-template aggregate base for Mock<Derived>. Holds non-owning
|
||||
/// pointers to every MockFunction registered against this mock so
|
||||
/// `verify_all()` can drive them collectively.
|
||||
class MockBase
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~MockBase() = default;
|
||||
|
||||
void register_function(Detail::MockFunctionBase* fn)
|
||||
{
|
||||
m_functions.push_back(fn);
|
||||
}
|
||||
|
||||
/// Verify every registered MockFunction. Each MockFunction
|
||||
/// also verifies in its own destructor; calling verify_all()
|
||||
/// explicitly produces failures earlier (before stack unwind)
|
||||
/// with the mock's full context still on hand.
|
||||
void verify_all()
|
||||
{
|
||||
for (auto* fn : m_functions) {
|
||||
fn->verify();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::vector<Detail::MockFunctionBase*> m_functions;
|
||||
};
|
||||
|
||||
/// CRTP base for user-defined mocks. Inherit alongside the interface
|
||||
/// being mocked; declare MockFunction members initialized with
|
||||
/// `*this` so they auto-register for `verify_all()`.
|
||||
///
|
||||
/// @code
|
||||
/// class MockDb : public Mock<MockDb>, public Database {
|
||||
/// public:
|
||||
/// MockFunction<int(std::string_view)> get_user_id{*this,
|
||||
/// "get_user_id"}; int get_user_id(std::string_view n) override {
|
||||
/// return get_user_id(n); }
|
||||
/// };
|
||||
/// @endcode
|
||||
template <typename Derived> class Mock : public MockBase
|
||||
{
|
||||
public:
|
||||
|
||||
Mock() = default;
|
||||
~Mock() override = default;
|
||||
|
||||
Mock(const Mock&) = delete;
|
||||
Mock& operator=(const Mock&) = delete;
|
||||
Mock(Mock&&) = delete;
|
||||
Mock& operator=(Mock&&) = delete;
|
||||
};
|
||||
|
||||
} // namespace Void::TestingMock
|
||||
@@ -1,5 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/TestingMock/Expectation.hpp>
|
||||
#include <Void/TestingMock/Mock.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <format>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
@@ -10,17 +16,35 @@ namespace Void::TestingMock
|
||||
|
||||
template <typename> class MockFunction;
|
||||
|
||||
template <typename R, typename... Args> class MockFunction<R(Args...)>
|
||||
template <typename R, typename... Args>
|
||||
class MockFunction<R(Args...)> : public Detail::MockFunctionBase
|
||||
{
|
||||
using Expectation = Detail::Expectation<R(Args...)>;
|
||||
|
||||
public:
|
||||
|
||||
/// Standalone constructor — no auto-registration. The
|
||||
/// destructor will still call verify() so isolated tests work
|
||||
/// without a Mock<Derived> wrapper.
|
||||
explicit MockFunction(std::string name = "unnamed_mock")
|
||||
: name_(std::move(name))
|
||||
: m_name(std::move(name))
|
||||
{
|
||||
}
|
||||
~MockFunction() { verify(); }
|
||||
|
||||
/// Owner-aware constructor — registers with `owner` so
|
||||
/// `owner.verify_all()` drives this mock's verify().
|
||||
MockFunction(MockBase& owner, std::string name = "unnamed_mock")
|
||||
: m_name(std::move(name))
|
||||
{
|
||||
owner.register_function(this);
|
||||
}
|
||||
|
||||
~MockFunction() override { verify(); }
|
||||
|
||||
MockFunction(const MockFunction&) = delete;
|
||||
MockFunction& operator=(const MockFunction&) = delete;
|
||||
MockFunction(MockFunction&&) = delete;
|
||||
MockFunction& operator=(MockFunction&&) = delete;
|
||||
|
||||
template <typename... MArgs>
|
||||
Expectation& expect(std::source_location loc, MArgs&&... matchers)
|
||||
@@ -30,7 +54,7 @@ namespace Void::TestingMock
|
||||
MatcherHolder<std::decay_t<Args>>(
|
||||
std::forward<MArgs>(matchers))...},
|
||||
loc);
|
||||
expectations_.push_back(exp);
|
||||
m_expectations.push_back(exp);
|
||||
return *exp;
|
||||
}
|
||||
|
||||
@@ -43,31 +67,68 @@ namespace Void::TestingMock
|
||||
|
||||
R operator()(Args... args)
|
||||
{
|
||||
std::lock_guard lock{mutex_};
|
||||
for (auto& exp : expectations_) {
|
||||
std::lock_guard lock{m_mutex};
|
||||
for (auto& exp : m_expectations) {
|
||||
if (!exp->is_saturated() && exp->matches(args...)) {
|
||||
exp->record_call();
|
||||
return exp->execute(args...);
|
||||
}
|
||||
}
|
||||
Void::Testing::fail(
|
||||
std::format("Unexpected call to {}", name_));
|
||||
Void::Testing::fail(build_unmatched_message(args...));
|
||||
if constexpr (!std::is_same_v<R, void>) {
|
||||
return R{};
|
||||
}
|
||||
}
|
||||
void verify()
|
||||
|
||||
void verify() override
|
||||
{
|
||||
for (auto& exp : expectations_) {
|
||||
exp->verify(name_);
|
||||
for (auto& exp : m_expectations) {
|
||||
exp->verify(m_name);
|
||||
}
|
||||
expectations_.clear();
|
||||
m_expectations.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::string name_;
|
||||
std::vector<std::shared_ptr<Expectation>> expectations_;
|
||||
std::mutex mutex_;
|
||||
std::string m_name;
|
||||
std::vector<std::shared_ptr<Expectation>> m_expectations;
|
||||
std::mutex m_mutex;
|
||||
|
||||
/// Build a multi-line error message that names the actual
|
||||
/// arguments and lists every registered expectation with its
|
||||
/// matcher tuple and call counts.
|
||||
std::string build_unmatched_message(const Args&... args) const
|
||||
{
|
||||
std::string msg =
|
||||
std::format("no expectation matched call to {}({})", m_name,
|
||||
render_args(args...));
|
||||
if (m_expectations.empty()) {
|
||||
msg += "\n (no expectations registered)";
|
||||
return msg;
|
||||
}
|
||||
for (std::size_t i = 0; i < m_expectations.size(); ++i) {
|
||||
const auto& exp = *m_expectations[i];
|
||||
msg += std::format("\n expectation {}: {} — {}/{} calls",
|
||||
i + 1, exp.describe_matchers(),
|
||||
exp.call_count(), exp.max_calls());
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
static std::string render_args(const Args&... args)
|
||||
{
|
||||
std::string out;
|
||||
bool first = true;
|
||||
auto append = [&](const auto& a) {
|
||||
if (!first) {
|
||||
out += ", ";
|
||||
}
|
||||
out += Void::Testing::Detail::repr(a);
|
||||
first = false;
|
||||
};
|
||||
(append(args), ...);
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Void::TestingMock
|
||||
Reference in new issue
Block a user