Fix some Node::operator[] regressions from 0.5.1
authorPaul Novotny <paul@paulnovo.us>
Wed, 12 Oct 2016 23:48:11 +0000 (00:48 +0100)
committerPaul Novotny <paul@paulnovo.us>
Wed, 12 Oct 2016 23:48:11 +0000 (00:48 +0100)
Bug-Debian: https://bugs.debian.org/835417
Origin: upstream, https://github.com/jbeder/yaml-cpp/commit/b426fafff
Last-Update: 2016-10-11

Gbp-Pq: Name backport-b426fafff.patch

include/yaml-cpp/node/impl.h
test/node/node_test.cpp

index 164c533dfbc35aa2c55bc6587de368c006649c56..9cedfa40f4036c265f2f28da2ab83e0e8038c72d 100644 (file)
@@ -142,7 +142,7 @@ inline const T Node::as() const {
 template <typename T, typename S>
 inline const T Node::as(const S& fallback) const {
   if (!m_isValid)
-    throw InvalidNode();
+    return fallback;
   return as_if<T, S>(*this)(fallback);
 }
 
@@ -275,26 +275,26 @@ inline std::size_t Node::size() const {
 
 inline const_iterator Node::begin() const {
   if (!m_isValid)
-    throw InvalidNode();
+    return const_iterator();
   return m_pNode ? const_iterator(m_pNode->begin(), m_pMemory)
                  : const_iterator();
 }
 
 inline iterator Node::begin() {
   if (!m_isValid)
-    throw InvalidNode();
+    return iterator();
   return m_pNode ? iterator(m_pNode->begin(), m_pMemory) : iterator();
 }
 
 inline const_iterator Node::end() const {
   if (!m_isValid)
-    throw InvalidNode();
+    return const_iterator();
   return m_pNode ? const_iterator(m_pNode->end(), m_pMemory) : const_iterator();
 }
 
 inline iterator Node::end() {
   if (!m_isValid)
-    throw InvalidNode();
+    return iterator();
   return m_pNode ? iterator(m_pNode->end(), m_pMemory) : iterator();
 }
 
index f54fcc9dbd8143bef6c1eb12de981e2095b94810..5ab4704fb2492e5f037192e6065b48cc1b925451 100644 (file)
@@ -73,6 +73,12 @@ TEST(NodeTest, MapWithUndefinedValues) {
   EXPECT_EQ(2, node.size());
 }
 
+TEST(NodeTest, UndefinedConstNodeWithFallback) {
+  Node node;
+  const Node& cn = node;
+  EXPECT_EQ(cn["undefined"].as<int>(3), 3);
+}
+
 TEST(NodeTest, MapIteratorWithUndefinedValues) {
   Node node;
   node["key"] = "value";
@@ -84,6 +90,32 @@ TEST(NodeTest, MapIteratorWithUndefinedValues) {
   EXPECT_EQ(1, count);
 }
 
+TEST(NodeTest, ConstIteratorOnConstUndefinedNode) {
+  Node node;
+  const Node& cn = node;
+  const Node& undefinedCn = cn["undefined"];
+
+  std::size_t count = 0;
+  for (const_iterator it = undefinedCn.begin(); it != undefinedCn.end(); ++it) {
+    count++;
+ }
+  EXPECT_EQ(0, count);
+}
+
+TEST(NodeTest, IteratorOnConstUndefinedNode) {
+  Node node;
+  const Node& cn = node;
+  const Node& undefinedCn = cn["undefined"];
+
+  Node& nonConstUndefinedNode = const_cast<Node&>(undefinedCn);
+
+  std::size_t count = 0;
+  for (iterator it = nonConstUndefinedNode.begin(); it != nonConstUndefinedNode.end(); ++it) {
+    count++;
+  }
+  EXPECT_EQ(0, count);
+}
+
 TEST(NodeTest, SimpleSubkeys) {
   Node node;
   node["device"]["udid"] = "12345";