Ver Fonte

Added StorableFile class

- added StorableFile class, which is an IStorable
implementation
- added tests for StorableFile class
patrickmattulat há 4 anos atrás
pai
commit
295ecb2004

+ 5 - 2
CMakeLists.txt

@@ -67,7 +67,9 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/FileReader.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/NewLine.hpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/utils/RegexUtils.hpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/IStorable.hpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/IStorable.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/StorableFile.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/StorableFile.cpp)
 
 set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/IntegerTest.cpp
@@ -83,7 +85,8 @@ set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/LongTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/FileWriterTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/FileReaderTest.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/utils/RegexUtilsTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/utils/RegexUtilsTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/StorableFileTest.cpp)
 
 ##########################################################
 # Build

+ 54 - 0
source/io/StorableFile.cpp

@@ -0,0 +1,54 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-19
+ * Changed:         2020-08-19
+ *
+ * */
+
+#include "StorableFile.hpp"
+#include "FileReader.hpp"
+#include "FileWriter.hpp"
+
+ls_std::StorableFile::StorableFile(const std::string &_path)
+{
+  this->_init(_path);
+}
+
+std::string ls_std::StorableFile::getContent()
+{
+  return this->content;
+}
+
+void ls_std::StorableFile::load()
+{
+  ls_std::FileReader reader {*this->file};
+  ls_std::byte* data = reader.read();
+  this->content = {data, (size_t) this->file->getSize()};
+}
+
+void ls_std::StorableFile::reset(const std::string &_path)
+{
+  this->_init(_path);
+}
+
+void ls_std::StorableFile::save()
+{
+  ls_std::FileWriter writer {*this->file};
+  writer.write(this->content.c_str());
+}
+
+void ls_std::StorableFile::setContent(const std::string &_content)
+{
+  this->content = _content;
+}
+
+void ls_std::StorableFile::_init(const std::string &_path)
+{
+  if(this->file == nullptr) {
+    this->file = std::make_shared<ls_std::File>(_path);
+  } else {
+    this->file->reset(_path);
+  }
+}

+ 40 - 0
source/io/StorableFile.hpp

@@ -0,0 +1,40 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-19
+ * Changed:         2020-08-19
+ *
+ * */
+
+#ifndef STORABLE_FILE_HPP
+#define STORABLE_FILE_HPP
+
+#include <string>
+#include <memory>
+#include "IStorable.hpp"
+#include "File.hpp"
+
+namespace ls_std {
+  class StorableFile : public IStorable {
+    public:
+
+      explicit StorableFile(const std::string& _path);
+      ~StorableFile() = default;
+
+      std::string getContent();
+      void load() override;
+      void reset(const std::string& _path);
+      void save() override;
+      void setContent(const std::string& _content);
+
+    private:
+
+      std::string content {};
+      std::shared_ptr<ls_std::File> file {};
+
+      void _init(const std::string& _path);
+  };
+}
+
+#endif

+ 87 - 0
test/cases/io/StorableFileTest.cpp

@@ -0,0 +1,87 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-19
+ * Changed:         2020-08-19
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include "../../TestHelper.hpp"
+#include "../../../source/io/StorableFile.hpp"
+#include "../../../source/io/NewLine.hpp"
+
+namespace {
+  class StorableFileTest : public ::testing::Test {
+    protected:
+
+      StorableFileTest() = default;
+      ~StorableFileTest() override = default;
+
+      std::string fileLocation = TestHelper::getResourcesFolderLocation() + "simple.txt";
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(StorableFileTest, getContent)
+  {
+    ls_std::StorableFile storableFile {this->fileLocation};
+    ASSERT_TRUE(storableFile.getContent().empty());
+  }
+
+  TEST_F(StorableFileTest, load)
+  {
+    ls_std::StorableFile storableFile {this->fileLocation};
+    storableFile.load();
+    std::string expected = "Hello!" + ls_std::NewLine::get();
+
+    ASSERT_STREQ(expected.c_str(), storableFile.getContent().c_str());
+  }
+
+  TEST_F(StorableFileTest, reset)
+  {
+    ls_std::StorableFile storableFile {this->fileLocation};
+    storableFile.load();
+    std::string expected = "Hello!" + ls_std::NewLine::get();
+
+    ASSERT_STREQ(expected.c_str(), storableFile.getContent().c_str());
+
+    // reset
+
+    std::string anotherFileLocation = TestHelper::getResourcesFolderLocation() + "list_test/bla.txt";
+    storableFile.reset(anotherFileLocation);
+    storableFile.load();
+    expected = "nothing to say!" + ls_std::NewLine::get();
+
+    ASSERT_STREQ(expected.c_str(), storableFile.getContent().c_str());
+  }
+
+  TEST_F(StorableFileTest, save)
+  {
+    std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_file.txt";
+    ls_std::File file {path};
+    file.createNewFile();
+
+    ls_std::StorableFile storableFile {path};
+    std::string text = "Testing save method!" + ls_std::NewLine::get();
+
+    ASSERT_TRUE(storableFile.getContent().empty());
+    storableFile.setContent(text);
+    ASSERT_STREQ(text.c_str(), storableFile.getContent().c_str());
+    storableFile.save();
+    storableFile.load();
+    ASSERT_STREQ(text.c_str(), storableFile.getContent().c_str());
+
+    file.remove();
+  }
+
+  TEST_F(StorableFileTest, setContent)
+  {
+    ls_std::StorableFile storableFile {this->fileLocation};
+    storableFile.setContent("Testing!");
+
+    ASSERT_STREQ("Testing!", storableFile.getContent().c_str());
+  }
+}