FileReaderTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Co-Author: Claude Sonnet 4.6 (LLM)
  4. * Company: Lynar Studios
  5. * E-Mail: webmaster@lynarstudios.com
  6. * Created: 2020-08-18
  7. * Changed: 2026-06-23
  8. *
  9. * */
  10. #include <classes/TestHelper.hpp>
  11. #include <gtest/gtest.h>
  12. #include <ls-std/ls-std-core.hpp>
  13. #include <ls-std/ls-std-io.hpp>
  14. using ls::standard::core::FileNotFoundException;
  15. using ls::standard::core::FileOperationException;
  16. using ls::standard::core::type::byte_field;
  17. using ls::standard::io::File;
  18. using ls::standard::io::FileReader;
  19. using ls::standard::io::NewLine;
  20. using ls::standard::test::TestHelper;
  21. using std::string;
  22. using testing::Test;
  23. namespace
  24. {
  25. class FileReaderTest : public Test
  26. {
  27. public:
  28. FileReaderTest() = default;
  29. ~FileReaderTest() override = default;
  30. };
  31. TEST_F(FileReaderTest, constructor_file_does_not_exist)
  32. {
  33. const 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. const File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  50. FileReader reader{file};
  51. const string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
  52. const string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();
  53. const 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. const 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. }