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

+33
View File
@@ -0,0 +1,33 @@
#pragma once
#include <Void/TestingMock/Matcher.hpp>
#include <source_location>
#include <functional>
#include <string>
#include <tuple>
namespace Void::TestingMock::Detail {
template<typename> class Expectation;
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
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include <Void/Testing/Assert.hpp>
#include <string>
#include <functional>
#include <memory>
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 "_"; } };
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>
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
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include <string>
namespace Void::TestingMock {
template<typename Derived>
class Mock {
public:
Mock() = default; virtual ~Mock() = default;
void verify_all() {}
};
} // namespace Void::TestingMock
+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