FileTest.cpp 9.3 KB

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