1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/file/dir.cpp
3 // Purpose: wxDir unit test
4 // Author: Francesco Montorsi (extracted from console sample)
6 // Copyright: (c) 2010 wxWidgets team
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
20 #include "wx/filename.h"
21 #include "wx/stdpaths.h"
23 #define DIRTEST_FOLDER wxString("dirTest_folder")
24 #define SEP wxFileName::GetPathSeparator()
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 class DirTestCase
: public CppUnit::TestCase
36 virtual void tearDown();
39 CPPUNIT_TEST_SUITE( DirTestCase
);
40 CPPUNIT_TEST( DirExists
);
41 CPPUNIT_TEST( Traverse
);
43 CPPUNIT_TEST( GetName
);
44 CPPUNIT_TEST_SUITE_END();
51 void CreateTempFile(const wxString
& path
);
52 wxArrayString
DirEnumHelper(wxDir
& dir
,
53 int flags
= wxDIR_DEFAULT
,
54 const wxString
& filespec
= wxEmptyString
);
56 wxDECLARE_NO_COPY_CLASS(DirTestCase
);
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 CPPUNIT_TEST_SUITE_REGISTRATION( DirTestCase
);
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DirTestCase
, "DirTestCase" );
66 // ----------------------------------------------------------------------------
67 // tests implementation
68 // ----------------------------------------------------------------------------
70 void DirTestCase::CreateTempFile(const wxString
& path
)
72 wxFile
f(path
, wxFile::write
);
73 f
.Write("dummy test file");
77 void DirTestCase::setUp()
79 // create a test directory hierarchy
80 wxDir::Make(DIRTEST_FOLDER
+ SEP
+ "folder1" + SEP
+ "subfolder1", wxS_DIR_DEFAULT
, wxPATH_MKDIR_FULL
);
81 wxDir::Make(DIRTEST_FOLDER
+ SEP
+ "folder1" + SEP
+ "subfolder2", wxS_DIR_DEFAULT
, wxPATH_MKDIR_FULL
);
82 wxDir::Make(DIRTEST_FOLDER
+ SEP
+ "folder2", wxS_DIR_DEFAULT
, wxPATH_MKDIR_FULL
);
83 wxDir::Make(DIRTEST_FOLDER
+ SEP
+ "folder3" + SEP
+ "subfolder1", wxS_DIR_DEFAULT
, wxPATH_MKDIR_FULL
);
85 CreateTempFile(DIRTEST_FOLDER
+ SEP
+ "folder1" + SEP
+ "subfolder2" + SEP
+ "dummy");
86 CreateTempFile(DIRTEST_FOLDER
+ SEP
+ "dummy");
87 CreateTempFile(DIRTEST_FOLDER
+ SEP
+ "folder3" + SEP
+ "subfolder1" + SEP
+ "dummy.foo");
88 CreateTempFile(DIRTEST_FOLDER
+ SEP
+ "folder3" + SEP
+ "subfolder1" + SEP
+ "dummy.foo.bar");
91 void DirTestCase::tearDown()
93 wxRemove(DIRTEST_FOLDER
+ SEP
+ "folder1" + SEP
+ "subfolder2" + SEP
+ "dummy");
94 wxRemove(DIRTEST_FOLDER
+ SEP
+ "dummy");
95 wxRemove(DIRTEST_FOLDER
+ SEP
+ "folder3" + SEP
+ "subfolder1" + SEP
+ "dummy.foo");
96 wxRemove(DIRTEST_FOLDER
+ SEP
+ "folder3" + SEP
+ "subfolder1" + SEP
+ "dummy.foo.bar");
97 wxDir::Remove(DIRTEST_FOLDER
, wxPATH_RMDIR_RECURSIVE
);
100 wxArrayString
DirTestCase::DirEnumHelper(wxDir
& dir
,
102 const wxString
& filespec
)
105 CPPUNIT_ASSERT( dir
.IsOpened() );
108 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
111 ret
.push_back(filename
);
112 cont
= dir
.GetNext(&filename
);
118 void DirTestCase::Enum()
120 wxDir
dir(DIRTEST_FOLDER
);
121 CPPUNIT_ASSERT( dir
.IsOpened() );
123 // enumerating everything in test directory
124 CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir
).size());
126 // enumerating really everything in test directory recursively
127 CPPUNIT_ASSERT_EQUAL(6, DirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
).size());
129 // enumerating object files in test directory
130 CPPUNIT_ASSERT_EQUAL(0, DirEnumHelper(dir
, wxDIR_DEFAULT
, "*.o*").size());
132 // enumerating directories in test directory
133 CPPUNIT_ASSERT_EQUAL(3, DirEnumHelper(dir
, wxDIR_DIRS
).size());
135 // enumerating files in test directory
136 CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir
, wxDIR_FILES
).size());
138 // enumerating files including hidden in test directory
139 CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
).size());
141 // enumerating files and folders in test directory
142 CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir
, wxDIR_FILES
| wxDIR_DIRS
).size());
145 class TestDirTraverser
: public wxDirTraverser
150 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
152 return wxDIR_CONTINUE
;
155 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
157 dirs
.push_back(dirname
);
158 return wxDIR_CONTINUE
;
162 void DirTestCase::Traverse()
166 CPPUNIT_ASSERT_EQUAL(4, wxDir::GetAllFiles(DIRTEST_FOLDER
, &files
));
168 // enum all files according to the filter
169 CPPUNIT_ASSERT_EQUAL(1, wxDir::GetAllFiles(DIRTEST_FOLDER
, &files
, "*.foo"));
171 // enum again with custom traverser
172 wxDir
dir(DIRTEST_FOLDER
);
173 TestDirTraverser traverser
;
174 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
175 CPPUNIT_ASSERT_EQUAL(6, traverser
.dirs
.size());
178 void DirTestCase::DirExists()
188 { "$USER_DOCS_DIR", true },
189 #if defined(__WINDOWS__)
190 { "$USER_DOCS_DIR\\", true },
191 { "$USER_DOCS_DIR\\\\", true },
193 { "$MSW_DRIVE", true },
194 { "$MSW_DRIVE\\", true },
195 { "$MSW_DRIVE\\\\", true },
196 { "\\\\non_existent_share\\file", false },
197 { "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist", false },
198 { "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist\\", false },
199 { "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist\\\\", false },
200 { "test.exe", false } // not a directory!
201 #elif defined(__UNIX__)
205 { "/usr/bin", true },
206 { "/usr//bin", true },
207 { "/usr///bin", true },
208 { "/tmp/a/directory/which/does/not/exist", false },
209 { "/bin/ls", false } // not a directory!
214 wxString homedrive
= wxGetenv("HOMEDRIVE");
215 if ( homedrive
.empty() )
217 #endif // __WINDOWS__
219 for ( size_t n
= 0; n
< WXSIZEOF(testData
); n
++ )
221 wxString dirname
= testData
[n
].dirname
;
222 dirname
.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir());
225 dirname
.Replace("$MSW_DRIVE", homedrive
);
226 #endif // __WINDOWS__
228 std::string errDesc
= wxString::Format("failed on directory '%s'", dirname
).ToStdString();
229 CPPUNIT_ASSERT_EQUAL_MESSAGE(errDesc
, testData
[n
].shouldExist
, wxDir::Exists(dirname
));
232 CPPUNIT_ASSERT_EQUAL(testData
[n
].shouldExist
, d
.IsOpened());
235 CPPUNIT_ASSERT( wxDir::Exists(wxGetCwd()) );
238 void DirTestCase::GetName()
242 CPPUNIT_ASSERT( d
.Open(".") );
243 CPPUNIT_ASSERT( d
.GetName().Last() != wxFILE_SEP_PATH
);
244 CPPUNIT_ASSERT( d
.GetNameWithSep().Last() == wxFILE_SEP_PATH
);
245 CPPUNIT_ASSERT_EQUAL( d
.GetName() + wxFILE_SEP_PATH
,
246 d
.GetNameWithSep() );
249 CPPUNIT_ASSERT( d
.Open("/") );
250 CPPUNIT_ASSERT_EQUAL( "/", d
.GetName() );
251 CPPUNIT_ASSERT_EQUAL( "/", d
.GetNameWithSep() );