Browse Source

Reduce polymorphism of String class

- remove ISerializable extension
- remove IStorable extension
- adjust tests for String class
Patrick-Christopher Mattulat 3 years ago
parent
commit
cebc1bb048

+ 0 - 2
CMakeLists.txt

@@ -99,7 +99,6 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/SerializableJsonFactory.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/boxing/SerializableJsonIntegerFactory.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/boxing/SerializableJsonLongFactory.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/boxing/SerializableJsonStringFactory.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/event/SerializableJsonEventFactory.cpp)
 
 if (${LS_STD_BUILD_WITH_TESTS})
@@ -163,7 +162,6 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/SerializableFactoryTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/boxing/SerializableJsonIntegerFactoryTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/boxing/SerializableJsonLongFactoryTest.cpp
-            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/boxing/SerializableJsonStringFactoryTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/event/SerializableJsonEventFactoryTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/classes/factory/SerializableTestFactory.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/exception/EventNotHandledExceptionTest.cpp

+ 3 - 14
include/ls_std/boxing/String.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2021-07-08
+ * Changed:         2021-07-12
  *
  * */
 
@@ -12,15 +12,14 @@
 
 #include "IBoxing.hpp"
 #include <ls_std/base/Class.hpp>
-#include <ls_std/serialization/ISerializable.hpp>
-#include <ls_std/io/IStorable.hpp>
+#include <ls_std/base/Types.hpp>
 #include <string>
 #include <memory>
 #include <vector>
 
 namespace ls_std
 {
-  class String : public ls_std::Class, public ls_std::IBoxing, public ls_std::ISerializable, public ls_std::IStorable
+  class String : public ls_std::Class, public ls_std::IBoxing
   {
     public:
 
@@ -60,12 +59,8 @@ namespace ls_std
 
       // implementation
 
-      ls_std::byte_field load() override;
-      ls_std::byte_field marshal() override;
       void parse(std::string _parseText) override;
-      void save(const ls_std::byte_field &_data) override;
       std::string toString() override;
-      void unmarshal(const ls_std::byte_field &_data) override;
 
       // additional functionality
 
@@ -77,20 +72,14 @@ namespace ls_std
       std::string padLeft(size_t _width, char _fillCharacter);
       std::string padRight(size_t _width, char _fillCharacter);
       std::string reverse();
-      void setSerializable(std::shared_ptr<ls_std::ISerializable> _serializable);
-      void setStorable(std::shared_ptr<ls_std::IStorable> _storable);
       bool startsWith(const std::string &_text);
       std::string toLowerCase();
       std::string toUpperCase();
 
     private:
 
-      std::shared_ptr<ls_std::ISerializable> serializable{};
-      std::shared_ptr<ls_std::IStorable> storable{};
       std::string value{};
 
-      void _assignSerializable(const std::shared_ptr<ls_std::ISerializable>& _serializable);
-      void _assignStorable(const std::shared_ptr<ls_std::IStorable>& _storable);
       static std::string _buildCharacterChain(size_t _amount, char _fillCharacter);
       static std::string _createFillContent(const std::string &_text, size_t _width, char _fillCharacter);
   };

+ 0 - 29
include/ls_std/factory/serialization/json/boxing/SerializableJsonStringFactory.hpp

@@ -1,29 +0,0 @@
-/*
- * Author:          Patrick-Christopher Mattulat
- * Company:         Lynar Studios
- * E-Mail:          webmaster@lynarstudios.com
- * Created:         2021-04-24
- * Changed:         2021-05-02
- *
- * */
-
-#ifndef LS_STD_SERIALIZABLE_JSON_STRING_FACTORY_HPP
-#define LS_STD_SERIALIZABLE_JSON_STRING_FACTORY_HPP
-
-#include <ls_std/factory/IFactory.hpp>
-#include <memory>
-
-namespace ls_std
-{
-  class SerializableJsonStringFactory : public ls_std::Class, public ls_std::IFactory
-  {
-    public:
-
-      SerializableJsonStringFactory();
-      ~SerializableJsonStringFactory() override = default;
-
-      std::shared_ptr<ls_std::Class> build() override;
-  };
-}
-
-#endif

+ 0 - 1
include/ls_std/ls_std.hpp

@@ -99,7 +99,6 @@
 #include "ls_std/factory/serialization/json/SerializableJsonFactory.hpp"
 #include "factory/serialization/json/boxing/SerializableJsonIntegerFactory.hpp"
 #include "factory/serialization/json/boxing/SerializableJsonLongFactory.hpp"
-#include "factory/serialization/json/boxing/SerializableJsonStringFactory.hpp"
 #include "factory/serialization/json/event/SerializableJsonEventFactory.hpp"
 
 #endif

+ 1 - 79
source/ls_std/boxing/String.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2021-07-08
+ * Changed:         2021-07-12
  *
  * */
 
@@ -98,64 +98,16 @@ bool ls_std::String::operator!=(const char *_value)
   return this->value != _value;
 }
 
