FileTest.cpp 12 KB

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