StorableFileTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-19
  6. * Changed: 2022-05-14
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std_io_test.hpp>
  11. #include <ls_std/ls_std_io.hpp>
  12. namespace
  13. {
  14. class StorableFileTest : public ::testing::Test
  15. {
  16. protected:
  17. StorableFileTest() = default;
  18. ~StorableFileTest() override = default;
  19. std::string fileLocation = ls_std_io_test::TestHelper::getResourcesFolderLocation() + "simple.txt";
  20. void SetUp() override
  21. {}
  22. void TearDown() override
  23. {}
  24. };
  25. TEST_F(StorableFileTest, getFile)
  26. {
  27. ls::std::io::StorableFile storableFile{this->fileLocation};
  28. ASSERT_STREQ(this->fileLocation.c_str(), storableFile.getFile()->getAbsoluteFilePath().c_str());
  29. }
  30. TEST_F(StorableFileTest, load)
  31. {
  32. ls::std::io::StorableFile storableFile{this->fileLocation};
  33. ls::std::core::type::byte_field content = storableFile.load();
  34. std::string expectedUnix = "Hello!" + ls::std::io::NewLine::getUnixNewLine();
  35. std::string expectedWindows = "Hello!" + ls::std::io::NewLine::getWindowsNewLine();
  36. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  37. }
  38. TEST_F(StorableFileTest, reset)
  39. {
  40. ls::std::io::StorableFile storableFile{this->fileLocation};
  41. ls::std::core::type::byte_field content = storableFile.load();
  42. std::string expectedUnix = "Hello!" + ls::std::io::NewLine::getUnixNewLine();
  43. std::string expectedWindows = "Hello!" + ls::std::io::NewLine::getWindowsNewLine();
  44. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  45. // reset
  46. std::string anotherFileLocation = ls_std_io_test::TestHelper::getResourcesFolderLocation() + "list_test/bla.txt";
  47. storableFile.reset(anotherFileLocation);
  48. content = storableFile.load();
  49. expectedUnix = "nothing to say!" + ls::std::io::NewLine::getUnixNewLine();
  50. expectedWindows = "nothing to say!" + ls::std::io::NewLine::getWindowsNewLine();
  51. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  52. }
  53. TEST_F(StorableFileTest, save)
  54. {
  55. std::string path = ls_std_io_test::TestHelper::getResourcesFolderLocation() + "tmp_storable_file.txt";
  56. ls::std::io::File file{path};
  57. file.createNewFile();
  58. ls::std::io::StorableFile storableFile{path};
  59. ls::std::core::type::byte_field textUnix = "Testing save method!" + ls::std::io::NewLine::getUnixNewLine();
  60. ls::std::core::type::byte_field textWindows = "Testing save method!" + ls::std::io::NewLine::getWindowsNewLine();
  61. storableFile.save(textUnix);
  62. ls::std::core::type::byte_field content = storableFile.load();
  63. ASSERT_TRUE(content == textUnix || content == textWindows);
  64. file.remove();
  65. }
  66. }