StorableFileTest.cpp 2.6 KB

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