1 parent
4fc129fb7e
commit
e7186f17f0
42 files changed
+1816
-413
No files matched your search
@@ -0,0 +1,82 @@
|
||||
#pragma once
|
||||
#include <Void/Testing/Config.hpp>
|
||||
#include <Void/Testing/Detail/Registry.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <concepts>
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Testing::Detail {
|
||||
struct TestContext {
|
||||
TestResult* current_result{nullptr};
|
||||
bool should_abort{false};
|
||||
void* fixture{nullptr};
|
||||
static TestContext& current() { thread_local TestContext ctx; return ctx; }
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
std::string repr(const T& val) {
|
||||
if constexpr (std::is_same_v<std::decay_t<T>, bool>) return val ? "true" : "false";
|
||||
else if constexpr (std::is_same_v<std::decay_t<T>, std::string> || std::is_same_v<std::decay_t<T>, std::string_view>) return std::format("\"{}\"", val);
|
||||
else if constexpr (std::is_null_pointer_v<std::decay_t<T>>) return "nullptr";
|
||||
else if constexpr (requires { std::format("{}", val); }) return std::format("{}", val);
|
||||
else return "<unrepresentable value>";
|
||||
}
|
||||
|
||||
void handle_assertion(AssertionResult result);
|
||||
|
||||
struct RequireFailure : std::runtime_error {
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
} // namespace Void::Testing::Detail
|
||||
|
||||
namespace Void::Testing {
|
||||
|
||||
template<typename T> requires std::convertible_to<T, bool>
|
||||
void expect(T&& condition, std::source_location loc = std::source_location::current()) {
|
||||
AssertionResult result; result.passed = static_cast<bool>(condition); result.kind = AssertKind::Expect; result.location = loc; result.expression = "expect(...)"; if (!result.passed) result.message = "expected condition to be true"; Detail::handle_assertion(std::move(result));
|
||||
}
|
||||
|
||||
template<typename T> requires std::convertible_to<T, bool>
|
||||
void require(T&& condition, std::source_location loc = std::source_location::current()) {
|
||||
AssertionResult result; result.passed = static_cast<bool>(condition); result.kind = AssertKind::Require; result.location = loc; result.expression = "require(...)"; if (!result.passed) result.message = "required condition to be true"; Detail::handle_assertion(std::move(result));
|
||||
}
|
||||
|
||||
template<typename T, typename U> requires std::equality_comparable_with<T, U>
|
||||
void expect_eq(const T& lhs, const U& rhs, std::source_location loc = std::source_location::current()) {
|
||||
AssertionResult result; result.passed = (lhs == rhs); result.kind = AssertKind::Expect; result.location = loc; result.expected_repr = Detail::repr(rhs); result.actual_repr = Detail::repr(lhs); if (!result.passed) result.message = std::format("expected {}, got {}", Detail::repr(rhs), Detail::repr(lhs)); Detail::handle_assertion(std::move(result));
|
||||
}
|
||||
|
||||
template<typename T, typename U> requires std::equality_comparable_with<T, U>
|
||||
void require_eq(const T& lhs, const U& rhs, std::source_location loc = std::source_location::current()) {
|
||||
AssertionResult result; result.passed = (lhs == rhs); result.kind = AssertKind::Require; result.location = loc; result.expected_repr = Detail::repr(rhs); result.actual_repr = Detail::repr(lhs); if (!result.passed) result.message = std::format("required {}, got {}", Detail::repr(rhs), Detail::repr(lhs)); Detail::handle_assertion(std::move(result));
|
||||
}
|
||||
|
||||
template<typename T, typename U> requires std::equality_comparable_with<T, U>
|
||||
void expect_ne(const T& lhs, const U& rhs, std::source_location loc = std::source_location::current()) {
|
||||
AssertionResult result; result.passed = (lhs != rhs); result.kind = AssertKind::Expect; result.location = loc; if (!result.passed) result.message = "expected values to differ"; Detail::handle_assertion(std::move(result));
|
||||
}
|
||||
|
||||
template<typename Exception, typename Fn> requires std::invocable<Fn>
|
||||
void expect_throw(Fn&& fn, std::source_location loc = std::source_location::current()) {
|
||||
bool threw = false; try { std::forward<Fn>(fn)(); } catch (const Exception&) { threw = true; } catch (...) {} AssertionResult result; result.passed = threw; result.kind = AssertKind::Expect; result.location = loc; if (!threw) result.message = "expected exception was not thrown"; Detail::handle_assertion(std::move(result));
|
||||
}
|
||||
|
||||
template<typename Fn> requires std::invocable<Fn>
|
||||
void expect_nothrow(Fn&& fn, std::source_location loc = std::source_location::current()) {
|
||||
bool threw = false; try { std::forward<Fn>(fn)(); } catch (...) { threw = true; } AssertionResult result; result.passed = !threw; result.kind = AssertKind::Expect; result.location = loc; if (threw) result.message = "expected no exception"; Detail::handle_assertion(std::move(result));
|
||||
}
|
||||
|
||||
inline void fail(std::string_view message = "", std::source_location loc = std::source_location::current()) {
|
||||
AssertionResult result; result.passed = false; result.kind = AssertKind::Expect; result.location = loc; result.message = message.empty() ? "explicit failure" : std::string{message}; Detail::handle_assertion(std::move(result));
|
||||
}
|
||||
|
||||
inline void skip(std::string_view reason, std::source_location loc = std::source_location::current()) {
|
||||
auto& ctx = Detail::TestContext::current(); if (ctx.current_result) { ctx.current_result->state = TestState::Skipped; ctx.current_result->skip_reason = reason; } throw Detail::RequireFailure{std::string{reason}};
|
||||
}
|
||||
|
||||
} // namespace Void::Testing
|
||||
@@ -0,0 +1,90 @@
|
||||
#pragma once
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Testing
|
||||
{
|
||||
|
||||
enum class AssertKind : uint8_t { Expect, Require, Assert };
|
||||
enum class TestState : uint8_t { Passed, Failed, Skipped, Crashed };
|
||||
|
||||
struct AssertionResult {
|
||||
bool passed{true};
|
||||
AssertKind kind{AssertKind::Expect};
|
||||
std::string expression;
|
||||
std::string expected_repr;
|
||||
std::string actual_repr;
|
||||
std::string message;
|
||||
std::source_location location;
|
||||
static AssertionResult
|
||||
ok(std::source_location loc = std::source_location::current())
|
||||
{
|
||||
return {.passed = true, .location = loc};
|
||||
}
|
||||
};
|
||||
|
||||
struct TestCase {
|
||||
std::function<void()> run;
|
||||
std::string name;
|
||||
std::string suite;
|
||||
std::string file;
|
||||
int line{0};
|
||||
bool disabled{false};
|
||||
std::string skip_reason;
|
||||
std::vector<std::string> tags;
|
||||
std::function<void*()> fixture_create;
|
||||
std::function<void(void*)> fixture_destroy;
|
||||
std::function<void(void*)> fixture_setup;
|
||||
std::function<void(void*)> fixture_teardown;
|
||||
};
|
||||
|
||||
struct TestResult {
|
||||
std::string name;
|
||||
std::string suite;
|
||||
TestState state{TestState::Passed};
|
||||
std::chrono::nanoseconds duration{0};
|
||||
std::vector<AssertionResult> assertions;
|
||||
std::string skip_reason;
|
||||
std::string crash_info;
|
||||
std::string stacktrace;
|
||||
std::vector<std::string> tags;
|
||||
};
|
||||
|
||||
struct SuiteResult {
|
||||
std::string name;
|
||||
std::vector<TestResult> tests;
|
||||
};
|
||||
|
||||
struct RunResult {
|
||||
std::vector<SuiteResult> suites;
|
||||
std::chrono::nanoseconds total_duration{0};
|
||||
int passed{0};
|
||||
int failed{0};
|
||||
int skipped{0};
|
||||
int crashed{0};
|
||||
int total{0};
|
||||
};
|
||||
|
||||
struct RunConfig {
|
||||
std::string filter_pattern;
|
||||
std::string suite_filter;
|
||||
std::string reporter{"console"};
|
||||
std::string output_file;
|
||||
std::string tag_filter;
|
||||
bool list_only{false};
|
||||
bool verbose{false};
|
||||
bool no_color{false};
|
||||
bool shuffle{false};
|
||||
bool show_help{false};
|
||||
unsigned repeat{1};
|
||||
unsigned random_seed{0};
|
||||
};
|
||||
|
||||
} // namespace Void::Testing
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include <Void/Testing/Reporter.hpp>
|
||||
#include <Void/Testing/Detail/Color.hpp>
|
||||
#include <ostream>
|
||||
|
||||
namespace Void::Testing {
|
||||
class ConsoleReporter final : public Reporter {
|
||||
public:
|
||||
explicit ConsoleReporter(std::ostream& os, bool color_enabled = true) : os_(os) {
|
||||
Detail::Color::g_color_enabled = color_enabled;
|
||||
}
|
||||
void on_run_start(const RunConfig& config, int total_tests) override;
|
||||
void on_suite_start(const std::string& suite_name, int test_count) override;
|
||||
void on_test_start(const TestCase& test) override;
|
||||
void on_test_end(const TestResult& result) override;
|
||||
void on_suite_end(const SuiteResult& result) override;
|
||||
void on_run_end(const RunResult& result) override;
|
||||
private:
|
||||
std::ostream& os_;
|
||||
void print_assertion_failure(const AssertionResult& ar);
|
||||
void print_crash_info(const TestResult& result);
|
||||
};
|
||||
} // namespace Void::Testing
|
||||
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include <string_view>
|
||||
#include <string>
|
||||
|
||||
namespace Void::Testing::Detail::Color {
|
||||
inline bool g_color_enabled{true};
|
||||
|
||||
constexpr std::string_view reset = "\033[0m";
|
||||
constexpr std::string_view bold = "\033[1m";
|
||||
constexpr std::string_view dim = "\033[2m";
|
||||
constexpr std::string_view bold_red = "\033[1;31m";
|
||||
constexpr std::string_view bold_green = "\033[1;32m";
|
||||
constexpr std::string_view bold_yellow = "\033[1;33m";
|
||||
constexpr std::string_view bold_blue = "\033[1;34m";
|
||||
constexpr std::string_view bold_magenta = "\033[1;35m";
|
||||
constexpr std::string_view bold_cyan = "\033[1;36m";
|
||||
constexpr std::string_view bold_white = "\033[1;37m";
|
||||
constexpr std::string_view red = "\033[31m";
|
||||
constexpr std::string_view green = "\033[32m";
|
||||
constexpr std::string_view yellow = "\033[33m";
|
||||
constexpr std::string_view cyan = "\033[36m";
|
||||
|
||||
inline std::string apply(std::string_view code, std::string_view text) {
|
||||
if (!g_color_enabled) return std::string{text};
|
||||
return std::string{code} + std::string{text} + std::string{reset};
|
||||
}
|
||||
inline std::string success(std::string_view text) { return apply(bold_green, text); }
|
||||
inline std::string fail(std::string_view text) { return apply(bold_red, text); }
|
||||
inline std::string skip(std::string_view text) { return apply(bold_yellow, text); }
|
||||
inline std::string info(std::string_view text) { return apply(bold_cyan, text); }
|
||||
inline std::string hint(std::string_view text) { return apply(dim, text); }
|
||||
inline std::string heading(std::string_view text) { return apply(bold_blue, text); }
|
||||
inline std::string emphasis(std::string_view text) { return apply(bold, text); }
|
||||
} // namespace Void::Testing::Detail::Color
|
||||
@@ -0,0 +1,107 @@
|
||||
#pragma once
|
||||
#include <Void/Testing/Config.hpp>
|
||||
#include <Void/Testing/Detail/Registry.hpp>
|
||||
#include <source_location>
|
||||
#include <type_traits>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Testing::Detail {
|
||||
|
||||
template<typename T>
|
||||
struct function_traits;
|
||||
|
||||
template<typename R, typename... Args>
|
||||
struct function_traits<R(*)(Args...)> {
|
||||
using return_type = R;
|
||||
using class_type = void;
|
||||
static constexpr bool is_member = false;
|
||||
};
|
||||
|
||||
template<typename R, typename C, typename... Args>
|
||||
struct function_traits<R(C::*)(Args...) const> {
|
||||
using return_type = R;
|
||||
using class_type = C;
|
||||
static constexpr bool is_member = true;
|
||||
};
|
||||
|
||||
template<typename R, typename C, typename... Args>
|
||||
struct function_traits<R(C::*)(Args...)> {
|
||||
using return_type = R;
|
||||
using class_type = C;
|
||||
static constexpr bool is_member = true;
|
||||
};
|
||||
|
||||
template<auto Fn>
|
||||
class test_registrar {
|
||||
public:
|
||||
explicit test_registrar(const char* suite, const char* file, int line,
|
||||
const char* test_name_str,
|
||||
std::vector<std::string> tags = {},
|
||||
bool is_skipped = false,
|
||||
std::string skip_reason = "") {
|
||||
auto& reg = Registry::instance();
|
||||
std::string test_name = test_name_str;
|
||||
|
||||
if (test_name.empty()) {
|
||||
test_name = std::format("test_at_line_{}", line);
|
||||
}
|
||||
|
||||
if constexpr (std::is_member_function_pointer_v<decltype(Fn)>) {
|
||||
using Class = typename function_traits<decltype(Fn)>::class_type;
|
||||
reg.add(TestCase{
|
||||
.name = std::move(test_name),
|
||||
.suite = suite,
|
||||
.file = file,
|
||||
.line = line,
|
||||
.disabled = is_skipped,
|
||||
.skip_reason = std::move(skip_reason),
|
||||
.tags = std::move(tags),
|
||||
.fixture_create = []() -> void* { return new Class(); },
|
||||
.fixture_destroy = [](void* p) { delete static_cast<Class*>(p); },
|
||||
.fixture_setup = [](void* p) {
|
||||
if constexpr (requires { static_cast<Class*>(p)->setup(); })
|
||||
static_cast<Class*>(p)->setup();
|
||||
},
|
||||
.fixture_teardown = [](void* p) {
|
||||
if constexpr (requires { static_cast<Class*>(p)->teardown(); })
|
||||
static_cast<Class*>(p)->teardown();
|
||||
}
|
||||
});
|
||||
|
||||
const_cast<TestCase&>(reg.tests().back()).run =
|
||||
[ptr = Fn, idx = reg.tests().size() - 1]() {
|
||||
auto& tc = Registry::instance().tests()[idx];
|
||||
void* fixture = tc.fixture_create();
|
||||
try {
|
||||
tc.fixture_setup(fixture);
|
||||
(static_cast<Class*>(fixture)->*ptr)();
|
||||
tc.fixture_teardown(fixture);
|
||||
} catch (...) {
|
||||
tc.fixture_teardown(fixture);
|
||||
tc.fixture_destroy(fixture);
|
||||
throw;
|
||||
}
|
||||
tc.fixture_destroy(fixture);
|
||||
};
|
||||
} else {
|
||||
reg.add(TestCase{
|
||||
.run = Fn,
|
||||
.name = std::move(test_name),
|
||||
.suite = suite,
|
||||
.file = file,
|
||||
.line = line,
|
||||
.disabled = is_skipped,
|
||||
.skip_reason = std::move(skip_reason),
|
||||
.tags = std::move(tags)
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Void::Testing::Detail
|
||||
|
||||
namespace Void::Testing {
|
||||
template<auto Fn>
|
||||
Detail::test_registrar<Fn> test{};
|
||||
} // namespace Void::Testing
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include <Void/Testing/Config.hpp>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
namespace Void::Testing::Detail {
|
||||
class Registry {
|
||||
public:
|
||||
static Registry& instance() {
|
||||
static Registry reg;
|
||||
return reg;
|
||||
}
|
||||
void add(TestCase test_case) {
|
||||
std::lock_guard lock{mutex_};
|
||||
tests_.push_back(std::move(test_case));
|
||||
}
|
||||
const std::vector<TestCase>& tests() const { return tests_; }
|
||||
private:
|
||||
Registry() = default;
|
||||
std::vector<TestCase> tests_;
|
||||
std::mutex mutex_;
|
||||
};
|
||||
} // namespace Void::Testing::Detail
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <csetjmp>
|
||||
#endif
|
||||
|
||||
namespace Void::Testing::Detail {
|
||||
using CrashCallback = std::function<void(int signal, const std::string& stacktrace)>;
|
||||
void install_signal_handlers(CrashCallback on_crash);
|
||||
void uninstall_signal_handlers();
|
||||
const char* signal_name(int sig);
|
||||
|
||||
#ifndef _WIN32
|
||||
sigjmp_buf& get_jump_buffer();
|
||||
void set_jump_ready(bool ready);
|
||||
#endif
|
||||
} // namespace Void::Testing::Detail
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <ostream>
|
||||
|
||||
namespace Void::Testing::Detail {
|
||||
struct StackFrame {
|
||||
std::string function_name;
|
||||
std::string source_file;
|
||||
int source_line{0};
|
||||
std::string source_snippet;
|
||||
};
|
||||
|
||||
std::vector<StackFrame> capture_stacktrace(unsigned skip_frames = 0, unsigned max_frames = 32);
|
||||
std::string read_source_snippet(const std::string& file, int line, int context_lines = 3);
|
||||
std::string format_stacktrace(const std::vector<StackFrame>& frames);
|
||||
void print_stacktrace(std::ostream& os, const std::vector<StackFrame>& frames);
|
||||
} // namespace Void::Testing::Detail
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace Void::Testing::Detail {
|
||||
std::string resolve_symbol_name(const void* address);
|
||||
std::string demangle(std::string_view mangled);
|
||||
std::string pretty_name(const void* address);
|
||||
std::string extract_test_name(std::string_view pretty_symbol);
|
||||
std::string suite_from_file(std::string_view file_path);
|
||||
} // namespace Void::Testing::Detail
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
namespace Void::Testing {
|
||||
class Fixture {
|
||||
public:
|
||||
virtual ~Fixture() = default;
|
||||
virtual void setup() {}
|
||||
virtual void teardown() {}
|
||||
};
|
||||
} // namespace Void::Testing
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <Void/Testing/Reporter.hpp>
|
||||
#include <ostream>
|
||||
|
||||
namespace Void::Testing {
|
||||
class JUnitReporter final : public Reporter {
|
||||
public:
|
||||
explicit JUnitReporter(std::ostream& os) : os_(os) {}
|
||||
void on_run_start(const RunConfig& config, int total_tests) override;
|
||||
void on_suite_start(const std::string& suite_name, int test_count) override;
|
||||
void on_test_start(const TestCase& test) override;
|
||||
void on_test_end(const TestResult& result) override;
|
||||
void on_suite_end(const SuiteResult& result) override;
|
||||
void on_run_end(const RunResult& result) override;
|
||||
private:
|
||||
std::ostream& os_;
|
||||
std::vector<SuiteResult> suite_results_;
|
||||
static std::string xml_escape(std::string_view s);
|
||||
};
|
||||
} // namespace Void::Testing
|
||||
@@ -1 +0,0 @@
|
||||
#pragma once
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include <Void/Testing/Config.hpp>
|
||||
#include <ostream>
|
||||
#include <memory>
|
||||
|
||||
namespace Void::Testing {
|
||||
class Reporter {
|
||||
public:
|
||||
virtual ~Reporter() = default;
|
||||
virtual void on_run_start(const RunConfig& config, int total_tests) = 0;
|
||||
virtual void on_suite_start(const std::string& suite_name, int test_count) = 0;
|
||||
virtual void on_test_start(const TestCase& test) = 0;
|
||||
virtual void on_test_end(const TestResult& result) = 0;
|
||||
virtual void on_suite_end(const SuiteResult& result) = 0;
|
||||
virtual void on_run_end(const RunResult& result) = 0;
|
||||
};
|
||||
} // namespace Void::Testing
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include <Void/Testing/Config.hpp>
|
||||
#include <Void/Testing/Reporter.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace Void::Testing {
|
||||
class Runner {
|
||||
public:
|
||||
static Runner& instance() {
|
||||
static Runner r;
|
||||
return r;
|
||||
}
|
||||
RunConfig parse_args(int argc, char** argv);
|
||||
RunResult run(const RunConfig& config);
|
||||
int main(int argc, char** argv);
|
||||
private:
|
||||
Runner() = default;
|
||||
std::vector<TestCase> filter_tests(const std::vector<TestCase>& tests, const RunConfig& config);
|
||||
std::unique_ptr<Reporter> create_reporter(const RunConfig& config);
|
||||
TestResult execute_test(TestCase& test_case);
|
||||
void run_test_body(TestCase& test_case, TestResult& result);
|
||||
};
|
||||
} // namespace Void::Testing
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
namespace Void::Testing {
|
||||
|
||||
// Aliases to void so users can omit return statements safely
|
||||
// and avoid compiler warnings/errors. Perfectly matches std::function<void()>.
|
||||
using Test = void;
|
||||
using DisabledTest = void;
|
||||
|
||||
} // namespace Void::Testing
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in new issue
Block a user