FileWriterTest.cpp 2.1 KB

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