fixed missing blank lines between consecutive empty tables/A-o-T

also:
- added additional value() testsfor inf/nan
- added additional formatting tests
This commit is contained in:
Mark Gillard 2021-01-07 12:06:26 +02:00
parent 00e080f126
commit 9be51e440a
6 changed files with 137 additions and 20 deletions

View File

@ -57,6 +57,17 @@ TOML_NAMESPACE_START
private:
using base = impl::formatter<Char>;
std::vector<std::string> key_path;
bool pending_table_separator_ = false;
void print_pending_table_separator()
{
if (pending_table_separator_)
{
base::print_newline(true);
base::print_newline(true);
pending_table_separator_ = false;
}
}
void print_key_segment(const std::string& str)
{
@ -181,6 +192,7 @@ TOML_NAMESPACE_START
|| (type == node_type::array && is_non_inline_array_of_tables(v)))
continue;
pending_table_separator_ = true;
base::print_newline();
base::print_indent();
print_key_segment(k);
@ -241,17 +253,13 @@ TOML_NAMESPACE_START
if (!skip_self)
{
if (!base::naked_newline())
{
base::print_newline();
base::print_newline(true);
}
print_pending_table_separator();
base::increase_indent();
base::print_indent();
impl::print_to_stream("["sv, base::stream());
print_key_path();
impl::print_to_stream("]"sv, base::stream());
base::print_newline();
pending_table_separator_ = true;
}
print(child_tbl);
@ -273,13 +281,12 @@ TOML_NAMESPACE_START
for (size_t i = 0; i < arr.size(); i++)
{
base::print_newline();
base::print_newline(true);
print_pending_table_separator();
base::print_indent();
impl::print_to_stream("[["sv, base::stream());
print_key_path();
impl::print_to_stream("]]"sv, base::stream());
base::print_newline(true);
pending_table_separator_ = true;
print(*reinterpret_cast<const table*>(&arr[i]));
}

View File

@ -751,7 +751,8 @@ TOML_NAMESPACE_START
else if constexpr (is_natively_one_of<T, int64_t>)
{
const double val = *ref_cast<double>();
if (static_cast<double>(static_cast<int64_t>(val)) == val)
if (impl::fpclassify(val) == fp_class::ok
&& static_cast<double>(static_cast<int64_t>(val)) == val)
return node_integer_cast<T>(static_cast<int64_t>(val));
else
return {};

View File

@ -437,5 +437,25 @@ c = 3)"sv;
CHECK(to_string(some_toml) == some_toml);
}
{
static constexpr auto some_toml = "[a]\n\n[b]\n\n[c]"sv;
CHECK(to_string(some_toml) == some_toml);
}
{
static constexpr auto some_toml = "[a]\nkey = 1\n\n[b]\n\n[c]"sv;
CHECK(to_string(some_toml) == some_toml);
}
{
static constexpr auto some_toml = "key = 1\n\n[a]\nkey = 1\n\n[b]\n\n[c]"sv;
CHECK(to_string(some_toml) == some_toml);
}
{
static constexpr auto some_toml = "key = 1\n\n[a]\nkey = 1\n\n[b]\n\n[[c]]\n\n[[c]]"sv;
CHECK(to_string(some_toml) == some_toml);
}
}

View File

