2022-09-16 11:16:01 +00:00
|
|
|
#include <toml.hpp>
|
|
|
|
|
2022-09-16 10:51:41 +00:00
|
|
|
#include "unit_test.hpp"
|
|
|
|
|
2022-09-16 11:16:01 +00:00
|
|
|
#include <deque>
|
2017-05-12 11:49:47 +00:00
|
|
|
#include <fstream>
|
2022-09-16 11:16:01 +00:00
|
|
|
#include <iostream>
|
2019-06-15 11:25:19 +00:00
|
|
|
#include <map>
|
2017-05-12 11:49:47 +00:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_example)
|
|
|
|
{
|
2022-09-16 09:51:32 +00:00
|
|
|
const auto data = toml::parse(testinput("example.toml"));
|
2017-05-12 11:49:47 +00:00
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "title") == "TOML Example");
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto& owner = toml::find(data, "owner");
|
2017-05-12 11:49:47 +00:00
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "name") == "Tom Preston-Werner");
|
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "organization") == "GitHub");
|
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "bio") ==
|
2017-05-12 11:49:47 +00:00
|
|
|
"GitHub Cofounder & CEO\nLikes tater tots and beer.");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<toml::offset_datetime>(owner, "dob") ==
|
2018-12-12 17:26:55 +00:00
|
|
|
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
|
|
|
|
toml::local_time(7, 32, 0), toml::time_offset(0, 0)));
|
2017-05-12 11:49:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto& database = toml::find(data, "database");
|
2017-05-12 11:49:47 +00:00
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(database, "server") == "192.168.1.1");
|
2017-05-12 11:49:47 +00:00
|
|
|
const std::vector<int> expected_ports{8001, 8001, 8002};
|
2019-06-21 06:58:22 +00:00
|
|
|
BOOST_CHECK(toml::find<std::vector<int>>(database, "ports") == expected_ports);
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<int >(database, "connection_max") == 5000);
|
|
|
|
BOOST_TEST(toml::find<bool>(database, "enabled") == true);
|
2017-05-12 11:49:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto& servers = toml::find(data, "servers");
|
2017-05-12 11:49:47 +00:00
|
|
|
{
|
2019-06-03 12:44:47 +00:00
|
|
|
toml::table alpha = toml::find<toml::table>(servers, "alpha");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(alpha.at("ip")) == "10.0.0.1");
|
|
|
|
BOOST_TEST(toml::get<std::string>(alpha.at("dc")) == "eqdc10");
|
2017-05-12 11:49:47 +00:00
|
|
|
|
2019-06-03 12:44:47 +00:00
|
|
|
toml::table beta = toml::find<toml::table>(servers, "beta");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(beta.at("ip")) == "10.0.0.2");
|
|
|
|
BOOST_TEST(toml::get<std::string>(beta.at("dc")) == "eqdc10");
|
|
|
|
BOOST_TEST(toml::get<std::string>(beta.at("country")) == "\xE4\xB8\xAD\xE5\x9B\xBD");
|
2017-05-12 11:49:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto& clients = toml::find(data, "clients");
|
2017-05-12 11:49:47 +00:00
|
|
|
{
|
2019-06-03 12:44:47 +00:00
|
|
|
toml::array clients_data = toml::find<toml::array>(clients, "data");
|
2019-06-21 06:22:46 +00:00
|
|
|
|
2017-05-12 11:49:47 +00:00
|
|
|
std::vector<std::string> expected_name{"gamma", "delta"};
|
2019-06-21 06:58:22 +00:00
|
|
|
BOOST_CHECK(toml::get<std::vector<std::string>>(clients_data.at(0)) == expected_name);
|
2019-06-21 06:22:46 +00:00
|
|
|
|
2017-05-12 11:49:47 +00:00
|
|
|
std::vector<int> expected_number{1, 2};
|
2019-06-21 06:58:22 +00:00
|
|
|
BOOST_CHECK(toml::get<std::vector<int>>(clients_data.at(1)) == expected_number);
|
2019-06-21 06:22:46 +00:00
|
|
|
|
2017-05-12 11:49:47 +00:00
|
|
|
std::vector<std::string> expected_hosts{"alpha", "omega"};
|
2019-06-21 06:58:22 +00:00
|
|
|
BOOST_CHECK(toml::find<std::vector<std::string>>(clients, "hosts") == expected_hosts);
|
2017-05-12 11:49:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 12:44:47 +00:00
|
|
|
std::vector<toml::table> products =
|
|
|
|
toml::find<std::vector<toml::table>>(data, "products");
|
2017-05-12 11:49:47 +00:00
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(0).at("name")) == "Hammer");
|
|
|
|
BOOST_TEST(toml::get<std::int64_t>(products.at(0).at("sku")) == 738594937);
|
2017-05-12 11:49:47 +00:00
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(1).at("name")) == "Nail");
|
|
|
|
BOOST_TEST(toml::get<std::int64_t>(products.at(1).at("sku")) == 284758393);
|
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(1).at("color")) == "gray");
|
2017-05-12 11:49:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 05:16:12 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(test_example_stream)
|
|
|
|
{
|
2022-09-16 09:51:32 +00:00
|
|
|
std::ifstream ifs(testinput("example.toml"), std::ios::binary);
|
2017-05-19 05:16:12 +00:00
|
|
|
const auto data = toml::parse(ifs);
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "title") == "TOML Example");
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto& owner = toml::find(data, "owner");
|
2017-05-19 05:16:12 +00:00
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "name") == "Tom Preston-Werner");
|
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "organization") == "GitHub");
|
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "bio") ==
|
2017-05-19 05:16:12 +00:00
|
|
|
"GitHub Cofounder & CEO\nLikes tater tots and beer.");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<toml::offset_datetime>(owner, "dob") ==
|
2018-12-12 17:26:55 +00:00
|
|
|
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
|
|
|
|
toml::local_time(7, 32, 0), toml::time_offset(0, 0)));
|
2017-05-19 05:16:12 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto& database = toml::find(data, "database");
|
2017-05-19 05:16:12 +00:00
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(database, "server") == "192.168.1.1");
|
2017-05-19 05:16:12 +00:00
|
|
|
const std::vector<int> expected_ports{8001, 8001, 8002};
|
2019-06-21 06:58:22 +00:00
|
|
|
BOOST_CHECK(toml::find<std::vector<int>>(database, "ports") == expected_ports);
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<int >(database, "connection_max") == 5000);
|
|
|
|
BOOST_TEST(toml::find<bool>(database, "enabled") == true);
|
2017-05-19 05:16:12 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto& servers = toml::find(data, "servers");
|
2017-05-19 05:16:12 +00:00
|
|
|
{
|
2019-06-03 12:44:47 +00:00
|
|
|
toml::table alpha = toml::find<toml::table>(servers, "alpha");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(alpha.at("ip")) == "10.0.0.1");
|
|
|
|
BOOST_TEST(toml::get<std::string>(alpha.at("dc")) == "eqdc10");
|
2017-05-19 05:16:12 +00:00
|
|
|
|
2019-06-03 12:44:47 +00:00
|
|
|
toml::table beta = toml::find<toml::table>(servers, "beta");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(beta.at("ip")) == "10.0.0.2");
|
|
|
|
BOOST_TEST(toml::get<std::string>(beta.at("dc")) == "eqdc10");
|
|
|
|
BOOST_TEST(toml::get<std::string>(beta.at("country")) == "\xE4\xB8\xAD\xE5\x9B\xBD");
|
2017-05-19 05:16:12 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto& clients = toml::find(data, "clients");
|
2017-05-19 05:16:12 +00:00
|
|
|
{
|
2019-06-03 12:44:47 +00:00
|
|
|
toml::array clients_data = toml::find<toml::array>(clients, "data");
|
2017-05-19 05:16:12 +00:00
|
|
|
std::vector<std::string> expected_name{"gamma", "delta"};
|
2019-06-21 06:58:22 +00:00
|
|
|
BOOST_CHECK(toml::get<std::vector<std::string>>(clients_data.at(0)) == expected_name);
|
2019-06-21 06:22:46 +00:00
|
|
|
|
2017-05-19 05:16:12 +00:00
|
|
|
std::vector<int> expected_number{1, 2};
|
2019-06-21 06:58:22 +00:00
|
|
|
BOOST_CHECK(toml::get<std::vector<int>>(clients_data.at(1)) == expected_number);
|
2019-06-21 06:22:46 +00:00
|
|
|
|
2017-05-19 05:16:12 +00:00
|
|
|
std::vector<std::string> expected_hosts{"alpha", "omega"};
|
2019-06-21 06:58:22 +00:00
|
|
|
BOOST_CHECK(toml::find<std::vector<std::string>>(clients, "hosts") == expected_hosts);
|
2017-05-19 05:16:12 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 12:44:47 +00:00
|
|
|
std::vector<toml::table> products =
|
|
|
|
toml::find<std::vector<toml::table>>(data, "products");
|
2017-05-19 05:16:12 +00:00
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(0).at("name")) ==
|
2017-05-19 05:16:12 +00:00
|
|
|
"Hammer");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::int64_t>(products.at(0).at("sku")) ==
|
2017-05-19 05:16:12 +00:00
|
|
|
738594937);
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(1).at("name")) ==
|
2017-05-19 05:16:12 +00:00
|
|
|
"Nail");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::int64_t>(products.at(1).at("sku")) ==
|
2017-05-19 05:16:12 +00:00
|
|
|
284758393);
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(1).at("color")) ==
|
2017-05-19 05:16:12 +00:00
|
|
|
"gray");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 11:53:32 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(test_example_file_pointer)
|
|
|
|
{
|
2022-09-16 09:51:32 +00:00
|
|
|
FILE * file = fopen(testinput("example.toml").c_str(), "rb");
|
2022-06-30 11:53:32 +00:00
|
|
|
const auto data = toml::parse(file, "toml/tests/example.toml");
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
BOOST_TEST(toml::find<std::string>(data, "title") == "TOML Example");
|
|
|
|
const auto& owner = toml::find(data, "owner");
|
|
|
|
{
|
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "name") == "Tom Preston-Werner");
|
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "organization") == "GitHub");
|
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "bio") ==
|
|
|
|
"GitHub Cofounder & CEO\nLikes tater tots and beer.");
|
|
|
|
BOOST_TEST(toml::find<toml::offset_datetime>(owner, "dob") ==
|
|
|
|
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
|
|
|
|
toml::local_time(7, 32, 0), toml::time_offset(0, 0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& database = toml::find(data, "database");
|
|
|
|
{
|
|
|
|
BOOST_TEST(toml::find<std::string>(database, "server") == "192.168.1.1");
|
|
|
|
const std::vector<int> expected_ports{8001, 8001, 8002};
|
|
|
|
BOOST_CHECK(toml::find<std::vector<int>>(database, "ports") == expected_ports);
|
|
|
|
BOOST_TEST(toml::find<int >(database, "connection_max") == 5000);
|
|
|
|
BOOST_TEST(toml::find<bool>(database, "enabled") == true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& servers = toml::find(data, "servers");
|
|
|
|
{
|
|
|
|
toml::table alpha = toml::find<toml::table>(servers, "alpha");
|
|
|
|
BOOST_TEST(toml::get<std::string>(alpha.at("ip")) == "10.0.0.1");
|
|
|
|
BOOST_TEST(toml::get<std::string>(alpha.at("dc")) == "eqdc10");
|
|
|
|
|
|
|
|
toml::table beta = toml::find<toml::table>(servers, "beta");
|
|
|
|
BOOST_TEST(toml::get<std::string>(beta.at("ip")) == "10.0.0.2");
|
|
|
|
BOOST_TEST(toml::get<std::string>(beta.at("dc")) == "eqdc10");
|
|
|
|
BOOST_TEST(toml::get<std::string>(beta.at("country")) == "\xE4\xB8\xAD\xE5\x9B\xBD");
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& clients = toml::find(data, "clients");
|
|
|
|
{
|
|
|
|
toml::array clients_data = toml::find<toml::array>(clients, "data");
|
|
|
|
std::vector<std::string> expected_name{"gamma", "delta"};
|
|
|
|
BOOST_CHECK(toml::get<std::vector<std::string>>(clients_data.at(0)) == expected_name);
|
|
|
|
|
|
|
|
std::vector<int> expected_number{1, 2};
|
|
|
|
BOOST_CHECK(toml::get<std::vector<int>>(clients_data.at(1)) == expected_number);
|
|
|
|
|
|
|
|
std::vector<std::string> expected_hosts{"alpha", "omega"};
|
|
|
|
BOOST_CHECK(toml::find<std::vector<std::string>>(clients, "hosts") == expected_hosts);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<toml::table> products =
|
|
|
|
toml::find<std::vector<toml::table>>(data, "products");
|
|
|
|
{
|
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(0).at("name")) ==
|
|
|
|
"Hammer");
|
|
|
|
BOOST_TEST(toml::get<std::int64_t>(products.at(0).at("sku")) ==
|
|
|
|
738594937);
|
|
|
|
|
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(1).at("name")) ==
|
|
|
|
"Nail");
|
|
|
|
BOOST_TEST(toml::get<std::int64_t>(products.at(1).at("sku")) ==
|
|
|
|
284758393);
|
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(1).at("color")) ==
|
|
|
|
"gray");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 11:49:47 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(test_fruit)
|
|
|
|
{
|
2022-09-16 09:51:32 +00:00
|
|
|
const auto data = toml::parse(testinput("fruit.toml"));
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto blah = toml::find<toml::array>(toml::find(data, "fruit"), "blah");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(blah.at(0), "name") == "apple");
|
|
|
|
BOOST_TEST(toml::find<std::string>(blah.at(1), "name") == "banana");
|
2017-05-12 11:49:47 +00:00
|
|
|
{
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto physical = toml::find(blah.at(0), "physical");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(physical, "color") == "red");
|
|
|
|
BOOST_TEST(toml::find<std::string>(physical, "shape") == "round");
|
2017-05-12 11:49:47 +00:00
|
|
|
}
|
|
|
|
{
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto physical = toml::find(blah.at(1), "physical");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(physical, "color") == "yellow");
|
|
|
|
BOOST_TEST(toml::find<std::string>(physical, "shape") == "bent");
|
2017-05-12 11:49:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 13:28:49 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(test_hard_example)
|
|
|
|
{
|
2022-09-16 09:51:32 +00:00
|
|
|
const auto data = toml::parse(testinput("hard_example.toml"));
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto the = toml::find(data, "the");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(the, "test_string") ==
|
2017-05-12 13:28:49 +00:00
|
|
|
"You'll hate me after this - #");
|
|
|
|
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto hard = toml::find(the, "hard");
|
2017-05-12 13:28:49 +00:00
|
|
|
const std::vector<std::string> expected_the_hard_test_array{"] ", " # "};
|
2019-06-03 12:44:47 +00:00
|
|
|
BOOST_CHECK(toml::find<std::vector<std::string>>(hard, "test_array") ==
|
2017-05-12 13:28:49 +00:00
|
|
|
expected_the_hard_test_array);
|
|
|
|
const std::vector<std::string> expected_the_hard_test_array2{
|
|
|
|
"Test #11 ]proved that", "Experiment #9 was a success"};
|
2019-06-03 12:44:47 +00:00
|
|
|
BOOST_CHECK(toml::find<std::vector<std::string>>(hard, "test_array2") ==
|
2017-05-12 13:28:49 +00:00
|
|
|
expected_the_hard_test_array2);
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(hard, "another_test_string") ==
|
2017-05-12 13:28:49 +00:00
|
|
|
" Same thing, but with a string #");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(hard, "harder_test_string") ==
|
2017-05-12 13:28:49 +00:00
|
|
|
" And when \"'s are in the string, along with # \"");
|
|
|
|
|
2019-06-03 12:44:47 +00:00
|
|
|
const auto bit = toml::find(hard, "bit#");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(bit, "what?") ==
|
2017-05-12 13:28:49 +00:00
|
|
|
"You don't think some user won't do that?");
|
|
|
|
const std::vector<std::string> expected_multi_line_array{"]"};
|
2019-06-03 12:44:47 +00:00
|
|
|
BOOST_CHECK(toml::find<std::vector<std::string>>(bit, "multi_line_array") ==
|
2017-05-12 13:28:49 +00:00
|
|
|
expected_multi_line_array);
|
|
|
|
}
|
2019-06-17 16:25:43 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(test_hard_example_comment)
|
|
|
|
{
|
2022-09-16 09:51:32 +00:00
|
|
|
const auto data = toml::parse<toml::preserve_comments>(testinput("hard_example.toml"));
|
2019-06-17 16:25:43 +00:00
|
|
|
const auto the = toml::find(data, "the");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(the, "test_string") ==
|
2019-06-17 16:25:43 +00:00
|
|
|
"You'll hate me after this - #");
|
|
|
|
|
|
|
|
const auto hard = toml::find(the, "hard");
|
|
|
|
const std::vector<std::string> expected_the_hard_test_array{"] ", " # "};
|
|
|
|
BOOST_CHECK(toml::find<std::vector<std::string>>(hard, "test_array") ==
|
|
|
|
expected_the_hard_test_array);
|
|
|
|
const std::vector<std::string> expected_the_hard_test_array2{
|
|
|
|
"Test #11 ]proved that", "Experiment #9 was a success"};
|
|
|
|
BOOST_CHECK(toml::find<std::vector<std::string>>(hard, "test_array2") ==
|
|
|
|
expected_the_hard_test_array2);
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(hard, "another_test_string") ==
|
2019-06-17 16:25:43 +00:00
|
|
|
" Same thing, but with a string #");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(hard, "harder_test_string") ==
|
2019-06-17 16:25:43 +00:00
|
|
|
" And when \"'s are in the string, along with # \"");
|
|
|
|
|
|
|
|
const auto bit = toml::find(hard, "bit#");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(bit, "what?") ==
|
2019-06-17 16:25:43 +00:00
|
|
|
"You don't think some user won't do that?");
|
|
|
|
const std::vector<std::string> expected_multi_line_array{"]"};
|
|
|
|
BOOST_CHECK(toml::find<std::vector<std::string>>(bit, "multi_line_array") ==
|
|
|
|
expected_multi_line_array);
|
|
|
|
}
|
|
|
|
|
2018-12-24 07:02:32 +00:00
|
|
|
|
2019-06-17 15:45:30 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(test_example_preserve_comment)
|
|
|
|
{
|
2022-09-16 09:51:32 +00:00
|
|
|
const auto data = toml::parse<toml::preserve_comments>(testinput("example.toml"));
|
2019-06-17 15:45:30 +00:00
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "title") == "TOML Example");
|
2019-06-17 15:45:30 +00:00
|
|
|
const auto& owner = toml::find(data, "owner");
|
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "name") == "Tom Preston-Werner");
|
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "organization") == "GitHub");
|
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "bio") ==
|
2019-06-17 15:45:30 +00:00
|
|
|
"GitHub Cofounder & CEO\nLikes tater tots and beer.");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<toml::offset_datetime>(owner, "dob") ==
|
2019-06-17 15:45:30 +00:00
|
|
|
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
|
|
|
|
toml::local_time(7, 32, 0), toml::time_offset(0, 0)));
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find(owner, "dob").comments().at(0) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
" First class dates? Why not?");
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& database = toml::find(data, "database");
|
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(database, "server") == "192.168.1.1");
|
2019-06-17 15:45:30 +00:00
|
|
|
const std::vector<int> expected_ports{8001, 8001, 8002};
|
|
|
|
BOOST_CHECK(toml::find<std::vector<int>>(database, "ports") == expected_ports);
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<int >(database, "connection_max") == 5000);
|
|
|
|
BOOST_TEST(toml::find<bool>(database, "enabled") == true);
|
2019-06-17 15:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto& servers = toml::find(data, "servers");
|
|
|
|
{
|
|
|
|
const auto& alpha = toml::find(servers, "alpha");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(alpha.comments().at(0) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
" You can indent as you please. Tabs or spaces. TOML don't care.");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(alpha, "ip") == "10.0.0.1");
|
|
|
|
BOOST_TEST(toml::find<std::string>(alpha, "dc") == "eqdc10");
|
2019-06-17 15:45:30 +00:00
|
|
|
|
|
|
|
const auto& beta = toml::find(servers, "beta");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(beta, "ip") == "10.0.0.2");
|
|
|
|
BOOST_TEST(toml::find<std::string>(beta, "dc") == "eqdc10");
|
|
|
|
BOOST_TEST(toml::find<std::string>(beta, "country") ==
|
2019-06-17 15:45:30 +00:00
|
|
|
"\xE4\xB8\xAD\xE5\x9B\xBD");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find(beta, "country").comments().at(0) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
" This should be parsed as UTF-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& clients = toml::find(data, "clients");
|
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find(clients, "data").comments().at(0) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
" just an update to make sure parsers support it");
|
|
|
|
|
|
|
|
|
|
|
|
toml::array clients_data = toml::find<toml::array>(clients, "data");
|
|
|
|
std::vector<std::string> expected_name{"gamma", "delta"};
|
|
|
|
BOOST_CHECK(toml::get<std::vector<std::string>>(clients_data.at(0)) ==
|
|
|
|
expected_name);
|
|
|
|
std::vector<int> expected_number{1, 2};
|
|
|
|
BOOST_CHECK(toml::get<std::vector<int>>(clients_data.at(1)) ==
|
|
|
|
expected_number);
|
|
|
|
std::vector<std::string> expected_hosts{"alpha", "omega"};
|
|
|
|
BOOST_CHECK(toml::find<std::vector<std::string>>(clients, "hosts") ==
|
|
|
|
expected_hosts);
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find(clients, "hosts").comments().at(0) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
" Line breaks are OK when inside arrays");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<toml::table> products =
|
|
|
|
toml::find<std::vector<toml::table>>(data, "products");
|
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(0).at("name")) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
"Hammer");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::int64_t>(products.at(0).at("sku")) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
738594937);
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(1).at("name")) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
"Nail");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::int64_t>(products.at(1).at("sku")) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
284758393);
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(1).at("color")) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
"gray");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_example_preserve_stdmap_stddeque)
|
|
|
|
{
|
2022-09-16 09:51:32 +00:00
|
|
|
const auto data = toml::parse<toml::preserve_comments, std::map, std::deque>(
|
|
|
|
testinput("example.toml"));
|
2019-06-17 15:45:30 +00:00
|
|
|
|
|
|
|
static_assert(std::is_same<typename decltype(data)::table_type,
|
|
|
|
std::map<toml::key, typename std::remove_cv<decltype(data)>::type>
|
|
|
|
>::value, "");
|
|
|
|
static_assert(std::is_same<typename decltype(data)::array_type,
|
|
|
|
std::deque<typename std::remove_cv<decltype(data)>::type>
|
|
|
|
>::value, "");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "title") == "TOML Example");
|
2019-06-17 15:45:30 +00:00
|
|
|
const auto& owner = toml::find(data, "owner");
|
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "name") == "Tom Preston-Werner");
|
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "organization") == "GitHub");
|
|
|
|
BOOST_TEST(toml::find<std::string>(owner, "bio") ==
|
2019-06-17 15:45:30 +00:00
|
|
|
"GitHub Cofounder & CEO\nLikes tater tots and beer.");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<toml::offset_datetime>(owner, "dob") ==
|
2019-06-17 15:45:30 +00:00
|
|
|
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
|
|
|
|
toml::local_time(7, 32, 0), toml::time_offset(0, 0)));
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find(owner, "dob").comments().at(0) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
" First class dates? Why not?");
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& database = toml::find(data, "database");
|
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(database, "server") == "192.168.1.1");
|
2019-06-17 15:45:30 +00:00
|
|
|
const std::vector<int> expected_ports{8001, 8001, 8002};
|
|
|
|
BOOST_CHECK(toml::find<std::vector<int>>(database, "ports") == expected_ports);
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<int >(database, "connection_max") == 5000);
|
|
|
|
BOOST_TEST(toml::find<bool>(database, "enabled") == true);
|
2019-06-17 15:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto& servers = toml::find(data, "servers");
|
|
|
|
{
|
|
|
|
const auto& alpha = toml::find(servers, "alpha");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(alpha.comments().at(0) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
" You can indent as you please. Tabs or spaces. TOML don't care.");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(alpha, "ip") == "10.0.0.1");
|
|
|
|
BOOST_TEST(toml::find<std::string>(alpha, "dc") == "eqdc10");
|
2019-06-17 15:45:30 +00:00
|
|
|
|
|
|
|
const auto& beta = toml::find(servers, "beta");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(beta, "ip") == "10.0.0.2");
|
|
|
|
BOOST_TEST(toml::find<std::string>(beta, "dc") == "eqdc10");
|
|
|
|
BOOST_TEST(toml::find<std::string>(beta, "country") ==
|
2019-06-17 15:45:30 +00:00
|
|
|
"\xE4\xB8\xAD\xE5\x9B\xBD");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find(beta, "country").comments().at(0) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
" This should be parsed as UTF-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& clients = toml::find(data, "clients");
|
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find(clients, "data").comments().at(0) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
" just an update to make sure parsers support it");
|
|
|
|
|
|
|
|
|
|
|
|
toml::array clients_data = toml::find<toml::array>(clients, "data");
|
|
|
|
std::vector<std::string> expected_name{"gamma", "delta"};
|
|
|
|
BOOST_CHECK(toml::get<std::vector<std::string>>(clients_data.at(0)) ==
|
|
|
|
expected_name);
|
|
|
|
std::vector<int> expected_number{1, 2};
|
|
|
|
BOOST_CHECK(toml::get<std::vector<int>>(clients_data.at(1)) ==
|
|
|
|
expected_number);
|
|
|
|
std::vector<std::string> expected_hosts{"alpha", "omega"};
|
|
|
|
BOOST_CHECK(toml::find<std::vector<std::string>>(clients, "hosts") ==
|
|
|
|
expected_hosts);
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find(clients, "hosts").comments().at(0) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
" Line breaks are OK when inside arrays");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<toml::table> products =
|
|
|
|
toml::find<std::vector<toml::table>>(data, "products");
|
|
|
|
{
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(0).at("name")) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
"Hammer");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::int64_t>(products.at(0).at("sku")) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
738594937);
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(1).at("name")) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
"Nail");
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::int64_t>(products.at(1).at("sku")) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
284758393);
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::get<std::string>(products.at(1).at("color")) ==
|
2019-06-17 15:45:30 +00:00
|
|
|
"gray");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-24 07:02:32 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// after here, the test codes generate the content of a file.
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_file_with_BOM)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"\xEF\xBB\xBF" // BOM
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss, "test_file_with_BOM.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
2019-02-13 10:18:09 +00:00
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"\xEF\xBB\xBF" // BOM
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
);
|
2019-02-13 10:26:52 +00:00
|
|
|
{
|
|
|
|
std::ofstream ofs("tmp.toml");
|
|
|
|
ofs << table;
|
|
|
|
}
|
2019-02-13 10:18:09 +00:00
|
|
|
const auto data = toml::parse("tmp.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2019-02-13 10:18:09 +00:00
|
|
|
}
|
2018-12-24 07:02:32 +00:00
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"\xEF\xBB\xBF" // BOM
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss, "test_file_with_BOM_CRLF.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2019-02-13 10:18:09 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"\xEF\xBB\xBF" // BOM
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
);
|
2019-02-13 10:26:52 +00:00
|
|
|
{
|
2019-02-13 10:51:54 +00:00
|
|
|
// with text-mode, "\n" is converted to "\r\n" and the resulting
|
|
|
|
// value will be "\r\r\n". To avoid the additional "\r", use binary
|
|
|
|
// mode.
|
|
|
|
std::ofstream ofs("tmp.toml", std::ios_base::binary);
|
2019-06-20 14:59:54 +00:00
|
|
|
ofs.write(table.data(), static_cast<std::streamsize>(table.size()));
|
2019-02-13 10:26:52 +00:00
|
|
|
}
|
2019-02-13 10:18:09 +00:00
|
|
|
const auto data = toml::parse("tmp.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_file_without_newline_at_the_end_of_file)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\""
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_file_without_newline_at_the_end_of_file.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\""
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_file_without_newline_at_the_end_of_file_CRLF.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\" # comment"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_file_without_newline_at_the_end_of_file_comment.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\" # comment"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_file_without_newline_at_the_end_of_file_comment.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\" \t"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_file_without_newline_at_the_end_of_file_ws.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\" \t"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_file_without_newline_at_the_end_of_file_ws.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_files_end_with_comment)
|
|
|
|
{
|
|
|
|
// comment w/o newline
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"# comment"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_comment.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"# comment\n"
|
|
|
|
"# one more comment"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_comment.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// comment w/ newline
|
|
|
|
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"# comment\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_comment.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"# comment\n"
|
|
|
|
"# one more comment\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_comment.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CRLF version
|
|
|
|
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"# comment"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_comment.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"# comment\r\n"
|
|
|
|
"# one more comment"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_comment.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"# comment\r\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_comment.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"# comment\r\n"
|
|
|
|
"# one more comment\r\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_comment.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_files_end_with_empty_lines)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// with whitespaces
|
|
|
|
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
" \n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
" \n"
|
|
|
|
" \n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"\n"
|
|
|
|
" \n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
" \n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// with whitespaces but no newline
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
" "
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 07:39:05 +00:00
|
|
|
// without newline
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"a = 0"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
|
|
|
}
|
|
|
|
|
2018-12-24 07:02:32 +00:00
|
|
|
|
|
|
|
// CRLF
|
|
|
|
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"\r\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"\r\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// with whitespaces
|
|
|
|
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
" \r\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"\r\n"
|
|
|
|
" \r\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
" \r\n"
|
|
|
|
"\r\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
" \r\n"
|
|
|
|
" \r\n"
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
"[table]\r\n"
|
|
|
|
"key = \"value\"\r\n"
|
|
|
|
" "
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_with_newline.toml");
|
|
|
|
|
2019-06-21 06:22:46 +00:00
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
2018-12-24 07:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-15 11:25:19 +00:00
|
|
|
|
2021-12-10 14:46:14 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(test_file_ends_without_lf)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
const std::string table(
|
|
|
|
"key = \"value\"\n"
|
|
|
|
"[table]\n"
|
|
|
|
"key = \"value\""
|
|
|
|
);
|
|
|
|
std::istringstream iss(table);
|
|
|
|
const auto data = toml::parse(iss,
|
|
|
|
"test_files_end_without_lf.toml");
|
|
|
|
|
|
|
|
BOOST_TEST(toml::find<std::string>(data, "key") == "value");
|
|
|
|
BOOST_TEST(toml::find<std::string>(toml::find(data, "table"), "key") == "value");
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 11:29:07 +00:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_parse_function_compiles)
|
|
|
|
{
|
2022-09-16 09:51:32 +00:00
|
|
|
const auto c = [](std::string& s) -> const std::string& { return s; };
|
|
|
|
/*mutable*/ std::string example = testinput("example.toml");
|
|
|
|
|
2020-08-05 11:29:07 +00:00
|
|
|
// toml::parse("");
|
2022-09-16 09:51:32 +00:00
|
|
|
using result_type = decltype(toml::parse("string literal"));
|
2020-08-05 11:29:07 +00:00
|
|
|
|
2020-08-05 11:43:48 +00:00
|
|
|
BOOST_TEST_MESSAGE("string_literal");
|
|
|
|
|
2020-08-05 11:29:07 +00:00
|
|
|
// toml::parse(const char*);
|
2022-09-16 09:51:32 +00:00
|
|
|
const result_type cstring = toml::parse(example.c_str());
|
2020-08-05 11:29:07 +00:00
|
|
|
|
2020-08-05 11:43:48 +00:00
|
|
|
BOOST_TEST_MESSAGE("const char*");
|
|
|
|
|
2020-08-05 11:29:07 +00:00
|
|
|
// toml::parse(char*);
|
2022-09-16 09:51:32 +00:00
|
|
|
const result_type char_ptr = toml::parse(&example.front());
|
2020-08-05 11:43:48 +00:00
|
|
|
|
|
|
|
BOOST_TEST_MESSAGE("char*");
|
2020-08-05 11:29:07 +00:00
|
|
|
|
|
|
|
// toml::parse(const std::string&);
|
2022-09-16 09:51:32 +00:00
|
|
|
const result_type string = toml::parse(c(example));
|
2020-08-05 11:29:07 +00:00
|
|
|
// toml::parse(std::string&);
|
2022-09-16 09:51:32 +00:00
|
|
|
const result_type string_mutref = toml::parse(example);
|
2020-08-05 11:29:07 +00:00
|
|
|
// toml::parse(std::string&&);
|
2022-09-16 09:51:32 +00:00
|
|
|
const result_type string_rref = toml::parse(std::move(example));
|
2020-08-05 11:29:07 +00:00
|
|
|
|
2020-08-05 11:43:48 +00:00
|
|
|
BOOST_TEST_MESSAGE("strings");
|
|
|
|
|
2020-08-05 11:29:07 +00:00
|
|
|
#ifdef TOML11_HAS_STD_FILESYSTEM
|
2022-09-16 09:51:32 +00:00
|
|
|
const std::filesystem::path fname_path(example.begin(), example.end());
|
|
|
|
const result_type filesystem_path = toml::parse(fname_path);
|
2020-08-05 11:43:48 +00:00
|
|
|
BOOST_TEST_MESSAGE("path");
|
2020-08-05 11:29:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
2022-06-30 11:53:32 +00:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_parse_nonexistent_file)
|
|
|
|
{
|
2022-09-08 14:58:56 +00:00
|
|
|
BOOST_CHECK_THROW(toml::parse("nonexistent.toml"), std::ios_base::failure);
|
2022-06-30 11:53:32 +00:00
|
|
|
}
|