Files
Library/tests/standalone/Data/Toml.cpp
T

235 lines
7.8 KiB
C++

#include <Void/Data/Toml.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cmath>
#include <cstdint>
#include <limits>
#include <string>
Void::Testing::Test toml_parses_scalars_multilines_and_numbers()
{
constexpr std::string_view kDoc = R"toml(
# Comment line
title = "Void Build"
literal_win = 'C:\Users\Void'
multi_basic = """
Hello \
world ""escaped"" \u0041 \U0001F600!"""
multi_quotes = """here are two ""quotes"" and two at end "" """
five_quotes = """say """""
multi_lit = '''
line1\nraw
line2 'ok' ''ok''
'''
flag_t = true
flag_f = false
dec_pos = +42
dec_neg = -1_000_000
hex_num = 0xDEAD_BEEF
oct_num = 0o755
bin_num = 0b1101_0110
min_i64 = -9223372036854775808
max_i64 = 9223372036854775807
flt = -1.25e+2
flt_zero = 0.0
inf_pos = +inf
inf_neg = -inf
not_a_num = nan
)toml";
const auto parsed = Void::Data::Toml::parse(kDoc);
Void::Testing::require(parsed.has_value());
const auto& root = parsed->root;
Void::Testing::require(root.find("title") != nullptr);
Void::Testing::expect_eq(
*root.find("title")->as_string(), std::string{"Void Build"});
Void::Testing::expect(root.find("title")->origin.line >= 3U);
Void::Testing::expect_eq(
*root.find("literal_win")->as_string(), std::string{R"(C:\Users\Void)"});
Void::Testing::expect_eq(
*root.find("multi_basic")->as_string(),
std::string{"Hello world \"\"escaped\"\" A \xF0\x9F\x98\x80!"});
Void::Testing::expect_eq(
*root.find("five_quotes")->as_string(), std::string{"say \"\""});
Void::Testing::expect_eq(
*root.find("multi_lit")->as_string(),
std::string{"line1\\nraw\nline2 'ok' ''ok''\n"});
Void::Testing::expect_eq(*root.find("flag_t")->as_bool(), true);
Void::Testing::expect_eq(*root.find("flag_f")->as_bool(), false);
Void::Testing::expect_eq(*root.find("dec_pos")->as_integer(),
std::int64_t{42});
Void::Testing::expect_eq(*root.find("dec_neg")->as_integer(),
std::int64_t{-1000000});
Void::Testing::expect_eq(*root.find("hex_num")->as_integer(),
std::int64_t{0xDEADBEEFLL});
Void::Testing::expect_eq(*root.find("oct_num")->as_integer(),
std::int64_t{0755});
Void::Testing::expect_eq(*root.find("bin_num")->as_integer(),
std::int64_t{0b11010110});
Void::Testing::expect_eq(*root.find("min_i64")->as_integer(),
std::numeric_limits<std::int64_t>::min());
Void::Testing::expect_eq(*root.find("max_i64")->as_integer(),
std::numeric_limits<std::int64_t>::max());
Void::Testing::expect_eq(*root.find("flt")->as_float(), -125.0);
Void::Testing::expect_eq(*root.find("flt_zero")->as_float(), 0.0);
Void::Testing::expect(std::isinf(*root.find("inf_pos")->as_float()) &&
*root.find("inf_pos")->as_float() > 0.0);
Void::Testing::expect(std::isinf(*root.find("inf_neg")->as_float()) &&
*root.find("inf_neg")->as_float() < 0.0);
Void::Testing::expect(std::isnan(*root.find("not_a_num")->as_float()));
}
Void::Testing::Test toml_parses_datetimes_tables_inline_and_arrays_of_tables()
{
constexpr std::string_view kDoc = R"toml(
odt_utc = 1979-05-27T07:32:00Z
odt_space = 1979-05-27 07:32:00.123456-07:00
ldt = 2000-02-29T12:00:00
ld = 2026-11-26
lt = 08:15:30.5
name.first = "Ada"
name."last.part" = "Lovelace"
inline_pt = { x = 1, y = 2, nested.z = "ok" }
arr = [
1,
"two", # comment inside array
{ k = true },
]
[server]
host = "127.0.0.1"
ports = [8080, 8443]
[server.tls]
enabled = true
[[products]]
name = "Hammer"
sku = 738594937
[[products]] # empty entry in middle is allowed
[[products]]
name = "Nail"
sku = 284758393
color = ["gray", "steel"]
[products.meta]
origin = "local"
)toml";
const auto parsed = Void::Data::Toml::parse(kDoc);
Void::Testing::require(parsed.has_value());
const auto& root = parsed->root;
const auto* odt = root.find("odt_utc")->as_datetime();
Void::Testing::require(odt != nullptr);
Void::Testing::expect_eq(odt->to_string(),
std::string{"1979-05-27T07:32:00Z"});
const auto* odt2 = root.find("odt_space")->as_datetime();
Void::Testing::require(odt2 != nullptr);
Void::Testing::expect_eq(
odt2->to_string(), std::string{"1979-05-27T07:32:00.123456-07:00"});
const auto* ldt = root.find("ldt")->as_datetime();
Void::Testing::require(ldt != nullptr);
Void::Testing::expect_eq(ldt->date.year, 2000);
Void::Testing::expect_eq(ldt->date.month, 2);
Void::Testing::expect_eq(ldt->date.day, 29);
const auto* name_tbl = root.find("name")->as_table();
Void::Testing::require(name_tbl != nullptr);
Void::Testing::expect_eq(*name_tbl->find("first")->as_string(),
std::string{"Ada"});
Void::Testing::expect_eq(*name_tbl->find("last.part")->as_string(),
std::string{"Lovelace"});
const auto* inline_pt = root.find("inline_pt")->as_table();
Void::Testing::require(inline_pt != nullptr);
Void::Testing::expect(inline_pt->is_inline);
Void::Testing::expect_eq(*inline_pt->find("x")->as_integer(),
std::int64_t{1});
Void::Testing::expect_eq(
*inline_pt->find("nested")->as_table()->find("z")->as_string(),
std::string{"ok"});
const auto* aot = root.find("products")->as_array_of_tables();
Void::Testing::require(aot != nullptr);
Void::Testing::expect_eq(aot->tables.size(), std::size_t{3});
Void::Testing::expect_eq(*aot->tables[0].find("name")->as_string(),
std::string{"Hammer"});
Void::Testing::expect(aot->tables[1].empty());
Void::Testing::expect_eq(*aot->tables[2].find("name")->as_string(),
std::string{"Nail"});
Void::Testing::expect_eq(
*aot->tables[2].find("meta")->as_table()->find("origin")->as_string(),
std::string{"local"});
// Deterministic canonical serialize & round-trip idempotence
const std::string out1 = Void::Data::Toml::serialize(*parsed);
const auto reparsed = Void::Data::Toml::parse(out1);
Void::Testing::require(reparsed.has_value());
const std::string out2 = Void::Data::Toml::serialize(*reparsed);
Void::Testing::expect_eq(out1, out2);
}
Void::Testing::Test toml_reports_exact_origin_and_rejects_errors()
{
// Line 1: ok, Line 2: ok, Line 3: duplicate key starts at col 1
constexpr std::string_view kDupKey = "a = 1\nb = 2\na = 3\n";
const auto e1 = Void::Data::Toml::parse(kDupKey);
Void::Testing::require(!e1.has_value());
Void::Testing::expect_eq(e1.error().origin.line, std::size_t{3});
Void::Testing::expect_eq(e1.error().origin.column, std::size_t{1});
// Duplicate [table]
constexpr std::string_view kDupTable = "[pkg]\nname = \"x\"\n\n[pkg]\n";
const auto e2 = Void::Data::Toml::parse(kDupTable);
Void::Testing::require(!e2.has_value());
Void::Testing::expect_eq(e2.error().origin.line, std::size_t{4});
// Defining explicit [table] after dotted key
const auto e3 = Void::Data::Toml::parse("a.b = 1\n[a]\n");
Void::Testing::expect(!e3.has_value());
// Extending inline table
const auto e4 = Void::Data::Toml::parse("t = { a = 1 }\nt.b = 2\n");
Void::Testing::expect(!e4.has_value());
// Multiline inline table forbidden in TOML 1.0
const auto e5 = Void::Data::Toml::parse("t = {\n a = 1\n}\n");
Void::Testing::expect(!e5.has_value());
// Trailing comma in inline table forbidden in TOML 1.0
const auto e6 = Void::Data::Toml::parse("t = { a = 1, }\n");
Void::Testing::expect(!e6.has_value());
// Invalid calendar date (non-leap year 1900-02-29)
const auto e7 = Void::Data::Toml::parse("d = 1900-02-29\n");
Void::Testing::expect(!e7.has_value());
// Invalid 6-quote basic string close
const auto e8 = Void::Data::Toml::parse("s = \"\"\"hello\"\"\"\"\"\"\n");
Void::Testing::expect(!e8.has_value());
// Invalid UTF-8 byte
const auto e9 = Void::Data::Toml::parse(std::string{"s = \"\xFF\"\n"});
Void::Testing::expect(!e9.has_value());
// Subtable before parent table IS valid per TOML 1.0
const auto super_after =
Void::Data::Toml::parse("[a.b]\nx = 1\n[a]\ny = 2\n");
Void::Testing::require(super_after.has_value());
Void::Testing::expect_eq(
*super_after->root.find("a")->as_table()->find("y")->as_integer(),
std::int64_t{2});
}