StorableFileTest.cpp 2.6 KB

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