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