diff --git a/.gitignore b/.gitignore index 567609b..0e29154 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build/ +/tags diff --git a/CMakeLists.txt b/CMakeLists.txt index d2d8810..154230a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,26 +1,11 @@ ### ### CMake settings ### -## Due to Mac OSX we need to keep compatibility with CMake 2.6 # see http://www.cmake.org/Wiki/CMake_Policies -cmake_minimum_required(VERSION 2.6) -# see http://www.cmake.org/cmake/help/cmake-2-8-docs.html#policy:CMP0012 -if(POLICY CMP0012) - cmake_policy(SET CMP0012 OLD) -endif() -# see http://www.cmake.org/cmake/help/cmake-2-8-docs.html#policy:CMP0015 -if(POLICY CMP0015) - cmake_policy(SET CMP0015 OLD) -endif() -# see https://cmake.org/cmake/help/latest/policy/CMP0042.html -if(POLICY CMP0042) - # Enable MACOSX_RPATH by default. - cmake_policy(SET CMP0042 NEW) -endif() +cmake_minimum_required(VERSION 3.1) include(CheckCXXCompilerFlag) - ### ### Project settings ### @@ -31,8 +16,6 @@ set(YAML_CPP_VERSION_MINOR "6") set(YAML_CPP_VERSION_PATCH "2") set(YAML_CPP_VERSION "${YAML_CPP_VERSION_MAJOR}.${YAML_CPP_VERSION_MINOR}.${YAML_CPP_VERSION_PATCH}") -enable_testing() - ### ### Project options @@ -48,6 +31,10 @@ option(YAML_CPP_BUILD_CONTRIB "Enable contrib stuff in library" ON) # http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_library option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF) +# Set minimum C++ to 2011 standards +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + # --> Apple option(APPLE_UNIVERSAL_BIN "Apple: Build universal binary" OFF) @@ -116,9 +103,10 @@ if(VERBOSE) message(STATUS "contrib_private_headers: ${contrib_private_headers}") endif() -include_directories(${YAML_CPP_SOURCE_DIR}/src) -include_directories(${YAML_CPP_SOURCE_DIR}/include) - +if (CMAKE_VERSION VERSION_LESS 2.8.12) + include_directories(${YAML_CPP_SOURCE_DIR}/src) + include_directories(${YAML_CPP_SOURCE_DIR}/include) +endif() ### @@ -187,7 +175,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR set(GCC_EXTRA_OPTIONS "${GCC_EXTRA_OPTIONS} ${FLAG_TESTED}") endif() # - set(yaml_cxx_flags "-Wall ${GCC_EXTRA_OPTIONS} -pedantic -Wno-long-long -std=c++11 ${yaml_cxx_flags}") + set(yaml_cxx_flags "-Wall ${GCC_EXTRA_OPTIONS} -pedantic -Wno-long-long ${yaml_cxx_flags}") ### Make specific if(${CMAKE_BUILD_TOOL} MATCHES make OR ${CMAKE_BUILD_TOOL} MATCHES gmake) @@ -275,6 +263,14 @@ set(_INSTALL_DESTINATIONS ### Library ### add_library(yaml-cpp ${library_sources}) + +if (NOT CMAKE_VERSION VERSION_LESS 2.8.12) + target_include_directories(yaml-cpp + PUBLIC $ + $ + PRIVATE $) +endif() + set_target_properties(yaml-cpp PROPERTIES COMPILE_FLAGS "${yaml_c_flags} ${yaml_cxx_flags}" ) @@ -351,6 +347,7 @@ endif() ### Extras ### if(YAML_CPP_BUILD_TESTS) + enable_testing() add_subdirectory(test) endif() if(YAML_CPP_BUILD_TOOLS) diff --git a/include/yaml-cpp/exceptions.h b/include/yaml-cpp/exceptions.h index 9c96859..87b92f5 100644 --- a/include/yaml-cpp/exceptions.h +++ b/include/yaml-cpp/exceptions.h @@ -15,7 +15,7 @@ // This is here for compatibility with older versions of Visual Studio // which don't support noexcept -#ifdef _MSC_VER +#if defined(_MSC_VER) && _MSC_VER < 1900 #define YAML_CPP_NOEXCEPT _NOEXCEPT #else #define YAML_CPP_NOEXCEPT noexcept diff --git a/include/yaml-cpp/node/detail/impl.h b/include/yaml-cpp/node/detail/impl.h index 09e55f8..c8853cf 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() || (key > 0 && !sequence[key-1]->is_defined())) + if (key > sequence.size() || (key > 0 && !sequence[key - 1]->is_defined())) return 0; if (key == sequence.size()) sequence.push_back(&pMemory->create_node()); @@ -56,6 +56,37 @@ struct get_idx::value>::type> { } }; +template +struct remove_idx { + static bool remove(std::vector&, const Key&) { return false; } +}; + +template +struct remove_idx< + Key, typename std::enable_if::value && + !std::is_same::value>::type> { + + static bool remove(std::vector& sequence, const Key& key) { + if (key >= sequence.size()) { + return false; + } else { + sequence.erase(sequence.begin() + key); + return true; + } + } +}; + +template +struct remove_idx::value>::type> { + + static bool remove(std::vector& sequence, const Key& key) { + return key >= 0 ? remove_idx::remove( + sequence, static_cast(key)) + : false; + } +}; + template inline bool node::equals(const T& rhs, shared_memory_holder pMemory) { T lhs; @@ -129,21 +160,23 @@ inline node& node_data::get(const Key& key, shared_memory_holder pMemory) { template inline bool node_data::remove(const Key& key, shared_memory_holder pMemory) { - if (m_type != NodeType::Map) - return false; + if (m_type == NodeType::Sequence) { + return remove_idx::remove(m_sequence, key); + } else if (m_type == NodeType::Map) { + kv_pairs::iterator it = m_undefinedPairs.begin(); + while (it != m_undefinedPairs.end()) { + kv_pairs::iterator jt = std::next(it); + if (it->first->equals(key, pMemory)) { + m_undefinedPairs.erase(it); + } + it = jt; + } - for (kv_pairs::iterator it = m_undefinedPairs.begin(); - it != m_undefinedPairs.end();) { - kv_pairs::iterator jt = std::next(it); - if (it->first->equals(key, pMemory)) - m_undefinedPairs.erase(it); - it = jt; - } - - for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) { - if (it->first->equals(key, pMemory)) { - m_map.erase(it); - return true; + for (node_map::iterator iter = m_map.begin(); iter != m_map.end(); ++iter) { + if (iter->first->equals(key, pMemory)) { + m_map.erase(iter); + return true; + } } } diff --git a/include/yaml-cpp/node/detail/iterator.h b/include/yaml-cpp/node/detail/iterator.h index deec8fb..966107d 100644 --- a/include/yaml-cpp/node/detail/iterator.h +++ b/include/yaml-cpp/node/detail/iterator.h @@ -8,19 +8,19 @@ #endif #include "yaml-cpp/dll.h" +#include "yaml-cpp/node/detail/node_iterator.h" #include "yaml-cpp/node/node.h" #include "yaml-cpp/node/ptr.h" -#include "yaml-cpp/node/detail/node_iterator.h" #include #include + namespace YAML { namespace detail { struct iterator_value; template -class iterator_base : public std::iterator { +class iterator_base { private: template @@ -37,7 +37,11 @@ class iterator_base : public std::iteratorscalar() : detail::node_data::empty_scalar; + return m_pNode ? m_pNode->scalar() : detail::node_data::empty_scalar(); } inline const std::string& Node::Tag() const { if (!m_isValid) throw InvalidNode(); - return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar; + return m_pNode ? m_pNode->tag() : detail::node_data::empty_scalar(); } inline void Node::SetTag(const std::string& tag) { diff --git a/src/binary.cpp b/src/binary.cpp index a7e5130..4db6d0b 100644 --- a/src/binary.cpp +++ b/src/binary.cpp @@ -1,5 +1,7 @@ #include "yaml-cpp/binary.h" +#include + namespace YAML { static const char encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -72,19 +74,24 @@ std::vector DecodeBase64(const std::string &input) { unsigned char *out = &ret[0]; unsigned value = 0; - for (std::size_t i = 0; i < input.size(); i++) { + for (std::size_t i = 0, cnt = 0; i < input.size(); i++) { + if (std::isspace(input[i])) { + // skip newlines + continue; + } unsigned char d = decoding[static_cast(input[i])]; if (d == 255) return ret_type(); value = (value << 6) | d; - if (i % 4 == 3) { + if (cnt % 4 == 3) { *out++ = value >> 16; if (i > 0 && input[i - 1] != '=') *out++ = value >> 8; if (input[i] != '=') *out++ = value; } + ++cnt; } ret.resize(out - &ret[0]); diff --git a/src/collectionstack.h b/src/collectionstack.h index 2302786..ebdc587 100644 --- a/src/collectionstack.h +++ b/src/collectionstack.h @@ -28,6 +28,7 @@ class CollectionStack { } void PopCollectionType(CollectionType::value type) { assert(type == GetCurCollectionType()); + (void)type; collectionStack.pop(); } diff --git a/src/contrib/graphbuilder.cpp b/src/contrib/graphbuilder.cpp index 416c135..bf25162 100644 --- a/src/contrib/graphbuilder.cpp +++ b/src/contrib/graphbuilder.cpp @@ -11,7 +11,7 @@ void* BuildGraphOfNextDocument(Parser& parser, if (parser.HandleNextDocument(eventHandler)) { return eventHandler.RootNode(); } else { - return NULL; + return nullptr; } } } diff --git a/src/contrib/graphbuilderadapter.cpp b/src/contrib/graphbuilderadapter.cpp index 02a3d97..b9e0b65 100644 --- a/src/contrib/graphbuilderadapter.cpp +++ b/src/contrib/graphbuilderadapter.cpp @@ -49,7 +49,7 @@ void GraphBuilderAdapter::OnMapStart(const Mark &mark, const std::string &tag, EmitterStyle::value /* style */) { void *pNode = m_builder.NewMap(mark, tag, GetCurrentParent()); m_containers.push(ContainerFrame(pNode, m_pKeyNode)); - m_pKeyNode = NULL; + m_pKeyNode = nullptr; RegisterAnchor(anchor, pNode); } @@ -62,7 +62,7 @@ void GraphBuilderAdapter::OnMapEnd() { void *GraphBuilderAdapter::GetCurrentParent() const { if (m_containers.empty()) { - return NULL; + return nullptr; } return m_containers.top().pContainer; } @@ -83,7 +83,7 @@ void GraphBuilderAdapter::DispositionNode(void *pNode) { if (m_containers.top().isMap()) { if (m_pKeyNode) { m_builder.AssignInMap(pContainer, m_pKeyNode, pNode); - m_pKeyNode = NULL; + m_pKeyNode = nullptr; } else { m_pKeyNode = pNode; } diff --git a/src/contrib/graphbuilderadapter.h b/src/contrib/graphbuilderadapter.h index 0d1e579..62b40a1 100644 --- a/src/contrib/graphbuilderadapter.h +++ b/src/contrib/graphbuilderadapter.h @@ -26,7 +26,7 @@ namespace YAML { class GraphBuilderAdapter : public EventHandler { public: GraphBuilderAdapter(GraphBuilderInterface& builder) - : m_builder(builder), m_pRootNode(NULL), m_pKeyNode(NULL) {} + : m_builder(builder), m_pRootNode(nullptr), m_pKeyNode(nullptr) {} virtual void OnDocumentStart(const Mark& mark) { (void)mark; } virtual void OnDocumentEnd() {} diff --git a/src/emitterutils.cpp b/src/emitterutils.cpp index 147738a..fffb775 100644 --- a/src/emitterutils.cpp +++ b/src/emitterutils.cpp @@ -134,12 +134,12 @@ void WriteCodePoint(ostream_wrapper& out, int codePoint) { if (codePoint < 0 || codePoint > 0x10FFFF) { codePoint = REPLACEMENT_CHARACTER; } - if (codePoint < 0x7F) { + if (codePoint <= 0x7F) { out << static_cast(codePoint); - } else if (codePoint < 0x7FF) { + } else if (codePoint <= 0x7FF) { out << static_cast(0xC0 | (codePoint >> 6)) << static_cast(0x80 | (codePoint & 0x3F)); - } else if (codePoint < 0xFFFF) { + } else if (codePoint <= 0xFFFF) { out << static_cast(0xE0 | (codePoint >> 12)) << static_cast(0x80 | ((codePoint >> 6) & 0x3F)) << static_cast(0x80 | (codePoint & 0x3F)); diff --git a/src/exceptions.cpp b/src/exceptions.cpp index 9b6d891..841549e 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -2,7 +2,7 @@ // This is here for compatibility with older versions of Visual Studio // which don't support noexcept -#ifdef _MSC_VER +#if defined(_MSC_VER) && _MSC_VER < 1900 #define YAML_CPP_NOEXCEPT _NOEXCEPT #else #define YAML_CPP_NOEXCEPT noexcept diff --git a/src/node_data.cpp b/src/node_data.cpp index 77cd465..04104b7 100644 --- a/src/node_data.cpp +++ b/src/node_data.cpp @@ -13,7 +13,10 @@ namespace YAML { namespace detail { -std::string node_data::empty_scalar; +const std::string& node_data::empty_scalar() { + static const std::string svalue; + return svalue; +} node_data::node_data() : m_isDefined(false), @@ -197,7 +200,7 @@ void node_data::insert(node& key, node& value, shared_memory_holder pMemory) { // indexing node* node_data::get(node& key, shared_memory_holder /* pMemory */) const { if (m_type != NodeType::Map) { - return NULL; + return nullptr; } for (node_map::const_iterator it = m_map.begin(); it != m_map.end(); ++it) { @@ -205,7 +208,7 @@ node* node_data::get(node& key, shared_memory_holder /* pMemory */) const { return it->second; } - return NULL; + return nullptr; } node& node_data::get(node& key, shared_memory_holder pMemory) { @@ -235,6 +238,14 @@ bool node_data::remove(node& key, shared_memory_holder /* pMemory */) { if (m_type != NodeType::Map) return false; + kv_pairs::iterator it = m_undefinedPairs.begin(); + while (it != m_undefinedPairs.end()) { + kv_pairs::iterator jt = std::next(it); + if (it->first->is(key)) + m_undefinedPairs.erase(it); + it = jt; + } + for (node_map::iterator it = m_map.begin(); it != m_map.end(); ++it) { if (it->first->is(key)) { m_map.erase(it); diff --git a/src/nodebuilder.cpp b/src/nodebuilder.cpp index 093d2ef..e79ac50 100644 --- a/src/nodebuilder.cpp +++ b/src/nodebuilder.cpp @@ -11,8 +11,8 @@ namespace YAML { struct Mark; NodeBuilder::NodeBuilder() - : m_pMemory(new detail::memory_holder), m_pRoot(0), m_mapDepth(0) { - m_anchors.push_back(0); // since the anchors start at 1 + : m_pMemory(new detail::memory_holder), m_pRoot(nullptr), m_mapDepth(0) { + m_anchors.push_back(nullptr); // since the anchors start at 1 } NodeBuilder::~NodeBuilder() {} diff --git a/src/ostream_wrapper.cpp b/src/ostream_wrapper.cpp index 357fc00..a3c7597 100644 --- a/src/ostream_wrapper.cpp +++ b/src/ostream_wrapper.cpp @@ -7,7 +7,7 @@ namespace YAML { ostream_wrapper::ostream_wrapper() : m_buffer(1, '\0'), - m_pStream(0), + m_pStream(nullptr), m_pos(0), m_row(0), m_col(0), diff --git a/src/regex_yaml.h b/src/regex_yaml.h index 8f28b85..1611cb2 100644 --- a/src/regex_yaml.h +++ b/src/regex_yaml.h @@ -77,10 +77,11 @@ class YAML_CPP_API RegEx { private: REGEX_OP m_op; - char m_a, m_z; + char m_a{}; + char m_z{}; std::vector m_params; }; -} +} // namespace YAML #include "regeximpl.h" diff --git a/src/scanner.cpp b/src/scanner.cpp index b5cfcc1..546a2f0 100644 --- a/src/scanner.cpp +++ b/src/scanner.cpp @@ -282,7 +282,7 @@ Scanner::IndentMarker* Scanner::PushIndentTo(int column, IndentMarker::INDENT_TYPE type) { // are we in flow? if (InFlowContext()) { - return 0; + return nullptr; } std::unique_ptr pIndent(new IndentMarker(column, type)); @@ -291,12 +291,12 @@ Scanner::IndentMarker* Scanner::PushIndentTo(int column, // is this actually an indentation? if (indent.column < lastIndent.column) { - return 0; + return nullptr; } if (indent.column == lastIndent.column && !(indent.type == IndentMarker::SEQ && lastIndent.type == IndentMarker::MAP)) { - return 0; + return nullptr; } // push a start token diff --git a/src/scanner.h b/src/scanner.h index 7bb2ccc..c653ac6 100644 --- a/src/scanner.h +++ b/src/scanner.h @@ -49,7 +49,7 @@ class Scanner { enum INDENT_TYPE { MAP, SEQ, NONE }; enum STATUS { VALID, INVALID, UNKNOWN }; IndentMarker(int column_, INDENT_TYPE type_) - : column(column_), type(type_), status(VALID), pStartToken(0) {} + : column(column_), type(type_), status(VALID), pStartToken(nullptr) {} int column; INDENT_TYPE type; diff --git a/src/simplekey.cpp b/src/simplekey.cpp index 70f56b6..c7a2135 100644 --- a/src/simplekey.cpp +++ b/src/simplekey.cpp @@ -5,7 +5,7 @@ namespace YAML { struct Mark; Scanner::SimpleKey::SimpleKey(const Mark& mark_, std::size_t flowLevel_) - : mark(mark_), flowLevel(flowLevel_), pIndent(0), pMapStart(0), pKey(0) {} + : mark(mark_), flowLevel(flowLevel_), pIndent(nullptr), pMapStart(nullptr), pKey(nullptr) {} void Scanner::SimpleKey::Validate() { // Note: pIndent will *not* be garbage here; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3633da5..8bdf303 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,26 +1,39 @@ -set(gtest_force_shared_crt ${MSVC_SHARED_RT} CACHE BOOL - "Use shared (DLL) run-time lib even when Google Test built as a static lib.") -add_subdirectory(gtest-1.8.0) -include_directories(SYSTEM gtest-1.8.0/googlemock/include) -include_directories(SYSTEM gtest-1.8.0/googletest/include) +include(ExternalProject) -if(WIN32 AND BUILD_SHARED_LIBS) - add_definitions("-DGTEST_LINKED_AS_SHARED_LIBRARY") +if(MSVC) + # MS Visual Studio expects lib prefix on static libraries, + # but CMake compiles them without prefix + # See https://gitlab.kitware.com/cmake/cmake/issues/17338 + set(CMAKE_STATIC_LIBRARY_PREFIX "") endif() +ExternalProject_Add( + googletest_project + SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.8.0" + INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/prefix" + CMAKE_ARGS + -DCMAKE_INSTALL_PREFIX:PATH= + -DBUILD_GMOCK=ON + -Dgtest_force_shared_crt=ON +) + +add_library(gmock UNKNOWN IMPORTED) +set_target_properties(gmock PROPERTIES + IMPORTED_LOCATION + ${PROJECT_BINARY_DIR}/test/prefix/lib/${CMAKE_STATIC_LIBRARY_PREFIX}gmock${CMAKE_STATIC_LIBRARY_SUFFIX} +) + +find_package(Threads) + +include_directories(SYSTEM "${PROJECT_BINARY_DIR}/test/prefix/include") + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR - CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(yaml_test_flags "-Wno-variadic-macros -Wno-sign-compare") + CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(yaml_test_flags "-Wno-variadic-macros -Wno-sign-compare") - if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(yaml_test_flags "${yaml_test_flags} -Wno-c99-extensions") - endif() - - if(CMAKE_COMPILER_IS_GNUCXX) - set(yaml_test_flags "${yaml_test_flags} -std=gnu++11") - else() - set(yaml_test_flags "${yaml_test_flags} -std=c++11") - endif() + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(yaml_test_flags "${yaml_test_flags} -Wno-c99-extensions") + endif() endif() file(GLOB test_headers [a-z_]*.h) @@ -30,15 +43,22 @@ file(GLOB test_new_api_sources new-api/[a-z]*.cpp) list(APPEND test_sources ${test_new_api_sources}) add_sources(${test_sources} ${test_headers}) +include_directories(${YAML_CPP_SOURCE_DIR}/src) include_directories(${YAML_CPP_SOURCE_DIR}/test) add_executable(run-tests - ${test_sources} - ${test_headers} + ${test_sources} + ${test_headers} ) + +add_dependencies(run-tests googletest_project) + set_target_properties(run-tests PROPERTIES - COMPILE_FLAGS "${yaml_c_flags} ${yaml_cxx_flags} ${yaml_test_flags}" + COMPILE_FLAGS "${yaml_c_flags} ${yaml_cxx_flags} ${yaml_test_flags}" ) -target_link_libraries(run-tests yaml-cpp gmock) +target_link_libraries(run-tests + yaml-cpp + gmock + ${CMAKE_THREAD_LIBS_INIT}) add_test(yaml-test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/run-tests) diff --git a/test/integration/load_node_test.cpp b/test/integration/load_node_test.cpp index 02bb8fe..4f4f28e 100644 --- a/test/integration/load_node_test.cpp +++ b/test/integration/load_node_test.cpp @@ -55,6 +55,26 @@ TEST(LoadNodeTest, Binary) { node[1].as()); } +TEST(LoadNodeTest, BinaryWithWhitespaces) { + Node node = Load( + "binaryText: !binary |-\n" + " TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieS\n" + " B0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIG\n" + " x1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbi\n" + " B0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZG\n" + " dlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS\n" + " 4K"); + EXPECT_EQ(Binary(reinterpret_cast( + "Man is distinguished, not only by his reason, " + "but by this singular passion from other " + "animals, which is a lust of the mind, that by " + "a perseverance of delight in the continued and " + "indefatigable generation of knowledge, exceeds " + "the short vehemence of any carnal pleasure.\n"), + 270), + node["binaryText"].as()); +} + TEST(LoadNodeTest, IterateSequence) { Node node = Load("[1, 3, 5, 7]"); int seq[] = {1, 3, 5, 7}; diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index 485ad09..61ba3e6 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -1,10 +1,10 @@ -#include "yaml-cpp/emitter.h" -#include "yaml-cpp/node/emit.h" #include "yaml-cpp/node/node.h" -#include "yaml-cpp/node/impl.h" +#include "yaml-cpp/emitter.h" #include "yaml-cpp/node/convert.h" -#include "yaml-cpp/node/iterator.h" #include "yaml-cpp/node/detail/impl.h" +#include "yaml-cpp/node/emit.h" +#include "yaml-cpp/node/impl.h" +#include "yaml-cpp/node/iterator.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -47,6 +47,30 @@ TEST(NodeTest, SimpleAppendSequence) { EXPECT_TRUE(node.IsSequence()); } +TEST(NodeTest, SequenceElementRemoval) { + Node node; + node[0] = "a"; + node[1] = "b"; + node[2] = "c"; + node.remove(1); + EXPECT_TRUE(node.IsSequence()); + EXPECT_EQ(2, node.size()); + EXPECT_EQ("a", node[0].as()); + EXPECT_EQ("c", node[1].as()); +} + +TEST(NodeTest, SequenceLastElementRemoval) { + Node node; + node[0] = "a"; + node[1] = "b"; + node[2] = "c"; + node.remove(2); + EXPECT_TRUE(node.IsSequence()); + EXPECT_EQ(2, node.size()); + EXPECT_EQ("a", node[0].as()); + EXPECT_EQ("b", node[1].as()); +} + TEST(NodeTest, MapElementRemoval) { Node node; node["foo"] = "bar"; @@ -54,6 +78,16 @@ TEST(NodeTest, MapElementRemoval) { EXPECT_TRUE(!node["foo"]); } +TEST(NodeTest, MapIntegerElementRemoval) { + Node node; + node[1] = "hello"; + node[2] = 'c'; + node["foo"] = "bar"; + EXPECT_TRUE(node.IsMap()); + node.remove(1); + EXPECT_TRUE(node.IsMap()); +} + TEST(NodeTest, SimpleAssignSequence) { Node node; node[0] = 10; @@ -106,6 +140,14 @@ TEST(NodeTest, RemoveUnassignedNode) { EXPECT_EQ(0, node.size()); } +TEST(NodeTest, RemoveUnassignedNodeFromMap) { + Node node(NodeType::Map); + Node n; + node[n]; + node.remove(n); + EXPECT_EQ(0, node.size()); +} + TEST(NodeTest, MapForceInsert) { Node node; Node k1("k1"); diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index 2286627..8a803b0 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -1,14 +1,14 @@ +cmake_minimum_required(VERSION 3.5) + add_sources(parse.cpp) add_executable(parse parse.cpp) target_link_libraries(parse yaml-cpp) -set_target_properties(parse PROPERTIES COMPILE_FLAGS "-std=c++11") add_sources(sandbox.cpp) add_executable(sandbox sandbox.cpp) target_link_libraries(sandbox yaml-cpp) -set_target_properties(sandbox PROPERTIES COMPILE_FLAGS "-std=c++11") add_sources(read.cpp) add_executable(read read.cpp) target_link_libraries(read yaml-cpp) -set_target_properties(read PROPERTIES COMPILE_FLAGS "-std=c++11") +