Bläddra i källkod

Improved IStorable interface

- improved signature of "load" and "save" methods
- adjusted StorableFile class by implementing new
interface signatures
- adjusted tests for StorableFile class
Patrick 4 år sedan
förälder
incheckning
737b89714b
4 ändrade filer med 29 tillägg och 45 borttagningar
  1. 3 3
      source/io/IStorable.hpp
  2. 7 13
      source/io/StorableFile.cpp
  3. 4 6
      source/io/StorableFile.hpp
  4. 15 23
      test/cases/io/StorableFileTest.cpp

+ 3 - 3
source/io/IStorable.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-19
- * Changed:         2020-08-19
+ * Changed:         2020-08-22
  *
  * */
 
@@ -18,8 +18,8 @@ namespace ls_std {
       IStorable() = default;
       ~IStorable() = default;
 
-      virtual void load() = 0;
-      virtual void save() = 0;
+      virtual ls_std::byte* load() = 0;
+      virtual void save(ls_std::byte* _data) = 0;
   };
 }
 

+ 7 - 13
source/io/StorableFile.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-19
- * Changed:         2020-08-19
+ * Changed:         2020-08-22
  *
  * */
 
@@ -16,16 +16,15 @@ ls_std::StorableFile::StorableFile(const std::string &_path)
   this->_init(_path);
 }
 
-std::string ls_std::StorableFile::getContent()
+std::shared_ptr<ls_std::File> ls_std::StorableFile::getFile()
 {
-  return this->content;
+  return this->file;
 }
 
-void ls_std::StorableFile::load()
+ls_std::byte* ls_std::StorableFile::load()
 {
   ls_std::FileReader reader {*this->file};
-  ls_std::byte* data = reader.read();
-  this->content = {data, (size_t) this->file->getSize()};
+  return reader.read();
 }
 
 void ls_std::StorableFile::reset(const std::string &_path)
@@ -33,15 +32,10 @@ void ls_std::StorableFile::reset(const std::string &_path)
   this->_init(_path);
 }
 
-void ls_std::StorableFile::save()
+void ls_std::StorableFile::save(ls_std::byte* _data)
 {
   ls_std::FileWriter writer {*this->file};
-  writer.write(this->content.c_str());
-}
-
-void ls_std::StorableFile::setContent(const std::string &_content)
-{
-  this->content = _content;
+  writer.write(_data);
 }
 
 void ls_std::StorableFile::_init(const std::string &_path)

+ 4 - 6
source/io/StorableFile.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-19
- * Changed:         2020-08-19
+ * Changed:         2020-08-22
  *
  * */
 
@@ -22,15 +22,13 @@ namespace ls_std {
       explicit StorableFile(const std::string& _path);
       ~StorableFile() = default;
 
-      std::string getContent();
-      void load() override;
+      std::shared_ptr<ls_std::File> getFile();
+      ls_std::byte* load() override;
       void reset(const std::string& _path);
-      void save() override;
-      void setContent(const std::string& _content);
+      void save(ls_std::byte* _data) override;
 
     private:
 
-      std::string content {};
       std::shared_ptr<ls_std::File> file {};
 
       void _init(const std::string& _path);

+ 15 - 23
test/cases/io/StorableFileTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-19
- * Changed:         2020-08-19
+ * Changed:         2020-08-22
  *
  * */
 
@@ -25,40 +25,43 @@ namespace {
       void TearDown() override {}
   };
 
-  TEST_F(StorableFileTest, getContent)
+  TEST_F(StorableFileTest, getFile)
   {
     ls_std::StorableFile storableFile {this->fileLocation};
-    ASSERT_TRUE(storableFile.getContent().empty());
+    ASSERT_STREQ(this->fileLocation.c_str(), storableFile.getFile()->getAbsoluteFilePath().c_str());
   }
 
   TEST_F(StorableFileTest, load)
   {
     ls_std::StorableFile storableFile {this->fileLocation};
-    storableFile.load();
+    std::string content = {storableFile.load(), (size_t) storableFile.getFile()->getSize()};
+
     std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
     std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
 
-    ASSERT_TRUE(storableFile.getContent() == expectedUnix || storableFile.getContent() == expectedWindows);
+    ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
   }
 
   TEST_F(StorableFileTest, reset)
   {
     ls_std::StorableFile storableFile {this->fileLocation};
-    storableFile.load();
+    std::string content = {storableFile.load(), (size_t) storableFile.getFile()->getSize()};
+
     std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
     std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
 
-    ASSERT_TRUE(storableFile.getContent() == expectedUnix || storableFile.getContent() == expectedWindows);
+    ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
 
     // reset
 
     std::string anotherFileLocation = TestHelper::getResourcesFolderLocation() + "list_test/bla.txt";
     storableFile.reset(anotherFileLocation);
-    storableFile.load();
+    content = {storableFile.load(), (size_t) storableFile.getFile()->getSize()};
+
     expectedUnix = "nothing to say!" + ls_std::NewLine::getUnixNewLine();
     expectedWindows = "nothing to say!" + ls_std::NewLine::getWindowsNewLine();
 
-    ASSERT_TRUE(storableFile.getContent() == expectedUnix || storableFile.getContent() == expectedWindows);
+    ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
   }
 
   TEST_F(StorableFileTest, save)
@@ -71,21 +74,10 @@ namespace {
     std::string textUnix = "Testing save method!" + ls_std::NewLine::getUnixNewLine();
     std::string textWindows = "Testing save method!" + ls_std::NewLine::getWindowsNewLine();
 
-    ASSERT_TRUE(storableFile.getContent().empty());
-    storableFile.setContent(textUnix);
-    ASSERT_TRUE(storableFile.getContent() == textUnix || storableFile.getContent() == textWindows);
-    storableFile.save();
-    storableFile.load();
-    ASSERT_TRUE(storableFile.getContent() == textUnix || storableFile.getContent() == textWindows);
+    storableFile.save((char*) textUnix.c_str());
+    std::string content = storableFile.load();
+    ASSERT_TRUE(content == textUnix || content == textWindows);
 
     file.remove();
   }
-
-  TEST_F(StorableFileTest, setContent)
-  {
-    ls_std::StorableFile storableFile {this->fileLocation};
-    storableFile.setContent("Testing!");
-
-    ASSERT_STREQ("Testing!", storableFile.getContent().c_str());
-  }
 }