1 parent
f0825fbd1a
commit
42268c6c35
45 files changed
+2732
-262
No files matched your search
@@ -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