Pārlūkot izejas kodu

Extended FileReader class

- added "reset" method to FileReader class for
setting a new file to read
- extended tests for FileReader class
patrickmattulat 4 gadi atpakaļ
vecāks
revīzija
a0e02f9ff4

+ 14 - 3
source/io/FileReader.cpp

@@ -15,9 +15,7 @@
 ls_std::FileReader::FileReader(File &_file) : Class("FileReader"),
 file(_file)
 {
-  if(!_file.exists()) {
-    throw ls_std::FileNotFoundException {};
-  }
+  ls_std::FileReader::_init(_file);
 }
 
 ls_std::byte * ls_std::FileReader::read()
@@ -35,3 +33,16 @@ ls_std::byte * ls_std::FileReader::read()
   inputStream.close();
   return data;
 }
+
+void ls_std::FileReader::reset(File &_file)
+{
+  ls_std::FileReader::_init(_file);
+  this->file = _file;
+}
+
+void ls_std::FileReader::_init(File &_file)
+{
+  if(!_file.exists()) {
+    throw ls_std::FileNotFoundException {};
+  }
+}

+ 4 - 1
source/io/FileReader.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2020-08-17
+ * Changed:         2020-08-19
  *
  * */
 
@@ -22,10 +22,13 @@ namespace ls_std {
       ~FileReader() = default;
 
       ls_std::byte* read() override;
+      void reset(File& _file);
 
     private:
 
       File file;
+
+      static void _init(File &_file);
   };
 }
 

+ 20 - 1
test/cases/io/FileReaderTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-18
- * Changed:         2020-08-18
+ * Changed:         2020-08-19
  *
  * */
 
@@ -33,4 +33,23 @@ namespace {
     std::string content {data, (size_t) file.getSize()};
     ASSERT_STREQ(expected.c_str(), content.c_str());
   }
+
+  TEST_F(FileReaderTest, reset)
+  {
+    ls_std::File file {TestHelper::getResourcesFolderLocation() + "simple.txt"};
+    ls_std::FileReader reader {file};
+    std::string expected = "Hello!" + ls_std::NewLine::get();
+
+    ls_std::byte* data = reader.read();
+    std::string content {data, (size_t) file.getSize()};
+    ASSERT_STREQ(expected.c_str(), content.c_str());
+
+    ls_std::File anotherFile {TestHelper::getResourcesFolderLocation() + "list_test/bla.txt"};
+    reader.reset(anotherFile);
+    expected = "nothing to say!" + ls_std::NewLine::get();
+
+    data = reader.read();
+    content = {data, (size_t) anotherFile.getSize()};
+    ASSERT_STREQ(expected.c_str(), content.c_str());
+  }
 }