diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a6b5a44a0..1ecbd2fc2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -75,6 +75,7 @@ ryml_add_test(yaml_events) ryml_add_test_case_group(empty_file) ryml_add_test_case_group(empty_map) ryml_add_test_case_group(empty_seq) +ryml_add_test_case_group(empty_scalar) ryml_add_test_case_group(simple_doc) ryml_add_test_case_group(simple_map) ryml_add_test_case_group(simple_seq) diff --git a/test/test_empty_scalar.cpp b/test/test_empty_scalar.cpp new file mode 100644 index 000000000..5e35d8247 --- /dev/null +++ b/test/test_empty_scalar.cpp @@ -0,0 +1,89 @@ +#include "./test_group.hpp" +#include "c4/error.hpp" +#include "c4/yml/node.hpp" +#include "c4/yml/parse.hpp" +#include "c4/yml/tree.hpp" +#include +#include + +namespace c4 { +namespace yml { + +/** + See also issue 263. + */ + +C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wuseless-cast") + +constexpr const NodeType_e DQV = (NodeType_e)(DOC | QV); + +TEST(empty_scalar, parse_zero_length_strings) +{ + char inp[] = R"( +- "" +- '' +- > +- | +)"; + + Tree tr = parse_in_place(inp); + + ASSERT_EQ(tr.rootref().num_children(), 4); + for(const auto &child : tr.rootref().children()) + { + ASSERT_EQ(child.type(), QV); + ASSERT_EQ(child.val(), ""); + ASSERT_FALSE(child.val_is_null()); + } +} + +TEST(empty_scalar, build_zero_length_string) +{ + Tree tr; + NodeRef root = tr.rootref(); + root |= MAP; + + NodeRef non_quoted = root["non_quoted"]; + non_quoted |= SEQ; + + const std::string e; // empty std::string + + non_quoted.append_child() << ""; + non_quoted.append_child() = ""; + non_quoted.append_child() << e; + + NodeRef quoted = root["quoted"]; + quoted |= SEQ; + {auto r = quoted.append_child(); r << ""; r.set_type(r.type() | VALQUO);} + {auto r = quoted.append_child(); r = ""; r.set_type(r.type() | VALQUO);} + {auto r = quoted.append_child(); r << e; r.set_type(r.type() | VALQUO);} + + ASSERT_EQ(non_quoted.num_children(), 3); + for(const auto &child : non_quoted.children()) + { + EXPECT_EQ(child.type(), VAL); + EXPECT_EQ(child.val(), ""); + EXPECT_FALSE(child.val_is_null()); + } + + ASSERT_EQ(quoted.num_children(), 3); + for(const auto &child : quoted.children()) + { + EXPECT_EQ(child.type(), VAL | VALQUO); + EXPECT_EQ(child.val(), ""); + EXPECT_FALSE(child.val_is_null()); + } +} + +CASE_GROUP(EMPTY_SCALAR) +{ +ADD_CASE_TO_GROUP("empty scalar, single quoted", + "''", + N(DQV, "") +); +} + +} // namespace yml +} // namespace c4 + +C4_SUPPRESS_WARNING_GCC_POP