FileWriterTest.cpp 2.2 KB

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