Browse Source

Add KVPair class

- add KVPair class to hold a key value pair
- add tests for KVPair class
Patrick-Christopher Mattulat 3 years ago
parent
commit
40ef5635de

+ 4 - 2
CMakeLists.txt

@@ -92,7 +92,8 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/event/Event.cpp
         ${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/serialization/json/event/SerializableJSONEvent.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KVPair.cpp)
 
 if (${LS_STD_BUILD_WITH_TESTS})
     set(TEST_FILES
@@ -147,7 +148,8 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/event/GossipNewsAgency.cpp
             ${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/serialization/json/event/SerializableJSONEventTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KVPairTest.cpp)
 endif ()
 
 ##########################################################

+ 36 - 0
include/ls_std/io/kv/KVPair.hpp

@@ -0,0 +1,36 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-25
+ * Changed:         2020-12-25
+ *
+ * */
+
+#ifndef LS_STD_KV_PAIR_HPP
+#define LS_STD_KV_PAIR_HPP
+
+#include <ls_std/base/Class.hpp>
+#include <string>
+
+namespace ls_std {
+  class KVPair : public ls_std::Class {
+    public:
+
+      explicit KVPair(const std::string& _key, std::string _value);
+      ~KVPair() override = default;
+
+      std::string getKey();
+      std::string getValue();
+      void setValue(const std::string& _value);
+
+    private:
+
+      std::string key {};
+      std::string value {};
+
+      void _assignKey(const std::string& _key);
+  };
+}
+
+#endif

+ 1 - 0
include/ls_std/ls_std.hpp

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

+ 41 - 0
source/ls_std/io/kv/KVPair.cpp

@@ -0,0 +1,41 @@
+/*
+ * 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/KVPair.hpp>
+#include <ls_std/exception/IllegalArgumentException.hpp>
+
+ls_std::KVPair::KVPair(const std::string& _key, std::string _value) : ls_std::Class("KVPair"),
+value(std::move(_value))
+{
+  this->_assignKey(_key);
+}
+
+std::string ls_std::KVPair::getKey()
+{
+  return this->key;
+}
+
+std::string ls_std::KVPair::getValue()
+{
+  return this->value;
+}
+
+void ls_std::KVPair::setValue(const std::string &_value)
+{
+  this->value = _value;
+}
+
+void ls_std::KVPair::_assignKey(const std::string &_key)
+{
+  if(_key.empty()) {
+    throw ls_std::IllegalArgumentException {};
+  }
+
+  this->key = _key;
+}

+ 44 - 0
test/cases/io/kv/KVPairTest.cpp

@@ -0,0 +1,44 @@
+/*
+ * 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 KVPairTest : public ::testing::Test {
+    protected:
+
+      KVPairTest() = default;
+      ~KVPairTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(KVPairTest, getKey)
+  {
+    ls_std::KVPair pair {"port", "13088"};
+    ASSERT_STREQ("port", pair.getKey().c_str());
+  }
+
+  TEST_F(KVPairTest, getValue)
+  {
+    ls_std::KVPair pair {"port", "13088"};
+    ASSERT_STREQ("13088", pair.getValue().c_str());
+  }
+
+  TEST_F(KVPairTest, setValue)
+  {
+    ls_std::KVPair pair {"port", "13088"};
+    ASSERT_STREQ("13088", pair.getValue().c_str());
+
+    pair.setValue("8080");
+    ASSERT_STREQ("8080", pair.getValue().c_str());
+  }
+}