StorableFileTest.cpp 2.6 KB

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