FileOutputStreamTest.cpp 1.9 KB

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