Ver código fonte

Reduce polymorphism of Boolean class

- remove ISerializable extension
- remove IStorable extension
- adjust tests for Boolean class
Patrick-Christopher Mattulat 3 anos atrás
pai
commit
b53c40b9d5

+ 0 - 2
CMakeLists.txt

@@ -97,7 +97,6 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KvParser.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KvReader.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/SerializableJsonFactory.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/boxing/SerializableJsonBooleanFactory.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/boxing/SerializableJsonDoubleFactory.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/boxing/SerializableJsonFloatFactory.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/factory/serialization/json/boxing/SerializableJsonIntegerFactory.cpp
@@ -164,7 +163,6 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KvParserTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KvReaderTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/SerializableFactoryTest.cpp
-            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/boxing/SerializableJsonBooleanFactoryTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/boxing/SerializableJsonDoubleFactoryTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/boxing/SerializableJsonFloatFactoryTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/factory/serialization/json/boxing/SerializableJsonIntegerFactoryTest.cpp

+ 2 - 14
include/ls_std/boxing/Boolean.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-09
- * Changed:         2021-05-30
+ * Changed:         2021-07-08
  *
  * */
 
@@ -13,12 +13,10 @@
 #include <memory>
 #include <ls_std/base/Class.hpp>
 #include "IBoxing.hpp"
-#include <ls_std/serialization/ISerializable.hpp>
-#include <ls_std/io/IStorable.hpp>
 
 namespace ls_std
 {
-  class Boolean : public ls_std::Class, public ls_std::IBoxing, public ls_std::ISerializable, public ls_std::IStorable
+  class Boolean : public ls_std::Class, public ls_std::IBoxing
   {
     public:
 
@@ -60,18 +58,12 @@ 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
 
       bool getValue() const;
-      void setSerializable(const std::shared_ptr<ls_std::ISerializable>& _serializable);
-      void setStorable(const std::shared_ptr<ls_std::IStorable>& _storable);
       static bool XOR(const ls_std::Boolean &_leftExpression, const ls_std::Boolean &_rightExpression);
       static bool XOR(const ls_std::Boolean &_leftExpression, bool _rightExpression);
       static bool XOR(bool _leftExpression, const ls_std::Boolean &_rightExpression);
@@ -79,15 +71,11 @@ namespace ls_std
 
     private:
 
-      std::shared_ptr<ls_std::ISerializable> serializable{};
-      std::shared_ptr<ls_std::IStorable> storable{};
       bool value{};
 
       const std::string FALSE_STRING = "false";
       const std::string TRUE_STRING = "true";
 
-      void _assignSerializable(const std::shared_ptr<ls_std::ISerializable>& _serializable);
-      void _assignStorable(const std::shared_ptr<ls_std::IStorable>& _storable);
       std::string _toString() const;
   };
 }

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

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-09
- * Changed:         2021-05-30
+ * Changed:         2021-07-08
  *
  * */
 
@@ -66,31 +66,6 @@ bool ls_std::Boolean::operator||(int _value) const
   return this->value || _value;
 }
 
-ls_std::byte_field ls_std::Boolean::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::Boolean::marshal()
-{
-  ls_std::byte_field data{};
-
-  if (this->serializable != nullptr)
-  {
-    data = this->serializable->marshal();
-  }
-
-  return data;
-}
-
 void ls_std::Boolean::parse(std::string _parseText)
 {
   std::transform(_parseText.begin(), _parseText.end(), _parseText.begin(), ::tolower);
@@ -113,49 +88,16 @@ void ls_std::Boolean::parse(std::string _parseText)
   }
 }
 
-void ls_std::Boolean::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::Boolean::toString()
 {
   return this->_toString();
 }
 
-void ls_std::Boolean::unmarshal(const ls_std::byte_field &_data)
-{
-  if (this->serializable != nullptr)
-  {
-    this->serializable->unmarshal(_data);
-  }
-}
-
 bool ls_std::Boolean::getValue() const
 {
   return this->value;
 }
 
-void ls_std::Boolean::setSerializable(const std::shared_ptr<ls_std::ISerializable>& _serializable)
-{
-  this->_assignSerializable(_serializable);
-}
-
-void ls_std::Boolean::setStorable(const std::shared_ptr<ls_std::IStorable>& _storable)
-{
-  this->_assignStorable(_storable);
-}
-
 bool ls_std::Boolean::XOR(const ls_std::Boolean &_leftExpression, const ls_std::Boolean &_rightExpression)
 {
   return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
@@ -176,26 +118,6 @@ bool ls_std::Boolean::XOR(bool _leftExpression, bool _rightExpression)
   return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
 }
 
