#include #include #include #include #include #include #include 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::min()); Void::Testing::expect_eq(*root.find("max_i64")->as_integer(), std::numeric_limits::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}); } namespace toml_limits_test_detail { [[nodiscard]] std::string repeated_key_path(std::size_t segments) { std::string out; for (std::size_t i = 0; i < segments; ++i) { if (i != 0) { out.push_back('.'); } out.push_back('a'); } return out; } [[nodiscard]] std::string nested_arrays(std::size_t depth) { return std::string(depth, '[') + "1" + std::string(depth, ']'); } [[nodiscard]] std::string nested_inline_tables(std::size_t depth) { std::string out; for (std::size_t i = 0; i < depth; ++i) { out += "{ k = "; } out.push_back('1'); for (std::size_t i = 0; i < depth; ++i) { out += " }"; } return out; } } // namespace toml_limits_test_detail Void::Testing::Test toml_depth_limit_is_uniform_for_key_paths() { using namespace Void::Data::Toml; using toml_limits_test_detail::repeated_key_path; const std::string at_limit = repeated_key_path(64); const std::string over_limit = repeated_key_path(65); // Table headers count every segment as one nesting level. const auto header_ok = parse("[" + at_limit + "]\nx = 1\n"); Void::Testing::require(header_ok.has_value()); const auto header_over = parse("[" + over_limit + "]\n"); Void::Testing::require(!header_over.has_value()); Void::Testing::expect_eq(header_over.error().origin.line, std::size_t{1}); Void::Testing::expect_eq(header_over.error().message, std::string{"key path exceeds max_depth"}); // Array-of-tables headers obey the same limit. const auto aot_over = parse("[[" + over_limit + "]]\n"); Void::Testing::require(!aot_over.has_value()); // A dotted key of N segments nests its value N-1 levels, so it may use // one more segment than a table header at the same limit. const auto dotted_ok = parse(repeated_key_path(65) + " = 1\n"); Void::Testing::require(dotted_ok.has_value()); const auto dotted_over = parse(repeated_key_path(66) + " = 1\n"); Void::Testing::require(!dotted_over.has_value()); Void::Testing::expect_eq(dotted_over.error().message, std::string{"key path exceeds max_depth"}); // Explicit small limits behave the same way. Void::Testing::require(!parse("a.b.c = 1\n", ParseOptions{.max_depth = 1}) .has_value()); Void::Testing::require(parse("a.b = 1\n", ParseOptions{.max_depth = 1}) .has_value()); Void::Testing::require(parse("[a.b]\n", ParseOptions{.max_depth = 2}) .has_value()); Void::Testing::require(!parse("[a.b.c]\n", ParseOptions{.max_depth = 2}) .has_value()); } Void::Testing::Test toml_depth_limit_is_uniform_for_values() { using namespace Void::Data::Toml; using toml_limits_test_detail::nested_arrays; using toml_limits_test_detail::nested_inline_tables; Void::Testing::require( parse("a = " + nested_arrays(64) + "\n").has_value()); Void::Testing::require( !parse("a = " + nested_arrays(65) + "\n").has_value()); Void::Testing::require( parse("a = " + nested_inline_tables(64) + "\n").has_value()); Void::Testing::require( !parse("a = " + nested_inline_tables(65) + "\n").has_value()); } Void::Testing::Test toml_depth_limit_combines_tables_and_values() { using namespace Void::Data::Toml; using toml_limits_test_detail::nested_arrays; // A value inside [t] starts one level deep, so only 63 nested arrays fit // under the default limit of 64. Void::Testing::require( parse("[t]\na = " + nested_arrays(63) + "\n").has_value()); Void::Testing::require( !parse("[t]\na = " + nested_arrays(64) + "\n").has_value()); // Dotted key paths contribute their intermediate tables too. Void::Testing::require(parse("a.b = " + nested_arrays(63) + "\n") .has_value()); Void::Testing::require(!parse("a.b = " + nested_arrays(64) + "\n") .has_value()); } Void::Testing::Test toml_input_byte_limit_boundaries() { using namespace Void::Data::Toml; const std::string doc = "a = 1\n"; Void::Testing::require( parse(doc, ParseOptions{.max_input_bytes = doc.size()}).has_value()); const auto under = parse(doc, ParseOptions{.max_input_bytes = doc.size() - 1}); Void::Testing::require(!under.has_value()); Void::Testing::expect_eq(under.error().origin.line, std::size_t{1}); Void::Testing::expect_eq(under.error().message, std::string{"TOML input exceeds max_input_bytes"}); // A huge but valid scalar fits under the default 16 MiB limit. const std::string scalar_body(1024U * 1024U, 'x'); const std::string huge = "s = \"" + scalar_body + "\"\n"; const auto huge_ok = parse(huge); Void::Testing::require(huge_ok.has_value()); Void::Testing::require(huge_ok->root.find("s") != nullptr); Void::Testing::expect_eq(huge_ok->root.find("s")->as_string()->size(), scalar_body.size()); Void::Testing::require( !parse(huge, ParseOptions{.max_input_bytes = huge.size() - 1}) .has_value()); // A huge numeric scalar is rejected as out of range, not accepted. const std::string huge_int = "i = " + std::string(64, '9') + "\n"; const auto int_over = parse(huge_int); Void::Testing::require(!int_over.has_value()); Void::Testing::expect_eq(int_over.error().message, std::string{"integer literal out of int64 range"}); } Void::Testing::Test toml_node_limit_is_opt_in() { using namespace Void::Data::Toml; // a = [1, 2, 3] creates one table entry plus three array elements. const std::string doc = "a = [1, 2, 3]\n"; Void::Testing::require( parse(doc, ParseOptions{.max_nodes = 4}).has_value()); const auto over = parse(doc, ParseOptions{.max_nodes = 3}); Void::Testing::require(!over.has_value()); Void::Testing::expect( over.error().message.find("max_nodes") != std::string::npos); // Default (zero) keeps the limit disabled. Void::Testing::require(parse(doc).has_value()); // Dotted keys and array-of-tables entries count their intermediate // containers as well. Void::Testing::require( parse("a.b.c = 1\n", ParseOptions{.max_nodes = 3}).has_value()); Void::Testing::require( !parse("a.b.c = 1\n", ParseOptions{.max_nodes = 2}).has_value()); Void::Testing::require( parse("[[p]]\n", ParseOptions{.max_nodes = 2}).has_value()); Void::Testing::require( !parse("[[p]]\n", ParseOptions{.max_nodes = 1}).has_value()); } Void::Testing::Test toml_rejects_invalid_utf8_with_stable_diagnostics() { using namespace Void::Data::Toml; const std::string invalid = "a = 1\n\xFF = 2\n"; const auto first = parse(invalid); const auto second = parse(invalid); Void::Testing::require(!first.has_value()); Void::Testing::require(!second.has_value()); Void::Testing::expect_eq(first.error().origin.line, std::size_t{2}); Void::Testing::expect_eq(first.error().origin.column, std::size_t{1}); Void::Testing::expect_eq( first.error().message, std::string{"invalid UTF-8 byte sequence in TOML document"}); Void::Testing::expect_eq(second.error().origin.line, first.error().origin.line); Void::Testing::expect_eq(second.error().origin.column, first.error().origin.column); Void::Testing::expect_eq(second.error().message, first.error().message); // Overlong encoding, UTF-16 surrogate and truncated sequence. Void::Testing::require(!parse(std::string{"s = \"\xC0\x80\"\n"}) .has_value()); Void::Testing::require(!parse(std::string{"s = \"\xED\xA0\x80\"\n"}) .has_value()); Void::Testing::require(!parse(std::string{"s = \"\xE2\x82\"\n"}) .has_value()); // Valid multi-byte UTF-8 remains accepted. const auto valid = parse("s = \"h\xC3\xA9llo \xF0\x9F\x98\x80\"\n"); Void::Testing::require(valid.has_value()); Void::Testing::expect_eq(valid->root.find("s")->as_string()->size(), std::size_t{11}); } Void::Testing::Test toml_duplicate_diagnostics_are_stable() { using namespace Void::Data::Toml; const std::string dup = "a = 1\nb = 2\na = 3\n"; const auto first = parse(dup); const auto second = parse(dup); Void::Testing::require(!first.has_value()); Void::Testing::require(!second.has_value()); Void::Testing::expect_eq(first.error().origin.line, std::size_t{3}); Void::Testing::expect_eq(first.error().origin.column, std::size_t{1}); Void::Testing::expect_eq(first.error().message, std::string{"duplicate key 'a'"}); Void::Testing::expect_eq(second.error().message, first.error().message); Void::Testing::expect_eq(second.error().origin.line, first.error().origin.line); const auto dup_table = parse("[pkg]\nname = \"x\"\n[pkg]\n"); Void::Testing::require(!dup_table.has_value()); Void::Testing::expect_eq(dup_table.error().origin.line, std::size_t{3}); const auto dup_dotted = parse("a.b = 1\na.b = 2\n"); Void::Testing::require(!dup_dotted.has_value()); Void::Testing::expect_eq(dup_dotted.error().origin.line, std::size_t{2}); } Void::Testing::Test toml_valid_documents_regression() { using namespace Void::Data::Toml; constexpr std::string_view kDoc = R"toml( title = "limits" numbers = [1, 2.5, -3, 0x10] matrix = [[1, 2], [3, 4]] inline = { a = 1, b.c = "d", nested = { e = true } } "quoted.key" = "value" unicode = "héllo 😀" [server] host = "localhost" ports = [8080, 8443] [server.tls] enabled = true cipher = "TLS_AES_256_GCM_SHA384" [[items]] name = "one" [[items]] name = "two" tags = ["a", "b"] [items.meta] count = 2 )toml"; const auto parsed = parse(kDoc); Void::Testing::require(parsed.has_value()); const auto& root = parsed->root; Void::Testing::require(root.find("matrix") != nullptr); Void::Testing::expect_eq(root.find("matrix")->as_array()->size(), std::size_t{2}); Void::Testing::require(root.find("inline") != nullptr); const auto* inline_tbl = root.find("inline")->as_table(); Void::Testing::require(inline_tbl != nullptr); Void::Testing::expect_eq( *inline_tbl->find("b")->as_table()->find("c")->as_string(), std::string{"d"}); const auto* items = root.find("items")->as_array_of_tables(); Void::Testing::require(items != nullptr); Void::Testing::expect_eq(items->tables.size(), std::size_t{2}); Void::Testing::expect_eq( *items->tables[1].find("meta")->as_table()->find("count")->as_integer(), std::int64_t{2}); // Canonical serialization round-trips without changing the document. const std::string once = serialize(*parsed); const auto reparsed = parse(once); Void::Testing::require(reparsed.has_value()); Void::Testing::expect_eq(serialize(*reparsed), once); }