FileOutputStreamTest.cpp 3.3 KB

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