FileOutputStreamTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2021-09-17
  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, constructor_file_does_not_exist)
  25. {
  26. std::string path = TestHelper::getResourcesFolderLocation() + "not_existing.txt";
  27. ls_std::File file{path};
  28. EXPECT_THROW({
  29. try
  30. {
  31. ls_std::FileOutputStream outputStream{file};
  32. }
  33. catch (const ls_std::FileNotFoundException &_exception)
  34. {
  35. throw;
  36. }
  37. }, ls_std::FileNotFoundException);
  38. }
  39. TEST_F(FileOutputStreamTest, write)
  40. {
  41. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_output_stream.txt";
  42. ls_std::File file{path};
  43. file.createNewFile();
  44. ASSERT_TRUE(file.exists());
  45. ls_std::FileOutputStream outputStream{file};
  46. ASSERT_TRUE(outputStream.write("Hello! "));
  47. ASSERT_TRUE(outputStream.write("How are you?"));
  48. outputStream.close();
  49. file.remove();
  50. ASSERT_FALSE(file.exists());
  51. }
  52. TEST_F(FileOutputStreamTest, write_with_another_appending_stream)
  53. {
  54. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_output_stream.txt";
  55. ls_std::File file{path};
  56. file.createNewFile();
  57. ASSERT_TRUE(file.exists());
  58. ls_std::FileOutputStream outputStream{file};
  59. ASSERT_TRUE(outputStream.write("Hello! "));
  60. ASSERT_TRUE(outputStream.write("How are you?"));
  61. outputStream.close();
  62. ls_std::FileOutputStream newOutputStream{file, true};
  63. ASSERT_TRUE(newOutputStream.write(" I'm fine! "));
  64. ASSERT_TRUE(newOutputStream.write("Thank you!"));
  65. newOutputStream.close();
  66. // validation
  67. ls_std::FileReader reader{file};
  68. ls_std::String content{reader.read()};
  69. ASSERT_TRUE(content.contains("Hello! How are you? I'm fine! Thank you!"));
  70. file.remove();
  71. ASSERT_FALSE(file.exists());
  72. }
  73. TEST_F(FileOutputStreamTest, write_no_permission_to_write)
  74. {
  75. #if defined(unix) || defined(__APPLE__)
  76. ls_std::File file{TestHelper::getResourcesFolderLocation() + "no_writable.txt"};
  77. #endif
  78. #ifdef _WIN32
  79. ls_std::File file{TestHelper::getResourcesFolderLocation() + "no_writable_windows.txt"};
  80. #endif
  81. ls_std::FileOutputStream outputStream{file};
  82. EXPECT_THROW({
  83. try
  84. {
  85. outputStream.write("something");
  86. }
  87. catch (const ls_std::FileOperationException &_exception)
  88. {
  89. throw;
  90. }
  91. }, ls_std::FileOperationException);
  92. }
  93. }