2020-12-18 16:08:15 +00:00
|
|
|
#include "tests.h"
|
|
|
|
|
2021-04-18 12:04:46 +00:00
|
|
|
TOML_DISABLE_WARNINGS;
|
2020-12-18 16:08:15 +00:00
|
|
|
#include <algorithm>
|
2021-04-18 12:04:46 +00:00
|
|
|
TOML_ENABLE_WARNINGS;
|
2020-12-18 16:08:15 +00:00
|
|
|
|
2020-12-19 11:27:22 +00:00
|
|
|
TEST_CASE("using iterators")
|
2020-12-18 16:08:15 +00:00
|
|
|
{
|
|
|
|
constexpr auto data = R"(array=[1,"Foo",true]
|
|
|
|
string="Bar"
|
|
|
|
number=5)"sv;
|
2021-10-26 13:49:23 +00:00
|
|
|
parsing_should_succeed(
|
|
|
|
FILE_LINE_ARGS,
|
|
|
|
data,
|
2021-10-26 18:03:56 +00:00
|
|
|
[](auto&& tbl)
|
2020-12-18 16:08:15 +00:00
|
|
|
{
|
2021-10-26 18:03:56 +00:00
|
|
|
const auto tbl_begin = tbl.begin();
|
|
|
|
const auto tbl_end = tbl.end();
|
2020-12-18 16:08:15 +00:00
|
|
|
|
2021-10-26 18:03:56 +00:00
|
|
|
auto count_table_lambda = [tbl_begin, tbl_end](node_type type) noexcept {
|
|
|
|
return std::count_if(tbl_begin,
|
|
|
|
tbl_end,
|
2021-10-26 13:49:23 +00:00
|
|
|
[type](const auto& pair) noexcept { return pair.second.type() == type; });
|
|
|
|
};
|
|
|
|
|
2021-10-26 18:03:56 +00:00
|
|
|
CHECK(std::distance(tbl_begin, tbl_end) == 3);
|
2021-10-26 13:49:23 +00:00
|
|
|
CHECK(count_table_lambda(node_type::table) == 0);
|
|
|
|
CHECK(count_table_lambda(node_type::integer) == 1);
|
|
|
|
CHECK(count_table_lambda(node_type::string) == 1);
|
2021-10-26 18:03:56 +00:00
|
|
|
CHECK(std::next(tbl_begin, 3) == tbl_end);
|
2021-10-26 13:49:23 +00:00
|
|
|
|
2021-10-26 18:03:56 +00:00
|
|
|
const auto arr_iter =
|
|
|
|
std::find_if(tbl_begin, tbl_end, [](const auto& pair) noexcept { return pair.second.is_array(); });
|
2021-10-26 13:49:23 +00:00
|
|
|
|
2021-10-26 18:03:56 +00:00
|
|
|
REQUIRE(arr_iter != tbl_end);
|
|
|
|
const auto& arr = arr_iter->second.as_array();
|
|
|
|
const auto arr_begin = arr->begin();
|
|
|
|
const auto arr_end = arr->end();
|
2021-10-26 13:49:23 +00:00
|
|
|
|
2021-10-26 18:03:56 +00:00
|
|
|
auto count_array_lambda = [arr_begin, arr_end](node_type type) noexcept {
|
|
|
|
return std::count_if(arr_begin,
|
|
|
|
arr_end,
|
2021-10-26 13:49:23 +00:00
|
|
|
[type](const auto& node) noexcept { return node.type() == type; });
|
|
|
|
};
|
|
|
|
|
2021-10-26 18:03:56 +00:00
|
|
|
CHECK(std::distance(arr_begin, arr_end) == 3);
|
2021-10-26 13:49:23 +00:00
|
|
|
CHECK(count_array_lambda(node_type::table) == 0);
|
|
|
|
CHECK(count_array_lambda(node_type::integer) == 1);
|
|
|
|
CHECK(count_array_lambda(node_type::string) == 1);
|
2021-10-26 18:03:56 +00:00
|
|
|
CHECK(std::next(arr_begin, 2) != arr_end);
|
2021-10-26 13:49:23 +00:00
|
|
|
});
|
2020-12-18 16:08:15 +00:00
|
|
|
}
|