Browse Source

Increase test coverage for FileWriter class

Patrick-Christopher Mattulat 2 years ago
parent
commit
5dd0650378
2 changed files with 21 additions and 14 deletions
  1. 3 13
      source/ls_std/io/FileWriter.cpp
  2. 18 1
      test/cases/io/FileWriterTest.cpp

+ 3 - 13
source/ls_std/io/FileWriter.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2021-04-23
+ * Changed:         2021-09-18
  *
  * */
 
@@ -29,19 +29,9 @@ bool ls_std::FileWriter::write(const ls_std::byte_field &_data)
 {
   std::ofstream outputStream{};
   outputStream.open(this->file.getAbsoluteFilePath());
-  bool succeeded;
+  outputStream << _data;
 
-  if (outputStream << _data)
-  {
-    succeeded = true;
-  }
-  else
-  {
-    throw ls_std::FileOperationException{};
-  }
-
-  outputStream.close();
-  return succeeded;
+  return !outputStream.fail();
 }
 
 void ls_std::FileWriter::_init(ls_std::File &_file)

+ 18 - 1
test/cases/io/FileWriterTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2021-04-23
+ * Changed:         2021-09-18
  *
  * */
 
@@ -27,6 +27,23 @@ namespace
       {}
   };
 
+  TEST_F(FileWriterTest, constructor_file_does_not_exist)
+  {
+    std::string path = TestHelper::getResourcesFolderLocation() + "not_existing_file.txt";
+    ls_std::File file{path};
+
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::FileWriter writer{file};
+                   }
+                   catch (const ls_std::FileNotFoundException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::FileNotFoundException);
+  }
+
   TEST_F(FileWriterTest, reset)
   {
     std::string path = TestHelper::getResourcesFolderLocation() + "tmp_file_writer_test.txt";