Files
Library/tests/standalone/Ini/Ini.cpp
T
faumarayandClaude Sonnet 4.6 cb4dd58500 Add Ini, Csv, Semver modules
Void::Ini — IniFile parser supporting [sections], key=value pairs, ; and #
comments, quoted values, error reporting with line numbers, set/get, has_key,
sections(), serialize().

Void::Csv — RFC 4180 CSV parse/serialize with quoted fields, doubled-quote
escaping, custom delimiter, CRLF support; CsvWithHeader for named column access.

Void::Semver — Version struct with major/minor/patch/pre-release/build; parse()
(accepts optional v-prefix); to_string(); spaceship comparison per semver spec
(release > pre-release, build metadata ignored); compatible_with(), is_stable(),
bumped_major/minor/patch().

47/47 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 23:02:29 +03:00

100 lines
3.1 KiB
C++

#include <Void/Ini/Ini.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
static constexpr const char* SAMPLE = R"(
; comment
[database]
host = localhost
port = 5432
name = "my db"
[server]
# another comment
host = 0.0.0.0
port = 8080
)";
// ── parse ─────────────────────────────────────────────────────────────────────
Void::Testing::Test ini_parse_sections()
{
auto f = Void::Ini::IniFile::from_string(SAMPLE);
Void::Testing::expect(f.has_section("database"));
Void::Testing::expect(f.has_section("server"));
}
Void::Testing::Test ini_parse_values()
{
auto f = Void::Ini::IniFile::from_string(SAMPLE);
const auto host = f.get("database", "host");
Void::Testing::expect(host.has_value());
Void::Testing::expect_eq(*host, std::string{"localhost"});
}
Void::Testing::Test ini_parse_quoted_value()
{
auto f = Void::Ini::IniFile::from_string(SAMPLE);
const auto name = f.get("database", "name");
Void::Testing::expect(name.has_value());
Void::Testing::expect_eq(*name, std::string{"my db"});
}
Void::Testing::Test ini_missing_key_returns_nullopt()
{
auto f = Void::Ini::IniFile::from_string(SAMPLE);
Void::Testing::expect(!f.get("database", "password").has_value());
}
Void::Testing::Test ini_missing_section_returns_nullopt()
{
auto f = Void::Ini::IniFile::from_string(SAMPLE);
Void::Testing::expect(!f.get("auth", "token").has_value());
}
Void::Testing::Test ini_get_or_fallback()
{
auto f = Void::Ini::IniFile::from_string(SAMPLE);
Void::Testing::expect_eq(f.get_or("database", "timeout", "30"), std::string{"30"});
}
Void::Testing::Test ini_has_key()
{
auto f = Void::Ini::IniFile::from_string(SAMPLE);
Void::Testing::expect(f.has_key("server", "port"));
Void::Testing::expect(!f.has_key("server", "ssl"));
}
// ── set / serialize ───────────────────────────────────────────────────────────
Void::Testing::Test ini_set_and_get()
{
Void::Ini::IniFile f;
f.set("app", "version", "1.0");
const auto v = f.get("app", "version");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(*v, std::string{"1.0"});
}
Void::Testing::Test ini_serialize_round_trip()
{
Void::Ini::IniFile f;
f.set("section", "key", "value");
const auto serialized = f.serialize();
auto f2 = Void::Ini::IniFile::from_string(serialized);
const auto v = f2.get("section", "key");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(*v, std::string{"value"});
}
// ── error handling ────────────────────────────────────────────────────────────
Void::Testing::Test ini_unclosed_section_is_error()
{
Void::Ini::IniFile f;
const auto err = Void::Ini::IniFile::parse(f, "[unclosed");
Void::Testing::expect(err.has_value());
}