FileOutputStreamTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2023-02-06
  7. *
  8. * */
  9. #include <classes/TestHelper.hpp>
  10. #include <gtest/gtest.h>
  11. #include <ls-std/ls-std-core.hpp>
  12. #include <ls-std/ls-std-io.hpp>
  13. using namespace ls::std::core;
  14. using namespace ls::std::io;
  15. using namespace ::std;
  16. using namespace ls::std::test;
  17. namespace
  18. {
  19. class FileOutputStreamTest : public ::testing::Test
  20. {
  21. protected:
  22. FileOutputStreamTest() = default;
  23. ~FileOutputStreamTest() override = default;
  24. void SetUp() override
  25. {}
  26. void TearDown() override
  27. {}
  28. };
  29. TEST_F(FileOutputStreamTest, constructor_file_does_not_exist)
  30. {
  31. string path = TestHelper::getResourcesFolderLocation() + "not-existing.txt";
  32. File file{path};
  33. EXPECT_THROW(
  34. {
  35. try
  36. {
  37. FileOutputStream outputStream{file};
  38. }
  39. catch (const FileNotFoundException &_exception)
  40. {
  41. throw;
  42. }
  43. },
  44. FileNotFoundException);
  45. }
  46. TEST_F(FileOutputStreamTest, write)
  47. {
  48. string path = TestHelper::getResourcesFolderLocation() + "tmp-output-stream.txt";
  49. File file{path};
  50. file.createNewFile();
  51. ASSERT_TRUE(file.exists());
  52. FileOutputStream outputStream{file};
  53. ASSERT_TRUE(outputStream.write("Hello! "));
  54. ASSERT_TRUE(outputStream.write("How are you?"));
  55. outputStream.close();
  56. file.remove();
  57. ASSERT_FALSE(file.exists());
  58. }
  59. TEST_F(FileOutputStreamTest, write_with_another_appending_stream)
  60. {
  61. string path = TestHelper::getResourcesFolderLocation() + "tmp-output-stream.txt";
  62. File file{path};
  63. file.createNewFile();
  64. ASSERT_TRUE(file.exists());
  65. FileOutputStream outputStream{file};
  66. ASSERT_TRUE(outputStream.write("Hello! "));
  67. ASSERT_TRUE(outputStream.write("How are you?"));
  68. outputStream.close();
  69. FileOutputStream newOutputStream{file, true};
  70. ASSERT_TRUE(newOutputStream.write(" I'm fine! "));
  71. ASSERT_TRUE(newOutputStream.write("Thank you!"));
  72. newOutputStream.close();
  73. // validation
  74. FileReader reader{file};
  75. string content{reader.read()};
  76. ASSERT_TRUE(content.find("Hello! How are you? I'm fine! Thank you!") != string::npos);
  77. file.remove();
  78. ASSERT_FALSE(file.exists());
  79. }
  80. TEST_F(FileOutputStreamTest, write_no_permission_to_write)
  81. {
  82. #if defined(unix) || defined(__APPLE__)
  83. File file{TestHelper::getResourcesFolderLocation() + "no-writable.txt"};
  84. #endif
  85. #ifdef _WIN32
  86. File file{TestHelper::getResourcesFolderLocation() + "no-writable-windows.txt"};
  87. #endif
  88. FileOutputStream outputStream{file};
  89. EXPECT_THROW(
  90. {
  91. try
  92. {
  93. outputStream.write("something");
  94. }
  95. catch (const FileOperationException &_exception)
  96. {
  97. throw;
  98. }
  99. },
  100. FileOperationException);
  101. }
  102. }