StorableFileTest.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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-06
  7. *
  8. * */
  9. #include <classes/TestHelper.hpp>
  10. #include <gtest/gtest.h>
  11. #include <ls-std/ls-std-io.hpp>
  12. using namespace ls::std::core::type;
  13. using namespace ls::std::io;
  14. using namespace ls::std::test;
  15. using namespace ::std;
  16. namespace
  17. {
  18. class StorableFileTest : public ::testing::Test
  19. {
  20. protected:
  21. StorableFileTest() = default;
  22. ~StorableFileTest() override = default;
  23. string fileLocation = TestHelper::getResourcesFolderLocation() + "simple.txt";
  24. void SetUp() override
  25. {}
  26. void TearDown() override
  27. {}
  28. };
  29. TEST_F(StorableFileTest, getFile)
  30. {
  31. 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. byte_field content = storableFile.load();
  38. string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
  39. 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. 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. string path = TestHelper::getResourcesFolderLocation() + "tmp-storable-file.txt";
  60. File file{path};
  61. file.createNewFile();
  62. StorableFile storableFile{path};
  63. byte_field textUnix = "Testing save method!" + NewLine::getUnixNewLine();
  64. byte_field textWindows = "Testing save method!" + NewLine::getWindowsNewLine();
  65. storableFile.save(textUnix);
  66. byte_field content = storableFile.load();
  67. ASSERT_TRUE(content == textUnix || content == textWindows);
  68. file.remove();
  69. }
  70. }