Files
Library/include/Void/Testing/Suite.hpp
T
faumarayandClaude Opus 4.8 80293ee56f Add async/resilience/reflection primitives + FSM v2 rewrite
A round of new universal capabilities (all generic — templated/reflection
based, no app coupling), verified by the test suite (1103 passing):

Data reflection:
- Reflect.hpp now (de)serializes chrono::duration, chrono::time_point,
  std::variant (tagged), std::array, and non-string-keyed std::map;
  Meta.hpp gains is_time_point_v et al.

Async:
- EventBus gains Mode::Async (worker-thread dispatch + flush()).
- ThreadPool gains a bounded queue, try_submit(), and rejection stats.

New modules / containers:
- Resilience: TokenBucket, KeyedRateLimiter<Key>, CircuitBreaker,
  ResourceMonitor.
- Containers: BufferPool<T> (size-class buffer reuse) and Cache<K,V>
  (per-entry TTL, LRU/LFU/FIFO eviction, hit/miss stats).
- System: process/CPU/memory metrics (Metrics.hpp) and a streaming
  Process API.

Plus FSM v2 (typed Success/Failure, sub-machines, parallel regions,
history states, deferred events), Configuration/Logging reflection
helpers, and the accompanying test suites.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 15:45:16 +03:00

176 lines
4.7 KiB
C++

#pragma once
#include <Void/Core/Meta.hpp>
#include <Void/Testing/Annotations.hpp>
#include <Void/Testing/Config.hpp>
#include <Void/Testing/Detail/Registry.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <vector>
#if __has_include(<meta>)
#include <meta>
#define VOID_TESTING_HAS_REFLECTION 1
#endif
// ── Reflection-based test registration
// ──────────────────────────────────────── Derive a test suite from
// `Suite<Derived>` (CRTP). Every public, 0-argument member function returning
// `Test`/`DisabledTest` (i.e. void) — except `setup` and `teardown` — is
// auto-registered as a test by reflecting `Derived`. No CMake scan, no macros,
// no registrar line: a `[[gnu::used]]` static in the base forces registration
// at program start even though the suite is never instantiated for its own
// sake.
//
// struct MathTests : Void::Testing::Suite<MathTests> {
// void setup() { … } // optional, per-test
// Void::Testing::Test addition() { expect_eq(1 + 1, 2); }
// [[= Testing::tag("slow")]] Void::Testing::Test big() { … }
// [[= Testing::skip("wip")]] Void::Testing::Test later() { … }
// };
//
// The suite label defaults to the struct's name; `[[= Testing::name("Math")]]`
// on the struct overrides it.
namespace Void::Testing
{
#ifdef VOID_TESTING_HAS_REFLECTION
namespace Detail
{
namespace meta = std::meta;
/// A reflected member is a test if it is a public, non-special,
/// 0-argument member function returning void, other than the
/// setup/teardown lifecycle hooks.
consteval bool is_test_method(meta::info m)
{
if (!meta::is_function(m) || meta::is_special_member_function(m)) {
return false;
}
if (meta::is_static_member(m)) {
return false;
}
if (meta::parameters_of(m).size() != 0) {
return false;
}
if (meta::dealias(meta::return_type_of(m)) != ^^void) {
return false;
}
const std::string_view id = meta::identifier_of(m);
return id != "setup" && id != "teardown";
}
} // namespace Detail
namespace meta = std::meta;
template <typename Derived> class Suite
{
protected:
Suite() = default;
private:
/// Run one reflected test method against a fresh Derived instance,
/// wrapping it in setup()/teardown() when present.
template <meta::info M> static void run_method()
{
Derived d{};
if constexpr (requires { d.setup(); }) {
d.setup();
}
try {
d.[:M:]();
} catch (...) {
if constexpr (requires { d.teardown(); }) {
d.teardown();
}
throw;
}
if constexpr (requires { d.teardown(); }) {
d.teardown();
}
}
static std::string suite_label()
{
if constexpr (constexpr auto n =
Void::Meta::annotation_of<Name>(^^Derived);
n.has_value()) {
return std::string{n->value.view()};
} else {
return Void::Meta::type_name<Derived>();
}
}
static void register_all()
{
auto& reg = Detail::Registry::instance();
const std::string suite = suite_label();
template for (constexpr auto m :
std::define_static_array(meta::members_of(
^^Derived,
meta::access_context::unchecked())))
{
if constexpr (Detail::is_test_method(m)) {
constexpr auto loc = meta::source_location_of(m);
std::vector<std::string> tags;
template for (constexpr auto a :
std::define_static_array(
meta::annotations_of_with_type(
m, ^^Tag)))
{
constexpr Tag t = meta::extract<Tag>(a);
tags.emplace_back(t.value.view());
}
bool disabled = false;
std::string skip_reason;
if constexpr (constexpr auto s =
Void::Meta::annotation_of<Skip>(m);
s.has_value()) {
disabled = true;
skip_reason = std::string{s->reason.view()};
}
reg.add(TestCase{
.run = [] { run_method<m>(); },
.name = std::string{meta::identifier_of(m)},
.suite = suite,
.file = loc.file_name(),
.line = static_cast<int>(loc.line()),
.disabled = disabled,
.skip_reason = std::move(skip_reason),
.tags = std::move(tags),
});
}
}
}
struct Registrar {
Registrar() { register_all(); }
};
// Force the static initializer to run even though Derived is never
// instantiated for its own sake (no test object, no registrar
// line).
[[gnu::used]] static inline Registrar _registrar{};
};
#else // no reflection — Suite is unavailable; use the CMake scanner path
template <typename Derived> class Suite
{
protected:
Suite() = default;
};
#endif // VOID_TESTING_HAS_REFLECTION
} // namespace Void::Testing