소스 검색

Extended String class

- implemented IStorable interface
- extended tests of String class
pcmattulat 4 년 전
부모
커밋
c82aa968a2
3개의 변경된 파일59개의 추가작업 그리고 1개의 파일을 삭제
  1. 25 0
      source/boxing/String.cpp
  2. 6 1
      source/boxing/String.hpp
  3. 28 0
      test/cases/boxing/StringTest.cpp

+ 25 - 0
source/boxing/String.cpp

@@ -82,6 +82,17 @@ 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 {};
 
@@ -96,6 +107,16 @@ 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;
 }
@@ -141,6 +162,10 @@ void ls_std::String::setSerializable(std::shared_ptr<ISerializable> _serializabl
   this->serializable = std::move(_serializable);
 }
 
+void ls_std::String::setStorable(std::shared_ptr<IStorable> _storable) {
+  this->storable = std::move(_storable);
+}
+
 bool ls_std::String::startsWith(const std::string &_text) {
   return this->value.rfind(_text, 0) == 0;
 }

+ 6 - 1
source/boxing/String.hpp

@@ -13,11 +13,12 @@
 #include "IBoxing.hpp"
 #include "../base/Class.hpp"
 #include "../serialization/ISerializable.hpp"
+#include "../io/IStorable.hpp"
 #include <string>
 #include <memory>
 
 namespace ls_std {
-  class String : public Class, public IBoxing, public ISerializable {
+  class String : public Class, public IBoxing, public ISerializable, public IStorable {
     public:
 
       String();
@@ -56,8 +57,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;
 
@@ -71,6 +74,7 @@ namespace ls_std {
       std::string padRight(size_t _width, const char _fillCharacter);
       std::string reverse();
       void setSerializable(std::shared_ptr<ISerializable> _serializable);
+      void setStorable(std::shared_ptr<IStorable> _storable);
       bool startsWith(const std::string& _text);
       std::string toLowerCase();
       std::string toUpperCase();
@@ -78,6 +82,7 @@ namespace ls_std {
     private:
 
       std::shared_ptr<ISerializable> serializable {};
+      std::shared_ptr<IStorable> storable {};
       std::string value {};
 
       static std::string _buildCharacterChain(size_t _amount, const char _fillCharacter);

+ 28 - 0
test/cases/boxing/StringTest.cpp

@@ -10,6 +10,9 @@
 #include <gtest/gtest.h>
 #include "../../../source/boxing/String.hpp"
 #include "../../../source/serialization/boxing/SerializableJSONString.hpp"
+#include "../../TestHelper.hpp"
+#include "../../../source/io/FileWriter.hpp"
+#include "../../../source/io/StorableFile.hpp"
 
 namespace {
   class StringTest : public ::testing::Test {
@@ -92,6 +95,31 @@ namespace {
 
   // implementation
 
+  TEST_F(StringTest, load)
+  {
+    // preparation
+
+    std::shared_ptr<ls_std::String> x = std::make_shared<ls_std::String>();
+    std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_string.json";
+    ls_std::File file {path};
+    file.createNewFile();
+    ls_std::FileWriter writer {file};
+    writer.write(R"({"class":"String","value":"Hello!"})");
+
+    auto serializable = std::make_shared<ls_std::SerializableJSONString>(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_STREQ("Hello!", *x);
+
+    file.remove();
+  }
+
   TEST_F(StringTest, marshal)
   {
     std::shared_ptr<ls_std::String> x = std::make_shared<ls_std::String>("Hello!");