StorableFileTest.cpp 2.6 KB

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