- Allowlist Configuration, Crypto and System for the Windows target. - System: link winmm for the Win32 audio backend; include windows.h before mmsystem.h. - Tests: add a whole-file // @requires-posix gate (CMake path and the VBS build.cpp) and mark the POSIX-only System suites; skip helper-based cases on Windows; make setenv/unsetenv and getpid portable. - The POSIX process helper is no longer built on Windows. Cross-built with x86_64-w64-mingw32 GCC 16.2: all modules and VoidTests.exe link. Linux System+Testing suite still passes (477 tests, 474 passed). Refs #65
135 lines
4.7 KiB
C++
135 lines
4.7 KiB
C++
#include <Void/Configuration/Config.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <cstdlib>
|
|
#include <string>
|
|
|
|
using Void::Configuration::Config;
|
|
|
|
namespace
|
|
{
|
|
// setenv/unsetenv are POSIX-only; _putenv_s is the Windows equivalent.
|
|
void set_env(const char* name, const char* value)
|
|
{
|
|
#ifdef _WIN32
|
|
_putenv_s(name, value);
|
|
#else
|
|
::setenv(name, value, 1);
|
|
#endif
|
|
}
|
|
|
|
void unset_env(const char* name)
|
|
{
|
|
#ifdef _WIN32
|
|
_putenv_s(name, "");
|
|
#else
|
|
::unsetenv(name);
|
|
#endif
|
|
}
|
|
} // namespace
|
|
|
|
// ── from_env_prefix ───────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test env_prefix_strips_and_normalizes()
|
|
{
|
|
set_env("VOIDTEST_LOG__LEVEL", "debug");
|
|
set_env("VOIDTEST_PORT", "9000");
|
|
set_env("UNRELATED_KEY", "x");
|
|
|
|
const auto cfg = Config::from_env_prefix("VOIDTEST_");
|
|
Void::Testing::expect(cfg.get_or("log.level") ==
|
|
"debug");
|
|
Void::Testing::expect(cfg.get_or("port") ==
|
|
"9000");
|
|
Void::Testing::expect(!cfg.contains("unrelated_key"));
|
|
|
|
unset_env("VOIDTEST_LOG__LEVEL");
|
|
unset_env("VOIDTEST_PORT");
|
|
unset_env("UNRELATED_KEY");
|
|
}
|
|
|
|
// ── from_ini_text ─────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test ini_sections_become_prefixes()
|
|
{
|
|
const auto cfg = Config::from_ini_text(
|
|
"global_key = 1\n"
|
|
"[log]\n"
|
|
"level = warn\n"
|
|
"[net]\n"
|
|
"port = 4242\n");
|
|
Void::Testing::expect(cfg.has_value());
|
|
Void::Testing::expect(cfg->get_or("global_key") ==
|
|
"1");
|
|
Void::Testing::expect(cfg->get_or("log.level") ==
|
|
"warn");
|
|
Void::Testing::expect_eq(*cfg->get_as<int>("net.port"), 4242);
|
|
}
|
|
|
|
Void::Testing::Test ini_parse_error_surfaces()
|
|
{
|
|
const auto cfg = Config::from_ini_text("[unterminated\nkey=1\n");
|
|
Void::Testing::expect(!cfg.has_value());
|
|
}
|
|
|
|
// ── from_json_text ────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test json_objects_flatten_to_dotted_keys()
|
|
{
|
|
const auto cfg = Config::from_json_text(R"({
|
|
"log": {"level": "error", "async": true},
|
|
"servers": [{"host": "a"}, {"host": "b"}],
|
|
"ratio": 0.5,
|
|
"count": 3
|
|
})");
|
|
Void::Testing::expect(cfg.has_value());
|
|
Void::Testing::expect(cfg->get_or("log.level") ==
|
|
"error");
|
|
Void::Testing::expect(*cfg->get_as<bool>("log.async"));
|
|
Void::Testing::expect(cfg->get_or("servers.0.host") ==
|
|
"a");
|
|
Void::Testing::expect(cfg->get_or("servers.1.host") ==
|
|
"b");
|
|
Void::Testing::expect_eq(*cfg->get_as<double>("ratio"), 0.5);
|
|
Void::Testing::expect(cfg->get_or("count") ==
|
|
"3"); // integral form
|
|
}
|
|
|
|
Void::Testing::Test json_parse_error_surfaces()
|
|
{
|
|
Void::Testing::expect(!Config::from_json_text("{nope").has_value());
|
|
}
|
|
|
|
// ── from_args ─────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test args_equals_space_and_flag_forms()
|
|
{
|
|
const char* argv[] = {"prog", "--log.level=debug",
|
|
"--net.port", "8080",
|
|
"--verbose", "positional",
|
|
"--trailing-flag"};
|
|
const auto cfg = Config::from_args(7, argv);
|
|
Void::Testing::expect(cfg.get_or("log.level") ==
|
|
"debug");
|
|
Void::Testing::expect(cfg.get_or("net.port") ==
|
|
"8080");
|
|
Void::Testing::expect(cfg.get_or("verbose") ==
|
|
"positional");
|
|
Void::Testing::expect(cfg.get_or("trailing-flag") ==
|
|
"true");
|
|
}
|
|
|
|
// ── overlay precedence ────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test overlay_last_wins_across_sources()
|
|
{
|
|
auto base = Config::from_ini_text("[log]\nlevel = info\n");
|
|
Void::Testing::expect(base.has_value());
|
|
|
|
const char* argv[] = {"prog", "--log.level=trace"};
|
|
base->overlay(Config::from_args(2, argv));
|
|
Void::Testing::expect(base->get_or("log.level") ==
|
|
"trace");
|
|
}
|