feat(data,text,testing): implement VL-002 TOML, VL-008 StrictJson UTF-8, VL-009 native tests, and VL-013 Semver shorthands

This commit is contained in:
faumaray committed 2026-11-26 20:27:42 +03:00
1 parent 53d42bda42
commit adfc8548dc
11 files changed
+2888 -28

No files matched your search

+185 -5
View File
@@ -1,10 +1,190 @@
#pragma once
namespace Void::Testing {
#include <Void/Testing/Config.hpp>
#include <Void/Testing/Detail/Registry.hpp>
// 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;
#include <initializer_list>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
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 Detail
{
[[nodiscard]] inline std::string
normalize_suite_label(std::string_view raw)
{
if (raw.size() >= 2 && raw.front() == '"' && raw.back() == '"') {
raw = raw.substr(1, raw.size() - 2);
}
return std::string{raw};
}
class NativeRegistrar final
{
public:
NativeRegistrar(void (*fn)(), std::string_view suite,
const char* file, int line,
std::string_view name,
std::initializer_list<const char*> tags = {},
bool disabled = false,
std::string_view skip_reason = {})
{
std::vector<std::string> tag_vec;
tag_vec.reserve(tags.size());
for (const char* t : tags) {
if (t != nullptr) {
tag_vec.emplace_back(t);
}
}
Registry::instance().add(TestCase{
.run = fn,
.name = std::string{name},
.suite = normalize_suite_label(suite),
.file = file != nullptr ? file : "",
.line = line,
.disabled = disabled,
.skip_reason = std::string{skip_reason},
.tags = std::move(tag_vec),
});
}
};
template <typename FixtureT> class NativeFixtureRegistrar final
{
public:
NativeFixtureRegistrar(void (*fn)(FixtureT&),
std::string_view suite,
const char* file, int line,
std::string_view name,
std::initializer_list<const char*> tags = {},
bool disabled = false,
std::string_view skip_reason = {})
{
std::vector<std::string> tag_vec;
tag_vec.reserve(tags.size());
for (const char* t : tags) {
if (t != nullptr) {
tag_vec.emplace_back(t);
}
}
Registry::instance().add(TestCase{
.run =
[fn]() {
FixtureT fixture{};
if constexpr (requires { fixture.setup(); }) {
fixture.setup();
}
try {
fn(fixture);
} catch (...) {
if constexpr (requires {
fixture.teardown();
}) {
fixture.teardown();
}
throw;
}
if constexpr (requires { fixture.teardown(); }) {
fixture.teardown();
}
},
.name = std::string{name},
.suite = normalize_suite_label(suite),
.file = file != nullptr ? file : "",
.line = line,
.disabled = disabled,
.skip_reason = std::string{skip_reason},
.tags = std::move(tag_vec),
});
}
};
} // namespace Detail
} // namespace Void::Testing
#define VOID_TESTING_DETAIL_CAT_IMPL(a, b) a##b
#define VOID_TESTING_DETAIL_CAT(a, b) VOID_TESTING_DETAIL_CAT_IMPL(a, b)
#ifdef __COUNTER__
#define VOID_TESTING_DETAIL_NEXT_ID __COUNTER__
#else
#define VOID_TESTING_DETAIL_NEXT_ID __LINE__
#endif
#define VOID_TESTING_DETAIL_DEFINE_TEST(suite_expr, test_name, disabled_flag, \
skip_msg, uid, ...) \
static void VOID_TESTING_DETAIL_CAT(_void_native_test_fn_, uid)(); \
namespace \
{ \
[[maybe_unused]] const ::Void::Testing::Detail::NativeRegistrar \
VOID_TESTING_DETAIL_CAT(_void_native_test_reg_, uid){ \
&VOID_TESTING_DETAIL_CAT(_void_native_test_fn_, uid), \
(suite_expr), \
__FILE__, \
__LINE__, \
#test_name, \
{__VA_ARGS__}, \
(disabled_flag), \
(skip_msg)}; \
} \
static void VOID_TESTING_DETAIL_CAT(_void_native_test_fn_, uid)()
#define VOID_TESTING_DETAIL_DEFINE_FIXTURE_TEST(fixture_type, test_name, \
disabled_flag, skip_msg, uid) \
static void VOID_TESTING_DETAIL_CAT(_void_native_fx_fn_, uid)( \
fixture_type&); \
namespace \
{ \
[[maybe_unused]] const ::Void::Testing::Detail::NativeFixtureRegistrar< \
fixture_type> \
VOID_TESTING_DETAIL_CAT(_void_native_fx_reg_, uid){ \
&VOID_TESTING_DETAIL_CAT(_void_native_fx_fn_, uid), \
#fixture_type, \
__FILE__, \
__LINE__, \
#test_name, \
{}, \
(disabled_flag), \
(skip_msg)}; \
} \
static void VOID_TESTING_DETAIL_CAT(_void_native_fx_fn_, uid)( \
[[maybe_unused]] fixture_type& fixture)
/// Declare a self-registering test case with zero CMake scanner dependency.
/// Symbols are internal-linkage and counter-disambiguated so files sharing the
/// same basename in different subdirectories link cleanly together.
#define VOID_TEST(suite, name) \
VOID_TESTING_DETAIL_DEFINE_TEST(#suite, name, false, "", \
VOID_TESTING_DETAIL_NEXT_ID)
/// Same as VOID_TEST, taking tags (string literals) after `name`.
#define VOID_TEST_TAGGED(suite, name, ...) \
VOID_TESTING_DETAIL_DEFINE_TEST(#suite, name, false, "", \
VOID_TESTING_DETAIL_NEXT_ID, __VA_ARGS__)
/// Declare a skipped/disabled self-registering test case.
#define VOID_TEST_DISABLED(suite, name, reason) \
VOID_TESTING_DETAIL_DEFINE_TEST(#suite, name, true, (reason), \
VOID_TESTING_DETAIL_NEXT_ID)
/// Declare a self-registering fixture test. The fixture instance is bound as
/// `fixture` inside the test body, with optional setup()/teardown() hooks.
#define VOID_TEST_FIXTURE(fixture_type, name) \
VOID_TESTING_DETAIL_DEFINE_FIXTURE_TEST(fixture_type, name, false, "", \
VOID_TESTING_DETAIL_NEXT_ID)
#define VOID_TEST_FIXTURE_DISABLED(fixture_type, name, reason) \
VOID_TESTING_DETAIL_DEFINE_FIXTURE_TEST(fixture_type, name, true, (reason), \
VOID_TESTING_DETAIL_NEXT_ID)