-void ls_std::Boolean::_assignSerializable(const std::shared_ptr<ls_std::ISerializable> &_serializable)
-{
-  if (_serializable == nullptr)
-  {
-    throw ls_std::IllegalArgumentException{};
-  }
-
-  this->serializable = _serializable;
-}
-
-void ls_std::Boolean::_assignStorable(const std::shared_ptr<ls_std::IStorable> &_storable)
-{
-  if (_storable == nullptr)
-  {
-    throw ls_std::IllegalArgumentException{};
-  }
-
-  this->storable = _storable;
-}
-
 std::string ls_std::Boolean::_toString() const
 {
   std::string booleanString{};

+ 1 - 2
source/ls_std/factory/serialization/json/SerializableJsonFactory.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2021-04-24
- * Changed:         2021-05-02
+ * Changed:         2021-07-08
  *
  * */
 
@@ -93,7 +93,6 @@ bool ls_std::SerializableJsonFactory::_hasFactory(const std::string &_relatedObj
 
 void ls_std::SerializableJsonFactory::_init()
 {
-  this->factories.insert({ls_std::Boolean{}.getClassName(), std::make_shared<ls_std::SerializableJsonBooleanFactory>()});
   this->factories.insert({ls_std::Double{}.getClassName(), std::make_shared<ls_std::SerializableJsonDoubleFactory>()});
   this->factories.insert({ls_std::Float{}.getClassName(), std::make_shared<ls_std::SerializableJsonFloatFactory>()});
   this->factories.insert({ls_std::Integer{}.getClassName(), std::make_shared<ls_std::SerializableJsonIntegerFactory>()});

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

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

+ 1 - 95
test/cases/boxing/BooleanTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-09
- * Changed:         2021-05-30
+ * Changed:         2021-07-08
  *
  * */
 
@@ -25,24 +25,6 @@ namespace
 
       void TearDown() override
       {}
-
-      static std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::Boolean>> createTestExpression()
-      {
-        std::shared_ptr<ls_std::Boolean> expression = std::make_shared<ls_std::Boolean>();
-        std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_bool.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":true})");
-
-        auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(expression);
-        expression->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
-
-        auto storable = std::make_shared<ls_std::StorableFile>(path);
-        expression->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
-
-        return std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::Boolean>>(file, expression);
-      }
   };
 
   // assignment operators
@@ -138,40 +120,6 @@ namespace
 
   // implementation
 
-  TEST_F(BooleanTest, load)
-  {
-    // preparation
-
-    auto storableExpression = createTestExpression();
-
-    // check
-
-    storableExpression.second->load();
-    ASSERT_TRUE(*storableExpression.second);
-
-    storableExpression.first->remove();
-  }
-
-  TEST_F(BooleanTest, marshal_positive_value)
-  {
-    std::shared_ptr<ls_std::Boolean> expression = std::make_shared<ls_std::Boolean>(true);
-
-    auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(expression);
-    expression->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
-
-    ASSERT_STREQ(R"({"value":true})", expression->marshal().c_str());
-  }
-
-  TEST_F(BooleanTest, marshal_negative_value)
-  {
-    std::shared_ptr<ls_std::Boolean> expression = std::make_shared<ls_std::Boolean>(false);
-
-    auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(expression);
-    expression->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
-
-    ASSERT_STREQ(R"({"value":false})", expression->marshal().c_str());
-  }
-
   TEST_F(BooleanTest, parse_true_value)
   {
     ls_std::Boolean expression{};
@@ -212,16 +160,6 @@ namespace
     ASSERT_STREQ("false", expression.toString().c_str());
   }
 
-  TEST_F(BooleanTest, unmarshal)
-  {
-    std::shared_ptr<ls_std::Boolean> expression = std::make_shared<ls_std::Boolean>(false);
-    auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(expression);
-    expression->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
-    expression->unmarshal(R"({"value":true})");
-
-    ASSERT_TRUE(*expression);
-  }
-
   // additional functionality
 
   TEST_F(BooleanTest, getValue)
@@ -230,38 +168,6 @@ namespace
     ASSERT_TRUE(x.getValue());
   }
 
-  TEST_F(BooleanTest, setSerialiable_no_reference)
-  {
-    ls_std::Boolean expression{};
-
-    EXPECT_THROW({
-                   try
-                   {
-                     expression.setSerializable(nullptr);
-                   }
-                   catch (const ls_std::IllegalArgumentException &_exception)
-                   {
-                     throw;
-                   }
-                 }, ls_std::IllegalArgumentException);
-  }
-
-  TEST_F(BooleanTest, setStorable_no_reference)
-  {
-    ls_std::Boolean expression{};
-
-    EXPECT_THROW({
-                   try
-                   {
-                     expression.setStorable(nullptr);
-                   }
-                   catch (const ls_std::IllegalArgumentException &_exception)
-                   {
-                     throw;
-                   }
-                 }, ls_std::IllegalArgumentException);
-  }
-
   TEST_F(BooleanTest, XOR_with_positive_result)
   {
     ls_std::Boolean x{2 < 3};

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

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2021-04-24
- * Changed:         2021-05-02
+ * Changed:         2021-07-08
  *
  * */
 
@@ -104,7 +104,6 @@ namespace
   TEST_F(SerializableFactoryTest, hasFactory_ofBoxingPackage)
   {
     ls_std::SerializableJsonFactory serializableFactory{};
-    ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Boolean{}.getClassName()));
     ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Double{}.getClassName()));
     ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Float{}.getClassName()));
     ASSERT_TRUE(serializableFactory.hasFactory(ls_std::Integer{}.getClassName()));

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

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