]>
Commit | Line | Data |
---|---|---|
f095b1fc VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/filename/filename.cpp | |
3 | // Purpose: wxFileName unit test | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2004-07-25 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2004 Vadim Zeitlin | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
8899b155 | 14 | #include "testprec.h" |
f095b1fc VZ |
15 | |
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
f7c69b90 | 21 | #include "wx/utils.h" |
f095b1fc VZ |
22 | #endif // WX_PRECOMP |
23 | ||
24 | #include "wx/filename.h" | |
c08dd08b | 25 | #include "wx/filefn.h" |
f095b1fc | 26 | |
2264775b VZ |
27 | // ---------------------------------------------------------------------------- |
28 | // local functions | |
29 | // ---------------------------------------------------------------------------- | |
30 | ||
31 | // define stream inserter for wxFileName to use it in CPPUNIT_ASSERT_EQUAL() | |
32 | inline std::ostream& operator<<(std::ostream& o, const wxFileName& fn) | |
33 | { | |
34 | return o << fn.GetFullPath(); | |
35 | } | |
36 | ||
f095b1fc VZ |
37 | // ---------------------------------------------------------------------------- |
38 | // test data | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | static struct FileNameInfo | |
42 | { | |
43 | const wxChar *fullname; | |
44 | const wxChar *volume; | |
45 | const wxChar *path; | |
46 | const wxChar *name; | |
47 | const wxChar *ext; | |
48 | bool isAbsolute; | |
49 | wxPathFormat format; | |
50 | } filenames[] = | |
51 | { | |
69858116 | 52 | // the empty string |
e7b54c07 VS |
53 | { _T(""), _T(""), _T(""), _T(""), _T(""), false, wxPATH_UNIX }, |
54 | { _T(""), _T(""), _T(""), _T(""), _T(""), false, wxPATH_DOS }, | |
55 | { _T(""), _T(""), _T(""), _T(""), _T(""), false, wxPATH_VMS }, | |
69858116 | 56 | |
f095b1fc VZ |
57 | // Unix file names |
58 | { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX }, | |
59 | { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX }, | |
60 | { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX }, | |
61 | { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX }, | |
62 | { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX }, | |
63 | { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX }, | |
64 | { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX }, | |
65 | { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX }, | |
66 | { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX }, | |
67 | ||
68 | // Windows file names | |
69 | { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS }, | |
70 | { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS }, | |
71 | { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS }, | |
72 | { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS }, | |
73 | { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS }, | |
bf7f7793 RR |
74 | |
75 | // NB: when using the wxFileName::GetLongPath() function on these two strings, | |
76 | // the program will hang various seconds. All those time is taken by the | |
77 | // call to the win32 API GetLongPathName()... | |
f095b1fc VZ |
78 | { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS }, |
79 | { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS }, | |
80 | ||
34841b21 VZ |
81 | // consecutive [back]slashes should be treated as single occurrences of |
82 | // them and not interpreted as share names if there is a volume name | |
83 | { _T("c:\\aaa\\bbb\\ccc"), _T("c"), _T("\\aaa\\bbb"), _T("ccc"), _T(""), true, wxPATH_DOS }, | |
9b9596de | 84 | { _T("c:\\\\aaa\\bbb\\ccc"), _T("c"), _T("\\\\aaa\\bbb"), _T("ccc"), _T(""), true, wxPATH_DOS }, |
bf7f7793 | 85 | |
f095b1fc VZ |
86 | // wxFileName support for Mac file names is broken currently |
87 | #if 0 | |
88 | // Mac file names | |
89 | { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC }, | |
90 | { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC }, | |
91 | { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC }, | |
92 | { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC }, | |
93 | { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC }, | |
94 | { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC }, | |
95 | #endif // 0 | |
96 | ||
97 | // VMS file names | |
bf7f7793 RR |
98 | // NB: on Windows they have the same effect of the \\server\\ strings |
99 | // (see the note above) | |
f095b1fc VZ |
100 | { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS }, |
101 | { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS }, | |
102 | }; | |
103 | ||
104 | // ---------------------------------------------------------------------------- | |
105 | // test class | |
106 | // ---------------------------------------------------------------------------- | |
107 | ||
108 | class FileNameTestCase : public CppUnit::TestCase | |
109 | { | |
110 | public: | |
111 | FileNameTestCase() { } | |
112 | ||
113 | private: | |
114 | CPPUNIT_TEST_SUITE( FileNameTestCase ); | |
115 | CPPUNIT_TEST( TestConstruction ); | |
33366127 | 116 | CPPUNIT_TEST( TestComparison ); |
f095b1fc | 117 | CPPUNIT_TEST( TestSplit ); |
4524a24b | 118 | CPPUNIT_TEST( TestSetPath ); |
c08dd08b | 119 | CPPUNIT_TEST( TestStrip ); |
bf7f7793 | 120 | CPPUNIT_TEST( TestNormalize ); |
60c0dfe5 VZ |
121 | #ifdef __WINDOWS__ |
122 | CPPUNIT_TEST( TestShortLongPath ); | |
123 | #endif // __WINDOWS__ | |
f095b1fc VZ |
124 | CPPUNIT_TEST_SUITE_END(); |
125 | ||
126 | void TestConstruction(); | |
33366127 | 127 | void TestComparison(); |
f095b1fc VZ |
128 | void TestSplit(); |
129 | void TestSetPath(); | |
c08dd08b | 130 | void TestStrip(); |
bf7f7793 | 131 | void TestNormalize(); |
60c0dfe5 VZ |
132 | #ifdef __WINDOWS__ |
133 | void TestShortLongPath(); | |
134 | #endif // __WINDOWS__ | |
f095b1fc VZ |
135 | |
136 | DECLARE_NO_COPY_CLASS(FileNameTestCase) | |
137 | }; | |
138 | ||
139 | // register in the unnamed registry so that these tests are run by default | |
140 | CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase ); | |
141 | ||
142 | // also include in it's own registry so that these tests can be run alone | |
143 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" ); | |
144 | ||
145 | void FileNameTestCase::TestConstruction() | |
146 | { | |
147 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
148 | { | |
149 | const FileNameInfo& fni = filenames[n]; | |
150 | ||
151 | wxFileName fn(fni.fullname, fni.format); | |
152 | ||
9b9596de VZ |
153 | // the original full name could contain consecutive [back]slashes, |
154 | // squeeze them except for the double backslash in the beginning in | |
155 | // Windows filenames where it has special meaning | |
156 | wxString fullnameOrig; | |
157 | if ( fni.format == wxPATH_DOS ) | |
158 | { | |
159 | // copy the backslashes at beginning unchanged | |
160 | const wxChar *p = fni.fullname; | |
161 | while ( *p == _T('\\') ) | |
162 | fullnameOrig += *p++; | |
163 | ||
164 | // replace consecutive slashes with single ones in the rest | |
165 | for ( wxChar chPrev = _T('\0'); *p; p++ ) | |
166 | { | |
167 | if ( *p == _T('\\') && chPrev == _T('\\') ) | |
168 | continue; | |
169 | ||
170 | chPrev = *p; | |
171 | fullnameOrig += chPrev; | |
172 | } | |
173 | } | |
174 | else // !wxPATH_DOS | |
175 | { | |
176 | fullnameOrig = fni.fullname; | |
177 | } | |
178 | ||
179 | fullnameOrig.Replace(_T("//"), _T("/")); | |
180 | ||
181 | ||
f095b1fc | 182 | wxString fullname = fn.GetFullPath(fni.format); |
9b9596de | 183 | CPPUNIT_ASSERT_EQUAL( fullnameOrig, fullname ); |
f095b1fc | 184 | |
ab8576b4 VZ |
185 | // notice that we use a dummy working directory to ensure that paths |
186 | // with "../.." in them could be normalized, otherwise this would fail | |
187 | // if the test is run from root directory or its direct subdirectory | |
2264775b VZ |
188 | CPPUNIT_ASSERT_MESSAGE |
189 | ( | |
b51abd73 | 190 | (const char *)wxString::Format(_T("Normalize(%s) failed"), fni.fullname).mb_str(), |
c2173965 | 191 | fn.Normalize(wxPATH_NORM_ALL, _T("/foo/bar/baz"), fni.format) |
2264775b | 192 | ); |
4c2deb19 VZ |
193 | |
194 | if ( *fni.volume && *fni.path ) | |
195 | { | |
196 | // check that specifying the volume separately or as part of the | |
197 | // path doesn't make any difference | |
198 | wxString pathWithVolume = fni.volume; | |
199 | pathWithVolume += wxFileName::GetVolumeSeparator(fni.format); | |
200 | pathWithVolume += fni.path; | |
201 | ||
2264775b | 202 | CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume, |
4c2deb19 VZ |
203 | fni.name, |
204 | fni.ext, | |
2264775b | 205 | fni.format), fn ); |
4c2deb19 | 206 | } |
f095b1fc | 207 | } |
69858116 VZ |
208 | |
209 | wxFileName fn; | |
210 | ||
211 | // empty strings | |
212 | fn.AssignDir(wxEmptyString); | |
213 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
214 | ||
215 | fn.Assign(wxEmptyString); | |
216 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
217 | ||
218 | fn.Assign(wxEmptyString, wxEmptyString); | |
219 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
220 | ||
221 | fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString); | |
222 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
223 | ||
224 | fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString, wxEmptyString); | |
225 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
f095b1fc VZ |
226 | } |
227 | ||
33366127 VZ |
228 | void FileNameTestCase::TestComparison() |
229 | { | |
523cd68a VZ |
230 | wxFileName fn1(wxT("/tmp/file1")); |
231 | wxFileName fn2(wxT("/tmp/dir2/../file2")); | |
232 | fn1.Normalize(); | |
233 | fn2.Normalize(); | |
2264775b | 234 | CPPUNIT_ASSERT_EQUAL(fn1.GetPath(), fn2.GetPath()); |
33366127 VZ |
235 | } |
236 | ||
f095b1fc VZ |
237 | void FileNameTestCase::TestSplit() |
238 | { | |
239 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
240 | { | |
241 | const FileNameInfo& fni = filenames[n]; | |
242 | wxString volume, path, name, ext; | |
243 | wxFileName::SplitPath(fni.fullname, | |
244 | &volume, &path, &name, &ext, fni.format); | |
245 | ||
2264775b VZ |
246 | CPPUNIT_ASSERT_EQUAL( wxString(fni.volume), volume ); |
247 | CPPUNIT_ASSERT_EQUAL( wxString(fni.path), path ); | |
248 | CPPUNIT_ASSERT_EQUAL( wxString(fni.name), name ); | |
249 | CPPUNIT_ASSERT_EQUAL( wxString(fni.ext), ext ); | |
f095b1fc | 250 | } |
dfecbee5 VZ |
251 | |
252 | // special case of empty extension | |
253 | wxFileName fn(_T("foo.")); | |
2264775b | 254 | CPPUNIT_ASSERT_EQUAL( wxString(_T("foo.")), fn.GetFullPath() ); |
f095b1fc VZ |
255 | } |
256 | ||
4524a24b VZ |
257 | void FileNameTestCase::TestSetPath() |
258 | { | |
259 | wxFileName fn(_T("d:\\test\\foo.bar"), wxPATH_DOS); | |
67050b6d VS |
260 | fn.SetPath(_T("c:\\temp"), wxPATH_DOS); |
261 | CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS)) ); | |
4524a24b | 262 | |
67050b6d VS |
263 | fn = wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX); |
264 | fn.SetPath(_T("/usr/local/bin"), wxPATH_UNIX); | |
265 | CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX)) ); | |
4524a24b VZ |
266 | } |
267 | ||
bf7f7793 RR |
268 | void FileNameTestCase::TestNormalize() |
269 | { | |
270 | // prepare some data to be used later | |
271 | wxString sep = wxFileName::GetPathSeparator(); | |
272 | wxString cwd = wxGetCwd(); | |
273 | wxString home = wxGetUserHome(); | |
274 | ||
275 | cwd.Replace(sep, wxT("/")); | |
276 | if (cwd.Last() != wxT('/')) | |
277 | cwd += wxT('/'); | |
278 | home.Replace(sep, wxT("/")); | |
279 | if (home.Last() != wxT('/')) | |
280 | home += wxT('/'); | |
281 | ||
282 | // since we will always be testing paths using the wxPATH_UNIX | |
283 | // format, we need to remove the volume, if present | |
284 | if (home.Contains(wxT(':'))) | |
285 | home = home.AfterFirst(wxT(':')); | |
286 | if (cwd.Contains(wxT(':'))) | |
287 | cwd = cwd.AfterFirst(wxT(':')); | |
288 | ||
289 | static struct FileNameTest | |
290 | { | |
2264775b | 291 | const wxChar *original; |
bf7f7793 RR |
292 | int flags; |
293 | wxString expected; | |
294 | } tests[] = | |
295 | { | |
296 | // test wxPATH_NORM_ENV_VARS | |
297 | #ifdef __WXMSW__ | |
298 | { wxT("%ABCDEF%/g/h/i"), wxPATH_NORM_ENV_VARS, wxT("abcdef/g/h/i") }, | |
299 | #else | |
300 | { wxT("$(ABCDEF)/g/h/i"), wxPATH_NORM_ENV_VARS, wxT("abcdef/g/h/i") }, | |
301 | #endif | |
302 | ||
303 | // test wxPATH_NORM_DOTS | |
304 | { wxT("a/.././b/c/../../"), wxPATH_NORM_DOTS, wxT("") }, | |
305 | ||
306 | // test wxPATH_NORM_TILDE | |
307 | // NB: do the tilde expansion also under Windows to test if it works there too | |
308 | { wxT("/a/b/~"), wxPATH_NORM_TILDE, wxT("/a/b/~") }, | |
309 | { wxT("/~/a/b"), wxPATH_NORM_TILDE, home + wxT("a/b") }, | |
310 | { wxT("~/a/b"), wxPATH_NORM_TILDE, home + wxT("a/b") }, | |
311 | ||
312 | // test wxPATH_NORM_ABSOLUTE | |
313 | { wxT("a/b/"), wxPATH_NORM_ABSOLUTE, cwd + wxT("a/b/") }, | |
314 | { wxT("a/b/c.ext"), wxPATH_NORM_ABSOLUTE, cwd + wxT("a/b/c.ext") }, | |
315 | { wxT("/a"), wxPATH_NORM_ABSOLUTE, wxT("/a") }, | |
316 | ||
317 | // test giving no flags at all to Normalize() | |
318 | { wxT("a/b/"), 0, wxT("a/b/") }, | |
319 | { wxT("a/b/c.ext"), 0, wxT("a/b/c.ext") }, | |
320 | { wxT("/a"), 0, wxT("/a") } | |
321 | }; | |
322 | ||
323 | // set the env var ABCDEF | |
324 | wxSetEnv(_T("ABCDEF"), _T("abcdef")); | |
325 | ||
2264775b | 326 | for ( size_t i = 0; i < WXSIZEOF(tests); i++ ) |
bf7f7793 RR |
327 | { |
328 | wxFileName fn(tests[i].original, wxPATH_UNIX); | |
329 | ||
330 | // be sure this normalization does not fail | |
2264775b VZ |
331 | CPPUNIT_ASSERT_MESSAGE |
332 | ( | |
b51abd73 | 333 | (const char *)wxString::Format(_T("Normalize(%s) failed"), tests[i].original).mb_str(), |
2264775b VZ |
334 | fn.Normalize(tests[i].flags, cwd, wxPATH_UNIX) |
335 | ); | |
bf7f7793 RR |
336 | |
337 | // compare result with expected string | |
338 | CPPUNIT_ASSERT_EQUAL( tests[i].expected, fn.GetFullPath(wxPATH_UNIX) ); | |
339 | } | |
340 | } | |
341 | ||
ff3d9a35 | 342 | wxString wxTestStripExtension(wxString szFile) |
c08dd08b RN |
343 | { |
344 | wxStripExtension(szFile); | |
345 | return szFile; | |
346 | } | |
347 | ||
348 | void FileNameTestCase::TestStrip() | |
349 | { | |
350 | //test a crash | |
2264775b | 351 | CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T("")) ); |
c08dd08b RN |
352 | |
353 | //others | |
2264775b VZ |
354 | CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".")) ); |
355 | CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".wav")) ); | |
356 | CPPUNIT_ASSERT_EQUAL( wxString(_T("good")), wxTestStripExtension(_T("good.wav")) ); | |
357 | CPPUNIT_ASSERT_EQUAL( wxString(_T("good.wav")), wxTestStripExtension(_T("good.wav.wav")) ); | |
ff3d9a35 | 358 | } |
60c0dfe5 VZ |
359 | |
360 | #ifdef __WINDOWS__ | |
361 | ||
362 | void FileNameTestCase::TestShortLongPath() | |
363 | { | |
3f302e75 | 364 | wxFileName fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe")); |
60c0dfe5 VZ |
365 | |
366 | // incredibly enough, GetLongPath() used to return different results during | |
367 | // the first and subsequent runs, test for this | |
368 | CPPUNIT_ASSERT_EQUAL( fn.GetLongPath(), fn.GetLongPath() ); | |
369 | CPPUNIT_ASSERT_EQUAL( fn.GetShortPath(), fn.GetShortPath() ); | |
370 | } | |
371 | ||
372 | #endif // __WINDOWS__ |