Prechádzať zdrojové kódy

Extended Boolean class

- implemented IStorable interface
- added tests for Boolean class
pcmattulat 4 rokov pred
rodič
commit
05a29a530a

+ 25 - 0
source/boxing/Boolean.cpp

@@ -65,6 +65,17 @@ 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 {};
 
@@ -92,6 +103,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();
@@ -111,6 +132,10 @@ void ls_std::Boolean::setSerializable(std::shared_ptr<ISerializable> _serializab
   this->serializable = std::move(_serializable);
 }
 
+void ls_std::Boolean::setStorable(std::shared_ptr<IStorable> _storable) {
+  this->storable = std::move(_storable);
+}
+
 bool ls_std::Boolean::XOR(const Boolean &_leftExpression, const Boolean &_rightExpression) {
   return (_leftExpression && !_rightExpression) || (!_leftExpression && _rightExpression);
 }

+ 8 - 2
source/boxing/Boolean.hpp

@@ -14,9 +14,10 @@
 #include "../base/Class.hpp"
 #include "IBoxing.hpp"
 #include "../serialization/ISerializable.hpp"
+#include "../io/IStorable.hpp"
 
 namespace ls_std {
-  class Boolean : public Class, public IBoxing, public ISerializable {
+  class Boolean : public Class, public IBoxing, public ISerializable, public IStorable {
     public:
 
       explicit Boolean(bool _value);
@@ -55,8 +56,10 @@ 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;
 
@@ -64,6 +67,7 @@ namespace ls_std {
 
       bool getValue();
       void setSerializable(std::shared_ptr<ISerializable> _serializable);
+      void setStorable(std::shared_ptr<IStorable> _storable);
       static bool XOR(const Boolean& _leftExpression, const Boolean& _rightExpression);
       static bool XOR(const Boolean& _leftExpression, bool _rightExpression);
       static bool XOR(bool _leftExpression, const Boolean& _rightExpression);
@@ -72,9 +76,11 @@ namespace ls_std {
     private:
 
       std::shared_ptr<ISerializable> serializable {};
+      std::shared_ptr<IStorable> storable {};
       bool value {};
-      const std::string TRUE_STRING = "true";
+
       const std::string FALSE_STRING = "false";
+      const std::string TRUE_STRING = "true";
 
       std::string _toString() const;
   };

+ 29 - 0
test/cases/boxing/BooleanTest.cpp

@@ -10,6 +10,10 @@
 #include <gtest/gtest.h>
 #include "../../../source/boxing/Boolean.hpp"
 #include "../../../source/serialization/boxing/SerializableJSONBoolean.hpp"
+#include "../../TestHelper.hpp"
+#include "../../../source/io/File.hpp"
+#include "../../../source/io/FileWriter.hpp"
+#include "../../../source/io/StorableFile.hpp"
 
 namespace {
   class BooleanTest : public ::testing::Test {
@@ -107,6 +111,31 @@ namespace {
 
   // implementation
 
+  TEST_F(BooleanTest, load)
+  {
+    // preparation
+
+    std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>();
+    std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_bool.json";
+    ls_std::File file {path};
+    file.createNewFile();
+    ls_std::FileWriter writer {file};
+    writer.write(R"({"value":true})");
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONBoolean>(x);
+    x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
+
+    auto storable = std::make_shared<ls_std::StorableFile>(path);
+    x->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
+
+    // check
+
+    x->load();
+    ASSERT_TRUE(*x);
+
+    file.remove();
+  }
+
   TEST_F(BooleanTest, marshal)
   {
     std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>(true);