FileReaderTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-18
  6. * Changed: 2023-03-25
  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. public:
  27. FileReaderTest() = default;
  28. ~FileReaderTest() override = default;
  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. }