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"
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_SUITE_END();
49 void CreateTempFile(const wxString
& path
);
50 wxArrayString
DirEnumHelper(wxDir
& dir
,
51 int flags
= wxDIR_DEFAULT
,
52 const wxString
& filespec
= wxEmptyString
);
54 wxDECLARE_NO_COPY_CLASS(DirTestCase
);
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 CPPUNIT_TEST_SUITE_REGISTRATION( DirTestCase
);
62 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DirTestCase
, "DirTestCase" );
64 // ----------------------------------------------------------------------------
65 // tests implementation
66 // ----------------------------------------------------------------------------
68 void DirTestCase::CreateTempFile(const wxString
& path
)
70 wxFile
f(path
, wxFile::write
);
71 f
.Write("dummy test file");
75 void DirTestCase::setUp()
77 // create a test directory hierarchy
78 wxDir::Make(DIRTEST_FOLDER
+ SEP
+ "folder1" + SEP
+ "subfolder1", wxS_DIR_DEFAULT
, wxPATH_MKDIR_FULL
);
79 wxDir::Make(DIRTEST_FOLDER
+ SEP
+ "folder1" + SEP
+ "subfolder2", wxS_DIR_DEFAULT
, wxPATH_MKDIR_FULL
);
80 wxDir::Make(DIRTEST_FOLDER
+ SEP
+ "folder2", wxS_DIR_DEFAULT
, wxPATH_MKDIR_FULL
);
81 wxDir::Make(DIRTEST_FOLDER
+ SEP
+ "folder3" + SEP
+ "subfolder1", wxS_DIR_DEFAULT
, wxPATH_MKDIR_FULL
);
83 CreateTempFile(DIRTEST_FOLDER
+ SEP
+ "folder1" + SEP
+ "subfolder2" + SEP
+ "dummy");
84 CreateTempFile(DIRTEST_FOLDER
+ SEP
+ "dummy");
87 void DirTestCase::tearDown()
89 wxRemove(DIRTEST_FOLDER
+ SEP
+ "folder1" + SEP
+ "subfolder2" + SEP
+ "dummy");
90 wxRemove(DIRTEST_FOLDER
+ SEP
+ "dummy");
91 wxDir::Remove(DIRTEST_FOLDER
, wxPATH_RMDIR_RECURSIVE
);
94 wxArrayString
DirTestCase::DirEnumHelper(wxDir
& dir
,
96 const wxString
& filespec
)
99 CPPUNIT_ASSERT( dir
.IsOpened() );
102 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
105 ret
.push_back(filename
);
106 cont
= dir
.GetNext(&filename
);
112 void DirTestCase::Enum()
114 wxDir
dir(DIRTEST_FOLDER
);
115 CPPUNIT_ASSERT( dir
.IsOpened() );
117 // enumerating everything in test directory
118 CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir
).size());
120 // enumerating really everything in test directory recursively
121 CPPUNIT_ASSERT_EQUAL(6, DirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
).size());
123 // enumerating object files in test directory
124 CPPUNIT_ASSERT_EQUAL(0, DirEnumHelper(dir
, wxDIR_DEFAULT
, "*.o*").size());
126 // enumerating directories in test directory
127 CPPUNIT_ASSERT_EQUAL(3, DirEnumHelper(dir
, wxDIR_DIRS
).size());
129 // enumerating files in test directory
130 CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir
, wxDIR_FILES
).size());
132 // enumerating files including hidden in test directory
133 CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
).size());
135 // enumerating files and folders in test directory
136 CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir
, wxDIR_FILES
| wxDIR_DIRS
).size());
139 class TestDirTraverser
: public wxDirTraverser
144 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
146 return wxDIR_CONTINUE
;
149 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
151 dirs
.push_back(dirname
);
152 return wxDIR_CONTINUE
;
156 void DirTestCase::Traverse()
160 CPPUNIT_ASSERT_EQUAL(2, wxDir::GetAllFiles(DIRTEST_FOLDER
, &files
));
162 // enum again with custom traverser
163 wxDir
dir(DIRTEST_FOLDER
);
164 TestDirTraverser traverser
;
165 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
166 CPPUNIT_ASSERT_EQUAL(6, traverser
.dirs
.size());
169 void DirTestCase::DirExists()
179 #if defined(__WXMSW__)
181 { "..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\..", true },
185 { "\\\\share\\file", false },
186 { "c:\\a\\directory\\which\\does\\not\\exist", false },
187 { "c:\\a\\directory\\which\\does\\not\\exist\\", false },
188 { "c:\\a\\directory\\which\\does\\not\\exist\\\\", false },
189 { "test.exe", false } // not a directory!
190 #elif defined(__UNIX__)
192 { "../../../../../../../../../../../../../../../../../../../..", true },
195 { "/usr/bin", true },
196 { "/usr//bin", false },
197 { "/usr///bin", false }
201 for ( size_t n
= 0; n
< WXSIZEOF(testData
); n
++ )
203 wxString errDesc
= wxString::Format("failed on directory '%s'", testData
[n
].dirname
);
204 CPPUNIT_ASSERT_EQUAL_MESSAGE(errDesc
.ToStdString(), testData
[n
].shouldExist
, wxDir::Exists(testData
[n
].dirname
));
206 if (!testData
[n
].shouldExist
)
208 wxDir
d(testData
[n
].dirname
);
209 CPPUNIT_ASSERT(!d
.IsOpened());
213 CPPUNIT_ASSERT( wxDir::Exists(wxGetCwd()) );