FileReaderTest.cpp 2.8 KB

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