Skip to content

Commit

Permalink
fixed missing blank lines between consecutive empty tables/A-o-T
Browse files Browse the repository at this point in the history
also:
- added additional value() testsfor inf/nan
- added additional formatting tests
  • Loading branch information
marzer committed Jan 7, 2021
1 parent 00e080f commit 9be51e4
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 20 deletions.
25 changes: 16 additions & 9 deletions include/toml++/toml_default_formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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]));
}

Expand Down
3 changes: 2 additions & 1 deletion include/toml++/toml_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down
20 changes: 20 additions & 0 deletions tests/manipulating_tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

62 changes: 62 additions & 0 deletions tests/manipulating_values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
20 changes: 20 additions & 0 deletions tests/user_feedback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

}

27 changes: 17 additions & 10 deletions toml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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]));
}

Expand Down

0 comments on commit 9be51e4

Please sign in to comment.