-ls_std::byte_field ls_std::String::load()
-{
-  ls_std::byte_field data{};
-
-  if (this->storable != nullptr && this->serializable != nullptr)
-  {
-    data = this->storable->load();
-    this->serializable->unmarshal(data);
-  }
-
-  return data;
-}
-
-ls_std::byte_field ls_std::String::marshal()
-{
-  ls_std::byte_field data{};
-
-  if (this->serializable != nullptr)
-  {
-    data = this->serializable->marshal();
-  }
-
-  return data;
-}
-
 void ls_std::String::parse(std::string _parseText)
 {
   this->value = std::move(_parseText);
 }
 
-void ls_std::String::save(const ls_std::byte_field &_data)
-{
-  if (this->serializable != nullptr)
-  {
-    if (_data.empty())
-    {
-      this->storable->save(this->serializable->marshal());
-    }
-    else
-    {
-      this->storable->save(_data);
-    }
-  }
-}
-
 std::string ls_std::String::toString()
 {
   return this->value;
 }
 
-void ls_std::String::unmarshal(const ls_std::byte_field &_data)
-{
-  if (this->serializable != nullptr)
-  {
-    this->serializable->unmarshal(_data);
-  }
-}
-
 bool ls_std::String::contains(const std::string &_text)
 {
   return this->value.find(_text) != std::string::npos;
@@ -202,16 +154,6 @@ std::string ls_std::String::reverse()
   return copy;
 }
 
-void ls_std::String::setSerializable(std::shared_ptr<ls_std::ISerializable> _serializable)
-{
-  this->_assignSerializable(_serializable);
-}
-
-void ls_std::String::setStorable(std::shared_ptr<ls_std::IStorable> _storable)
-{
-  this->_assignStorable(_storable);
-}
-
 bool ls_std::String::startsWith(const std::string &_text)
 {
   return this->value.rfind(_text, 0) == 0;
@@ -233,26 +175,6 @@ std::string ls_std::String::toUpperCase()
   return copy;
 }
 
-void ls_std::String::_assignSerializable(const std::shared_ptr<ls_std::ISerializable> &_serializable)
-{
-  if (_serializable == nullptr)
-  {
-    throw ls_std::IllegalArgumentException{};
-  }
-
-  this->serializable = _serializable;
-}
-
-void ls_std::String::_assignStorable(const std::shared_ptr<ls_std::IStorable> &_storable)
-{
-  if (_storable == nullptr)
-  {
-    throw ls_std::IllegalArgumentException{};
-  }
-
-  this->storable = _storable;
-}
-
 std::string ls_std::String::_buildCharacterChain(size_t _amount, const char _fillCharacter)
 {
   std::string fillContent{};

+ 0 - 3
source/ls_std/factory/serialization/json/SerializableJsonFactory.cpp

@@ -11,10 +11,8 @@
 #include <ls_std/boxing/Float.hpp>
 #include <ls_std/boxing/Integer.hpp>
 #include <ls_std/boxing/Long.hpp>
-#include <ls_std/boxing/String.hpp>
 #include <ls_std/factory/serialization/json/boxing/SerializableJsonIntegerFactory.hpp>
 #include <ls_std/factory/serialization/json/boxing/SerializableJsonLongFactory.hpp>
-#include <ls_std/factory/serialization/json/boxing/SerializableJsonStringFactory.hpp>
 #include <ls_std/exception/IllegalArgumentException.hpp>
 #include <ls_std/exception/NullPointerException.hpp>
 
@@ -90,5 +88,4 @@ void ls_std::SerializableJsonFactory::_init()
 {
   this->factories.insert({ls_std::Integer{}.getClassName(), std::make_shared<ls_std::SerializableJsonIntegerFactory>()});
   this->factories.insert({ls_std::Long{}.getClassName(), std::make_shared<ls_std::SerializableJsonLongFactory>()});
-  this->factories.insert({ls_std::String{}.getClassName(), std::make_shared<ls_std::SerializableJsonStringFactory>()});
 }

+ 0 - 24
source/ls_std/factory/serialization/json/boxing/SerializableJsonStringFactory.cpp

@@ -1,24 +0,0 @@
-/*
- * Author:          Patrick-Christopher Mattulat
- * Company:         Lynar Studios
- * E-Mail:          webmaster@lynarstudios.com
- * Created:         2021-04-25
- * Changed:         2021-05-02
- *
- * */
-
-#include <ls_std/factory/serialization/json/boxing/SerializableJsonStringFactory.hpp>
-#include <ls_std/boxing/String.hpp>
-#include <ls_std/serialization/json/boxing/SerializableJsonString.hpp>
-
-ls_std::SerializableJsonStringFactory::SerializableJsonStringFactory() : ls_std::Class("SerializableJsonStringFactory")
-{}
-
-std::shared_ptr<ls_std::Class> ls_std::SerializableJsonStringFactory::build()
-{
-  std::shared_ptr<ls_std::String> requestedObject = std::make_shared<ls_std::String>();
-  std::shared_ptr<ls_std::ISerializable> serializable = std::make_shared<ls_std::SerializableJsonString>(requestedObject);
-  requestedObject->setSerializable(serializable);
-
-  return std::dynamic_pointer_cast<ls_std::Class>(requestedObject);
-}

+ 1 - 87
test/cases/boxing/StringTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2021-07-08
+ * Changed:         2021-07-12
  *
  * */
 
@@ -25,24 +25,6 @@ namespace
 
       void TearDown() override
       {}
-
-      static std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::String>> createPersistentTestString()
-      {
-        std::shared_ptr<ls_std::String> sentence = std::make_shared<ls_std::String>();
-        std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_string.json";
-        std::shared_ptr<ls_std::File> file = std::make_shared<ls_std::File>(path);
-        file->createNewFile();
-        ls_std::FileWriter writer{*file};
-        writer.write(R"({"value":"Hello!"})");
-
-        auto serializable = std::make_shared<ls_std::SerializableJsonString>(sentence);
-        sentence->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
-
-        auto storable = std::make_shared<ls_std::StorableFile>(path);
-        sentence->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
-
-        return std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::String>>(file, sentence);
-      }
   };
 
   // assignment operators
