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

+246
View File
@@ -1,5 +1,6 @@
#pragma once
#include <array>
#include <charconv>
#include <cmath>
#include <cstddef>
@@ -41,6 +42,72 @@ namespace Void::Json::Strict
ParseError error;
};
[[nodiscard]] constexpr std::size_t
validate_utf8_at(const std::string_view input,
const std::size_t pos) noexcept
{
if (pos >= input.size()) {
return 0U;
}
const auto b0 = static_cast<unsigned char>(input[pos]);
if (b0 <= 0x7FU) {
return 1U;
}
if (b0 >= 0xC2U && b0 <= 0xDFU) {
if (pos + 1U >= input.size()) {
return 0U;
}
const auto b1 = static_cast<unsigned char>(input[pos + 1U]);
return (b1 >= 0x80U && b1 <= 0xBFU) ? 2U : 0U;
}
if (b0 >= 0xE0U && b0 <= 0xEFU) {
if (pos + 2U >= input.size()) {
return 0U;
}
const auto b1 = static_cast<unsigned char>(input[pos + 1U]);
const auto b2 = static_cast<unsigned char>(input[pos + 2U]);
const unsigned char lo = (b0 == 0xE0U) ? 0xA0U : 0x80U;
const unsigned char hi = (b0 == 0xEDU) ? 0x9FU : 0xBFU;
if (b1 < lo || b1 > hi || b2 < 0x80U || b2 > 0xBFU) {
return 0U;
}
return 3U;
}
if (b0 >= 0xF0U && b0 <= 0xF4U) {
if (pos + 3U >= input.size()) {
return 0U;
}
const auto b1 = static_cast<unsigned char>(input[pos + 1U]);
const auto b2 = static_cast<unsigned char>(input[pos + 2U]);
const auto b3 = static_cast<unsigned char>(input[pos + 3U]);
const unsigned char lo = (b0 == 0xF0U) ? 0x90U : 0x80U;
const unsigned char hi = (b0 == 0xF4U) ? 0x8FU : 0xBFU;
if (b1 < lo || b1 > hi || b2 < 0x80U || b2 > 0xBFU ||
b3 < 0x80U || b3 > 0xBFU) {
return 0U;
}
return 4U;
}
return 0U;
}
[[nodiscard]] inline std::expected<void, ParseError>
validate_utf8_bytes(const std::string_view input)
{
std::size_t pos = 0U;
while (pos < input.size()) {
const std::size_t len = validate_utf8_at(input, pos);
if (len == 0U) {
return std::unexpected{ParseError{
.offset = pos,
.message = "invalid UTF-8 sequence",
}};
}
pos += len;
}
return {};
}
class Parser final
{
public:
@@ -179,6 +246,18 @@ namespace Void::Json::Strict
fail("control character in string");
}
if (value != '\\') {
if (value >= 0x80U) {
const std::size_t seq_start = position_ - 1U;
const std::size_t len =
validate_utf8_at(input_, seq_start);
if (len == 0U) {
position_ = seq_start;
fail("invalid UTF-8 sequence");
}
output.append(input_.substr(seq_start, len));
position_ = seq_start + len;
continue;
}
output.push_back(static_cast<char>(value));
continue;
}
@@ -435,4 +514,171 @@ namespace Void::Json::Strict
.allowDecimals = true});
}
[[nodiscard]] inline bool is_valid_utf8(const std::string_view input) noexcept
{
std::size_t pos = 0U;
while (pos < input.size()) {
const std::size_t len = Detail::validate_utf8_at(input, pos);
if (len == 0U) {
return false;
}
pos += len;
}
return true;
}
[[nodiscard]] inline std::expected<void, ParseError>
validate_utf8(const std::string_view input)
{
return Detail::validate_utf8_bytes(input);
}
namespace Detail
{
inline void append_escaped_string(std::string& out,
const std::string_view text)
{
const auto utf8 = validate_utf8_bytes(text);
if (!utf8.has_value()) {
throw ParseFailure{.error = utf8.error()};
}
out.push_back('"');
std::size_t pos = 0U;
while (pos < text.size()) {
const auto ch = static_cast<unsigned char>(text[pos]);
switch (ch) {
case '"':
out += "\\\"";
++pos;
break;
case '\\':
out += "\\\\";
++pos;
break;
case '\b':
out += "\\b";
++pos;
break;
case '\f':
out += "\\f";
++pos;
break;
case '\n':
out += "\\n";
++pos;
break;
case '\r':
out += "\\r";
++pos;
break;
case '\t':
out += "\\t";
++pos;
break;
default:
if (ch < 0x20U) {
static constexpr char kHex[] = "0123456789abcdef";
out += "\\u00";
out.push_back(kHex[(ch >> 4U) & 0x0FU]);
out.push_back(kHex[ch & 0x0FU]);
++pos;
} else if (ch >= 0x80U) {
const std::size_t len = validate_utf8_at(text, pos);
out.append(text.substr(pos, len));
pos += len;
} else {
out.push_back(static_cast<char>(ch));
++pos;
}
break;
}
}
out.push_back('"');
}
inline void serialize_value(std::string& out, const Value& val,
const std::size_t depth,
const std::size_t maximumDepth)
{
if (depth > maximumDepth) {
throw ParseFailure{
.error =
{
.offset = out.size(),
.message = "nesting limit exceeded during serialize",
},
};
}
std::visit(
[&](const auto& node) {
using T = std::decay_t<decltype(node)>;
if constexpr (std::same_as<T, std::nullptr_t>) {
out += "null";
} else if constexpr (std::same_as<T, bool>) {
out += (node ? "true" : "false");
} else if constexpr (std::same_as<T, std::int64_t>) {
std::array<char, 32> buf{};
const auto res = std::to_chars(
buf.data(), buf.data() + buf.size(), node);
out.append(buf.data(), res.ptr);
} else if constexpr (std::same_as<T, double>) {
if (!std::isfinite(node)) {
throw ParseFailure{
.error =
{
.offset = out.size(),
.message =
"non-finite double is not valid JSON",
},
};
}
std::array<char, 64> buf{};
const auto res = std::to_chars(
buf.data(), buf.data() + buf.size(), node,
std::chars_format::general);
out.append(buf.data(), res.ptr);
} else if constexpr (std::same_as<T, std::string>) {
append_escaped_string(out, node);
} else if constexpr (std::same_as<T, Value::Array>) {
out.push_back('[');
for (std::size_t i = 0; i < node.size(); ++i) {
if (i > 0U) {
out.push_back(',');
}
serialize_value(out, node[i], depth + 1U,
maximumDepth);
}
out.push_back(']');
} else if constexpr (std::same_as<T, Value::Object>) {
out.push_back('{');
bool first = true;
for (const auto& [k, v] : node) {
if (!first) {
out.push_back(',');
}
first = false;
append_escaped_string(out, k);
out.push_back(':');
serialize_value(out, v, depth + 1U, maximumDepth);
}
out.push_back('}');
}
},
val.value);
}
} // namespace Detail
[[nodiscard]] inline std::expected<std::string, ParseError>
serialize(const Value& value,
const std::size_t maximumDepth = default_maximum_depth)
{
try {
std::string out;
Detail::serialize_value(out, value, 0U, maximumDepth);
return out;
} catch (const Detail::ParseFailure& failure) {
return std::unexpected{failure.error};
}
}
} // namespace Void::Json::Strict
File diff suppressed because it is too large. Load diff
+1
View File
@@ -75,6 +75,7 @@ namespace Void::Result
inline constexpr std::uint32_t kPluginErrorDomain = 23;
inline constexpr std::uint32_t kConfigurationErrorDomain = 24;
inline constexpr std::uint32_t kFsmErrorDomain = 25;
inline constexpr std::uint32_t kNetErrorDomain = 26;
struct ErrorCode {
std::uint32_t domain{kGenericErrorDomain};
+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)
+94 -23
View File
@@ -438,6 +438,54 @@ namespace Void::Semver
using Clause = std::vector<Comparator>;
std::vector<Clause> m_alternatives;
struct PartialVersion {
Version version{};
int components{0};
};
[[nodiscard]] static std::optional<PartialVersion>
parse_partial(std::string_view token)
{
if (token.empty()) {
return std::nullopt;
}
if (token.find('-') != std::string_view::npos ||
token.find('+') != std::string_view::npos) {
const auto full = parse_strict(token);
if (!full) {
return std::nullopt;
}
return PartialVersion{*full, 3};
}
const auto first_dot = token.find('.');
if (first_dot == std::string_view::npos) {
unsigned major = 0;
if (!Detail::parse_component(token, major)) {
return std::nullopt;
}
return PartialVersion{
Version{.major = major, .minor = 0, .patch = 0}, 1};
}
const auto second_dot = token.find('.', first_dot + 1);
if (second_dot == std::string_view::npos) {
unsigned major = 0;
unsigned minor = 0;
if (!Detail::parse_component(token.substr(0, first_dot),
major) ||
!Detail::parse_component(token.substr(first_dot + 1),
minor)) {
return std::nullopt;
}
return PartialVersion{
Version{.major = major, .minor = minor, .patch = 0}, 2};
}
const auto full = parse_strict(token);
if (!full) {
return std::nullopt;
}
return PartialVersion{*full, 3};
}
[[nodiscard]] static bool append_token(Clause& clause,
std::string_view token)
{
@@ -447,53 +495,76 @@ namespace Void::Semver
if (token.starts_with('^') || token.starts_with('~')) {
const char kind = token.front();
token.remove_prefix(1);
const auto lower = parse_strict(token);
if (!lower) {
const auto partial = parse_partial(token);
if (!partial) {
return false;
}
const Version& lower = partial->version;
Version upper;
if (kind == '~') {
if (lower->minor ==
if (partial->components == 1) {
if (lower.major ==
std::numeric_limits<unsigned>::max()) {
return false;
}
upper = Version{.major = lower.major + 1,
.minor = 0,
.patch = 0,
.pre_release = {},
.build = {}};
} else {
if (lower.minor ==
std::numeric_limits<unsigned>::max()) {
return false;
}
upper = Version{.major = lower.major,
.minor = lower.minor + 1,
.patch = 0,
.pre_release = {},
.build = {}};
}
} else if (partial->components == 1) {
if (lower.major ==
std::numeric_limits<unsigned>::max()) {
return false;
}
upper = Version{.major = lower->major,
.minor = lower->minor + 1,
.patch = 0,
.pre_release = {},
.build = {}};
} else if (lower->major > 0) {
if (lower->major ==
std::numeric_limits<unsigned>::max()) {
return false;
}
upper = Version{.major = lower->major + 1,
upper = Version{.major = lower.major + 1,
.minor = 0,
.patch = 0,
.pre_release = {},
.build = {}};
} else if (lower->minor > 0) {
if (lower->minor ==
} else if (lower.major > 0) {
if (lower.major ==
std::numeric_limits<unsigned>::max()) {
return false;
}
upper = Version{.major = lower.major + 1,
.minor = 0,
.patch = 0,
.pre_release = {},
.build = {}};
} else if (lower.minor > 0) {
if (lower.minor ==
std::numeric_limits<unsigned>::max()) {
return false;
}
upper = Version{.major = 0,
.minor = lower->minor + 1,
.minor = lower.minor + 1,
.patch = 0,
.pre_release = {},
.build = {}};
} else {
if (lower->patch ==
if (lower.patch ==
std::numeric_limits<unsigned>::max()) {
return false;
}
upper = Version{.major = 0,
.minor = 0,
.patch = lower->patch + 1,
.patch = lower.patch + 1,
.pre_release = {},
.build = {}};
}
clause.push_back({Operation::GreaterEqual, *lower});
clause.push_back({Operation::GreaterEqual, lower});
clause.push_back({Operation::Less, std::move(upper)});
return true;
}
@@ -518,11 +589,11 @@ namespace Void::Semver
} else if (token.starts_with('=')) {
token.remove_prefix(1);
}
const auto version = parse_strict(token);
if (!version) {
const auto partial = parse_partial(token);
if (!partial) {
return false;
}
clause.push_back({operation, *version});
clause.push_back({operation, partial->version});
return true;
}