FileOutputStreamTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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-22
  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. using namespace ::testing;
  18. namespace
  19. {
  20. class FileOutputStreamTest : public Test
  21. {
  22. protected:
  23. FileOutputStreamTest() = default;
  24. ~FileOutputStreamTest() override = default;
  25. void SetUp() override
  26. {}
  27. void TearDown() override
  28. {}
  29. };
  30. TEST_F(FileOutputStreamTest, constructor_file_does_not_exist)
  31. {
  32. string path = TestHelper::getResourcesFolderLocation() + "not-existing.txt";
  33. File file{path};
  34. EXPECT_THROW(
  35. {
  36. try
  37. {
  38. FileOutputStream outputStream{file};
  39. }
  40. catch (const FileNotFoundException &_exception)
  41. {
  42. throw;
  43. }
  44. },
  45. FileNotFoundException);
  46. }
  47. TEST_F(FileOutputStreamTest, write)
  48. {
  49. string path = TestHelper::getResourcesFolderLocation() + "tmp-output-stream.txt";
  50. File file{path};
  51. file.createNewFile();
  52. ASSERT_TRUE(file.exists());
  53. FileOutputStream outputStream{file};
  54. ASSERT_TRUE(outputStream.write("Hello! "));
  55. ASSERT_TRUE(outputStream.write("How are you?"));
  56. outputStream.close();
  57. file.remove();
  58. ASSERT_FALSE(file.exists());
  59. }
  60. TEST_F(FileOutputStreamTest, write_with_another_appending_stream)
  61. {
  62. string path = TestHelper::getResourcesFolderLocation() + "tmp-output-stream.txt";
  63. File file{path};
  64. file.createNewFile();
  65. ASSERT_TRUE(file.exists());
  66. FileOutputStream outputStream{file};
  67. ASSERT_TRUE(outputStream.write("Hello! "));
  68. ASSERT_TRUE(outputStream.write("How are you?"));
  69. outputStream.close();
  70. FileOutputStream newOutputStream{file, true};
  71. ASSERT_TRUE(newOutputStream.write(" I'm fine! "));
  72. ASSERT_TRUE(newOutputStream.write("Thank you!"));
  73. newOutputStream.close();
  74. // validation
  75. FileReader reader{file};
  76. string content{reader.read()};
  77. ASSERT_TRUE(content.find("Hello! How are you? I'm fine! Thank you!") != string::npos);
  78. file.remove();
  79. ASSERT_FALSE(file.exists());
  80. }
  81. TEST_F(FileOutputStreamTest, write_no_permission_to_write)
  82. {
  83. #if defined(unix) || defined(__APPLE__)
  84. File file{TestHelper::getResourcesFolderLocation() + "no-writable.txt"};
  85. #endif
  86. #ifdef _WIN32
  87. File file{TestHelper::getResourcesFolderLocation() + "no-writable-windows.txt"};
  88. #endif
  89. FileOutputStream outputStream{file};
  90. EXPECT_THROW(
  91. {
  92. try
  93. {
  94. outputStream.write("something");
  95. }
  96. catch (const FileOperationException &_exception)
  97. {
  98. throw;
  99. }
  100. },
  101. FileOperationException);
  102. }
  103. }