#include #include #include #include #include using Void::Configuration::Config; // ── from_env_prefix ─────────────────────────────────────────────────────────── Void::Testing::Test env_prefix_strips_and_normalizes() { ::setenv("VOIDTEST_LOG__LEVEL", "debug", 1); ::setenv("VOIDTEST_PORT", "9000", 1); ::setenv("UNRELATED_KEY", "x", 1); 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")); ::unsetenv("VOIDTEST_LOG__LEVEL"); ::unsetenv("VOIDTEST_PORT"); ::unsetenv("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("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("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("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"); }