Browse Source

Increase test coverage for FileReader class

Patrick-Christopher Mattulat 2 years ago
parent
commit
c8c69987f4
2 changed files with 37 additions and 2 deletions
  1. 1 1
      source/ls_std/io/FileReader.cpp
  2. 36 1
      test/cases/io/FileReaderTest.cpp

+ 1 - 1
source/ls_std/io/FileReader.cpp

@@ -27,7 +27,7 @@ ls_std::byte_field ls_std::FileReader::read()
   data = new ls_std::byte[length];
   inputStream.read(data, length);
 
-  if (!inputStream)
+  if (inputStream.fail())
   {
     throw ls_std::FileOperationException{};
   }

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

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-18
- * Changed:         2021-04-23
+ * Changed:         2021-09-17
  *
  * */
 
@@ -27,6 +27,22 @@ namespace
       {}
   };
 
+  TEST_F(FileReaderTest, constructor_file_does_not_exist)
+  {
+    ls_std::File file{TestHelper::getResourcesFolderLocation() + "does_not_exist.txt"};
+
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::FileReader reader{file};
+                   }
+                   catch (const ls_std::FileNotFoundException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::FileNotFoundException);
+  }
+
   TEST_F(FileReaderTest, read)
   {
     ls_std::File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
@@ -38,6 +54,25 @@ namespace
     ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
   }
 
+  TEST_F(FileReaderTest, read_file_gets_lost_in_between)
+  {
+    ls_std::File file{TestHelper::getResourcesFolderLocation() + "lost_readable_file.txt"};
+    file.createNewFile();
+    ls_std::FileReader reader{file};
+    file.remove();
+
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::byte_field content = reader.read();
+                   }
+                   catch (const ls_std::FileOperationException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::FileOperationException);
+  }
+
   TEST_F(FileReaderTest, reset)
   {
     ls_std::File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};