Skip to content

Commit

Permalink
Merge 6cdf281 into d949136
Browse files Browse the repository at this point in the history
  • Loading branch information
fktn-k committed May 3, 2024
2 parents d949136 + 6cdf281 commit 6c68c36
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/fkYAML/detail/input/deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ class basic_deserializer {
mp_current_node->template get_value_ref<sequence_type&>().emplace_back(node_type::sequence());
mp_current_node = &(mp_current_node->template get_value_ref<sequence_type&>().back());
m_context_stack.emplace_back(line, indent, context_state_t::BLOCK_SEQUENCE, mp_current_node);
apply_directive_set(*mp_current_node);
apply_node_properties(*mp_current_node);
break;
}

Expand Down
2 changes: 2 additions & 0 deletions single_include/fkYAML/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4560,6 +4560,8 @@ class basic_deserializer {
mp_current_node->template get_value_ref<sequence_type&>().emplace_back(node_type::sequence());
mp_current_node = &(mp_current_node->template get_value_ref<sequence_type&>().back());
m_context_stack.emplace_back(line, indent, context_state_t::BLOCK_SEQUENCE, mp_current_node);
apply_directive_set(*mp_current_node);
apply_node_properties(*mp_current_node);
break;
}

Expand Down
99 changes: 99 additions & 0 deletions test/unit_test/test_deserializer_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@ TEST_CASE("Deserializer_BlockSequence") {
REQUIRE(test_1_node.get_value_ref<std::string&>() == "bar");
}

SECTION("child block sequence whose prefixes are put as indentation") {
REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter("test:\n- foo\n- 123")));

REQUIRE(root.is_mapping());
REQUIRE(root.size() == 1);
REQUIRE(root.contains("test"));

fkyaml::node& root_test_node = root["test"];
REQUIRE(root_test_node.is_sequence());
REQUIRE(root_test_node.size() == 2);

fkyaml::node& root_test_0_node = root_test_node[0];
REQUIRE(root_test_0_node.is_string());
REQUIRE(root_test_0_node.get_value_ref<std::string&>() == "foo");

fkyaml::node& root_test_1_node = root_test_node[1];
REQUIRE(root_test_1_node.is_integer());
REQUIRE(root_test_1_node.get_value<int>() == 123);
}

SECTION("block sequence with nested mappings") {
REQUIRE_NOTHROW(
root = deserializer.deserialize(
Expand Down Expand Up @@ -1633,6 +1653,32 @@ TEST_CASE("Deserializer_Anchor") {
REQUIRE(foo_foo_node.get_value<int>() == 123);
}

SECTION("parse anchored child block sequence") {
std::string input = "test: &anchor\n"
"- foo\n"
"- 123";
REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter(input)));

REQUIRE(root.is_mapping());
REQUIRE(root.size() == 1);
REQUIRE(root.contains("test"));

fkyaml::node& test_node = root["test"];
REQUIRE(test_node.is_anchor());
REQUIRE(test_node.has_anchor_name());
REQUIRE(test_node.get_anchor_name() == "anchor");
REQUIRE(test_node.is_sequence());
REQUIRE(test_node.size() == 2);

fkyaml::node& test_0_node = test_node[0];
REQUIRE(test_0_node.is_string());
REQUIRE(test_0_node.get_value_ref<std::string&>() == "foo");

fkyaml::node& test_1_node = test_node[1];
REQUIRE(test_1_node.is_integer());
REQUIRE(test_1_node.get_value<int>() == 123);
}

SECTION("multiple anchors specified") {
auto input =
GENERATE(std::string("foo: &anchor &anchor2\n bar: baz"), std::string("&anchor &anchor2 foo: bar"));
Expand Down Expand Up @@ -1770,6 +1816,59 @@ TEST_CASE("Deserializer_Tag") {
REQUIRE(seq_flow_1_node.get_value<float>() == 3.14f);
}

SECTION("valid tags for block sequence/mapping") {
std::string input = "seq: !!seq\n"
"- !!bool true\n"
"- !!seq\n"
" - !!str true\n"
"map: !!map\n"
" foo: bar";
REQUIRE_NOTHROW(root = deserializer.deserialize(fkyaml::detail::input_adapter(input)));

REQUIRE(root.is_mapping());
REQUIRE(root.size() == 2);
REQUIRE(root.contains("seq"));
REQUIRE(root.contains("map"));

fkyaml::node& seq_node = root["seq"];
REQUIRE(seq_node.has_tag_name());
REQUIRE(seq_node.get_tag_name() == "!!seq");
REQUIRE(seq_node.is_sequence());
REQUIRE(seq_node.size() == 2);

fkyaml::node& seq_0_node = seq_node[0];
REQUIRE(seq_0_node.has_tag_name());
REQUIRE(seq_0_node.get_tag_name() == "!!bool");
REQUIRE(seq_0_node.is_boolean());
REQUIRE(seq_0_node.get_value<bool>() == true);

fkyaml::node& seq_1_node = seq_node[1];
REQUIRE(seq_1_node.has_tag_name());
REQUIRE(seq_1_node.get_tag_name() == "!!seq");
REQUIRE(seq_1_node.is_sequence());
REQUIRE(seq_1_node.size() == 1);

fkyaml::node& seq_1_0_node = seq_1_node[0];
REQUIRE(seq_1_0_node.has_tag_name());
REQUIRE(seq_1_0_node.get_tag_name() == "!!str");
REQUIRE(seq_1_0_node.is_string());
REQUIRE(seq_1_0_node.get_value_ref<std::string&>() == "true");

fkyaml::node& map_node = root["map"];
REQUIRE(map_node.has_tag_name());
REQUIRE(map_node.get_tag_name() == "!!map");
REQUIRE(map_node.is_mapping());
REQUIRE(map_node.size() == 1);
REQUIRE(map_node.contains("foo"));

// Make sure that the !!map tag is not applied to the child mapping key.
REQUIRE_FALSE(map_node.begin().value().has_tag_name());

fkyaml::node& map_foo_node = map_node["foo"];
REQUIRE(map_foo_node.is_string());
REQUIRE(map_foo_node.get_value_ref<std::string&>() == "bar");
}

SECTION("specify tags using TAG directives") {
std::string input = "%TAG !e! tag:example.com,2000:app/\n"
"---\n"
Expand Down

0 comments on commit 6c68c36

Please sign in to comment.