FileOutputStreamTest.cpp 1.8 KB

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