FileReaderTest.cpp 3.0 KB

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