From f82861001a6535626e3f695aa0f96e720fb33a45 Mon Sep 17 00:00:00 2001 From: butataatawa Date: Mon, 2 Jan 2017 22:44:22 +0100 Subject: [PATCH] Fix sequence sometimes not turning into a map (#450) Previously, just referencing the next element in the sequence (and so constructing it, as an undefined element) would allow you to skip defining an element without turning the sequence into a map. E.g: node[0] = "foo"; // sequence of size 1 node[1]; // sequence of size 1, with an undefined element at 1 node[2] = "bar"; // FIX: should be map of size 2 (since there's no element at index 1) --- include/yaml-cpp/node/detail/impl.h | 2 +- test/node/node_test.cpp | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/yaml-cpp/node/detail/impl.h b/include/yaml-cpp/node/detail/impl.h index e22bc70..5931826 100644 --- a/include/yaml-cpp/node/detail/impl.h +++ b/include/yaml-cpp/node/detail/impl.h @@ -32,7 +32,7 @@ struct get_idx& sequence, const Key& key, shared_memory_holder pMemory) { - if (key > sequence.size()) + if (key > sequence.size() || (key > 0 && !sequence[key-1]->is_defined())) return 0; if (key == sequence.size()) sequence.push_back(&pMemory->create_node()); diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index b5ba155..485ad09 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -88,11 +88,22 @@ TEST(NodeTest, MapWithUndefinedValues) { EXPECT_EQ(2, node.size()); } +TEST(NodeTest, SeqIntoMap) { + Node node; + node[0] = "test"; + node[1]; + node[2] = "value"; + EXPECT_TRUE(node.IsMap()); + EXPECT_EQ("test", node[0].as()); + EXPECT_EQ("value", node[2].as()); + EXPECT_EQ(2, node.size()); +} + TEST(NodeTest, RemoveUnassignedNode) { Node node(NodeType::Map); node["key"]; node.remove("key"); - EXPECT_EQ(node.size(), 0); + EXPECT_EQ(0, node.size()); } TEST(NodeTest, MapForceInsert) {