dir.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/file/dir.cpp
  3. // Purpose: wxDir unit test
  4. // Author: Francesco Montorsi (extracted from console sample)
  5. // Created: 2010-06-19
  6. // Copyright: (c) 2010 wxWidgets team
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #include "wx/dir.h"
  16. #include "wx/filename.h"
  17. #include "wx/stdpaths.h"
  18. #define DIRTEST_FOLDER wxString("dirTest_folder")
  19. #define SEP wxFileName::GetPathSeparator()
  20. // ----------------------------------------------------------------------------
  21. // test class
  22. // ----------------------------------------------------------------------------
  23. class DirTestCase : public CppUnit::TestCase
  24. {
  25. public:
  26. DirTestCase() { }
  27. virtual void setUp();
  28. virtual void tearDown();
  29. private:
  30. CPPUNIT_TEST_SUITE( DirTestCase );
  31. CPPUNIT_TEST( DirExists );
  32. CPPUNIT_TEST( Traverse );
  33. CPPUNIT_TEST( Enum );
  34. CPPUNIT_TEST( GetName );
  35. CPPUNIT_TEST_SUITE_END();
  36. void DirExists();
  37. void Traverse();
  38. void Enum();
  39. void GetName();
  40. void CreateTempFile(const wxString& path);
  41. wxArrayString DirEnumHelper(wxDir& dir,
  42. int flags = wxDIR_DEFAULT,
  43. const wxString& filespec = wxEmptyString);
  44. wxDECLARE_NO_COPY_CLASS(DirTestCase);
  45. };
  46. // ----------------------------------------------------------------------------
  47. // CppUnit macros
  48. // ----------------------------------------------------------------------------
  49. CPPUNIT_TEST_SUITE_REGISTRATION( DirTestCase );
  50. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DirTestCase, "DirTestCase" );
  51. // ----------------------------------------------------------------------------
  52. // tests implementation
  53. // ----------------------------------------------------------------------------
  54. void DirTestCase::CreateTempFile(const wxString& path)
  55. {
  56. wxFile f(path, wxFile::write);
  57. f.Write("dummy test file");
  58. f.Close();
  59. }
  60. void DirTestCase::setUp()
  61. {
  62. // create a test directory hierarchy
  63. wxDir::Make(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder1", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
  64. wxDir::Make(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
  65. wxDir::Make(DIRTEST_FOLDER + SEP + "folder2", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
  66. wxDir::Make(DIRTEST_FOLDER + SEP + "folder3" + SEP + "subfolder1", wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
  67. CreateTempFile(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2" + SEP + "dummy");
  68. CreateTempFile(DIRTEST_FOLDER + SEP + "dummy");
  69. CreateTempFile(DIRTEST_FOLDER + SEP + "folder3" + SEP + "subfolder1" + SEP + "dummy.foo");
  70. CreateTempFile(DIRTEST_FOLDER + SEP + "folder3" + SEP + "subfolder1" + SEP + "dummy.foo.bar");
  71. }
  72. void DirTestCase::tearDown()
  73. {
  74. wxRemove(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2" + SEP + "dummy");
  75. wxRemove(DIRTEST_FOLDER + SEP + "dummy");
  76. wxRemove(DIRTEST_FOLDER + SEP + "folder3" + SEP + "subfolder1" + SEP + "dummy.foo");
  77. wxRemove(DIRTEST_FOLDER + SEP + "folder3" + SEP + "subfolder1" + SEP + "dummy.foo.bar");
  78. wxDir::Remove(DIRTEST_FOLDER, wxPATH_RMDIR_RECURSIVE);
  79. }
  80. wxArrayString DirTestCase::DirEnumHelper(wxDir& dir,
  81. int flags,
  82. const wxString& filespec)
  83. {
  84. wxArrayString ret;
  85. CPPUNIT_ASSERT( dir.IsOpened() );
  86. wxString filename;
  87. bool cont = dir.GetFirst(&filename, filespec, flags);
  88. while ( cont )
  89. {
  90. ret.push_back(filename);
  91. cont = dir.GetNext(&filename);
  92. }
  93. return ret;
  94. }
  95. void DirTestCase::Enum()
  96. {
  97. wxDir dir(DIRTEST_FOLDER);
  98. CPPUNIT_ASSERT( dir.IsOpened() );
  99. // enumerating everything in test directory
  100. CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir).size());
  101. // enumerating really everything in test directory recursively
  102. CPPUNIT_ASSERT_EQUAL(6, DirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT).size());
  103. // enumerating object files in test directory
  104. CPPUNIT_ASSERT_EQUAL(0, DirEnumHelper(dir, wxDIR_DEFAULT, "*.o*").size());
  105. // enumerating directories in test directory
  106. CPPUNIT_ASSERT_EQUAL(3, DirEnumHelper(dir, wxDIR_DIRS).size());
  107. // enumerating files in test directory
  108. CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir, wxDIR_FILES).size());
  109. // enumerating files including hidden in test directory
  110. CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN).size());
  111. // enumerating files and folders in test directory
  112. CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir, wxDIR_FILES | wxDIR_DIRS).size());
  113. }
  114. class TestDirTraverser : public wxDirTraverser
  115. {
  116. public:
  117. wxArrayString dirs;
  118. virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
  119. {
  120. return wxDIR_CONTINUE;
  121. }
  122. virtual wxDirTraverseResult OnDir(const wxString& dirname)
  123. {
  124. dirs.push_back(dirname);
  125. return wxDIR_CONTINUE;
  126. }
  127. };
  128. void DirTestCase::Traverse()
  129. {
  130. // enum all files
  131. wxArrayString files;
  132. CPPUNIT_ASSERT_EQUAL(4, wxDir::GetAllFiles(DIRTEST_FOLDER, &files));
  133. // enum all files according to the filter
  134. CPPUNIT_ASSERT_EQUAL(1, wxDir::GetAllFiles(DIRTEST_FOLDER, &files, "*.foo"));
  135. // enum again with custom traverser
  136. wxDir dir(DIRTEST_FOLDER);
  137. TestDirTraverser traverser;
  138. dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN);
  139. CPPUNIT_ASSERT_EQUAL(6, traverser.dirs.size());
  140. }
  141. void DirTestCase::DirExists()
  142. {
  143. struct
  144. {
  145. const char *dirname;
  146. bool shouldExist;
  147. } testData[] =
  148. {
  149. { ".", true },
  150. { "..", true },
  151. { "$USER_DOCS_DIR", true },
  152. #if defined(__WINDOWS__)
  153. { "$USER_DOCS_DIR\\", true },
  154. { "$USER_DOCS_DIR\\\\", true },
  155. { "..\\..", true },
  156. { "$MSW_DRIVE", true },
  157. { "$MSW_DRIVE\\", true },
  158. { "$MSW_DRIVE\\\\", true },
  159. { "\\\\non_existent_share\\file", false },
  160. { "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist", false },
  161. { "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist\\", false },
  162. { "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist\\\\", false },
  163. { "test.exe", false } // not a directory!
  164. #elif defined(__UNIX__)
  165. { "../..", true },
  166. { "/", true },
  167. { "//", true },
  168. { "/usr/bin", true },
  169. { "/usr//bin", true },
  170. { "/usr///bin", true },
  171. { "/tmp/a/directory/which/does/not/exist", false },
  172. { "/bin/ls", false } // not a directory!
  173. #endif
  174. };
  175. #ifdef __WINDOWS__
  176. wxString homedrive = wxGetenv("HOMEDRIVE");
  177. if ( homedrive.empty() )
  178. homedrive = "c:";
  179. #endif // __WINDOWS__
  180. for ( size_t n = 0; n < WXSIZEOF(testData); n++ )
  181. {
  182. wxString dirname = testData[n].dirname;
  183. dirname.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir());
  184. #ifdef __WINDOWS__
  185. dirname.Replace("$MSW_DRIVE", homedrive);
  186. #endif // __WINDOWS__
  187. std::string errDesc = wxString::Format("failed on directory '%s'", dirname).ToStdString();
  188. CPPUNIT_ASSERT_EQUAL_MESSAGE(errDesc, testData[n].shouldExist, wxDir::Exists(dirname));
  189. wxDir d(dirname);
  190. CPPUNIT_ASSERT_EQUAL(testData[n].shouldExist, d.IsOpened());
  191. }
  192. CPPUNIT_ASSERT( wxDir::Exists(wxGetCwd()) );
  193. }
  194. void DirTestCase::GetName()
  195. {
  196. wxDir d;
  197. CPPUNIT_ASSERT( d.Open(".") );
  198. CPPUNIT_ASSERT( d.GetName().Last() != wxFILE_SEP_PATH );
  199. CPPUNIT_ASSERT( d.GetNameWithSep().Last() == wxFILE_SEP_PATH );
  200. CPPUNIT_ASSERT_EQUAL( d.GetName() + wxFILE_SEP_PATH,
  201. d.GetNameWithSep() );
  202. #ifdef __UNIX__
  203. CPPUNIT_ASSERT( d.Open("/") );
  204. CPPUNIT_ASSERT_EQUAL( "/", d.GetName() );
  205. CPPUNIT_ASSERT_EQUAL( "/", d.GetNameWithSep() );
  206. #endif
  207. }