FileTest.cpp 11 KB

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