// This file is a part of toml++ and is subject to the the terms of the MIT license. // Copyright (c) 2019-2020 Mark Gillard // See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text. // SPDX-License-Identifier: MIT #include "tests.h" TOML_PUSH_WARNINGS TOML_DISABLE_INIT_WARNINGS TEST_CASE("parsing - TOML spec example") { static constexpr auto toml_text = R"( # This is a TOML document. title = "TOML Example" [owner] name = "Tom Preston-Werner" dob = 1979-05-27T07:32:00-08:00 # First class dates [database] server = "192.168.1.1" ports = [ 8001, 8001, 8002 ] connection_max = 5000 enabled = true [servers] # Indentation (tabs and/or spaces) is allowed but not required [servers.alpha] ip = "10.0.0.1" dc = "eqdc10" [servers.beta] ip = "10.0.0.2" dc = "eqdc10" [clients] data = [ ["gamma", "delta"], [1, 2] ] # Line breaks are OK when inside arrays hosts = [ "alpha", "omega" ] )"sv; parsing_should_succeed( FILE_LINE_ARGS, toml_text, [](table&& tbl) { CHECK(tbl.size() == 5); CHECK(tbl["title"] == "TOML Example"sv); CHECK(tbl["owner"]); CHECK(tbl["owner"].as()); CHECK(tbl["owner"]["name"] == "Tom Preston-Werner"sv); const auto dob = date_time{ { 1979, 5, 27 }, { 7, 32 }, { -8, 0 } }; CHECK(tbl["owner"]["dob"] == dob); CHECK(tbl["database"].as
()); CHECK(tbl["database"]["server"] == "192.168.1.1"sv); const auto ports = { 8001, 8001, 8002 }; CHECK(tbl["database"]["ports"] == ports); CHECK(tbl["database"]["connection_max"] == 5000); CHECK(tbl["database"]["enabled"] == true); CHECK(tbl["servers"].as
()); CHECK(tbl["servers"]["alpha"].as
()); CHECK(tbl["servers"]["alpha"]["ip"] == "10.0.0.1"sv); CHECK(tbl["servers"]["alpha"]["dc"] == "eqdc10"sv); CHECK(tbl["servers"]["beta"].as
()); CHECK(tbl["servers"]["beta"]["ip"] == "10.0.0.2"sv); CHECK(tbl["servers"]["beta"]["dc"] == "eqdc10"sv); CHECK(tbl["clients"].as
()); REQUIRE(tbl["clients"]["data"].as()); CHECK(tbl["clients"]["data"].as()->size() == 2); REQUIRE(tbl["clients"]["data"][0].as()); CHECK(tbl["clients"]["data"][0].as()->size() == 2); CHECK(tbl["clients"]["data"][0][0] == "gamma"sv); CHECK(tbl["clients"]["data"][0][1] == "delta"sv); REQUIRE(tbl["clients"]["data"][1].as()); CHECK(tbl["clients"]["data"][1].as()->size() == 2); CHECK(tbl["clients"]["data"][1][0] == 1); CHECK(tbl["clients"]["data"][1][1] == 2); REQUIRE(tbl["clients"]["hosts"].as()); CHECK(tbl["clients"]["hosts"].as()->size() == 2); CHECK(tbl["clients"]["hosts"][0] == "alpha"sv); CHECK(tbl["clients"]["hosts"][1] == "omega"sv); } ); } TOML_POP_WARNINGS