1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/file/dir.cpp
3 // Purpose: wxDir unit test
4 // Author: Francesco Montorsi (extracted from console sample)
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
21 #include "wx/filename.h"
22 #include "wx/stdpaths.h"
24 #define DIRTEST_FOLDER wxString("dirTest_folder")
25 #define SEP wxFileName::GetPathSeparator()
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 class DirTestCase
: public CppUnit::TestCase
37 virtual void tearDown();
40 CPPUNIT_TEST_SUITE( DirTestCase
);
41 CPPUNIT_TEST( DirExists
);
42 CPPUNIT_TEST( Traverse
);
44 CPPUNIT_TEST_SUITE_END();
50 void CreateTempFile(const wxString
& path
);
51 wxArrayString
DirEnumHelper(wxDir
& dir
,
52 int flags
= wxDIR_DEFAULT
,
53 const wxString
& filespec
= wxEmptyString
);
55 wxDECLARE_NO_COPY_CLASS(DirTestCase
);
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 CPPUNIT_TEST_SUITE_REGISTRATION( DirTestCase
);
63 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DirTestCase
, "DirTestCase" );
65 // ----------------------------------------------------------------------------
66 // tests implementation
67 // ----------------------------------------------------------------------------
69 void DirTestCase::CreateTempFile(const wxString
& path
)
71 wxFile
f(path
, wxFile::write
);
72 f
.Write("dummy test file");
76 void DirTestCase::setUp()
78 // create a test directory hierarchy
79 wxDir::Make(DIRTEST_FOLDER
+ SEP
+ "folder1" + SEP
+ "subfolder1", wxS_DIR_DEFAULT
, wxPATH_MKDIR_FULL
);
80 wxDir::Make(DIRTEST_FOLDER
+ SEP
+ "folder1" + SEP
+ "subfolder2", wxS_DIR_DEFAULT
, wxPATH_MKDIR_FULL
);
81 wxDir::Make(DIRTEST_FOLDER
+ SEP
+ "folder2", wxS_DIR_DEFAULT
, wxPATH_MKDIR_FULL
);
82 wxDir::Make(DIRTEST_FOLDER
+ SEP
+ "folder3" + SEP
+ "subfolder1", wxS_DIR_DEFAULT
, wxPATH_MKDIR_FULL
);
84 CreateTempFile(DIRTEST_FOLDER
+ SEP
+ "folder1" + SEP
+ "subfolder2" + SEP
+ "dummy");
85 CreateTempFile(DIRTEST_FOLDER
+ SEP
+ "dummy");
88 void DirTestCase::tearDown()
90 wxRemove(DIRTEST_FOLDER
+ SEP
+ "folder1" + SEP
+ "subfolder2" + SEP
+ "dummy");
91 wxRemove(DIRTEST_FOLDER
+ SEP
+ "dummy");
92 wxDir::Remove(DIRTEST_FOLDER
, wxPATH_RMDIR_RECURSIVE
);
95 wxArrayString
DirTestCase::DirEnumHelper(wxDir
& dir
,
97 const wxString
& filespec
)
100 CPPUNIT_ASSERT( dir
.IsOpened() );
103 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
106 ret
.push_back(filename
);
107 cont
= dir
.GetNext(&filename
);
113 void DirTestCase::Enum()
115 wxDir
dir(DIRTEST_FOLDER
);
116 CPPUNIT_ASSERT( dir
.IsOpened() );
118 // enumerating everything in test directory
119 CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir
).size());
121 // enumerating really everything in test directory recursively
122 CPPUNIT_ASSERT_EQUAL(6, DirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
).size());
124 // enumerating object files in test directory
125 CPPUNIT_ASSERT_EQUAL(0, DirEnumHelper(dir
, wxDIR_DEFAULT
, "*.o*").size());
127 // enumerating directories in test directory
128 CPPUNIT_ASSERT_EQUAL(3, DirEnumHelper(dir
, wxDIR_DIRS
).size());
130 // enumerating files in test directory
131 CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir
, wxDIR_FILES
).size());
133 // enumerating files including hidden in test directory
134 CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
).size());
136 // enumerating files and folders in test directory
137 CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir
, wxDIR_FILES
| wxDIR_DIRS
).size());
140 class TestDirTraverser
: public wxDirTraverser
145 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
147 return wxDIR_CONTINUE
;
150 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
152 dirs
.push_back(dirname
);
153 return wxDIR_CONTINUE
;
157 void DirTestCase::Traverse()
161 CPPUNIT_ASSERT_EQUAL(2, wxDir::GetAllFiles(DIRTEST_FOLDER
, &files
));
163 // enum again with custom traverser
164 wxDir
dir(DIRTEST_FOLDER
);
165 TestDirTraverser traverser
;
166 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
167 CPPUNIT_ASSERT_EQUAL(6, traverser
.dirs
.size());
170 void DirTestCase::DirExists()
180 { "$USER_DOCS_DIR", true },
181 #if defined(__WXMSW__)
182 { "$USER_DOCS_DIR\\", true },
183 { "$USER_DOCS_DIR\\\\", true },
185 { "$MSW_DRIVE", true },
186 { "$MSW_DRIVE\\", true },
187 { "$MSW_DRIVE\\\\", true },
188 { "\\\\non_existent_share\\file", false },
189 { "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist", false },
190 { "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist\\", false },
191 { "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist\\\\", false },
192 { "test.exe", false } // not a directory!
193 #elif defined(__UNIX__)
197 { "/usr/bin", true },
198 { "/usr//bin", true },
199 { "/usr///bin", true },
200 { "/tmp/a/directory/which/does/not/exist", false },
201 { "/bin/ls", false } // not a directory!
206 wxString homedrive
= wxGetenv("HOMEDRIVE");
207 if ( homedrive
.empty() )
211 for ( size_t n
= 0; n
< WXSIZEOF(testData
); n
++ )
213 wxString dirname
= testData
[n
].dirname
;
214 dirname
.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir());
217 dirname
.Replace("$MSW_DRIVE", homedrive
);
220 std::string errDesc
= wxString::Format("failed on directory '%s'", dirname
).ToStdString();
221 CPPUNIT_ASSERT_EQUAL_MESSAGE(errDesc
, testData
[n
].shouldExist
, wxDir::Exists(dirname
));
224 CPPUNIT_ASSERT_EQUAL(testData
[n
].shouldExist
, d
.IsOpened());
227 CPPUNIT_ASSERT( wxDir::Exists(wxGetCwd()) );