FileWriterTest.cpp 2.1 KB

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