Files
Library/tests/standalone/Configuration/Config.cpp
T
faumarayandClaude Sonnet 4.6 a09e000f0f Add Hash, Random, Uuid, Encoding, Bits, Math modules + make Testing deps optional
New header-only modules:
- Void::Hash — FNV-1a 32/64, CRC-32, DJB2, MurmurHash3-32, UDL literals
- Void::Random — Xoshiro256** PRNG, uniform_int/real, coin_flip, shuffle, random_string
- Void::Uuid — v4 (random), v5_lite (name-based), parse, RFC 4122 namespaces
- Void::Encoding — Base64 (standard + URL-safe), Hex encode/decode
- Void::Bits — popcount, bit extract/deposit, rotation, align, byteswap, masks
- Void::Math — Vec2/Vec3/Vec4, Mat4 (identity, translation, scale, rotation)

Testing module: move Stacktrace from INTERNAL_DEPS to OPTIONAL_DEPS. Added
Detail/Color.hpp shim that delegates to Stacktrace::Color when present or
provides inline ANSI fallback otherwise. All Stacktrace calls in SignalHandler,
ConsoleReporter, and Runner are gated on VOID_HAS_STACKTRACE.

Fix Repr.hpp reflect_struct to handle template specializations that lack
std::meta::identifier_of (now guarded by has_identifier check).

33/33 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 21:52:22 +03:00

122 lines
3.8 KiB
C++

#include <Void/Configuration/Config.hpp>
#include <Void/Configuration/Env.hpp>
#include <Void/FileSystem/TempDir.hpp>
#include <Void/FileSystem/Path.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cstdlib>
#include <string>
Void::Testing::Test get_env_finds_set_variable()
{
::setenv("VOID_TEST_VAR_A", "hello", 1);
auto r = Void::Configuration::get_env("VOID_TEST_VAR_A");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"hello"});
::unsetenv("VOID_TEST_VAR_A");
}
Void::Testing::Test get_env_returns_error_for_missing()
{
::unsetenv("VOID_TEST_VAR_MISSING_XYZ");
auto r = Void::Configuration::get_env("VOID_TEST_VAR_MISSING_XYZ");
Void::Testing::expect(!r.has_value());
}
Void::Testing::Test get_env_or_returns_fallback_when_absent()
{
::unsetenv("VOID_TEST_VAR_MISSING_XYZ");
auto val = Void::Configuration::get_env_or("VOID_TEST_VAR_MISSING_XYZ", "default");
Void::Testing::expect_eq(val, std::string{"default"});
}
Void::Testing::Test get_env_or_returns_value_when_present()
{
::setenv("VOID_TEST_VAR_B", "world", 1);
auto val = Void::Configuration::get_env_or("VOID_TEST_VAR_B", "fallback");
Void::Testing::expect_eq(val, std::string{"world"});
::unsetenv("VOID_TEST_VAR_B");
}
Void::Testing::Test config_set_and_get()
{
Void::Configuration::Config cfg;
cfg.set("key", "value");
auto r = cfg.get("key");
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(std::string{*r}, std::string{"value"});
}
Void::Testing::Test config_get_missing_key_fails()
{
Void::Configuration::Config cfg;
auto r = cfg.get("nonexistent");
Void::Testing::expect(!r.has_value());
}
Void::Testing::Test config_get_or_returns_fallback()
{
Void::Configuration::Config cfg;
auto val = cfg.get_or("missing", "fb");
Void::Testing::expect_eq(std::string{val}, std::string{"fb"});
}
Void::Testing::Test config_from_env_reads_listed_keys()
{
::setenv("VOID_TEST_CFG_X", "42", 1);
::unsetenv("VOID_TEST_CFG_Y");
auto cfg = Void::Configuration::Config::from_env({"VOID_TEST_CFG_X", "VOID_TEST_CFG_Y"});
Void::Testing::expect_eq(cfg.size(), std::size_t{1});
Void::Testing::expect_eq(std::string{cfg.get_or("VOID_TEST_CFG_X")}, std::string{"42"});
::unsetenv("VOID_TEST_CFG_X");
}
Void::Testing::Test config_from_file_parses_key_value()
{
Void::FileSystem::TempDir tmp;
auto path = tmp / "cfg.env";
(void)Void::FileSystem::write_file(path, "HOST=localhost\nPORT=5432\n");
auto r = Void::Configuration::Config::from_file(path);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(std::string{r->get_or("HOST")}, std::string{"localhost"});
Void::Testing::expect_eq(std::string{r->get_or("PORT")}, std::string{"5432"});
}
Void::Testing::Test config_from_file_skips_comments_and_blank_lines()
{
Void::FileSystem::TempDir tmp;
auto path = tmp / "cfg.env";
(void)Void::FileSystem::write_file(path, "# this is a comment\n\nFOO=bar\n");
auto r = Void::Configuration::Config::from_file(path);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(r->size(), std::size_t{1});
Void::Testing::expect_eq(std::string{r->get_or("FOO")}, std::string{"bar"});
}
Void::Testing::Test config_overlay_later_wins_on_conflict()
{
Void::Configuration::Config base;
base.set("A", "from_base");
base.set("B", "only_base");
Void::Configuration::Config top;
top.set("A", "from_top");
top.set("C", "only_top");
base.overlay(top);
Void::Testing::expect_eq(std::string{base.get_or("A")}, std::string{"from_top"});
Void::Testing::expect_eq(std::string{base.get_or("B")}, std::string{"only_base"});
Void::Testing::expect_eq(std::string{base.get_or("C")}, std::string{"only_top"});
}
Void::Testing::Test config_from_file_missing_path_fails()
{
auto r = Void::Configuration::Config::from_file("/no/such/file.env");
Void::Testing::expect(!r.has_value());
}