]>
Commit | Line | Data |
---|---|---|
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" | |
22 | #include "wx/stdpaths.h" | |
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 ); | |
44 | CPPUNIT_TEST( GetName ); | |
45 | CPPUNIT_TEST_SUITE_END(); | |
46 | ||
47 | void DirExists(); | |
48 | void Traverse(); | |
49 | void Enum(); | |
50 | void GetName(); | |
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); | |
85 | ||
86 | CreateTempFile(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2" + SEP + "dummy"); | |
87 | CreateTempFile(DIRTEST_FOLDER + SEP + "dummy"); | |
88 | } | |
89 | ||
90 | void DirTestCase::tearDown() | |
91 | { | |
92 | wxRemove(DIRTEST_FOLDER + SEP + "folder1" + SEP + "subfolder2" + SEP + "dummy"); | |
93 | wxRemove(DIRTEST_FOLDER + SEP + "dummy"); | |
94 | wxDir::Remove(DIRTEST_FOLDER, wxPATH_RMDIR_RECURSIVE); | |
95 | } | |
96 | ||
97 | wxArrayString DirTestCase::DirEnumHelper(wxDir& dir, | |
98 | int flags, | |
99 | const wxString& filespec) | |
100 | { | |
101 | wxArrayString ret; | |
102 | CPPUNIT_ASSERT( dir.IsOpened() ); | |
103 | ||
104 | wxString filename; | |
105 | bool cont = dir.GetFirst(&filename, filespec, flags); | |
106 | while ( cont ) | |
107 | { | |
108 | ret.push_back(filename); | |
109 | cont = dir.GetNext(&filename); | |
110 | } | |
111 | ||
112 | return ret; | |
113 | } | |
114 | ||
115 | void DirTestCase::Enum() | |
116 | { | |
117 | wxDir dir(DIRTEST_FOLDER); | |
118 | CPPUNIT_ASSERT( dir.IsOpened() ); | |
119 | ||
120 | // enumerating everything in test directory | |
121 | CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir).size()); | |
122 | ||
123 | // enumerating really everything in test directory recursively | |
124 | CPPUNIT_ASSERT_EQUAL(6, DirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT).size()); | |
125 | ||
126 | // enumerating object files in test directory | |
127 | CPPUNIT_ASSERT_EQUAL(0, DirEnumHelper(dir, wxDIR_DEFAULT, "*.o*").size()); | |
128 | ||
129 | // enumerating directories in test directory | |
130 | CPPUNIT_ASSERT_EQUAL(3, DirEnumHelper(dir, wxDIR_DIRS).size()); | |
131 | ||
132 | // enumerating files in test directory | |
133 | CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir, wxDIR_FILES).size()); | |
134 | ||
135 | // enumerating files including hidden in test directory | |
136 | CPPUNIT_ASSERT_EQUAL(1, DirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN).size()); | |
137 | ||
138 | // enumerating files and folders in test directory | |
139 | CPPUNIT_ASSERT_EQUAL(4, DirEnumHelper(dir, wxDIR_FILES | wxDIR_DIRS).size()); | |
140 | } | |
141 | ||
142 | class TestDirTraverser : public wxDirTraverser | |
143 | { | |
144 | public: | |
145 | wxArrayString dirs; | |
146 | ||
147 | virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename)) | |
148 | { | |
149 | return wxDIR_CONTINUE; | |
150 | } | |
151 | ||
152 | virtual wxDirTraverseResult OnDir(const wxString& dirname) | |
153 | { | |
154 | dirs.push_back(dirname); | |
155 | return wxDIR_CONTINUE; | |
156 | } | |
157 | }; | |
158 | ||
159 | void DirTestCase::Traverse() | |
160 | { | |
161 | // enum all files | |
162 | wxArrayString files; | |
163 | CPPUNIT_ASSERT_EQUAL(2, wxDir::GetAllFiles(DIRTEST_FOLDER, &files)); | |
164 | ||
165 | // enum again with custom traverser | |
166 | wxDir dir(DIRTEST_FOLDER); | |
167 | TestDirTraverser traverser; | |
168 | dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN); | |
169 | CPPUNIT_ASSERT_EQUAL(6, traverser.dirs.size()); | |
170 | } | |
171 | ||
172 | void DirTestCase::DirExists() | |
173 | { | |
174 | struct | |
175 | { | |
176 | const char *dirname; | |
177 | bool shouldExist; | |
178 | } testData[] = | |
179 | { | |
180 | { ".", true }, | |
181 | { "..", true }, | |
182 | { "$USER_DOCS_DIR", true }, | |
183 | #if defined(__WINDOWS__) | |
184 | { "$USER_DOCS_DIR\\", true }, | |
185 | { "$USER_DOCS_DIR\\\\", true }, | |
186 | { "..\\..", true }, | |
187 | { "$MSW_DRIVE", true }, | |
188 | { "$MSW_DRIVE\\", true }, | |
189 | { "$MSW_DRIVE\\\\", true }, | |
190 | { "\\\\non_existent_share\\file", false }, | |
191 | { "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist", false }, | |
192 | { "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist\\", false }, | |
193 | { "$MSW_DRIVE\\a\\directory\\which\\does\\not\\exist\\\\", false }, | |
194 | { "test.exe", false } // not a directory! | |
195 | #elif defined(__UNIX__) | |
196 | { "../..", true }, | |
197 | { "/", true }, | |
198 | { "//", true }, | |
199 | { "/usr/bin", true }, | |
200 | { "/usr//bin", true }, | |
201 | { "/usr///bin", true }, | |
202 | { "/tmp/a/directory/which/does/not/exist", false }, | |
203 | { "/bin/ls", false } // not a directory! | |
204 | #endif | |
205 | }; | |
206 | ||
207 | #ifdef __WINDOWS__ | |
208 | wxString homedrive = wxGetenv("HOMEDRIVE"); | |
209 | if ( homedrive.empty() ) | |
210 | homedrive = "c:"; | |
211 | #endif // __WINDOWS__ | |
212 | ||
213 | for ( size_t n = 0; n < WXSIZEOF(testData); n++ ) | |
214 | { | |
215 | wxString dirname = testData[n].dirname; | |
216 | dirname.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir()); | |
217 | ||
218 | #ifdef __WINDOWS__ | |
219 | dirname.Replace("$MSW_DRIVE", homedrive); | |
220 | #endif // __WINDOWS__ | |
221 | ||
222 | std::string errDesc = wxString::Format("failed on directory '%s'", dirname).ToStdString(); | |
223 | CPPUNIT_ASSERT_EQUAL_MESSAGE(errDesc, testData[n].shouldExist, wxDir::Exists(dirname)); | |
224 | ||
225 | wxDir d(dirname); | |
226 | CPPUNIT_ASSERT_EQUAL(testData[n].shouldExist, d.IsOpened()); | |
227 | } | |
228 | ||
229 | CPPUNIT_ASSERT( wxDir::Exists(wxGetCwd()) ); | |
230 | } | |
231 | ||
232 | void DirTestCase::GetName() | |
233 | { | |
234 | wxDir d; | |
235 | ||
236 | CPPUNIT_ASSERT( d.Open(".") ); | |
237 | CPPUNIT_ASSERT( d.GetName().Last() != wxFILE_SEP_PATH ); | |
238 | CPPUNIT_ASSERT( d.GetNameWithSep().Last() == wxFILE_SEP_PATH ); | |
239 | CPPUNIT_ASSERT_EQUAL( d.GetName() + wxFILE_SEP_PATH, | |
240 | d.GetNameWithSep() ); | |
241 | ||
242 | #ifdef __UNIX__ | |
243 | CPPUNIT_ASSERT( d.Open("/") ); | |
244 | CPPUNIT_ASSERT_EQUAL( "/", d.GetName() ); | |
245 | CPPUNIT_ASSERT_EQUAL( "/", d.GetNameWithSep() ); | |
246 | #endif | |
247 | } |