From 72f699f5ce2d22a8f70cd59d73ce2fbb42dd960e Mon Sep 17 00:00:00 2001 From: "Igor [hyperxor]" <56217938+hyperxor@users.noreply.github.com> Date: Mon, 4 Nov 2019 19:19:02 +0300 Subject: [PATCH] Remove redundant checks and add more unit tests (#783) --- include/yaml-cpp/node/impl.h | 34 +--------------------------------- test/node/node_test.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index f2f1f69..06a4884 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -172,8 +172,6 @@ inline const std::string& Node::Tag() const { } inline void Node::SetTag(const std::string& tag) { - if (!m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); m_pNode->set_tag(tag); } @@ -185,8 +183,6 @@ inline EmitterStyle::value Node::Style() const { } inline void Node::SetStyle(EmitterStyle::value style) { - if (!m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); m_pNode->set_style(style); } @@ -202,15 +198,11 @@ inline bool Node::is(const Node& rhs) const { template inline Node& Node::operator=(const T& rhs) { - if (!m_isValid) - throw InvalidNode(m_invalidKey); Assign(rhs); return *this; } inline Node& Node::operator=(const Node& rhs) { - if (!m_isValid || !rhs.m_isValid) - throw InvalidNode(m_invalidKey); if (is(rhs)) return *this; AssignNode(rhs); @@ -233,29 +225,21 @@ inline void Node::Assign(const T& rhs) { template <> inline void Node::Assign(const std::string& rhs) { - if (!m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); m_pNode->set_scalar(rhs); } inline void Node::Assign(const char* rhs) { - if (!m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); m_pNode->set_scalar(rhs); } inline void Node::Assign(char* rhs) { - if (!m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); m_pNode->set_scalar(rhs); } inline void Node::AssignData(const Node& rhs) { - if (!m_isValid || !rhs.m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); rhs.EnsureNodeExists(); @@ -264,7 +248,7 @@ inline void Node::AssignData(const Node& rhs) { } inline void Node::AssignNode(const Node& rhs) { - if (!m_isValid || !rhs.m_isValid) + if (!m_isValid) throw InvalidNode(m_invalidKey); rhs.EnsureNodeExists(); @@ -320,8 +304,6 @@ inline void Node::push_back(const T& rhs) { } inline void Node::push_back(const Node& rhs) { - if (!m_isValid || !rhs.m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); rhs.EnsureNodeExists(); @@ -382,8 +364,6 @@ std::string key_to_string(const Key& key) { // indexing template inline const Node Node::operator[](const Key& key) const { - if (!m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); detail::node* value = static_cast(*m_pNode).get( detail::to_value(key), m_pMemory); @@ -395,8 +375,6 @@ inline const Node Node::operator[](const Key& key) const { template inline Node Node::operator[](const Key& key) { - if (!m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); detail::node& value = m_pNode->get(detail::to_value(key), m_pMemory); return Node(value, m_pMemory); @@ -404,15 +382,11 @@ inline Node Node::operator[](const Key& key) { template inline bool Node::remove(const Key& key) { - if (!m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); return m_pNode->remove(detail::to_value(key), m_pMemory); } inline const Node Node::operator[](const Node& key) const { - if (!m_isValid || !key.m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); key.EnsureNodeExists(); m_pMemory->merge(*key.m_pMemory); @@ -425,8 +399,6 @@ inline const Node Node::operator[](const Node& key) const { } inline Node Node::operator[](const Node& key) { - if (!m_isValid || !key.m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); key.EnsureNodeExists(); m_pMemory->merge(*key.m_pMemory); @@ -435,8 +407,6 @@ inline Node Node::operator[](const Node& key) { } inline bool Node::remove(const Node& key) { - if (!m_isValid || !key.m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); key.EnsureNodeExists(); return m_pNode->remove(*key.m_pNode, m_pMemory); @@ -445,8 +415,6 @@ inline bool Node::remove(const Node& key) { // map template inline void Node::force_insert(const Key& key, const Value& value) { - if (!m_isValid) - throw InvalidNode(m_invalidKey); EnsureNodeExists(); m_pNode->force_insert(detail::to_value(key), detail::to_value(value), m_pMemory); diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index 18234db..0677ad5 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -59,6 +59,18 @@ TEST(NodeTest, SequenceElementRemoval) { EXPECT_EQ("c", node[1].as()); } +TEST(NodeTest, SequenceFirstElementRemoval) { + Node node; + node[0] = "a"; + node[1] = "b"; + node[2] = "c"; + node.remove(0); + EXPECT_TRUE(node.IsSequence()); + EXPECT_EQ(2, node.size()); + EXPECT_EQ("b", node[0].as()); + EXPECT_EQ("c", node[1].as()); +} + TEST(NodeTest, SequenceLastElementRemoval) { Node node; node[0] = "a"; @@ -71,6 +83,19 @@ TEST(NodeTest, SequenceLastElementRemoval) { EXPECT_EQ("b", node[1].as()); } +TEST(NodeTest, NodeAssignment) { + Node node1; + Node node2; + node1[1] = 1; + node1[2] = 2; + node1[3] = 3; + node2 = node1; + EXPECT_EQ(node1, node2); + EXPECT_EQ(node1[1], node2[1]); + EXPECT_EQ(node1[2], node2[2]); + EXPECT_EQ(node1[3], node2[3]); +} + TEST(NodeTest, MapElementRemoval) { Node node; node["foo"] = "bar";