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>
This commit is contained in:
faumarayandClaude Sonnet 4.6 committed 2026-05-14 23:02:29 +03:00
1 parent b54f2156c5
commit cb4dd58500
6 files changed
+801

No files matched your search

+108
View File
@@ -0,0 +1,108 @@
#include <Void/Csv/Csv.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <vector>
// ── parse ─────────────────────────────────────────────────────────────────────
Void::Testing::Test csv_parse_simple()
{
const auto t = Void::Csv::parse("a,b,c\n1,2,3\n");
Void::Testing::expect_eq(t.size(), std::size_t{2});
Void::Testing::expect_eq(t[0][0], std::string{"a"});
Void::Testing::expect_eq(t[1][2], std::string{"3"});
}
Void::Testing::Test csv_parse_quoted_field()
{
const auto t = Void::Csv::parse("\"hello, world\",42\n");
Void::Testing::expect_eq(t[0][0], std::string{"hello, world"});
Void::Testing::expect_eq(t[0][1], std::string{"42"});
}
Void::Testing::Test csv_parse_doubled_quote()
{
const auto t = Void::Csv::parse("\"say \"\"hi\"\"\"\n");
Void::Testing::expect_eq(t[0][0], std::string{"say \"hi\""});
}
Void::Testing::Test csv_parse_empty_fields()
{
const auto t = Void::Csv::parse(",b,\n");
Void::Testing::expect_eq(t[0].size(), std::size_t{3});
Void::Testing::expect(t[0][0].empty());
Void::Testing::expect(t[0][2].empty());
}
Void::Testing::Test csv_parse_custom_delimiter()
{
const auto t = Void::Csv::parse("a;b;c\n", ';');
Void::Testing::expect_eq(t[0].size(), std::size_t{3});
Void::Testing::expect_eq(t[0][1], std::string{"b"});
}
Void::Testing::Test csv_parse_crlf()
{
const auto t = Void::Csv::parse("a,b\r\n1,2\r\n");
Void::Testing::expect_eq(t.size(), std::size_t{2});
}
Void::Testing::Test csv_parse_empty_input()
{
const auto t = Void::Csv::parse("");
Void::Testing::expect(t.empty());
}
// ── serialize ─────────────────────────────────────────────────────────────────
Void::Testing::Test csv_serialize_simple()
{
const Void::Csv::Table t = {{"a","b","c"},{"1","2","3"}};
const auto s = Void::Csv::serialize(t);
Void::Testing::expect(s.find("a,b,c") != std::string::npos);
Void::Testing::expect(s.find("1,2,3") != std::string::npos);
}
Void::Testing::Test csv_serialize_quotes_commas()
{
const Void::Csv::Table t = {{"hello, world"}};
const auto s = Void::Csv::serialize(t);
Void::Testing::expect(s.find('"') != std::string::npos);
}
Void::Testing::Test csv_round_trip()
{
const Void::Csv::Table original = {
{"Name","Score"},{"Alice","100"},{"Bob","90"}};
const auto serialized = Void::Csv::serialize(original);
const auto parsed = Void::Csv::parse(serialized);
Void::Testing::expect_eq(parsed.size(), original.size());
Void::Testing::expect_eq(parsed[0][0], original[0][0]);
}
// ── parse_with_header ─────────────────────────────────────────────────────────
Void::Testing::Test csv_with_header_get_column()
{
const auto csv = Void::Csv::parse_with_header("Name,Score\nAlice,100\n");
Void::Testing::expect(csv.has_value());
const auto score = csv->get(csv->rows[0], "Score");
Void::Testing::expect(score.has_value());
Void::Testing::expect_eq(*score, std::string_view{"100"});
}
Void::Testing::Test csv_with_header_unknown_col()
{
const auto csv = Void::Csv::parse_with_header("Name\nAlice\n");
Void::Testing::expect(csv.has_value());
Void::Testing::expect(!csv->get(csv->rows[0], "Missing").has_value());
}
Void::Testing::Test csv_with_header_col_index()
{
const auto csv = Void::Csv::parse_with_header("A,B,C\n1,2,3\n");
Void::Testing::expect(csv.has_value());
Void::Testing::expect_eq(*csv->col_index("B"), std::size_t{1});
}
+99
View File
@@ -0,0 +1,99 @@
#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());
}
+161
View File
@@ -0,0 +1,161 @@
#include <Void/Semver/Semver.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
// ── parse ─────────────────────────────────────────────────────────────────────
Void::Testing::Test semver_parse_simple()
{
const auto v = Void::Semver::parse("1.2.3");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->major, 1u);
Void::Testing::expect_eq(v->minor, 2u);
Void::Testing::expect_eq(v->patch, 3u);
}
Void::Testing::Test semver_parse_with_v_prefix()
{
const auto v = Void::Semver::parse("v2.0.0");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->major, 2u);
}
Void::Testing::Test semver_parse_pre_release()
{
const auto v = Void::Semver::parse("1.0.0-alpha.1");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->pre_release, std::string{"alpha.1"});
}
Void::Testing::Test semver_parse_build_metadata()
{
const auto v = Void::Semver::parse("1.0.0+build.123");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->build, std::string{"build.123"});
}
Void::Testing::Test semver_parse_full()
{
const auto v = Void::Semver::parse("2.3.4-beta.2+20240101");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->major, 2u);
Void::Testing::expect_eq(v->pre_release, std::string{"beta.2"});
Void::Testing::expect_eq(v->build, std::string{"20240101"});
}
Void::Testing::Test semver_parse_invalid()
{
Void::Testing::expect(!Void::Semver::parse("not-a-version").has_value());
Void::Testing::expect(!Void::Semver::parse("1.2").has_value());
}
// ── to_string ─────────────────────────────────────────────────────────────────
Void::Testing::Test semver_to_string_simple()
{
Void::Semver::Version v{1, 2, 3};
Void::Testing::expect_eq(v.to_string(), std::string{"1.2.3"});
}
Void::Testing::Test semver_to_string_with_pre_release()
{
Void::Semver::Version v{1, 0, 0};
v.pre_release = "alpha";
Void::Testing::expect_eq(v.to_string(), std::string{"1.0.0-alpha"});
}
Void::Testing::Test semver_round_trip()
{
const auto v = Void::Semver::parse("3.1.4-rc.1+build1");
Void::Testing::expect(v.has_value());
Void::Testing::expect_eq(v->to_string(), std::string{"3.1.4-rc.1+build1"});
}
// ── comparison ───────────────────────────────────────────────────────────────
Void::Testing::Test semver_compare_major()
{
const auto a = *Void::Semver::parse("2.0.0");
const auto b = *Void::Semver::parse("1.9.9");
Void::Testing::expect(a > b);
}
Void::Testing::Test semver_compare_minor()
{
const auto a = *Void::Semver::parse("1.2.0");
const auto b = *Void::Semver::parse("1.1.9");
Void::Testing::expect(a > b);
}
Void::Testing::Test semver_compare_patch()
{
const auto a = *Void::Semver::parse("1.0.1");
const auto b = *Void::Semver::parse("1.0.0");
Void::Testing::expect(a > b);
}
Void::Testing::Test semver_release_gt_pre_release()
{
const auto a = *Void::Semver::parse("1.0.0");
const auto b = *Void::Semver::parse("1.0.0-alpha");
Void::Testing::expect(a > b);
}
Void::Testing::Test semver_equal()
{
const auto a = *Void::Semver::parse("1.2.3");
const auto b = *Void::Semver::parse("1.2.3");
Void::Testing::expect(a == b);
}
// ── bump ─────────────────────────────────────────────────────────────────────
Void::Testing::Test semver_bump_major()
{
const auto v = Void::Semver::Version{1, 2, 3};
const auto b = v.bumped_major();
Void::Testing::expect_eq(b.major, 2u);
Void::Testing::expect_eq(b.minor, 0u);
Void::Testing::expect_eq(b.patch, 0u);
}
Void::Testing::Test semver_bump_minor()
{
const auto v = Void::Semver::Version{1, 2, 3};
const auto b = v.bumped_minor();
Void::Testing::expect_eq(b.minor, 3u);
Void::Testing::expect_eq(b.patch, 0u);
}
Void::Testing::Test semver_bump_patch()
{
const auto v = Void::Semver::Version{1, 2, 3};
Void::Testing::expect_eq(v.bumped_patch().patch, 4u);
}
// ── compatible_with ───────────────────────────────────────────────────────────
Void::Testing::Test semver_compatible_same_major()
{
const auto a = *Void::Semver::parse("1.3.0");
const auto req = *Void::Semver::parse("1.2.0");
Void::Testing::expect(a.compatible_with(req));
}
Void::Testing::Test semver_incompatible_different_major()
{
const auto a = *Void::Semver::parse("2.0.0");
const auto req = *Void::Semver::parse("1.9.9");
Void::Testing::expect(!a.compatible_with(req));
}
Void::Testing::Test semver_is_stable()
{
Void::Testing::expect(Void::Semver::Version{1,0,0}.is_stable());
Void::Testing::expect(!Void::Semver::Version{0,9,0}.is_stable());
Void::Semver::Version pre{1,0,0};
pre.pre_release = "alpha";
Void::Testing::expect(!pre.is_stable());
}