Skip to content

Commit

Permalink
Add test cases for building document with empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Gei0r committed Jun 10, 2022
1 parent ff54931 commit 4a5eeb1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
89 changes: 89 additions & 0 deletions test/test_empty_scalar.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>
#include <string>

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

0 comments on commit 4a5eeb1

Please sign in to comment.