FileTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-15
  6. * Changed: 2021-09-17
  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, operator_equals)
  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, operator_not_equals)
  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, canExecute_not_executable)
  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, canRead_file_does_not_exist)
  66. {
  67. ls_std::File file{""};
  68. EXPECT_THROW({
  69. try
  70. {
  71. file.canRead();
  72. }
  73. catch (const ls_std::FileOperationException &_exception)
  74. {
  75. throw;
  76. }
  77. }, ls_std::FileOperationException);
  78. }
  79. TEST_F(FileTest, canWrite)
  80. {
  81. ls_std::File readableFile{this->fileLocation};
  82. ASSERT_TRUE(readableFile.canWrite());
  83. }
  84. TEST_F(FileTest, canWrite_not_writable)
  85. {
  86. #if defined(unix) || defined(__APPLE__)
  87. ls_std::File noWritableFile{TestHelper::getResourcesFolderLocation() + "no_writable.txt"};
  88. ASSERT_FALSE(noWritableFile.canWrite());
  89. #endif
  90. #ifdef _WIN32
  91. ls_std::File noWritableFile{TestHelper::getResourcesFolderLocation() + "no_writable_windows.txt"};
  92. ASSERT_FALSE(noWritableFile.canWrite());
  93. #endif
  94. }
  95. TEST_F(FileTest, createNewFile)
  96. {
  97. ls_std::File file{TestHelper::getResourcesFolderLocation() + "tmp.txt"};
  98. ASSERT_FALSE(file.exists());
  99. file.createNewFile();
  100. ASSERT_TRUE(file.exists());
  101. file.remove();
  102. ASSERT_FALSE(file.exists());
  103. }
  104. TEST_F(FileTest, createNewFile_file_does_already_exist)
  105. {
  106. ls_std::File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
  107. EXPECT_THROW({
  108. try
  109. {
  110. file.createNewFile();
  111. }
  112. catch (const ls_std::FileOperationException &_exception)
  113. {
  114. throw;
  115. }
  116. }, ls_std::FileOperationException);
  117. }
  118. TEST_F(FileTest, exists)
  119. {
  120. ls_std::File file{this->fileLocation};
  121. ls_std::File directory{TestHelper::getResourcesFolderLocation()};
  122. ASSERT_TRUE(file.exists());
  123. ASSERT_TRUE(directory.exists());
  124. }
  125. TEST_F(FileTest, exists_does_not_exist)
  126. {
  127. ls_std::File file{TestHelper::getResourcesFolderLocation() + "bla.txt"};
  128. ASSERT_FALSE(file.exists());
  129. }
  130. TEST_F(FileTest, getAbsoluteFilePath)
  131. {
  132. ls_std::File file{this->fileLocation};
  133. ASSERT_STREQ(this->fileLocation.c_str(), file.getAbsoluteFilePath().c_str());
  134. std::string s = {ls_std::FilePathSeparator::get()};
  135. std::string wrongFilePath = "home" + s + s + s + "user" + s + "bla" + s + s + "sub_folder";
  136. std::string expectedFilePath = "home" + s + "user" + s + "bla" + s + "sub_folder";
  137. ls_std::File anotherFile{wrongFilePath};
  138. ASSERT_STREQ(expectedFilePath.c_str(), anotherFile.getAbsoluteFilePath().c_str());
  139. }
  140. TEST_F(FileTest, getName)
  141. {
  142. ls_std::File file{this->fileLocation};
  143. ls_std::File executableFile{TestHelper::getResourcesFolderLocation() + "app.exe"};
  144. ls_std::File anotherFile{"bla.txt"};
  145. ls_std::File directory{TestHelper::getResourcesFolderLocation()};
  146. ASSERT_STREQ("simple.txt", file.getName().c_str());
  147. ASSERT_STREQ("app.exe", executableFile.getName().c_str());
  148. ASSERT_STREQ("bla.txt", anotherFile.getName().c_str());
  149. ASSERT_STREQ("resources", directory.getName().c_str());
  150. }
  151. TEST_F(FileTest, getParent)
  152. {
  153. ls_std::File file{this->fileLocation};
  154. ASSERT_STREQ(TestHelper::getResourcesFolderLocation().c_str(), file.getParent().c_str());
  155. }
  156. TEST_F(FileTest, getSize)
  157. {
  158. ls_std::File file{this->fileLocation};
  159. size_t size = file.getSize();
  160. ASSERT_TRUE(size == 7 || size == 8); // different OS specific new lines
  161. }
  162. TEST_F(FileTest, isDirectory)
  163. {
  164. ls_std::File directory{TestHelper::getResourcesFolderLocation()};
  165. ASSERT_TRUE(directory.isDirectory());
  166. }
  167. TEST_F(FileTest, isDirectory_is_a_file)
  168. {
  169. ls_std::File file{this->fileLocation};
  170. ASSERT_FALSE(file.isDirectory());
  171. }
  172. TEST_F(FileTest, isFile)
  173. {
  174. const char separator = ls_std::FilePathSeparator::get();
  175. ls_std::File file{this->fileLocation};
  176. ASSERT_TRUE(file.isFile());
  177. ls_std::File file2{TestHelper::getResourcesFolderLocation() + "list_test" + separator + "bla.txt"};
  178. ASSERT_TRUE(file2.isFile());
  179. }
  180. TEST_F(FileTest, isFile_is_a_directory)
  181. {
  182. ls_std::File directory{TestHelper::getResourcesFolderLocation()};
  183. ASSERT_FALSE(directory.isFile());
  184. }
  185. TEST_F(FileTest, lastModified)
  186. {
  187. ls_std::File file{this->fileLocation};
  188. ASSERT_TRUE(file.lastModified() > 1590000000);
  189. }
  190. TEST_F(FileTest, list)
  191. {
  192. ls_std::File file{TestHelper::getResourcesFolderLocation() + "list_test"};
  193. std::list<std::string> filesInDirectory = file.list();
  194. std::string expectedFile{};
  195. const char separator = ls_std::FilePathSeparator::get();
  196. auto filesIterator = filesInDirectory.begin();
  197. ASSERT_FALSE(filesInDirectory.empty());
  198. ASSERT_EQ(7, filesInDirectory.size());
  199. expectedFile = file.getAbsoluteFilePath() + separator + ".";
  200. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  201. expectedFile = file.getAbsoluteFilePath() + separator + "..";
  202. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  203. expectedFile = file.getAbsoluteFilePath() + separator + "another_file.txt";
  204. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  205. expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
  206. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  207. expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
  208. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  209. expectedFile = file.getAbsoluteFilePath() + separator + "list_test_sub";
  210. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  211. expectedFile = file.getAbsoluteFilePath() + separator + ".hidden_file.txt";
  212. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  213. }
  214. TEST_F(FileTest, listFiles)
  215. {
  216. ls_std::File file{TestHelper::getResourcesFolderLocation() + "list_test"};
  217. std::list<std::string> filesInDirectory = file.listFiles();
  218. std::string expectedFile{};
  219. const char separator = ls_std::FilePathSeparator::get();
  220. ASSERT_FALSE(filesInDirectory.empty());
  221. ASSERT_EQ(4, filesInDirectory.size());
  222. expectedFile = file.getAbsoluteFilePath() + separator + "another_file.txt";
  223. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  224. expectedFile = file.getAbsoluteFilePath() + separator + "bla.txt";
  225. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  226. expectedFile = file.getAbsoluteFilePath() + separator + "hello.txt";
  227. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  228. expectedFile = file.getAbsoluteFilePath() + separator + ".hidden_file.txt";
  229. ASSERT_TRUE((ls_std::STLUtils::contains(filesInDirectory, expectedFile)));
  230. }
  231. TEST_F(FileTest, makeDirectory)
  232. {
  233. ls_std::File directory{TestHelper::getResourcesFolderLocation() + "testDir"};
  234. ASSERT_FALSE(directory.exists());
  235. directory.makeDirectory();
  236. ASSERT_TRUE(directory.exists());
  237. directory.remove();
  238. ASSERT_FALSE(directory.exists());
  239. }
  240. TEST_F(FileTest, makeDirectory_directory_already_exists)
  241. {
  242. ls_std::File directory{TestHelper::getResourcesFolderLocation() + "list_test"};
  243. EXPECT_THROW({
  244. try
  245. {
  246. directory.makeDirectory();
  247. }
  248. catch (const ls_std::FileOperationException &_exception)
  249. {
  250. throw;
  251. }
  252. }, ls_std::FileOperationException);
  253. }
  254. TEST_F(FileTest, makeDirectories)
  255. {
  256. ls_std::File directory{TestHelper::getResourcesFolderLocation() + "testDir/sub/tmp/bla"};
  257. ASSERT_FALSE(directory.exists());
  258. directory.makeDirectories();
  259. ASSERT_TRUE(directory.exists());
  260. // clean up
  261. directory.remove();
  262. directory = ls_std::File(TestHelper::getResourcesFolderLocation() + "testDir/sub/tmp");
  263. directory.remove();
  264. directory = ls_std::File(TestHelper::getResourcesFolderLocation() + "testDir/sub");
  265. directory.remove();
  266. directory = ls_std::File(TestHelper::getResourcesFolderLocation() + "testDir");
  267. directory.remove();
  268. }
  269. TEST_F(FileTest, remove)
  270. {
  271. std::string fileName = TestHelper::getResourcesFolderLocation() + "removable_file.txt";
  272. ls_std::File file{fileName};
  273. file.createNewFile();
  274. ASSERT_TRUE(file.exists());
  275. file.remove();
  276. ASSERT_FALSE(file.exists());
  277. }
  278. TEST_F(FileTest, renameTo)
  279. {
  280. std::string currentName = TestHelper::getResourcesFolderLocation() + "tmp_rename_to.txt";
  281. std::string newName = TestHelper::getResourcesFolderLocation() + "tmp_rename_to_better_name.txt";
  282. ls_std::File file{currentName};
  283. file.createNewFile();
  284. ASSERT_TRUE(file.exists());
  285. ASSERT_STREQ(currentName.c_str(), file.getAbsoluteFilePath().c_str());
  286. file.renameTo(newName);
  287. ASSERT_TRUE(file.exists());
  288. ASSERT_STREQ(newName.c_str(), file.getAbsoluteFilePath().c_str());
  289. file.remove();
  290. }
  291. TEST_F(FileTest, reset)
  292. {
  293. ls_std::File file{this->fileLocation};
  294. ASSERT_TRUE(file.exists());
  295. file.reset(TestHelper::getResourcesFolderLocation() + "list_test/hello.txt");
  296. ASSERT_TRUE(file.exists());
  297. }
  298. }