FileWriterTest.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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-06
  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. 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. {
  35. try
  36. {
  37. FileWriter writer{file};
  38. }
  39. catch (const FileNotFoundException &_exception)
  40. {
  41. throw;
  42. }
  43. },
  44. FileNotFoundException);
  45. }
  46. TEST_F(FileWriterTest, reset)
  47. {
  48. string path = TestHelper::getResourcesFolderLocation() + "tmp-file-writer-test.txt";
  49. File file{path};
  50. file.createNewFile();
  51. FileWriter writer{file};
  52. ASSERT_TRUE(writer.write("Testing something!\n"));
  53. // reset
  54. path = TestHelper::getResourcesFolderLocation() + "tmp-file-writer-test2.txt";
  55. File anotherFile{path};
  56. anotherFile.createNewFile();
  57. writer.reset(anotherFile);
  58. ASSERT_TRUE(writer.write("Testing something else!\n"));
  59. // remove
  60. file.remove();
  61. ASSERT_FALSE(file.exists());
  62. anotherFile.remove();
  63. ASSERT_FALSE(anotherFile.exists());
  64. }
  65. TEST_F(FileWriterTest, write)
  66. {
  67. string path = TestHelper::getResourcesFolderLocation() + "tmp-file-writer-test.txt";
  68. File file{path};
  69. ASSERT_FALSE(file.exists());
  70. file.createNewFile();
  71. ASSERT_TRUE(file.exists());
  72. FileWriter writer{file};
  73. ASSERT_TRUE(writer.write("Testing something!\n"));
  74. file.remove();
  75. ASSERT_FALSE(file.exists());
  76. }
  77. }