FileReaderTest.cpp 2.9 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: 2022-05-12
  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::io::File file{TestHelper::getResourcesFolderLocation() + "does_not_exist.txt"};
  27. EXPECT_THROW({
  28. try
  29. {
  30. ls::std::io::FileReader reader{file};
  31. }
  32. catch (const ls::std::core::FileNotFoundException &_exception)
  33. {
  34. throw;
  35. }
  36. }, ls::std::core::FileNotFoundException);
  37. }
  38. TEST_F(FileReaderTest, read)
  39. {
  40. ls::std::io::File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  41. ls::std::io::FileReader reader{file};
  42. std::string expectedUnix = "Hello!" + ls::std::io::NewLine::getUnixNewLine();
  43. std::string expectedWindows = "Hello!" + ls::std::io::NewLine::getWindowsNewLine();
  44. ls::std::core::type::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::io::File file{TestHelper::getResourcesFolderLocation() + "lost_readable_file.txt"};
  50. file.createNewFile();
  51. ls::std::io::FileReader reader{file};
  52. file.remove();
  53. EXPECT_THROW({
  54. try
  55. {
  56. ls::std::core::type::byte_field content = reader.read();
  57. }
  58. catch (const ls::std::core::FileOperationException &_exception)
  59. {
  60. throw;
  61. }
  62. }, ls::std::core::FileOperationException);
  63. }
  64. TEST_F(FileReaderTest, reset)
  65. {
  66. ls::std::io::File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  67. ls::std::io::FileReader reader{file};
  68. std::string expectedUnix = "Hello!" + ls::std::io::NewLine::getUnixNewLine();
  69. std::string expectedWindows = "Hello!" + ls::std::io::NewLine::getWindowsNewLine();
  70. ls::std::core::type::byte_field content = reader.read();
  71. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  72. ls::std::io::File anotherFile{TestHelper::getResourcesFolderLocation() + "list_test/bla.txt"};
  73. reader.reset(anotherFile);
  74. expectedUnix = "nothing to say!" + ls::std::io::NewLine::getUnixNewLine();
  75. expectedWindows = "nothing to say!" + ls::std::io::NewLine::getWindowsNewLine();
  76. content = reader.read();
  77. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  78. }
  79. }