FileWriterTest.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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-23
  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. protected:
  24. FileWriterTest() = default;
  25. ~FileWriterTest() override = default;
  26. void SetUp() override
  27. {}
  28. void TearDown() override
  29. {}
  30. };
  31. TEST_F(FileWriterTest, constructor_file_does_not_exist)
  32. {
  33. string path = TestHelper::getResourcesFolderLocation() + "not-existing-file.txt";
  34. File file{path};
  35. EXPECT_THROW(
  36. {
  37. try
  38. {
  39. FileWriter writer{file};
  40. }
  41. catch (const FileNotFoundException &_exception)
  42. {
  43. throw;
  44. }
  45. },
  46. FileNotFoundException);
  47. }
  48. TEST_F(FileWriterTest, reset)
  49. {
  50. string path = TestHelper::getResourcesFolderLocation() + "tmp-file-writer-test.txt";
  51. File file{path};
  52. file.createNewFile();
  53. FileWriter writer{file};
  54. ASSERT_TRUE(writer.write("Testing something!\n"));
  55. // reset
  56. path = TestHelper::getResourcesFolderLocation() + "tmp-file-writer-test2.txt";
  57. File anotherFile{path};
  58. anotherFile.createNewFile();
  59. writer.reset(anotherFile);
  60. ASSERT_TRUE(writer.write("Testing something else!\n"));
  61. // remove
  62. file.remove();
  63. ASSERT_FALSE(file.exists());
  64. anotherFile.remove();
  65. ASSERT_FALSE(anotherFile.exists());
  66. }
  67. TEST_F(FileWriterTest, write)
  68. {
  69. string path = TestHelper::getResourcesFolderLocation() + "tmp-file-writer-test.txt";
  70. File file{path};
  71. ASSERT_FALSE(file.exists());
  72. file.createNewFile();
  73. ASSERT_TRUE(file.exists());
  74. FileWriter writer{file};
  75. ASSERT_TRUE(writer.write("Testing something!\n"));
  76. file.remove();
  77. ASSERT_FALSE(file.exists());
  78. }
  79. }