Reflection and filesystem

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-05-03 18:09:46 +03:00
1 parent f0825fbd1a
commit 42268c6c35
45 files changed
+2732 -262

No files matched your search

+142 -24
View File
@@ -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