FileReaderTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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-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::core::type;
  15. using namespace ls::std::io;
  16. using namespace ls::std::test;
  17. using namespace ::std;
  18. using namespace ::testing;
  19. namespace
  20. {
  21. class FileReaderTest : public Test
  22. {
  23. protected:
  24. FileReaderTest() = default;
  25. ~FileReaderTest() override = default;
  26. void SetUp() override
  27. {}
  28. void TearDown() override
  29. {}
  30. };
  31. TEST_F(FileReaderTest, constructor_file_does_not_exist)
  32. {
  33. File file{TestHelper::getResourcesFolderLocation() + "does-not-exist.txt"};
  34. EXPECT_THROW(
  35. {
  36. try
  37. {
  38. FileReader reader{file};
  39. }
  40. catch (const FileNotFoundException &_exception)
  41. {
  42. throw;
  43. }
  44. },
  45. FileNotFoundException);
  46. }
  47. TEST_F(FileReaderTest, read)
  48. {
  49. File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  50. FileReader reader{file};
  51. string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
  52. string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();
  53. byte_field content = reader.read();
  54. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  55. }
  56. TEST_F(FileReaderTest, read_file_gets_lost_in_between)
  57. {
  58. File file{TestHelper::getResourcesFolderLocation() + "lost-readable-file.txt"};
  59. file.createNewFile();
  60. FileReader reader{file};
  61. file.remove();
  62. EXPECT_THROW(
  63. {
  64. try
  65. {
  66. byte_field content = reader.read();
  67. }
  68. catch (const FileOperationException &_exception)
  69. {
  70. throw;
  71. }
  72. },
  73. FileOperationException);
  74. }
  75. TEST_F(FileReaderTest, reset)
  76. {
  77. File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  78. FileReader reader{file};
  79. string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
  80. string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();
  81. byte_field content = reader.read();
  82. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  83. File anotherFile{TestHelper::getResourcesFolderLocation() + "list-test/bla.txt"};
  84. reader.reset(anotherFile);
  85. expectedUnix = "nothing to say!" + NewLine::getUnixNewLine();
  86. expectedWindows = "nothing to say!" + NewLine::getWindowsNewLine();
  87. content = reader.read();
  88. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  89. }
  90. }