Jelajahi Sumber

Extended Long class

- implemented IStorable interface
- extended tests of Long class
pcmattulat 4 tahun lalu
induk
melakukan
e5eacdaa22
3 mengubah file dengan 62 tambahan dan 1 penghapusan
  1. 27 0
      source/boxing/Long.cpp
  2. 6 1
      source/boxing/Long.hpp
  3. 29 0
      test/cases/boxing/LongTest.cpp

+ 27 - 0
source/boxing/Long.cpp

@@ -249,6 +249,18 @@ void ls_std::Long::operator--()
   this->value -= 1;
 }
 
+ls_std::byte_field ls_std::Long::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::Long::marshal()
 {
   ls_std::byte_field data {};
@@ -265,6 +277,17 @@ void ls_std::Long::parse(std::string _parseText)
   this->value = std::stoi(_parseText);
 }
 
+void ls_std::Long::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::Long::toString()
 {
   return std::to_string(this->value);
@@ -284,3 +307,7 @@ ls_std::long_type ls_std::Long::getValue() const {
 void ls_std::Long::setSerializable(std::shared_ptr<ISerializable> _serializable) {
   this->serializable = std::move(_serializable);
 }
+
+void ls_std::Long::setStorable(std::shared_ptr<IStorable> _storable) {
+  this->storable = std::move(_storable);
+}

+ 6 - 1
source/boxing/Long.hpp

@@ -15,9 +15,10 @@
 #include "IBoxing.hpp"
 #include "../base/Types.hpp"
 #include "../serialization/ISerializable.hpp"
+#include "../io/IStorable.hpp"
 
 namespace ls_std {
-  class Long : public Class, public IBoxing, public ISerializable {
+  class Long : public Class, public IBoxing, public ISerializable, public IStorable {
     public:
 
       explicit Long(ls_std::long_type _value);
@@ -92,8 +93,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;
 
@@ -101,10 +104,12 @@ namespace ls_std {
 
       ls_std::long_type getValue() const;
       void setSerializable(std::shared_ptr<ISerializable> _serializable);
+      void setStorable(std::shared_ptr<IStorable> _storable);
 
     private:
 
       std::shared_ptr<ISerializable> serializable {};
+      std::shared_ptr<IStorable> storable {};
       ls_std::long_type value {};
   };
 }

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

@@ -10,6 +10,10 @@
 #include <gtest/gtest.h>
 #include "../../../source/boxing/Long.hpp"
 #include "../../../source/serialization/boxing/SerializableJSONLong.hpp"
+#include "../../TestHelper.hpp"
+#include "../../../source/io/File.hpp"
+#include "../../../source/io/FileWriter.hpp"
+#include "../../../source/io/StorableFile.hpp"
 
 namespace {
   class LongTest : public ::testing::Test {
@@ -338,6 +342,31 @@ namespace {
 
   // implementation
 
+  TEST_F(LongTest, load)
+  {
+    // preparation
+
+    std::shared_ptr<ls_std::Long> x = std::make_shared<ls_std::Long>();
+    std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_long.json";
+    ls_std::File file {path};
+    file.createNewFile();
+    ls_std::FileWriter writer {file};
+    writer.write(R"({"class":"Long","value":1990})");
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONLong>(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_EQ(1990, *x);
+
+    file.remove();
+  }
+
   TEST_F(LongTest, marshal)
   {
     std::shared_ptr<ls_std::Long> x = std::make_shared<ls_std::Long>(3);