StorableFileTest.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-19
  6. * Changed: 2023-03-25
  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. public:
  24. StorableFileTest() = default;
  25. ~StorableFileTest() override = default;
  26. string fileLocation = TestHelper::getResourcesFolderLocation() + "simple.txt";
  27. };
  28. TEST_F(StorableFileTest, getFile)
  29. {
  30. StorableFile storableFile{this->fileLocation};
  31. ASSERT_STREQ(this->fileLocation.c_str(), storableFile.getFile()->getAbsoluteFilePath().c_str());
  32. }
  33. TEST_F(StorableFileTest, load)
  34. {
  35. StorableFile storableFile{this->fileLocation};
  36. byte_field content = storableFile.load();
  37. string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
  38. string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();
  39. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  40. }
  41. TEST_F(StorableFileTest, reset)
  42. {
  43. StorableFile storableFile{this->fileLocation};
  44. byte_field content = storableFile.load();
  45. string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
  46. string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();
  47. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  48. // reset
  49. string anotherFileLocation = TestHelper::getResourcesFolderLocation() + "list-test/bla.txt";
  50. storableFile.reset(anotherFileLocation);
  51. content = storableFile.load();
  52. expectedUnix = "nothing to say!" + NewLine::getUnixNewLine();
  53. expectedWindows = "nothing to say!" + NewLine::getWindowsNewLine();
  54. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  55. }
  56. TEST_F(StorableFileTest, save)
  57. {
  58. string path = TestHelper::getResourcesFolderLocation() + "tmp-storable-file.txt";
  59. File file{path};
  60. file.createNewFile();
  61. StorableFile storableFile{path};
  62. byte_field textUnix = "Testing save method!" + NewLine::getUnixNewLine();
  63. byte_field textWindows = "Testing save method!" + NewLine::getWindowsNewLine();
  64. storableFile.save(textUnix);
  65. byte_field content = storableFile.load();
  66. ASSERT_TRUE(content == textUnix || content == textWindows);
  67. file.remove();
  68. }
  69. }