1 parent
4fc129fb7e
commit
e7186f17f0
42 files changed
+1816
-413
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user