Browse Source

Add KVDocument class

- add KVDocument class to hold a map of
ls_std::KVPair instances
- add tests for KVDocument class
Patrick-Christopher Mattulat 3 years ago
parent
commit
105bbb1eae

+ 4 - 2
CMakeLists.txt

@@ -93,7 +93,8 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/event/EventHandler.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/event/EventManager.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/serialization/json/event/SerializableJSONEvent.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KVPair.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KVPair.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KVDocument.cpp)
 
 if (${LS_STD_BUILD_WITH_TESTS})
     set(TEST_FILES
@@ -149,7 +150,8 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/event/EventHandlerTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/event/EventManagerTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/json/event/SerializableJSONEventTest.cpp
-            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KVPairTest.cpp)
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KVPairTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KVDocumentTest.cpp)
 endif ()
 
 ##########################################################

+ 39 - 0
include/ls_std/io/kv/KVDocument.hpp

@@ -0,0 +1,39 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-25
+ * Changed:         2020-12-25
+ *
+ * */
+
+#ifndef LS_STD_KV_DOCUMENT_HPP
+#define LS_STD_KV_DOCUMENT_HPP
+
+#include <ls_std/base/Class.hpp>
+#include "KVPair.hpp"
+#include "KVTypes.hpp"
+#include <map>
+
+namespace ls_std {
+  class KVDocument : public ls_std::Class {
+    public:
+
+      KVDocument();
+      ~KVDocument() override = default;
+
+      bool addPair(ls_std::KVPair _pair);
+      void clear();
+      std::map<ls_std::kv_key, ls_std::KVPair> getPairs();
+      bool hasPair(const ls_std::kv_key& _key);
+      void removePair(const ls_std::kv_key& _key);
+
+    private:
+
+      std::map<ls_std::kv_key, ls_std::KVPair> pairs {};
+
+      bool _hasPair(const ls_std::kv_key& _key);
+  };
+}
+
+#endif

+ 1 - 0
include/ls_std/ls_std.hpp

@@ -54,6 +54,7 @@
 #include "io/NewLine.hpp"
 #include "io/StorableFile.hpp"
 #include "io/kv/KVPair.hpp"
+#include "io/kv/KVDocument.hpp"
 
 #include "logic/State.hpp"
 #include "logic/StateConnection.hpp"

+ 52 - 0
source/ls_std/io/kv/KVDocument.cpp

@@ -0,0 +1,52 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-25
+ * Changed:         2020-12-25
+ *
+ * */
+
+#include <ls_std/io/kv/KVDocument.hpp>
+
+ls_std::KVDocument::KVDocument() : ls_std::Class("KVDocument")
+{}
+
+bool ls_std::KVDocument::addPair(ls_std::KVPair _pair)
+{
+  bool added {};
+
+  if(!this->_hasPair(_pair.getKey())) {
+    std::pair<ls_std::kv_key, ls_std::KVPair> pair = std::make_pair(_pair.getKey(), _pair);
+    added = this->pairs.insert(pair).second;
+  }
+
+  return added;
+}
+
+void ls_std::KVDocument::clear()
+{
+  this->pairs.clear();
+}
+
+std::map<ls_std::kv_key, ls_std::KVPair> ls_std::KVDocument::getPairs()
+{
+  return this->pairs;
+}
+
+bool ls_std::KVDocument::hasPair(const ls_std::kv_key &_key)
+{
+  return this->_hasPair(_key);
+}
+
+void ls_std::KVDocument::removePair(const ls_std::kv_key &_key)
+{
+  if(_hasPair(_key)) {
+    this->pairs.erase(_key);
+  }
+}
+
+bool ls_std::KVDocument::_hasPair(const ls_std::kv_key& _key)
+{
+  return this->pairs.find(_key) != this->pairs.end();
+}

+ 95 - 0
test/cases/io/kv/KVDocumentTest.cpp

@@ -0,0 +1,95 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-25
+ * Changed:         2020-12-25
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include <ls_std/ls_std.hpp>
+
+namespace {
+  class KVDocumentTest : public ::testing::Test {
+    protected:
+
+      KVDocumentTest() = default;
+      ~KVDocumentTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(KVDocumentTest, addPair)
+  {
+    ls_std::KVDocument document {};
+    ls_std::KVPair pair {"port", "13088"};
+
+    ASSERT_TRUE(document.getPairs().empty());
+    ASSERT_TRUE(document.addPair(pair));
+    ASSERT_EQ(1, document.getPairs().size());
+  }
+
+  TEST_F(KVDocumentTest, addPairNegative)
+  {
+    ls_std::KVDocument document {};
+    ls_std::KVPair pair {"port", "13088"};
+
+    ASSERT_TRUE(document.getPairs().empty());
+    ASSERT_TRUE(document.addPair(pair));
+    ASSERT_EQ(1, document.getPairs().size());
+
+    ASSERT_FALSE(document.addPair(pair));
+  }
+
+  TEST_F(KVDocumentTest, clear)
+  {
+    ls_std::KVDocument document {};
+    ls_std::KVPair pair {"port", "13088"};
+
+    ASSERT_TRUE(document.getPairs().empty());
+    ASSERT_TRUE(document.addPair(pair));
+    ASSERT_FALSE(document.getPairs().empty());
+    ASSERT_EQ(1, document.getPairs().size());
+
+    document.clear();
+    ASSERT_TRUE(document.getPairs().empty());
+    ASSERT_EQ(0, document.getPairs().size());
+  }
+
+  TEST_F(KVDocumentTest, getPairs)
+  {
+    ls_std::KVDocument document {};
+    ASSERT_TRUE(document.getPairs().empty());
+  }
+
+  TEST_F(KVDocumentTest, hasPair)
+  {
+    ls_std::KVDocument document {};
+    ASSERT_TRUE(document.getPairs().empty());
+    ASSERT_TRUE(document.addPair(ls_std::KVPair {"port", "80"}));
+    ASSERT_TRUE(document.hasPair("port"));
+  }
+
+  TEST_F(KVDocumentTest, hasPairNegative)
+  {
+    ls_std::KVDocument document {};
+    ASSERT_TRUE(document.getPairs().empty());
+    ASSERT_FALSE(document.hasPair("port"));
+  }
+
+  TEST_F(KVDocumentTest, removePair)
+  {
+    ls_std::KVDocument document {};
+    ASSERT_TRUE(document.addPair(ls_std::KVPair {"port", "80"}));
+    ASSERT_TRUE(document.addPair(ls_std::KVPair {"host", "localhost"}));
+    ASSERT_TRUE(document.addPair(ls_std::KVPair {"protocol", "TCP"}));
+    ASSERT_EQ(3, document.getPairs().size());
+
+    document.removePair("protocol");
+    ASSERT_EQ(2, document.getPairs().size());
+    ASSERT_TRUE(document.hasPair("port"));
+    ASSERT_TRUE(document.hasPair("host"));
+  }
+}