#include "tests.h" TEST_CASE("parsing - arrays") { parsing_should_succeed( FILE_LINE_ARGS, S(R"( integers = [ 1, 2, 3 ] integers2 = [ 1, 2, 3 ] integers3 = [ 1, 2, # this is ok ] colors = [ "red", "yellow", "green" ] nested_array_of_int = [ [ 1, 2 ], [3, 4, 5] ] nested_mixed_array = [ [ 1, 2 ], ["a", "b", "c"] ] string_array = [ "all", 'strings', """are the same""", '''type''' ] )"sv), [](table&& tbl) noexcept { REQUIRE(tbl[S("integers")].as()); CHECK(tbl[S("integers")].as()->is_homogeneous()); CHECK(tbl[S("integers")].as()->size() == 3); CHECK(tbl[S("integers")][0] == 1); CHECK(tbl[S("integers")][1] == 2); CHECK(tbl[S("integers")][2] == 3); REQUIRE(tbl[S("integers2")].as()); CHECK(tbl[S("integers2")].as()->is_homogeneous()); CHECK(tbl[S("integers2")].as()->size() == 3); CHECK(tbl[S("integers2")][0] == 1); CHECK(tbl[S("integers2")][1] == 2); CHECK(tbl[S("integers2")][2] == 3); REQUIRE(tbl[S("integers3")].as()); CHECK(tbl[S("integers3")].as()->is_homogeneous()); CHECK(tbl[S("integers3")].as()->size() == 2); CHECK(tbl[S("integers3")][0] == 1); CHECK(tbl[S("integers3")][1] == 2); REQUIRE(tbl[S("colors")].as()); CHECK(tbl[S("colors")].as()->is_homogeneous()); CHECK(tbl[S("colors")].as()->size() == 3); CHECK(tbl[S("colors")][0] == S("red"sv)); CHECK(tbl[S("colors")][1] == S("yellow"sv)); CHECK(tbl[S("colors")][2] == S("green"sv)); REQUIRE(tbl[S("nested_array_of_int")].as()); CHECK(tbl[S("nested_array_of_int")].as()->is_homogeneous()); CHECK(tbl[S("nested_array_of_int")].as()->size() == 2); REQUIRE(tbl[S("nested_array_of_int")][0].as()); CHECK(tbl[S("nested_array_of_int")][0].as()->is_homogeneous()); CHECK(tbl[S("nested_array_of_int")][0].as()->size() == 2); CHECK(tbl[S("nested_array_of_int")][0][0] == 1); CHECK(tbl[S("nested_array_of_int")][0][1] == 2); REQUIRE(tbl[S("nested_array_of_int")][1].as()); CHECK(tbl[S("nested_array_of_int")][1].as()->is_homogeneous()); CHECK(tbl[S("nested_array_of_int")][1].as()->size() == 3); CHECK(tbl[S("nested_array_of_int")][1][0] == 3); CHECK(tbl[S("nested_array_of_int")][1][1] == 4); CHECK(tbl[S("nested_array_of_int")][1][2] == 5); REQUIRE(tbl[S("nested_mixed_array")].as()); CHECK(tbl[S("nested_mixed_array")].as()->is_homogeneous()); CHECK(tbl[S("nested_mixed_array")].as()->size() == 2); REQUIRE(tbl[S("nested_mixed_array")][0].as()); CHECK(tbl[S("nested_mixed_array")][0].as()->is_homogeneous()); CHECK(tbl[S("nested_mixed_array")][0].as()->size() == 2); CHECK(tbl[S("nested_mixed_array")][0][0] == 1); CHECK(tbl[S("nested_mixed_array")][0][1] == 2); REQUIRE(tbl[S("nested_mixed_array")][1].as()); CHECK(tbl[S("nested_mixed_array")][1].as()->is_homogeneous()); CHECK(tbl[S("nested_mixed_array")][1].as()->size() == 3); CHECK(tbl[S("nested_mixed_array")][1][0] == S("a"sv)); CHECK(tbl[S("nested_mixed_array")][1][1] == S("b"sv)); CHECK(tbl[S("nested_mixed_array")][1][2] == S("c"sv)); REQUIRE(tbl[S("string_array")].as()); CHECK(tbl[S("string_array")].as()->is_homogeneous()); CHECK(tbl[S("string_array")].as()->size() == 4); CHECK(tbl[S("string_array")][0] == S("all"sv)); CHECK(tbl[S("string_array")][1] == S("strings"sv)); CHECK(tbl[S("string_array")][2] == S("are the same"sv)); CHECK(tbl[S("string_array")][3] == S("type"sv)); REQUIRE(tbl[S("integers")].as()); CHECK(tbl[S("integers")].as()->is_homogeneous()); CHECK(tbl[S("integers")].as()->size() == 3); CHECK(tbl[S("integers")][0] == 1); CHECK(tbl[S("integers")][1] == 2); CHECK(tbl[S("integers")][2] == 3); } ); // toml/issues/665 (heterogeneous arrays) #if TOML_LANG_AT_LEAST(1, 0, 0) parsing_should_succeed( FILE_LINE_ARGS, S(R"( # Mixed-type arrays are allowed numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ] contributors = [ "Foo Bar ", { name = "Baz Qux", email = "bazqux@example.com", url = "https://example.com/bazqux" } ] )"sv), [](table&& tbl) noexcept { REQUIRE(tbl[S("numbers")].as()); CHECK(!tbl[S("numbers")].as()->is_homogeneous()); CHECK(tbl[S("numbers")].as()->size() == 6); CHECK(tbl[S("numbers")][0].as()); CHECK(tbl[S("numbers")][1].as()); CHECK(tbl[S("numbers")][2].as()); CHECK(tbl[S("numbers")][3].as()); CHECK(tbl[S("numbers")][4].as()); CHECK(tbl[S("numbers")][5].as()); CHECK(tbl[S("numbers")][0] == 0.1); CHECK(tbl[S("numbers")][1] == 0.2); CHECK(tbl[S("numbers")][2] == 0.5); CHECK(tbl[S("numbers")][3] == 1); CHECK(tbl[S("numbers")][4] == 2); CHECK(tbl[S("numbers")][5] == 5); REQUIRE(tbl[S("contributors")].as()); CHECK(!tbl[S("contributors")].as()->is_homogeneous()); CHECK(tbl[S("contributors")].as()->size() == 2); CHECK(tbl[S("contributors")][0].as()); CHECK(tbl[S("contributors")][1].as()); CHECK(tbl[S("contributors")][0] == S("Foo Bar "sv)); CHECK(tbl[S("contributors")][1][S("name")] == S("Baz Qux"sv)); CHECK(tbl[S("contributors")][1][S("email")] == S("bazqux@example.com"sv)); CHECK(tbl[S("contributors")][1][S("url")] == S("https://example.com/bazqux"sv)); } ); #else parsing_should_fail(FILE_LINE_ARGS, S("numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]"sv)); #endif }