#define BOOST_TEST_MODULE "test_parse_file" #ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST #include #else #include #endif #include #include #include #include #include #include static auto testinput(const std::string& basename) -> std::string { const auto this_or_that = [](const char *const s, const char *const t) { return s ? s : t; }; std::string directory = this_or_that(std::getenv("TOMLDIR"), "toml"); if (!directory.empty() && directory.back() != '/') { directory.push_back('/'); } return directory.append("tests/").append(basename); } BOOST_AUTO_TEST_CASE(test_example) { const auto data = toml::parse(testinput("example.toml")); BOOST_TEST(toml::find(data, "title") == "TOML Example"); const auto& owner = toml::find(data, "owner"); { BOOST_TEST(toml::find(owner, "name") == "Tom Preston-Werner"); BOOST_TEST(toml::find(owner, "organization") == "GitHub"); BOOST_TEST(toml::find(owner, "bio") == "GitHub Cofounder & CEO\nLikes tater tots and beer."); BOOST_TEST(toml::find(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(database, "server") == "192.168.1.1"); const std::vector expected_ports{8001, 8001, 8002}; BOOST_CHECK(toml::find>(database, "ports") == expected_ports); BOOST_TEST(toml::find(database, "connection_max") == 5000); BOOST_TEST(toml::find(database, "enabled") == true); } const auto& servers = toml::find(data, "servers"); { toml::table alpha = toml::find(servers, "alpha"); BOOST_TEST(toml::get(alpha.at("ip")) == "10.0.0.1"); BOOST_TEST(toml::get(alpha.at("dc")) == "eqdc10"); toml::table beta = toml::find(servers, "beta"); BOOST_TEST(toml::get(beta.at("ip")) == "10.0.0.2"); BOOST_TEST(toml::get(beta.at("dc")) == "eqdc10"); BOOST_TEST(toml::get(beta.at("country")) == "\xE4\xB8\xAD\xE5\x9B\xBD"); } const auto& clients = toml::find(data, "clients"); { toml::array clients_data = toml::find(clients, "data"); std::vector expected_name{"gamma", "delta"}; BOOST_CHECK(toml::get>(clients_data.at(0)) == expected_name); std::vector expected_number{1, 2}; BOOST_CHECK(toml::get>(clients_data.at(1)) == expected_number); std::vector expected_hosts{"alpha", "omega"}; BOOST_CHECK(toml::find>(clients, "hosts") == expected_hosts); } std::vector products = toml::find>(data, "products"); { BOOST_TEST(toml::get(products.at(0).at("name")) == "Hammer"); BOOST_TEST(toml::get(products.at(0).at("sku")) == 738594937); BOOST_TEST(toml::get(products.at(1).at("name")) == "Nail"); BOOST_TEST(toml::get(products.at(1).at("sku")) == 284758393); BOOST_TEST(toml::get(products.at(1).at("color")) == "gray"); } } BOOST_AUTO_TEST_CASE(test_example_stream) { std::ifstream ifs(testinput("example.toml"), std::ios::binary); const auto data = toml::parse(ifs); BOOST_TEST(toml::find(data, "title") == "TOML Example"); const auto& owner = toml::find(data, "owner"); { BOOST_TEST(toml::find(owner, "name") == "Tom Preston-Werner"); BOOST_TEST(toml::find(owner, "organization") == "GitHub"); BOOST_TEST(toml::find(owner, "bio") == "GitHub Cofounder & CEO\nLikes tater tots and beer."); BOOST_TEST(toml::find(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(database, "server") == "192.168.1.1"); const std::vector expected_ports{8001, 8001, 8002}; BOOST_CHECK(toml::find>(database, "ports") == expected_ports); BOOST_TEST(toml::find(database, "connection_max") == 5000); BOOST_TEST(toml::find(database, "enabled") == true); } const auto& servers = toml::find(data, "servers"); { toml::table alpha = toml::find(servers, "alpha"); BOOST_TEST(toml::get(alpha.at("ip")) == "10.0.0.1"); BOOST_TEST(toml::get(alpha.at("dc")) == "eqdc10"); toml::table beta = toml::find(servers, "beta"); BOOST_TEST(toml::get(beta.at("ip")) == "10.0.0.2"); BOOST_TEST(toml::get(beta.at("dc")) == "eqdc10"); BOOST_TEST(toml::get(beta.at("country")) == "\xE4\xB8\xAD\xE5\x9B\xBD"); } const auto& clients = toml::find(data, "clients"); { toml::array clients_data = toml::find(clients, "data"); std::vector expected_name{"gamma", "delta"}; BOOST_CHECK(toml::get>(clients_data.at(0)) == expected_name); std::vector expected_number{1, 2}; BOOST_CHECK(toml::get>(clients_data.at(1)) == expected_number); std::vector expected_hosts{"alpha", "omega"}; BOOST_CHECK(toml::find>(clients, "hosts") == expected_hosts); } std::vector products = toml::find>(data, "products"); { BOOST_TEST(toml::get(products.at(0).at("name")) == "Hammer"); BOOST_TEST(toml::get(products.at(0).at("sku")) == 738594937); BOOST_TEST(toml::get(products.at(1).at("name")) == "Nail"); BOOST_TEST(toml::get(products.at(1).at("sku")) == 284758393); BOOST_TEST(toml::get(products.at(1).at("color")) == "gray"); } } BOOST_AUTO_TEST_CASE(test_example_file_pointer) { FILE * file = fopen(testinput("example.toml").c_str(), "rb"); const auto data = toml::parse(file, "toml/tests/example.toml"); fclose(file); BOOST_TEST(toml::find(data, "title") == "TOML Example"); const auto& owner = toml::find(data, "owner"); { BOOST_TEST(toml::find(owner, "name") == "Tom Preston-Werner"); BOOST_TEST(toml::find(owner, "organization") == "GitHub"); BOOST_TEST(toml::find(owner, "bio") == "GitHub Cofounder & CEO\nLikes tater tots and beer."); BOOST_TEST(toml::find(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(database, "server") == "192.168.1.1"); const std::vector expected_ports{8001, 8001, 8002}; BOOST_CHECK(toml::find>(database, "ports") == expected_ports); BOOST_TEST(toml::find(database, "connection_max") == 5000); BOOST_TEST(toml::find(database, "enabled") == true); } const auto& servers = toml::find(data, "servers"); { toml::table alpha = toml::find(servers, "alpha"); BOOST_TEST(toml::get(alpha.at("ip")) == "10.0.0.1"); BOOST_TEST(toml::get(alpha.at("dc")) == "eqdc10"); toml::table beta = toml::find(servers, "beta"); BOOST_TEST(toml::get(beta.at("ip")) == "10.0.0.2"); BOOST_TEST(toml::get(beta.at("dc")) == "eqdc10"); BOOST_TEST(toml::get(beta.at("country")) == "\xE4\xB8\xAD\xE5\x9B\xBD"); } const auto& clients = toml::find(data, "clients"); { toml::array clients_data = toml::find(clients, "data"); std::vector expected_name{"gamma", "delta"}; BOOST_CHECK(toml::get>(clients_data.at(0)) == expected_name); std::vector expected_number{1, 2}; BOOST_CHECK(toml::get>(clients_data.at(1)) == expected_number); std::vector expected_hosts{"alpha", "omega"}; BOOST_CHECK(toml::find>(clients, "hosts") == expected_hosts); } std::vector products = toml::find>(data, "products"); { BOOST_TEST(toml::get(products.at(0).at("name")) == "Hammer"); BOOST_TEST(toml::get(products.at(0).at("sku")) == 738594937); BOOST_TEST(toml::get(products.at(1).at("name")) == "Nail"); BOOST_TEST(toml::get(products.at(1).at("sku")) == 284758393); BOOST_TEST(toml::get(products.at(1).at("color")) == "gray"); } } BOOST_AUTO_TEST_CASE(test_fruit) { const auto data = toml::parse(testinput("fruit.toml")); const auto blah = toml::find(toml::find(data, "fruit"), "blah"); BOOST_TEST(toml::find(blah.at(0), "name") == "apple"); BOOST_TEST(toml::find(blah.at(1), "name") == "banana"); { const auto physical = toml::find(blah.at(0), "physical"); BOOST_TEST(toml::find(physical, "color") == "red"); BOOST_TEST(toml::find(physical, "shape") == "round"); } { const auto physical = toml::find(blah.at(1), "physical"); BOOST_TEST(toml::find(physical, "color") == "yellow"); BOOST_TEST(toml::find(physical, "shape") == "bent"); } } BOOST_AUTO_TEST_CASE(test_hard_example) { const auto data = toml::parse(testinput("hard_example.toml")); const auto the = toml::find(data, "the"); BOOST_TEST(toml::find(the, "test_string") == "You'll hate me after this - #"); const auto hard = toml::find(the, "hard"); const std::vector expected_the_hard_test_array{"] ", " # "}; BOOST_CHECK(toml::find>(hard, "test_array") == expected_the_hard_test_array); const std::vector expected_the_hard_test_array2{ "Test #11 ]proved that", "Experiment #9 was a success"}; BOOST_CHECK(toml::find>(hard, "test_array2") == expected_the_hard_test_array2); BOOST_TEST(toml::find(hard, "another_test_string") == " Same thing, but with a string #"); BOOST_TEST(toml::find(hard, "harder_test_string") == " And when \"'s are in the string, along with # \""); const auto bit = toml::find(hard, "bit#"); BOOST_TEST(toml::find(bit, "what?") == "You don't think some user won't do that?"); const std::vector expected_multi_line_array{"]"}; BOOST_CHECK(toml::find>(bit, "multi_line_array") == expected_multi_line_array); } BOOST_AUTO_TEST_CASE(test_hard_example_comment) { const auto data = toml::parse(testinput("hard_example.toml")); const auto the = toml::find(data, "the"); BOOST_TEST(toml::find(the, "test_string") == "You'll hate me after this - #"); const auto hard = toml::find(the, "hard"); const std::vector expected_the_hard_test_array{"] ", " # "}; BOOST_CHECK(toml::find>(hard, "test_array") == expected_the_hard_test_array); const std::vector expected_the_hard_test_array2{ "Test #11 ]proved that", "Experiment #9 was a success"}; BOOST_CHECK(toml::find>(hard, "test_array2") == expected_the_hard_test_array2); BOOST_TEST(toml::find(hard, "another_test_string") == " Same thing, but with a string #"); BOOST_TEST(toml::find(hard, "harder_test_string") == " And when \"'s are in the string, along with # \""); const auto bit = toml::find(hard, "bit#"); BOOST_TEST(toml::find(bit, "what?") == "You don't think some user won't do that?"); const std::vector expected_multi_line_array{"]"}; BOOST_CHECK(toml::find>(bit, "multi_line_array") == expected_multi_line_array); } BOOST_AUTO_TEST_CASE(test_example_preserve_comment) { const auto data = toml::parse(testinput("example.toml")); BOOST_TEST(toml::find(data, "title") == "TOML Example"); const auto& owner = toml::find(data, "owner"); { BOOST_TEST(toml::find(owner, "name") == "Tom Preston-Werner"); BOOST_TEST(toml::find(owner, "organization") == "GitHub"); BOOST_TEST(toml::find(owner, "bio") == "GitHub Cofounder & CEO\nLikes tater tots and beer."); BOOST_TEST(toml::find(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))); BOOST_TEST(toml::find(owner, "dob").comments().at(0) == " First class dates? Why not?"); } const auto& database = toml::find(data, "database"); { BOOST_TEST(toml::find(database, "server") == "192.168.1.1"); const std::vector expected_ports{8001, 8001, 8002}; BOOST_CHECK(toml::find>(database, "ports") == expected_ports); BOOST_TEST(toml::find(database, "connection_max") == 5000); BOOST_TEST(toml::find(database, "enabled") == true); } const auto& servers = toml::find(data, "servers"); { const auto& alpha = toml::find(servers, "alpha"); BOOST_TEST(alpha.comments().at(0) == " You can indent as you please. Tabs or spaces. TOML don't care."); BOOST_TEST(toml::find(alpha, "ip") == "10.0.0.1"); BOOST_TEST(toml::find(alpha, "dc") == "eqdc10"); const auto& beta = toml::find(servers, "beta"); BOOST_TEST(toml::find(beta, "ip") == "10.0.0.2"); BOOST_TEST(toml::find(beta, "dc") == "eqdc10"); BOOST_TEST(toml::find(beta, "country") == "\xE4\xB8\xAD\xE5\x9B\xBD"); BOOST_TEST(toml::find(beta, "country").comments().at(0) == " This should be parsed as UTF-8"); } const auto& clients = toml::find(data, "clients"); { BOOST_TEST(toml::find(clients, "data").comments().at(0) == " just an update to make sure parsers support it"); toml::array clients_data = toml::find(clients, "data"); std::vector expected_name{"gamma", "delta"}; BOOST_CHECK(toml::get>(clients_data.at(0)) == expected_name); std::vector expected_number{1, 2}; BOOST_CHECK(toml::get>(clients_data.at(1)) == expected_number); std::vector expected_hosts{"alpha", "omega"}; BOOST_CHECK(toml::find>(clients, "hosts") == expected_hosts); BOOST_TEST(toml::find(clients, "hosts").comments().at(0) == " Line breaks are OK when inside arrays"); } std::vector products = toml::find>(data, "products"); { BOOST_TEST(toml::get(products.at(0).at("name")) == "Hammer"); BOOST_TEST(toml::get(products.at(0).at("sku")) == 738594937); BOOST_TEST(toml::get(products.at(1).at("name")) == "Nail"); BOOST_TEST(toml::get(products.at(1).at("sku")) == 284758393); BOOST_TEST(toml::get(products.at(1).at("color")) == "gray"); } } BOOST_AUTO_TEST_CASE(test_example_preserve_stdmap_stddeque) { const auto data = toml::parse( testinput("example.toml")); static_assert(std::is_same::type> >::value, ""); static_assert(std::is_same::type> >::value, ""); BOOST_TEST(toml::find(data, "title") == "TOML Example"); const auto& owner = toml::find(data, "owner"); { BOOST_TEST(toml::find(owner, "name") == "Tom Preston-Werner"); BOOST_TEST(toml::find(owner, "organization") == "GitHub"); BOOST_TEST(toml::find(owner, "bio") == "GitHub Cofounder & CEO\nLikes tater tots and beer."); BOOST_TEST(toml::find(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))); BOOST_TEST(toml::find(owner, "dob").comments().at(0) == " First class dates? Why not?"); } const auto& database = toml::find(data, "database"); { BOOST_TEST(toml::find(database, "server") == "192.168.1.1"); const std::vector expected_ports{8001, 8001, 8002}; BOOST_CHECK(toml::find>(database, "ports") == expected_ports); BOOST_TEST(toml::find(database, "connection_max") == 5000); BOOST_TEST(toml::find(database, "enabled") == true); } const auto& servers = toml::find(data, "servers"); { const auto& alpha = toml::find(servers, "alpha"); BOOST_TEST(alpha.comments().at(0) == " You can indent as you please. Tabs or spaces. TOML don't care."); BOOST_TEST(toml::find(alpha, "ip") == "10.0.0.1"); BOOST_TEST(toml::find(alpha, "dc") == "eqdc10"); const auto& beta = toml::find(servers, "beta"); BOOST_TEST(toml::find(beta, "ip") == "10.0.0.2"); BOOST_TEST(toml::find(beta, "dc") == "eqdc10"); BOOST_TEST(toml::find(beta, "country") == "\xE4\xB8\xAD\xE5\x9B\xBD"); BOOST_TEST(toml::find(beta, "country").comments().at(0) == " This should be parsed as UTF-8"); } const auto& clients = toml::find(data, "clients"); { BOOST_TEST(toml::find(clients, "data").comments().at(0) == " just an update to make sure parsers support it"); toml::array clients_data = toml::find(clients, "data"); std::vector expected_name{"gamma", "delta"}; BOOST_CHECK(toml::get>(clients_data.at(0)) == expected_name); std::vector expected_number{1, 2}; BOOST_CHECK(toml::get>(clients_data.at(1)) == expected_number); std::vector expected_hosts{"alpha", "omega"}; BOOST_CHECK(toml::find>(clients, "hosts") == expected_hosts); BOOST_TEST(toml::find(clients, "hosts").comments().at(0) == " Line breaks are OK when inside arrays"); } std::vector products = toml::find>(data, "products"); { BOOST_TEST(toml::get(products.at(0).at("name")) == "Hammer"); BOOST_TEST(toml::get(products.at(0).at("sku")) == 738594937); BOOST_TEST(toml::get(products.at(1).at("name")) == "Nail"); BOOST_TEST(toml::get(products.at(1).at("sku")) == 284758393); BOOST_TEST(toml::get(products.at(1).at("color")) == "gray"); } } // --------------------------------------------------------------------------- // 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { const std::string table( "\xEF\xBB\xBF" // BOM "key = \"value\"\n" "[table]\n" "key = \"value\"\n" ); { std::ofstream ofs("tmp.toml"); ofs << table; } const auto data = toml::parse("tmp.toml"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { const std::string table( "\xEF\xBB\xBF" // BOM "key = \"value\"\r\n" "[table]\r\n" "key = \"value\"\r\n" ); { // 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); ofs.write(table.data(), static_cast(table.size())); } const auto data = toml::parse("tmp.toml"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } } 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } } 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } // 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } // 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } } 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } // 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } // 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } // 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(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } // 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } // 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } { 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"); BOOST_TEST(toml::find(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } } 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(data, "key") == "value"); BOOST_TEST(toml::find(toml::find(data, "table"), "key") == "value"); } } BOOST_AUTO_TEST_CASE(test_parse_function_compiles) { const auto c = [](std::string& s) -> const std::string& { return s; }; /*mutable*/ std::string example = testinput("example.toml"); // toml::parse(""); using result_type = decltype(toml::parse("string literal")); BOOST_TEST_MESSAGE("string_literal"); // toml::parse(const char*); const result_type cstring = toml::parse(example.c_str()); BOOST_TEST_MESSAGE("const char*"); // toml::parse(char*); const result_type char_ptr = toml::parse(&example.front()); BOOST_TEST_MESSAGE("char*"); // toml::parse(const std::string&); const result_type string = toml::parse(c(example)); // toml::parse(std::string&); const result_type string_mutref = toml::parse(example); // toml::parse(std::string&&); const result_type string_rref = toml::parse(std::move(example)); BOOST_TEST_MESSAGE("strings"); #ifdef TOML11_HAS_STD_FILESYSTEM const std::filesystem::path fname_path(example.begin(), example.end()); const result_type filesystem_path = toml::parse(fname_path); BOOST_TEST_MESSAGE("path"); #endif } BOOST_AUTO_TEST_CASE(test_parse_nonexistent_file) { BOOST_CHECK_THROW(toml::parse("nonexistent.toml"), std::ios_base::failure); }