StorableFileTest.cpp 2.6 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: 2021-09-18
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <TestHelper.hpp>
  11. #include <ls_std/ls_std.hpp>
  12. namespace
  13. {
  14. class StorableFileTest : public ::testing::Test
  15. {
  16. protected:
  17. StorableFileTest() = default;
  18. ~StorableFileTest() override = default;
  19. std::string fileLocation = TestHelper::getResourcesFolderLocation() + "simple.txt";
  20. void SetUp() override
  21. {}
  22. void TearDown() override
  23. {}
  24. };
  25. TEST_F(StorableFileTest, getFile)
  26. {
  27. ls_std::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::StorableFile storableFile{this->fileLocation};
  33. ls_std::byte_field content = storableFile.load();
  34. std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
  35. std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
  36. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  37. }
  38. TEST_F(StorableFileTest, reset)
  39. {
  40. ls_std::StorableFile storableFile{this->fileLocation};
  41. ls_std::byte_field content = storableFile.load();
  42. std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
  43. std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
  44. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  45. // reset
  46. std::string anotherFileLocation = TestHelper::getResourcesFolderLocation() + "list_test/bla.txt";
  47. storableFile.reset(anotherFileLocation);
  48. content = storableFile.load();
  49. expectedUnix = "nothing to say!" + ls_std::NewLine::getUnixNewLine();
  50. expectedWindows = "nothing to say!" + ls_std::NewLine::getWindowsNewLine();
  51. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  52. }
  53. TEST_F(StorableFileTest, save)
  54. {
  55. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_file.txt";
  56. ls_std::File file{path};
  57. file.createNewFile();
  58. ls_std::StorableFile storableFile{path};
  59. ls_std::byte_field textUnix = "Testing save method!" + ls_std::NewLine::getUnixNewLine();
  60. ls_std::byte_field textWindows = "Testing save method!" + ls_std::NewLine::getWindowsNewLine();
  61. storableFile.save(textUnix);
  62. ls_std::byte_field content = storableFile.load();
  63. ASSERT_TRUE(content == textUnix || content == textWindows);
  64. file.remove();
  65. }
  66. }