@@ -128,30 +110,6 @@ namespace
 
   // implementation
 
-  TEST_F(StringTest, load)
-  {
-    // preparation
-
-    auto storableString = createPersistentTestString();
-
-    // check
-
-    storableString.second->load();
-    ASSERT_STREQ("Hello!", *storableString.second);
-
-    storableString.first->remove();
-  }
-
-  TEST_F(StringTest, marshal)
-  {
-    std::shared_ptr<ls_std::String> sentence = std::make_shared<ls_std::String>("Hello!");
-
-    auto serializable = std::make_shared<ls_std::SerializableJsonString>(sentence);
-    sentence->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
-
-    ASSERT_STREQ(R"({"value":"Hello!"})", sentence->marshal().c_str());
-  }
-
   TEST_F(StringTest, parse)
   {
     ls_std::String text{};
@@ -166,18 +124,6 @@ namespace
     ASSERT_STREQ("Hello!", text.toString().c_str());
   }
 
-  TEST_F(StringTest, unmarshal)
-  {
-    std::shared_ptr<ls_std::String> sentence = std::make_shared<ls_std::String>("Hello!");
-
-    auto serializable = std::make_shared<ls_std::SerializableJsonString>(sentence);
-    sentence->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
-
-    sentence->unmarshal(R"({"value":"Test!"})");
-
-    ASSERT_STREQ("Test!", *sentence);
-  }
-
   // additional functionality
 
   TEST_F(StringTest, contains)
@@ -295,36 +241,4 @@ namespace
     ASSERT_STREQ("ABCDEFGHIJKLMN", text.toUpperCase().c_str());
     ASSERT_STREQ("aBCdeFgHIJKLmn", text); // verify, that original String didn't change
   }
-
-  TEST_F(StringTest, setSerializable_no_reference)
-  {
-    ls_std::String x{};
-
-    EXPECT_THROW({
-                   try
-                   {
-                     x.setSerializable(nullptr);
-                   }
-                   catch (const ls_std::IllegalArgumentException &_exception)
-                   {
-                     throw;
-                   }
-                 }, ls_std::IllegalArgumentException);
-  }
-
-  TEST_F(StringTest, setStorable_no_reference)
-  {
-    ls_std::String x{};
-
-    EXPECT_THROW({
-                   try
-                   {
-                     x.setStorable(nullptr);
-                   }
-                   catch (const ls_std::IllegalArgumentException &_exception)
-                   {
-                     throw;
-                   }
-                 }, ls_std::IllegalArgumentException);
-  }
 }

+ 0 - 1
test/cases/factory/serialization/json/SerializableFactoryTest.cpp

@@ -106,7 +106,6 @@ namespace
     ls_std::SerializableJsonFactory serializableFactory{};
     ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Integer{}.getClassName()));
     ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Long{}.getClassName()));
-    ASSERT_TRUE(serializableFactory.hasFactory(ls_std::String{}.getClassName()));
   }
 
   TEST_F(SerializableFactoryTest, hasFactory_factoryNotAvailable)

+ 0 - 35
test/cases/factory/serialization/json/boxing/SerializableJsonStringFactoryTest.cpp

@@ -1,35 +0,0 @@
-/*
- * Author:          Patrick-Christopher Mattulat
- * Company:         Lynar Studios
- * E-Mail:          webmaster@lynarstudios.com
- * Created:         2021-04-25
- * Changed:         2021-05-02
- *
- * */
-
-#include <gtest/gtest.h>
-#include <ls_std/ls_std.hpp>
-
-namespace
-{
-  class SerializableJsonStringFactoryTest : public ::testing::Test
-  {
-    protected:
-
-      SerializableJsonStringFactoryTest() = default;
-      ~SerializableJsonStringFactoryTest() override = default;
-
-      void SetUp() override
-      {}
-
-      void TearDown() override
-      {}
-  };
-
-  TEST_F(SerializableJsonStringFactoryTest, build)
-  {
-    std::shared_ptr<ls_std::String> value = std::dynamic_pointer_cast<ls_std::String>(ls_std::SerializableJsonStringFactory{}.build());
-    ASSERT_TRUE(value != nullptr);
-    ASSERT_STREQ("String", value->getClassName().c_str());
-  }
-}