Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test cases for serializing empty collection nodes #457

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions include/fkYAML/detail/output/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,23 @@ class basic_serializer {
str += " ";
serialize_node(seq_item, cur_indent, str);
str += "\n";
continue;
}

const bool is_empty = seq_item.empty();
if (!is_empty) {
str += "\n";
serialize_node(seq_item, cur_indent + 2, str);
continue;
}
else if (seq_item.is_sequence() && seq_item.size() == 0) {

// an empty sequence or mapping
if (seq_item.is_sequence()) {
str += " []\n";
}
else if (seq_item.is_mapping() && seq_item.size() == 0) {
else /*seq_item.is_mapping()*/ {
str += " {}\n";
}
else {
str += "\n";
serialize_node(seq_item, cur_indent + 2, str);
}
}
break;
case node_type::MAPPING:
Expand Down Expand Up @@ -212,17 +218,23 @@ class basic_serializer {
str += " ";
serialize_node(*itr, cur_indent, str);
str += "\n";
continue;
}

const bool is_empty = itr->empty();
if (!is_empty) {
str += "\n";
serialize_node(*itr, cur_indent + 2, str);
continue;
}
else if (itr->is_sequence() && itr->size() == 0) {

// an empty sequence or mapping
if (itr->is_sequence()) {
str += " []\n";
}
else if (itr->is_mapping() && itr->size() == 0) {
else /*itr->is_mapping()*/ {
str += " {}\n";
}
else {
str += "\n";
serialize_node(*itr, cur_indent + 2, str);
}
}
break;
case node_type::NULL_OBJECT:
Expand Down
36 changes: 24 additions & 12 deletions single_include/fkYAML/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10529,17 +10529,23 @@ class basic_serializer {
str += " ";
serialize_node(seq_item, cur_indent, str);
str += "\n";
continue;
}

const bool is_empty = seq_item.empty();
if (!is_empty) {
str += "\n";
serialize_node(seq_item, cur_indent + 2, str);
continue;
}
else if (seq_item.is_sequence() && seq_item.size() == 0) {

// an empty sequence or mapping
if (seq_item.is_sequence()) {
str += " []\n";
}
else if (seq_item.is_mapping() && seq_item.size() == 0) {
else /*seq_item.is_mapping()*/ {
str += " {}\n";
}
else {
str += "\n";
serialize_node(seq_item, cur_indent + 2, str);
}
}
break;
case node_type::MAPPING:
Expand Down Expand Up @@ -10586,17 +10592,23 @@ class basic_serializer {
str += " ";
serialize_node(*itr, cur_indent, str);
str += "\n";
continue;
}

const bool is_empty = itr->empty();
if (!is_empty) {
str += "\n";
serialize_node(*itr, cur_indent + 2, str);
continue;
}
else if (itr->is_sequence() && itr->size() == 0) {

// an empty sequence or mapping
if (itr->is_sequence()) {
str += " []\n";
}
else if (itr->is_mapping() && itr->size() == 0) {
else /*itr->is_mapping()*/ {
str += " {}\n";
}
else {
str += "\n";
serialize_node(*itr, cur_indent + 2, str);
}
}
break;
case node_type::NULL_OBJECT:
Expand Down
30 changes: 30 additions & 0 deletions tests/unit_test/test_serializer_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,36 @@ TEST_CASE("Serializer_MappingNode") {
REQUIRE(serializer.serialize(node_str_pair.first) == node_str_pair.second);
}

TEST_CASE("Serializer_EmptyCollectionNode") {
auto seq = fkyaml::node::sequence();
auto map = fkyaml::node::mapping();
fkyaml::detail::basic_serializer<fkyaml::node> serializer;

SECTION("child sequence item is an empty sequence node") {
seq.get_value_ref<fkyaml::node::sequence_type&>().emplace_back(fkyaml::node::sequence());
std::string expected = "- []\n";
REQUIRE(serializer.serialize(seq) == expected);
}

SECTION("child sequence item is an empty mapping node") {
seq.get_value_ref<fkyaml::node::sequence_type&>().emplace_back(fkyaml::node::mapping());
std::string expected = "- {}\n";
REQUIRE(serializer.serialize(seq) == expected);
}

SECTION("mapping value is an empty sequence node") {
map["foo"] = seq;
std::string expected = "foo: []\n";
REQUIRE(serializer.serialize(map) == expected);
}

SECTION("mapping value is an empty mapping node") {
map["foo"] = fkyaml::node::mapping();
std::string expected = "foo: {}\n";
REQUIRE(serializer.serialize(map) == expected);
}
}

TEST_CASE("Serializer_NullNode") {
fkyaml::detail::basic_serializer<fkyaml::node> serializer;
fkyaml::node node;
Expand Down
Loading