#pragma once #include #include #include #include #include #include #if __has_include() #include #include #define VOID_TESTING_HAS_REFLECTION 1 #endif // ── Reflection-based test registration // ──────────────────────────────────────── Derive a test suite from // `Suite` (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 { // 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 class Suite { protected: Suite() = default; private: /// Run one reflected test method against a fresh Derived instance, /// wrapping it in setup()/teardown() when present. template 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(); } } /// Non-capturing thunk: a template argument is a constant /// expression, unlike a reflected member captured by a lambda. template static void run_method_thunk() { run_method(); } static std::string suite_label() { if constexpr (constexpr auto n = Void::Meta::annotation_of(^^Derived); n.has_value()) { return std::string{n->value.view()}; } else { return Void::Meta::type_name(); } } 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 tags; template for (constexpr auto a : std::define_static_array( meta::annotations_of_with_type( m, ^^Tag))) { constexpr Tag t = meta::extract(a); tags.emplace_back(t.value.view()); } bool disabled = false; std::string skip_reason; if constexpr (constexpr auto s = Void::Meta::annotation_of(m); s.has_value()) { disabled = true; skip_reason = std::string{s->reason.view()}; } constexpr auto name = meta::identifier_of(m); constexpr const char* file = loc.file_name(); constexpr int line = static_cast(loc.line()); constexpr auto run = &run_method_thunk; reg.add(TestCase{ .run = run, .name = std::string{name}, .suite = suite, .file = file, .line = 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 class Suite { protected: Suite() = default; }; #endif // VOID_TESTING_HAS_REFLECTION } // namespace Void::Testing