Testing module

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-04-29 22:49:33 +03:00
1 parent 4fc129fb7e
commit e7186f17f0
42 files changed
+1816 -413

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
#pragma once
#include <Void/TestingMock/Expectation.hpp>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
namespace Void::TestingMock
{
template <typename> class MockFunction;
template <typename R, typename... Args> class MockFunction<R(Args...)>
{
using Expectation = Detail::Expectation<R(Args...)>;
public:
explicit MockFunction(std::string name = "unnamed_mock")
: name_(std::move(name))
{
}
~MockFunction() { verify(); }
template <typename... MArgs>
Expectation& expect(std::source_location loc, MArgs&&... matchers)
{
auto exp = std::make_shared<Expectation>(
typename Expectation::MatcherTuple{
MatcherHolder<std::decay_t<Args>>(
std::forward<MArgs>(matchers))...},
loc);
expectations_.push_back(exp);
return *exp;
}
template <typename... MArgs>
Expectation& expect(MArgs&&... matchers)
{
return expect(std::source_location::current(),
std::forward<MArgs>(matchers)...);
}
R operator()(Args... args)
{
std::lock_guard lock{mutex_};
for (auto& exp : expectations_) {
if (!exp->is_saturated() && exp->matches(args...)) {
exp->record_call();
return exp->execute(args...);
}
}
Void::Testing::fail(
std::format("Unexpected call to {}", name_));
if constexpr (!std::is_same_v<R, void>) {
return R{};
}
}
void verify()
{
for (auto& exp : expectations_) {
exp->verify(name_);
}
expectations_.clear();
}
private:
std::string name_;
std::vector<std::shared_ptr<Expectation>> expectations_;
std::mutex mutex_;
};
} // namespace Void::TestingMock