FileWriterTest.cpp 2.1 KB

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