FileOutputStreamTest.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2021-04-23
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <TestHelper.hpp>
  11. #include <ls_std/ls_std.hpp>
  12. namespace
  13. {
  14. class FileOutputStreamTest : public ::testing::Test
  15. {
  16. protected:
  17. FileOutputStreamTest() = default;
  18. ~FileOutputStreamTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. };
  24. TEST_F(FileOutputStreamTest, write)
  25. {
  26. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_output_stream.txt";
  27. ls_std::File file{path};
  28. file.createNewFile();
  29. ASSERT_TRUE(file.exists());
  30. ls_std::FileOutputStream outputStream{file};
  31. ASSERT_TRUE(outputStream.write("Hello! "));
  32. ASSERT_TRUE(outputStream.write("How are you?"));
  33. outputStream.close();
  34. file.remove();
  35. ASSERT_FALSE(file.exists());
  36. }
  37. TEST_F(FileOutputStreamTest, append)
  38. {
  39. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_output_stream.txt";
  40. ls_std::File file{path};
  41. file.createNewFile();
  42. ASSERT_TRUE(file.exists());
  43. ls_std::FileOutputStream outputStream{file};
  44. ASSERT_TRUE(outputStream.write("Hello! "));
  45. ASSERT_TRUE(outputStream.write("How are you?"));
  46. outputStream.close();
  47. ls_std::FileOutputStream newOutputStream{file, true};
  48. ASSERT_TRUE(newOutputStream.write(" I'm fine! "));
  49. ASSERT_TRUE(newOutputStream.write("Thank you!"));
  50. newOutputStream.close();
  51. // validation
  52. ls_std::FileReader reader{file};
  53. ls_std::String content{reader.read()};
  54. ASSERT_TRUE(content.contains("Hello! How are you? I'm fine! Thank you!"));
  55. file.remove();
  56. ASSERT_FALSE(file.exists());
  57. }
  58. }