FileReaderTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-18
  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::core::type;
  15. using namespace ls::std::io;
  16. using namespace ls_std_test;
  17. using namespace ::std;
  18. namespace
  19. {
  20. class FileReaderTest : public ::testing::Test
  21. {
  22. protected:
  23. FileReaderTest() = default;
  24. ~FileReaderTest() override = default;
  25. void SetUp() override
  26. {}
  27. void TearDown() override
  28. {}
  29. };
  30. TEST_F(FileReaderTest, constructor_file_does_not_exist)
  31. {
  32. File file{TestHelper::getResourcesFolderLocation() + "does_not_exist.txt"};
  33. EXPECT_THROW({
  34. try
  35. {
  36. FileReader reader{file};
  37. }
  38. catch (const FileNotFoundException &_exception)
  39. {
  40. throw;
  41. }
  42. }, FileNotFoundException);
  43. }
  44. TEST_F(FileReaderTest, read)
  45. {
  46. File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  47. FileReader reader{file};
  48. string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
  49. string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();
  50. byte_field content = reader.read();
  51. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  52. }
  53. TEST_F(FileReaderTest, read_file_gets_lost_in_between)
  54. {
  55. File file{TestHelper::getResourcesFolderLocation() + "lost_readable_file.txt"};
  56. file.createNewFile();
  57. FileReader reader{file};
  58. file.remove();
  59. EXPECT_THROW({
  60. try
  61. {
  62. byte_field content = reader.read();
  63. }
  64. catch (const FileOperationException &_exception)
  65. {
  66. throw;
  67. }
  68. }, FileOperationException);
  69. }
  70. TEST_F(FileReaderTest, reset)
  71. {
  72. File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  73. FileReader reader{file};
  74. string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
  75. string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();
  76. byte_field content = reader.read();
  77. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  78. File anotherFile{TestHelper::getResourcesFolderLocation() + "list_test/bla.txt"};
  79. reader.reset(anotherFile);
  80. expectedUnix = "nothing to say!" + NewLine::getUnixNewLine();
  81. expectedWindows = "nothing to say!" + NewLine::getWindowsNewLine();
  82. content = reader.read();
  83. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  84. }
  85. }