StorableFileTest.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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-25
  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, getFile)
  23. {
  24. ls_std::StorableFile storableFile {this->fileLocation};
  25. ASSERT_STREQ(this->fileLocation.c_str(), storableFile.getFile()->getAbsoluteFilePath().c_str());
  26. }
  27. TEST_F(StorableFileTest, load)
  28. {
  29. ls_std::StorableFile storableFile {this->fileLocation};
  30. ls_std::byte_field content = storableFile.load();
  31. std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
  32. std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
  33. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  34. }
  35. TEST_F(StorableFileTest, reset)
  36. {
  37. ls_std::StorableFile storableFile {this->fileLocation};
  38. ls_std::byte_field content = storableFile.load();
  39. std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
  40. std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
  41. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  42. // reset
  43. std::string anotherFileLocation = TestHelper::getResourcesFolderLocation() + "list_test/bla.txt";
  44. storableFile.reset(anotherFileLocation);
  45. content = storableFile.load();
  46. expectedUnix = "nothing to say!" + ls_std::NewLine::getUnixNewLine();
  47. expectedWindows = "nothing to say!" + ls_std::NewLine::getWindowsNewLine();
  48. ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  49. }
  50. TEST_F(StorableFileTest, save)
  51. {
  52. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_file.txt";
  53. ls_std::File file {path};
  54. file.createNewFile();
  55. ls_std::StorableFile storableFile {path};
  56. ls_std::byte_field textUnix = "Testing save method!" + ls_std::NewLine::getUnixNewLine();
  57. ls_std::byte_field textWindows = "Testing save method!" + ls_std::NewLine::getWindowsNewLine();
  58. storableFile.save(textUnix);
  59. ls_std::byte_field content = storableFile.load();
  60. ASSERT_TRUE(content == textUnix || content == textWindows);
  61. file.remove();
  62. }
  63. }