FileOutputStreamTest.cpp 3.1 KB

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