FileReaderTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-18
  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::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. {
  35. try
  36. {
  37. FileReader reader{file};
  38. }
  39. catch (const FileNotFoundException &_exception)
  40. {
  41. throw;
  42. }
  43. },
  44. FileNotFoundException);
  45. }
  46. TEST_F(FileReaderTest, read)
  47. {
  48. File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  49. FileReader reader{file};
  50. string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
  51. string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();
  52. byte_field content = reader.read();
  53. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  54. }
  55. TEST_F(FileReaderTest, read_file_gets_lost_in_between)
  56. {
  57. File file{TestHelper::getResourcesFolderLocation() + "lost-readable-file.txt"};
  58. file.createNewFile();
  59. FileReader reader{file};
  60. file.remove();
  61. EXPECT_THROW(
  62. {
  63. try
  64. {
  65. byte_field content = reader.read();
  66. }
  67. catch (const FileOperationException &_exception)
  68. {
  69. throw;
  70. }
  71. },
  72. FileOperationException);
  73. }
  74. TEST_F(FileReaderTest, reset)
  75. {
  76. File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  77. FileReader reader{file};
  78. string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
  79. string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();
  80. byte_field content = reader.read();
  81. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  82. File anotherFile{TestHelper::getResourcesFolderLocation() + "list-test/bla.txt"};
  83. reader.reset(anotherFile);
  84. expectedUnix = "nothing to say!" + NewLine::getUnixNewLine();
  85. expectedWindows = "nothing to say!" + NewLine::getWindowsNewLine();
  86. content = reader.read();
  87. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  88. }
  89. }