FileReaderTest.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-18
  6. * Changed: 2021-09-17
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <TestHelper.hpp>
  11. #include <ls_std/ls_std.hpp>
  12. namespace
  13. {
  14. class FileReaderTest : public ::testing::Test
  15. {
  16. protected:
  17. FileReaderTest() = default;
  18. ~FileReaderTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. };
  24. TEST_F(FileReaderTest, constructor_file_does_not_exist)
  25. {
  26. ls_std::File file{TestHelper::getResourcesFolderLocation() + "does_not_exist.txt"};
  27. EXPECT_THROW({
  28. try
  29. {
  30. ls_std::FileReader reader{file};
  31. }
  32. catch (const ls_std::FileNotFoundException &_exception)
  33. {
  34. throw;
  35. }
  36. }, ls_std::FileNotFoundException);
  37. }
  38. TEST_F(FileReaderTest, read)
  39. {
  40. ls_std::File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  41. ls_std::FileReader reader{file};
  42. std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
  43. std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
  44. ls_std::byte_field content = reader.read();
  45. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  46. }
  47. TEST_F(FileReaderTest, read_file_gets_lost_in_between)
  48. {
  49. ls_std::File file{TestHelper::getResourcesFolderLocation() + "lost_readable_file.txt"};
  50. file.createNewFile();
  51. ls_std::FileReader reader{file};
  52. file.remove();
  53. EXPECT_THROW({
  54. try
  55. {
  56. ls_std::byte_field content = reader.read();
  57. }
  58. catch (const ls_std::FileOperationException &_exception)
  59. {
  60. throw;
  61. }
  62. }, ls_std::FileOperationException);
  63. }
  64. TEST_F(FileReaderTest, reset)
  65. {
  66. ls_std::File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  67. ls_std::FileReader reader{file};
  68. std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
  69. std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
  70. ls_std::byte_field content = reader.read();
  71. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  72. ls_std::File anotherFile{TestHelper::getResourcesFolderLocation() + "list_test/bla.txt"};
  73. reader.reset(anotherFile);
  74. expectedUnix = "nothing to say!" + ls_std::NewLine::getUnixNewLine();
  75. expectedWindows = "nothing to say!" + ls_std::NewLine::getWindowsNewLine();
  76. content = reader.read();
  77. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  78. }
  79. }