]> git.saurik.com Git - wxWidgets.git/blob - tests/file/dir.cpp
XRC spec: document the "focused" window property.
[wxWidgets.git] / tests / file / dir.cpp
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 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #include "wx/dir.h"
20 #include "wx/filename.h"
21 #include "wx/stdpaths.h"
22
23 #define DIRTEST_FOLDER wxString("dirTest_folder")
24 #define SEP wxFileName::GetPathSeparator()
25
26 // ----------------------------------------------------------------------------
27 // test class
28 // ----------------------------------------------------------------------------
29
30 class DirTestCase : public CppUnit::TestCase
31 {
32 public:
33 DirTestCase() { }
34
35 virtual void setUp();
36 virtual void tearDown();
37
38 private:
39 CPPUNIT_TEST_SUITE( DirTestCase );
40 CPPUNIT_TEST( DirExists );
41 CPPUNIT_TEST( Traverse );
42 CPPUNIT_TEST( Enum );
43 CPPUNIT_TEST( GetName );
44 CPPUNIT_TEST_SUITE_END();
45
46 void DirExists();
47 void Traverse();
48 void Enum();
49 void GetName();
50
51 void CreateTempFile(const wxString& path);
52 wxArrayString DirEnumHelper(wxDir& dir,
53 int flags = wxDIR_DEFAULT,
54 const wxString& filespec = wxEmptyString);
55
56 wxDECLARE_NO_COPY_CLASS(DirTestCase);
57 };
58
59 // ----------------------------------------------------------------------------
60 // CppUnit macros
61 // ----------------------------------------------------------------------------
62
63 CPPUNIT_TEST_SUITE_REGISTRATION( DirTestCase );
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DirTestCase, "DirTestCase" );
65
66 // ----------------------------------------------------------------------------
67 // tests implementation
68 // ----------------------------------------------------------------------------
69
70 void DirTestCase::CreateTempFile(const wxString& path)
71 {
72 wxFile f(path, wxFile::write);
73 f.Write("dummy test file");
74 f.Close();
75 }
76
77 void DirTestCase::setUp()
78 {
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);
84
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");
89 }
90
91 void DirTestCase::tearDown()
92 {
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);
98 }
99
100 wxArrayString DirTestCase::DirEnumHelper(wxDir& dir,
101 int flags,
102 const wxString& filespec)
103 {
104 wxArrayString ret;
105 CPPUNIT_ASSERT( dir.IsOpened() );
106
107 wxString filename;
108 bool cont = dir.GetFirst(&filename, filespec, flags);
109 while ( cont )
110 {
111 ret.push_back(filename);
112 cont = dir.GetNext(&filename);
113 }
114
115 return ret;
116 }
117
118 void DirTestCase::Enum()
119 {
120 wxDir dir(DIRTEST_FOLDER);
121 CPPUNIT_ASSERT( dir.IsOpened() );
122
123 // enumerating everything in test directory
124 CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir).size());
125
126 // enumerating really everything in test directory recursively
127 CPPUNIT_ASSERT_EQUAL(6, DirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT).size());
128
129 // enumerating object files in test directory
130 CPPUNIT_ASSERT_EQUAL(0, DirEnumHelper(dir, wxDIR_DEFAULT, "*.o*").size());
131
132 // enumerating directories in test directory
133 CPPUNIT_ASSERT_EQUAL(3, DirEnumHelper(dir, wxDIR_DIRS).size());
134
135 // enumerating files in test directory
136 CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir, wxDIR_FILES).size());
137
138 // enumerating files including hidden in test directory
139 CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN).size());
140
141 // enumerating files and folders in test directory
142 CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir, wxDIR_FILES | wxDIR_DIRS).size());
143 }
144
145 class TestDirTraverser : public wxDirTraverser
146 {
147 public:
148 wxArrayString dirs;
149
150 virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
151 {
152 return wxDIR_CONTINUE;
153 }
154
155 virtual wxDirTraverseResult OnDir(const wxString& dirname)
156 {
157 dirs.push_back(dirname);
158 return wxDIR_CONTINUE;
159 }
160 };
161
162 void DirTestCase::Traverse()
163 {
164 // enum all files
165 wxArrayString files;
166 CPPUNIT_ASSERT_EQUAL(4, wxDir::GetAllFiles(DIRTEST_FOLDER, &files));
167
168 // enum all files according to the filter
169 CPPUNIT_ASSERT_EQUAL(1, wxDir::GetAllFiles(DIRTEST_FOLDER, &files, "*.foo"));
170
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());
176 }
177
178 void DirTestCase::DirExists()
179 {
180 struct
181 {
182 const char *dirname;
183 bool shouldExist;
184 } testData[] =
185 {
186 { ".", true },
187 { "..", true },
188 { "$USER_DOCS_DIR", true },
189 #if defined(__WINDOWS__)
190 { "$USER_DOCS_DIR\\", true },
191 { "$USER_DOCS_DIR\\\\", true },
192 { "..\\..", 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__)
202 { "../..", true },
203 { "/", true },
204 { "//", true },
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!
210 #endif
211 };
212
213 #ifdef __WINDOWS__
214 wxString homedrive = wxGetenv("HOMEDRIVE");
215 if ( homedrive.empty() )
216 homedrive = "c:";
217 #endif // __WINDOWS__
218
219 for ( size_t n = 0; n < WXSIZEOF(testData); n++ )
220 {
221 wxString dirname = testData[n].dirname;
222 dirname.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir());
223
224 #ifdef __WINDOWS__
225 dirname.Replace("$MSW_DRIVE", homedrive);
226 #endif // __WINDOWS__
227
228 std::string errDesc = wxString::Format("failed on directory '%s'", dirname).ToStdString();
229 CPPUNIT_ASSERT_EQUAL_MESSAGE(errDesc, testData[n].shouldExist, wxDir::Exists(dirname));
230
231 wxDir d(dirname);
232 CPPUNIT_ASSERT_EQUAL(testData[n].shouldExist, d.IsOpened());
233 }
234
235 CPPUNIT_ASSERT( wxDir::Exists(wxGetCwd()) );
236 }
237
238 void DirTestCase::GetName()
239 {
240 wxDir d;
241
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() );
247
248 #ifdef __UNIX__
249 CPPUNIT_ASSERT( d.Open("/") );
250 CPPUNIT_ASSERT_EQUAL( "/", d.GetName() );
251 CPPUNIT_ASSERT_EQUAL( "/", d.GetNameWithSep() );
252 #endif
253 }