FileWriterTest.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-17
  6. * Changed: 2023-03-25
  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 ls::std::core::FileNotFoundException;
  14. using ls::std::io::File;
  15. using ls::std::io::FileWriter;
  16. using ls::std::test::TestHelper;
  17. using std::string;
  18. using testing::Test;
  19. namespace
  20. {
  21. class FileWriterTest : public Test
  22. {
  23. public:
  24. FileWriterTest() = default;
  25. ~FileWriterTest() override = default;
  26. };
  27. TEST_F(FileWriterTest, constructor_file_does_not_exist)
  28. {
  29. string path = TestHelper::getResourcesFolderLocation() + "not-existing-file.txt";
  30. File file{path};
  31. EXPECT_THROW(
  32. {
  33. try
  34. {
  35. FileWriter writer{file};
  36. }
  37. catch (const FileNotFoundException &_exception)
  38. {
  39. throw;
  40. }
  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. }