FileTest.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-15
  6. * Changed: 2021-04-23
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <TestHelper.hpp>
  11. #include <ls_std/ls_std.hpp>
  12. namespace
  13. {
  14. class FileTest : public ::testing::Test
  15. {
  16. protected:
  17. FileTest() = default;
  18. ~FileTest() override = default;
  19. std::string fileLocation = TestHelper::getResourcesFolderLocation() + "simple.txt";
  20. void SetUp() override
  21. {}
  22. void TearDown() override
  23. {}
  24. };
  25. // comparison operators
  26. TEST_F(FileTest, operatorEqual)
  27. {
  28. ls_std::File file{this->fileLocation};
  29. ls_std::File file2{this->fileLocation};
  30. ls_std::File file3{"/home/bla/text.txt"};
  31. ls_std::File file4{"\\home/bla\\text.txt"};
  32. ASSERT_TRUE(file == file2);
  33. ASSERT_TRUE(file2 == file);
  34. ASSERT_TRUE(file3 == file4);
  35. ASSERT_TRUE(file4 == file3);
  36. }
  37. TEST_F(FileTest, operatorNotEqual)
  38. {
  39. ls_std::File file{this->fileLocation};
  40. ls_std::File file2{TestHelper::getResourcesFolderLocation() + "app.exe"};
  41. ASSERT_TRUE(file != file2);
  42. ASSERT_TRUE(file2 != file);
  43. }
  44. // additional functionality
  45. TEST_F(FileTest, canExecute)
  46. {
  47. #ifdef _WIN32
  48. ls_std::File executableFile {TestHelper::getResourcesFolderLocation() + "app.exe"};
  49. #endif
  50. #if defined(unix) || defined(__APPLE__)
  51. ls_std::File executableFile{TestHelper::getResourcesFolderLocation() + "app"};
  52. #endif
  53. ASSERT_TRUE(executableFile.canExecute());
  54. }
  55. TEST_F(FileTest, canExecuteNegative)
  56. {
  57. ls_std::File file{this->fileLocation};
  58. ASSERT_FALSE(file.canExecute());
  59. }
  60. TEST_F(FileTest, canRead)
  61. {
  62. ls_std::File readableFile{this->fileLocation};
  63. ASSERT_TRUE(readableFile.canRead());
  64. }
  65. TEST_F(FileTest, canWrite)
  66. {
  67. ls_std::File readableFile{this->fileLocation};
  68. ASSERT_TRUE(readableFile.canWrite());
  69. }
  70. TEST_F(FileTest, canWriteNegative)
  71. {
  72. #if defined(unix) || defined(__APPLE__)
  73. ls_std::File noWritableFile{TestHelper::getResourcesFolderLocation() + "no_writable.txt"};
  74. ASSERT_FALSE(noWritableFile.canWrite());
  75. #endif
  76. #ifdef _WIN32
  77. ls_std::File noWritableFile {TestHelper::getResourcesFolderLocation() + "no_writable_windows.txt"};
  78. ASSERT_FALSE(noWritableFile.canWrite());
  79. #endif
  80. }
  81. TEST_F(FileTest, createNewFileAndRemove)
  82. {
  83. ls_std::File file{TestHelper::getResourcesFolderLocation() + "tmp.txt"};
  84. ASSERT_FALSE(file.exists());
  85. file.createNewFile();
  86. ASSERT_TRUE(file.exists());
  87. file.remove();
  88. ASSERT_FALSE(file.exists());
  89. }
  90. TEST_F(FileTest, exists)
  91. {
  92. ls_std::File file{this->fileLocation};
  93. ls_std::File directory{TestHelper::getResourcesFolderLocation()};
  94. ASSERT_TRUE(file.exists());
  95. ASSERT_TRUE(directory.exists());
  96. }
  97. TEST_F(FileTest, existsNegative)
  98. {
  99. ls_std::File file{TestHelper::getResourcesFolderLocation() + "bla.txt"};
  100. ASSERT_FALSE(file.exists());
  101. }
  102. TEST_F(FileTest, getAbsoluteFilePath)
  103. {
  104. ls_std::File file{this->fileLocation};
  105. ASSERT_STREQ(this->fileLocation.c_str(), file.getAbsoluteFilePath().c_str());
  106. std::string s = {ls_std::FilePathSeparator::get()};
  107. std::string wrongFilePath = "home" + s + s + s + "user" + s + "bla" + s + s + "sub_folder";
  108. std::string expectedFilePath = "home" + s + "user" + s + "bla" + s + "sub_folder";
  109. ls_std::File anotherFile{wrongFilePath};
  110. ASSERT_STREQ(expectedFilePath.c_str(), anotherFile.getAbsoluteFilePath().c_str());
  111. }
  112. TEST_F(FileTest, getName)
  113. {
  114. ls_std::File file{this->fileLocation};
  115. ls_std::File executableFile{TestHelper::getResourcesFolderLocation() + "app.exe"};
  116. ls_std::File anotherFile{"bla.txt"};
  117. ls_std::File directory{TestHelper::getResourcesFolderLocation()};
  118. ASSERT_STREQ("simple.txt", file.getName().c_str());
  119. ASSERT_STREQ("app.exe", executableFile.getName().c_str());
  120. ASSERT_STREQ("bla.txt", anotherFile.getName().c_str());
  121. ASSERT_STREQ("resources", directory.getName().c_str());
  122. }
  123. TEST_F(FileTest, getParent)
  124. {
  125. ls_std::File file{this->fileLocation};
  126. ASSERT_STREQ(TestHelper::getResourcesFolderLocation().c_str(), file.getParent().c_str());
  127. }
  128. TEST_F(FileTest, getSize)
  129. {
  130. ls_std::File file{this->fileLocation};
  131. size_t size = file.getSize();
  132. ASSERT_TRUE(size == 7 || size == 8); // different OS specific new lines
  133. }
  134. TEST_F(FileTest, isDirectory)
  135. {
  136. ls_std::File directory{TestHelper::getResourcesFolderLocation()};
  137. ASSERT_TRUE(directory.isDirectory());
  138. }
  139. TEST_F(FileTest, isDirectoryNegative)
  140. {
  141. ls_std::File file{this->fileLocation};
  142. ASSERT_FALSE(file.isDirectory());
  143. }
  144. TEST_F(FileTest, isFile)
  145. {
  146. const char separator = ls_std::FilePathSeparator::get();
  147. ls_std::File file{this->fileLocation};
  148. ASSERT_TRUE(file.isFile());
  149. ls_std::File file2{TestHelper::getResourcesFolderLocation() + "list_test" + separator + "bla.txt"};
  150. ASSERT_TRUE(file2.isFile());
  151. }
  152. TEST_F(FileTest, isFileNegative)
  153. {
  154. ls_std::File directory{TestHelper::getResourcesFolderLocation()};
  155. ASSERT_FALSE(directory.isFile());
  156. }
  157. TEST_F(FileTest, lastModified)
  158. {
  159. ls_std::File file{this->fileLocation};
  160. ASSERT_TRUE(file.lastModified() > 1590000000);
  161. }
  162. TEST_F(FileTest, list)
  163. {
  164. ls_std::File file{TestHelper::getResourcesFolderLocation() + "list_test"};
  165. std::list<std::string> filesInDirectory = file.list();
  166. std::string expectedFile{};
  167. const char separator = ls_std::FilePathSeparator::get();
  168. auto filesIterator = filesInDirectory.begin();
  169. ASSERT_FALSE(filesInDirectory.empty());
  170. ASSERT_EQ(7, filesInDirectory.size());
  171. expectedFile = file.getAbsoluteFilePath() + separator + ".";
  172. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  173. expectedFile = file.getAbsoluteFilePath() + separator + "..";
  174. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  175. expectedFile = file.getAbsoluteFilePath() + separator + "another_file.txt";
  176. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  177. expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
  178. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  179. expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
  180. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  181. expectedFile = file.getAbsoluteFilePath() + separator + "list_test_sub";
  182. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  183. expectedFile = file.getAbsoluteFilePath() + separator + ".hidden_file.txt";
  184. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  185. }
  186. TEST_F(FileTest, listFiles)
  187. {
  188. ls_std::File file{TestHelper::getResourcesFolderLocation() + "list_test"};
  189. std::list<std::string> filesInDirectory = file.listFiles();
  190. std::string expectedFile{};
  191. const char separator = ls_std::FilePathSeparator::get();
  192. ASSERT_FALSE(filesInDirectory.empty());
  193. ASSERT_EQ(4, filesInDirectory.size());
  194. expectedFile = file.getAbsoluteFilePath() + separator + "another_file.txt";
  195. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  196. expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
  197. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  198. expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
  199. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  200. expectedFile = file.getAbsoluteFilePath() + separator + ".hidden_file.txt";
  201. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  202. }
  203. TEST_F(FileTest, makeDirectory)
  204. {
  205. ls_std::File directory{TestHelper::getResourcesFolderLocation() + "testDir"};
  206. ASSERT_FALSE(directory.exists());
  207. directory.makeDirectory();
  208. ASSERT_TRUE(directory.exists());
  209. directory.remove();
  210. ASSERT_FALSE(directory.exists());
  211. }
  212. TEST_F(FileTest, makeDirectories)
  213. {
  214. ls_std::File directory{TestHelper::getResourcesFolderLocation() + "testDir/sub/tmp/bla"};
  215. ASSERT_FALSE(directory.exists());
  216. directory.makeDirectories();
  217. ASSERT_TRUE(directory.exists());
  218. // clean up
  219. directory.remove();
  220. directory = ls_std::File(TestHelper::getResourcesFolderLocation() + "testDir/sub/tmp");
  221. directory.remove();
  222. directory = ls_std::File(TestHelper::getResourcesFolderLocation() + "testDir/sub");
  223. directory.remove();
  224. directory = ls_std::File(TestHelper::getResourcesFolderLocation() + "testDir");
  225. directory.remove();
  226. }
  227. TEST_F(FileTest, renameTo)
  228. {
  229. std::string currentName = TestHelper::getResourcesFolderLocation() + "tmp_rename_to.txt";
  230. std::string newName = TestHelper::getResourcesFolderLocation() + "tmp_rename_to_better_name.txt";
  231. ls_std::File file{currentName};
  232. file.createNewFile();
  233. ASSERT_TRUE(file.exists());
  234. ASSERT_STREQ(currentName.c_str(), file.getAbsoluteFilePath().c_str());
  235. file.renameTo(newName);
  236. ASSERT_TRUE(file.exists());
  237. ASSERT_STREQ(newName.c_str(), file.getAbsoluteFilePath().c_str());
  238. file.remove();
  239. }
  240. TEST_F(FileTest, reset)
  241. {
  242. ls_std::File file{this->fileLocation};
  243. ASSERT_TRUE(file.exists());
  244. file.reset(TestHelper::getResourcesFolderLocation() + "list_test/hello.txt");
  245. ASSERT_TRUE(file.exists());
  246. }
  247. }