@ -400,5 +400,67 @@ TEST_CASE("nodes - value() int/float/bool conversions")
CHECK_VALUE_FAIL(toml::date);
CHECK_VALUE_FAIL(toml::time);
CHECK_VALUE_FAIL(toml::date_time);
*val = std::numeric_limits<double>::infinity();
CHECK_VALUE_FAIL(bool);
CHECK_VALUE_FAIL(int8_t);
CHECK_VALUE_FAIL(uint8_t);
CHECK_VALUE_FAIL(int16_t);
CHECK_VALUE_FAIL(uint16_t);
CHECK_VALUE_FAIL(int32_t);
CHECK_VALUE_FAIL(uint32_t);
CHECK_VALUE_FAIL(int64_t);
CHECK_VALUE_FAIL(uint64_t);
CHECK_VALUE_PASS(float, std::numeric_limits<float>::infinity());
CHECK_VALUE_PASS(double, std::numeric_limits<double>::infinity());
CHECK_VALUE_FAIL(std::string);
CHECK_VALUE_FAIL(std::string_view);
CHECK_VALUE_FAIL(toml::date);
CHECK_VALUE_FAIL(toml::time);
CHECK_VALUE_FAIL(toml::date_time);
*val = -std::numeric_limits<double>::infinity();
CHECK_VALUE_FAIL(bool);
CHECK_VALUE_FAIL(int8_t);
CHECK_VALUE_FAIL(uint8_t);
CHECK_VALUE_FAIL(int16_t);
CHECK_VALUE_FAIL(uint16_t);
CHECK_VALUE_FAIL(int32_t);
CHECK_VALUE_FAIL(uint32_t);
CHECK_VALUE_FAIL(int64_t);
CHECK_VALUE_FAIL(uint64_t);
CHECK_VALUE_PASS(float, -std::numeric_limits<float>::infinity());
CHECK_VALUE_PASS(double, -std::numeric_limits<double>::infinity());
CHECK_VALUE_FAIL(std::string);
CHECK_VALUE_FAIL(std::string_view);
CHECK_VALUE_FAIL(toml::date);
CHECK_VALUE_FAIL(toml::time);
CHECK_VALUE_FAIL(toml::date_time);
*val = std::numeric_limits<double>::quiet_NaN();
CHECK_VALUE_FAIL(bool);
CHECK_VALUE_FAIL(int8_t);
CHECK_VALUE_FAIL(uint8_t);
CHECK_VALUE_FAIL(int16_t);
CHECK_VALUE_FAIL(uint16_t);
CHECK_VALUE_FAIL(int32_t);
CHECK_VALUE_FAIL(uint32_t);
CHECK_VALUE_FAIL(int64_t);
CHECK_VALUE_FAIL(uint64_t);
{
auto fval = n.value<float>();
REQUIRE(fval.has_value());
CHECK(impl::fpclassify(*fval) == impl::fp_class::nan);
}
{
auto fval = n.value<double>();
REQUIRE(fval.has_value());
CHECK(impl::fpclassify(*fval) == impl::fp_class::nan);
}
CHECK_VALUE_FAIL(std::string);
CHECK_VALUE_FAIL(std::string_view);
CHECK_VALUE_FAIL(toml::date);
CHECK_VALUE_FAIL(toml::time);
CHECK_VALUE_FAIL(toml::date_time);
}
}

View File

@ -142,5 +142,25 @@ TEST_CASE("user feedback")
using namespace toml::literals; // should compile without namespace ambiguity
auto table = "[table]\nkey=\"value\""_toml;
}
SECTION("github/pull/80") // https://github.com/marzer/tomlplusplus/pull/80
{
const auto data = R"(
a = { "key" = 1 } # inline table
b = [] # array value
[[c]] # array-of-tables with a single, empty table element
)"sv;
parsing_should_succeed(FILE_LINE_ARGS, data, [](auto&& table)
{
std::stringstream ss;
ss << table;
CHECK(ss.str() == R"(a = { key = 1 }
b = []
[[c]])"sv);
});
}
}

View File

@ -3224,7 +3224,8 @@ TOML_NAMESPACE_START
else if constexpr (is_natively_one_of<T, int64_t>)
{
const double val = *ref_cast<double>();
if (static_cast<double>(static_cast<int64_t>(val)) == val)
if (impl::fpclassify(val) == fp_class::ok
&& static_cast<double>(static_cast<int64_t>(val)) == val)
return node_integer_cast<T>(static_cast<int64_t>(val));
else
return {};
@ -6168,6 +6169,16 @@ TOML_NAMESPACE_START
private:
using base = impl::formatter<Char>;
std::vector<std::string> key_path;
bool pending_table_separator_ = false;
void print_pending_table_separator()
{
if (pending_table_separator_)
{
base::print_newline(true);
base::print_newline(true);
pending_table_separator_ = false;
}
}
void print_key_segment(const std::string& str)
{
@ -6291,6 +6302,7 @@ TOML_NAMESPACE_START
|| (type == node_type::array && is_non_inline_array_of_tables(v)))
continue;
pending_table_separator_ = true;
base::print_newline();
base::print_indent();
print_key_segment(k);
@ -6351,17 +6363,13 @@ TOML_NAMESPACE_START
if (!skip_self)
{
if (!base::naked_newline())
{
base::print_newline();
base::print_newline(true);
}
print_pending_table_separator();
base::increase_indent();
base::print_indent();
impl::print_to_stream("["sv, base::stream());
print_key_path();
impl::print_to_stream("]"sv, base::stream());
base::print_newline();
pending_table_separator_ = true;
}
print(child_tbl);
@ -6383,13 +6391,12 @@ TOML_NAMESPACE_START
for (size_t i = 0; i < arr.size(); i++)
{
base::print_newline();
base::print_newline(true);
print_pending_table_separator();
base::print_indent();
impl::print_to_stream("[["sv, base::stream());
print_key_path();
impl::print_to_stream("]]"sv, base::stream());
base::print_newline(true);
pending_table_separator_ = true;
print(*reinterpret_cast<const table*>(&arr[i]));
}