StorableFileTest.cpp 2.4 KB

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