FileWriterTest.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-17
  6. * Changed: 2021-09-18
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <TestHelper.hpp>
  12. namespace
  13. {
  14. class FileWriterTest : public ::testing::Test
  15. {
  16. protected:
  17. FileWriterTest() = default;
  18. ~FileWriterTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. };
  24. TEST_F(FileWriterTest, constructor_file_does_not_exist)
  25. {
  26. std::string path = TestHelper::getResourcesFolderLocation() + "not_existing_file.txt";
  27. ls_std::File file{path};
  28. EXPECT_THROW({
  29. try
  30. {
  31. ls_std::FileWriter writer{file};
  32. }
  33. catch (const ls_std::FileNotFoundException &_exception)
  34. {
  35. throw;
  36. }
  37. }, ls_std::FileNotFoundException);
  38. }
  39. TEST_F(FileWriterTest, reset)
  40. {
  41. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_file_writer_test.txt";
  42. ls_std::File file{path};
  43. file.createNewFile();
  44. ls_std::FileWriter writer{file};
  45. ASSERT_TRUE(writer.write("Testing something!\n"));
  46. // reset
  47. path = TestHelper::getResourcesFolderLocation() + "tmp_file_writer_test2.txt";
  48. ls_std::File anotherFile{path};
  49. anotherFile.createNewFile();
  50. writer.reset(anotherFile);
  51. ASSERT_TRUE(writer.write("Testing something else!\n"));
  52. // remove
  53. file.remove();
  54. ASSERT_FALSE(file.exists());
  55. anotherFile.remove();
  56. ASSERT_FALSE(anotherFile.exists());
  57. }
  58. TEST_F(FileWriterTest, write)
  59. {
  60. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_file_writer_test.txt";
  61. ls_std::File file{path};
  62. ASSERT_FALSE(file.exists());
  63. file.createNewFile();
  64. ASSERT_TRUE(file.exists());
  65. ls_std::FileWriter writer{file};
  66. ASSERT_TRUE(writer.write("Testing something!\n"));
  67. file.remove();
  68. ASSERT_FALSE(file.exists());
  69. }
  70. }