FileTest.cpp 9.8 KB

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