From 12f1164bfc3768940d3fb277c4f35e710934f9df Mon Sep 17 00:00:00 2001 From: Vincent Cogne Date: Thu, 9 Jun 2016 11:17:39 +0200 Subject: [PATCH] Apply code review changes --- include/yaml-cpp/node/convert.h | 26 +++++++++++++------------- test/node/node_test.cpp | 26 ++++++++++++++++++-------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index 2db270f..eae692a 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -7,12 +7,12 @@ #pragma once #endif +#include #include #include #include #include #include -#include #include "yaml-cpp/binary.h" #include "yaml-cpp/node/impl.h" @@ -243,29 +243,29 @@ struct convert > { }; // std::array -template -struct convert > { - static Node encode(const std::array& rhs) { +template +struct convert> { + static Node encode(const std::array& rhs) { Node node(NodeType::Sequence); - for (typename std::array::const_iterator it = rhs.begin(); - it != rhs.end(); ++it) - node.push_back(*it); + for (auto element : rhs) { + node.push_back(element); + } return node; } - static bool decode(const Node& node, std::array& rhs) { + static bool decode(const Node& node, std::array& rhs) { if (!node.IsSequence()) return false; + if (node.size() != N) + return false; - std::size_t index = 0; - for (const_iterator it = node.begin(); it != node.end(); ++it) { + for (auto i = 0u; i < node.size(); ++i) { #if defined(__GNUC__) && __GNUC__ < 4 // workaround for GCC 3: - rhs.at(index) = it->template as(); + rhs[i] = node[i].template as(); #else - rhs.at(index) = it->as(); + rhs[i] = node[i].as(); #endif - ++index; } return true; } diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index 5825c04..2202f72 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -12,6 +12,14 @@ using ::testing::AnyOf; using ::testing::Eq; +#define EXPECT_THROW_REPRESENTATION_EXCEPTION(statement, message) \ + ASSERT_THROW(statement, RepresentationException); \ + try { \ + statement; \ + } catch (const RepresentationException& e) { \ + EXPECT_EQ(e.msg, message); \ + } + namespace YAML { namespace { TEST(NodeTest, SimpleScalar) { @@ -155,19 +163,21 @@ TEST(NodeTest, SimpleSubkeys) { } TEST(NodeTest, StdArray) { - std::array evens; - evens[0] = 2; - evens[1] = 4; - evens[2] = 6; - evens[3] = 8; - evens[4] = 10; - + std::array evens {{ 2, 4, 6, 8, 10 }}; Node node; node["evens"] = evens; - std::array actualEvens = node["evens"].as >(); + std::array actualEvens = node["evens"].as>(); EXPECT_EQ(evens, actualEvens); } +TEST(NodeTest, StdArrayWrongSize) { + std::array evens {{ 2, 4, 6 }}; + Node node; + node["evens"] = evens; + EXPECT_THROW_REPRESENTATION_EXCEPTION((node["evens"].as>()), + ErrorMsg::BAD_CONVERSION); +} + TEST(NodeTest, StdVector) { std::vector primes; primes.push_back(2);