FileTest.cpp 10 KB

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