Files
Library/include/Void/TestingMock/Expectation.hpp
2026-05-03 18:09:46 +03:00

152 lines
3.3 KiB
C++

#pragma once
#include <Void/TestingMock/Matcher.hpp>
#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
{
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;
}
}
};
} // namespace Void::TestingMock::Detail