]>
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" |
1fe1aecb | 26 | #include "wx/stdpaths.h" |
f095b1fc | 27 | |
525711d7 VZ |
28 | #ifdef __WXMSW__ |
29 | #include "wx/msw/registry.h" | |
30 | #endif // __WXMSW__ | |
31 | ||
40152925 | 32 | #include "testfile.h" |
2264775b | 33 | |
f095b1fc VZ |
34 | // ---------------------------------------------------------------------------- |
35 | // test data | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
5fed01a9 | 38 | static struct TestFileNameInfo |
f095b1fc | 39 | { |
9630954d VZ |
40 | const char *fullname; |
41 | const char *volume; | |
42 | const char *path; | |
43 | const char *name; | |
44 | const char *ext; | |
f095b1fc VZ |
45 | bool isAbsolute; |
46 | wxPathFormat format; | |
47 | } filenames[] = | |
48 | { | |
69858116 | 49 | // the empty string |
9630954d VZ |
50 | { "", "", "", "", "", false, wxPATH_UNIX }, |
51 | { "", "", "", "", "", false, wxPATH_DOS }, | |
52 | { "", "", "", "", "", false, wxPATH_VMS }, | |
69858116 | 53 | |
f095b1fc | 54 | // Unix file names |
9630954d VZ |
55 | { "/usr/bin/ls", "", "/usr/bin", "ls", "", true, wxPATH_UNIX }, |
56 | { "/usr/bin/", "", "/usr/bin", "", "", true, wxPATH_UNIX }, | |
57 | { "~/.zshrc", "", "~", ".zshrc", "", true, wxPATH_UNIX }, | |
58 | { "../../foo", "", "../..", "foo", "", false, wxPATH_UNIX }, | |
59 | { "foo.bar", "", "", "foo", "bar", false, wxPATH_UNIX }, | |
60 | { "~/foo.bar", "", "~", "foo", "bar", true, wxPATH_UNIX }, | |
be5be16a VZ |
61 | { "~user/foo.bar", "", "~user", "foo", "bar", true, wxPATH_UNIX }, |
62 | { "~user/", "", "~user", "", "", true, wxPATH_UNIX }, | |
9630954d VZ |
63 | { "/foo", "", "/", "foo", "", true, wxPATH_UNIX }, |
64 | { "Mahogany-0.60/foo.bar", "", "Mahogany-0.60", "foo", "bar", false, wxPATH_UNIX }, | |
65 | { "/tmp/wxwin.tar.bz", "", "/tmp", "wxwin.tar", "bz", true, wxPATH_UNIX }, | |
f095b1fc VZ |
66 | |
67 | // Windows file names | |
9630954d VZ |
68 | { "foo.bar", "", "", "foo", "bar", false, wxPATH_DOS }, |
69 | { "\\foo.bar", "", "\\", "foo", "bar", false, wxPATH_DOS }, | |
70 | { "c:foo.bar", "c", "", "foo", "bar", false, wxPATH_DOS }, | |
71 | { "c:\\foo.bar", "c", "\\", "foo", "bar", true, wxPATH_DOS }, | |
72 | { "c:\\Windows\\command.com", "c", "\\Windows", "command", "com", true, wxPATH_DOS }, | |
e01a788e VZ |
73 | { "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\", |
74 | "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", "\\", "", "", true, wxPATH_DOS }, | |
75 | { "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\Program Files\\setup.exe", | |
76 | "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", "\\Program Files", "setup", "exe", true, wxPATH_DOS }, | |
bf7f7793 | 77 | |
ea6319cb | 78 | #if 0 |
c7099635 VZ |
79 | // NB: when using the wxFileName::GetLongPath() function on these two |
80 | // strings, the program will hang for several seconds blocking inside | |
81 | // Win32 GetLongPathName() function | |
9630954d VZ |
82 | { "\\\\server\\foo.bar", "server", "\\", "foo", "bar", true, wxPATH_DOS }, |
83 | { "\\\\server\\dir\\foo.bar", "server", "\\dir", "foo", "bar", true, wxPATH_DOS }, | |
ea6319cb | 84 | #endif |
f095b1fc | 85 | |
34841b21 VZ |
86 | // consecutive [back]slashes should be treated as single occurrences of |
87 | // them and not interpreted as share names if there is a volume name | |
9630954d VZ |
88 | { "c:\\aaa\\bbb\\ccc", "c", "\\aaa\\bbb", "ccc", "", true, wxPATH_DOS }, |
89 | { "c:\\\\aaa\\bbb\\ccc", "c", "\\\\aaa\\bbb", "ccc", "", true, wxPATH_DOS }, | |
bf7f7793 | 90 | |
f095b1fc VZ |
91 | // wxFileName support for Mac file names is broken currently |
92 | #if 0 | |
93 | // Mac file names | |
9630954d VZ |
94 | { "Volume:Dir:File", "Volume", "Dir", "File", "", true, wxPATH_MAC }, |
95 | { "Volume:Dir:Subdir:File", "Volume", "Dir:Subdir", "File", "", true, wxPATH_MAC }, | |
96 | { "Volume:", "Volume", "", "", "", true, wxPATH_MAC }, | |
97 | { ":Dir:File", "", "Dir", "File", "", false, wxPATH_MAC }, | |
98 | { ":File.Ext", "", "", "File", ".Ext", false, wxPATH_MAC }, | |
99 | { "File.Ext", "", "", "File", ".Ext", false, wxPATH_MAC }, | |
f095b1fc VZ |
100 | #endif // 0 |
101 | ||
ea6319cb | 102 | #if 0 |
f095b1fc | 103 | // VMS file names |
bf7f7793 RR |
104 | // NB: on Windows they have the same effect of the \\server\\ strings |
105 | // (see the note above) | |
9630954d | 106 | { "device:[dir1.dir2.dir3]file.txt", "device", "dir1.dir2.dir3", "file", "txt", true, wxPATH_VMS }, |
ea6319cb | 107 | #endif |
9630954d | 108 | { "file.txt", "", "", "file", "txt", false, wxPATH_VMS }, |
f095b1fc VZ |
109 | }; |
110 | ||
111 | // ---------------------------------------------------------------------------- | |
112 | // test class | |
113 | // ---------------------------------------------------------------------------- | |
114 | ||
115 | class FileNameTestCase : public CppUnit::TestCase | |
116 | { | |
117 | public: | |
118 | FileNameTestCase() { } | |
119 | ||
120 | private: | |
121 | CPPUNIT_TEST_SUITE( FileNameTestCase ); | |
122 | CPPUNIT_TEST( TestConstruction ); | |
33366127 | 123 | CPPUNIT_TEST( TestComparison ); |
f095b1fc | 124 | CPPUNIT_TEST( TestSplit ); |
4524a24b | 125 | CPPUNIT_TEST( TestSetPath ); |
c08dd08b | 126 | CPPUNIT_TEST( TestStrip ); |
bf7f7793 | 127 | CPPUNIT_TEST( TestNormalize ); |
395f3aa8 | 128 | CPPUNIT_TEST( TestReplace ); |
b2edb8f3 | 129 | CPPUNIT_TEST( TestGetHumanReadable ); |
60c0dfe5 VZ |
130 | #ifdef __WINDOWS__ |
131 | CPPUNIT_TEST( TestShortLongPath ); | |
132 | #endif // __WINDOWS__ | |
7b611a3a | 133 | CPPUNIT_TEST( TestUNC ); |
e01a788e | 134 | CPPUNIT_TEST( TestVolumeUniqueName ); |
1fe1aecb FM |
135 | CPPUNIT_TEST( TestCreateTempFileName ); |
136 | CPPUNIT_TEST( TestGetTimes ); | |
901504c3 | 137 | CPPUNIT_TEST( TestExists ); |
f095b1fc VZ |
138 | CPPUNIT_TEST_SUITE_END(); |
139 | ||
140 | void TestConstruction(); | |
33366127 | 141 | void TestComparison(); |
f095b1fc VZ |
142 | void TestSplit(); |
143 | void TestSetPath(); | |
c08dd08b | 144 | void TestStrip(); |
bf7f7793 | 145 | void TestNormalize(); |
395f3aa8 | 146 | void TestReplace(); |
b2edb8f3 | 147 | void TestGetHumanReadable(); |
60c0dfe5 VZ |
148 | #ifdef __WINDOWS__ |
149 | void TestShortLongPath(); | |
150 | #endif // __WINDOWS__ | |
7b611a3a | 151 | void TestUNC(); |
e01a788e | 152 | void TestVolumeUniqueName(); |
1fe1aecb FM |
153 | void TestCreateTempFileName(); |
154 | void TestGetTimes(); | |
901504c3 | 155 | void TestExists(); |
f095b1fc VZ |
156 | |
157 | DECLARE_NO_COPY_CLASS(FileNameTestCase) | |
158 | }; | |
159 | ||
160 | // register in the unnamed registry so that these tests are run by default | |
161 | CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase ); | |
162 | ||
e3778b4d | 163 | // also include in its own registry so that these tests can be run alone |
f095b1fc VZ |
164 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" ); |
165 | ||
166 | void FileNameTestCase::TestConstruction() | |
167 | { | |
168 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
169 | { | |
5fed01a9 | 170 | const TestFileNameInfo& fni = filenames[n]; |
f095b1fc VZ |
171 | |
172 | wxFileName fn(fni.fullname, fni.format); | |
173 | ||
9b9596de VZ |
174 | // the original full name could contain consecutive [back]slashes, |
175 | // squeeze them except for the double backslash in the beginning in | |
176 | // Windows filenames where it has special meaning | |
177 | wxString fullnameOrig; | |
178 | if ( fni.format == wxPATH_DOS ) | |
179 | { | |
180 | // copy the backslashes at beginning unchanged | |
9630954d VZ |
181 | const char *p = fni.fullname; |
182 | while ( *p == '\\' ) | |
9b9596de VZ |
183 | fullnameOrig += *p++; |
184 | ||
185 | // replace consecutive slashes with single ones in the rest | |
9630954d | 186 | for ( char chPrev = '\0'; *p; p++ ) |
9b9596de | 187 | { |
9630954d | 188 | if ( *p == '\\' && chPrev == '\\' ) |
9b9596de VZ |
189 | continue; |
190 | ||
191 | chPrev = *p; | |
192 | fullnameOrig += chPrev; | |
193 | } | |
194 | } | |
195 | else // !wxPATH_DOS | |
196 | { | |
197 | fullnameOrig = fni.fullname; | |
198 | } | |
199 | ||
9630954d | 200 | fullnameOrig.Replace("//", "/"); |
9b9596de VZ |
201 | |
202 | ||
f095b1fc | 203 | wxString fullname = fn.GetFullPath(fni.format); |
9b9596de | 204 | CPPUNIT_ASSERT_EQUAL( fullnameOrig, fullname ); |
f095b1fc | 205 | |
ab8576b4 VZ |
206 | // notice that we use a dummy working directory to ensure that paths |
207 | // with "../.." in them could be normalized, otherwise this would fail | |
208 | // if the test is run from root directory or its direct subdirectory | |
2264775b VZ |
209 | CPPUNIT_ASSERT_MESSAGE |
210 | ( | |
9630954d VZ |
211 | (const char *)wxString::Format("Normalize(%s) failed", fni.fullname).mb_str(), |
212 | fn.Normalize(wxPATH_NORM_ALL, "/foo/bar/baz", fni.format) | |
2264775b | 213 | ); |
4c2deb19 VZ |
214 | |
215 | if ( *fni.volume && *fni.path ) | |
216 | { | |
217 | // check that specifying the volume separately or as part of the | |
218 | // path doesn't make any difference | |
219 | wxString pathWithVolume = fni.volume; | |
220 | pathWithVolume += wxFileName::GetVolumeSeparator(fni.format); | |
221 | pathWithVolume += fni.path; | |
222 | ||
2264775b | 223 | CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume, |
4c2deb19 VZ |
224 | fni.name, |
225 | fni.ext, | |
2264775b | 226 | fni.format), fn ); |
4c2deb19 | 227 | } |
f095b1fc | 228 | } |
69858116 VZ |
229 | |
230 | wxFileName fn; | |
231 | ||
232 | // empty strings | |
233 | fn.AssignDir(wxEmptyString); | |
234 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
235 | ||
236 | fn.Assign(wxEmptyString); | |
237 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
238 | ||
239 | fn.Assign(wxEmptyString, wxEmptyString); | |
240 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
241 | ||
242 | fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString); | |
243 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
244 | ||
245 | fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString, wxEmptyString); | |
246 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
f095b1fc VZ |
247 | } |
248 | ||
33366127 VZ |
249 | void FileNameTestCase::TestComparison() |
250 | { | |
523cd68a VZ |
251 | wxFileName fn1(wxT("/tmp/file1")); |
252 | wxFileName fn2(wxT("/tmp/dir2/../file2")); | |
253 | fn1.Normalize(); | |
254 | fn2.Normalize(); | |
2264775b | 255 | CPPUNIT_ASSERT_EQUAL(fn1.GetPath(), fn2.GetPath()); |
33366127 VZ |
256 | } |
257 | ||
f095b1fc VZ |
258 | void FileNameTestCase::TestSplit() |
259 | { | |
260 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
261 | { | |
5fed01a9 | 262 | const TestFileNameInfo& fni = filenames[n]; |
f095b1fc VZ |
263 | wxString volume, path, name, ext; |
264 | wxFileName::SplitPath(fni.fullname, | |
265 | &volume, &path, &name, &ext, fni.format); | |
266 | ||
2264775b VZ |
267 | CPPUNIT_ASSERT_EQUAL( wxString(fni.volume), volume ); |
268 | CPPUNIT_ASSERT_EQUAL( wxString(fni.path), path ); | |
269 | CPPUNIT_ASSERT_EQUAL( wxString(fni.name), name ); | |
270 | CPPUNIT_ASSERT_EQUAL( wxString(fni.ext), ext ); | |
f095b1fc | 271 | } |
dfecbee5 VZ |
272 | |
273 | // special case of empty extension | |
9630954d VZ |
274 | wxFileName fn("foo."); |
275 | CPPUNIT_ASSERT_EQUAL( wxString("foo."), fn.GetFullPath() ); | |
f095b1fc VZ |
276 | } |
277 | ||
4524a24b VZ |
278 | void FileNameTestCase::TestSetPath() |
279 | { | |
9630954d VZ |
280 | wxFileName fn("d:\\test\\foo.bar", wxPATH_DOS); |
281 | fn.SetPath("c:\\temp", wxPATH_DOS); | |
282 | CPPUNIT_ASSERT( fn.SameAs(wxFileName("c:\\temp\\foo.bar", wxPATH_DOS)) ); | |
4524a24b | 283 | |
9630954d VZ |
284 | fn = wxFileName("/usr/bin/ls", wxPATH_UNIX); |
285 | fn.SetPath("/usr/local/bin", wxPATH_UNIX); | |
286 | CPPUNIT_ASSERT( fn.SameAs(wxFileName("/usr/local/bin/ls", wxPATH_UNIX)) ); | |
4524a24b VZ |
287 | } |
288 | ||
bf7f7793 RR |
289 | void FileNameTestCase::TestNormalize() |
290 | { | |
291 | // prepare some data to be used later | |
292 | wxString sep = wxFileName::GetPathSeparator(); | |
293 | wxString cwd = wxGetCwd(); | |
294 | wxString home = wxGetUserHome(); | |
295 | ||
296 | cwd.Replace(sep, wxT("/")); | |
297 | if (cwd.Last() != wxT('/')) | |
298 | cwd += wxT('/'); | |
299 | home.Replace(sep, wxT("/")); | |
300 | if (home.Last() != wxT('/')) | |
301 | home += wxT('/'); | |
302 | ||
303 | // since we will always be testing paths using the wxPATH_UNIX | |
304 | // format, we need to remove the volume, if present | |
305 | if (home.Contains(wxT(':'))) | |
306 | home = home.AfterFirst(wxT(':')); | |
307 | if (cwd.Contains(wxT(':'))) | |
308 | cwd = cwd.AfterFirst(wxT(':')); | |
309 | ||
8e083702 | 310 | static const struct FileNameTest |
bf7f7793 | 311 | { |
527587d3 | 312 | const char *original; |
bf7f7793 | 313 | int flags; |
527587d3 | 314 | const char *expected; |
c7099635 | 315 | wxPathFormat fmt; |
bf7f7793 RR |
316 | } tests[] = |
317 | { | |
318 | // test wxPATH_NORM_ENV_VARS | |
319 | #ifdef __WXMSW__ | |
c7099635 | 320 | { "%ABCDEF%/g/h/i", wxPATH_NORM_ENV_VARS, "abcdef/g/h/i", wxPATH_UNIX }, |
bf7f7793 | 321 | #else |
c7099635 | 322 | { "$(ABCDEF)/g/h/i", wxPATH_NORM_ENV_VARS, "abcdef/g/h/i", wxPATH_UNIX }, |
bf7f7793 RR |
323 | #endif |
324 | ||
325 | // test wxPATH_NORM_DOTS | |
c7099635 | 326 | { "a/.././b/c/../../", wxPATH_NORM_DOTS, "", wxPATH_UNIX }, |
f822acce VZ |
327 | { "", wxPATH_NORM_DOTS, "", wxPATH_UNIX }, |
328 | { "./foo", wxPATH_NORM_DOTS, "foo", wxPATH_UNIX }, | |
329 | { "b/../bar", wxPATH_NORM_DOTS, "bar", wxPATH_UNIX }, | |
330 | { "c/../../quux", wxPATH_NORM_DOTS, "../quux", wxPATH_UNIX }, | |
331 | { "/c/../../quux", wxPATH_NORM_DOTS, "/quux", wxPATH_UNIX }, | |
bf7f7793 | 332 | |
be5be16a VZ |
333 | // test wxPATH_NORM_TILDE: notice that ~ is only interpreted specially |
334 | // when it is the first character in the file name | |
c7099635 | 335 | { "/a/b/~", wxPATH_NORM_TILDE, "/a/b/~", wxPATH_UNIX }, |
be5be16a | 336 | { "/~/a/b", wxPATH_NORM_TILDE, "/~/a/b", wxPATH_UNIX }, |
527587d3 | 337 | { "~/a/b", wxPATH_NORM_TILDE, "HOME/a/b", wxPATH_UNIX }, |
c7099635 VZ |
338 | |
339 | // test wxPATH_NORM_CASE | |
340 | { "Foo", wxPATH_NORM_CASE, "Foo", wxPATH_UNIX }, | |
341 | { "Foo", wxPATH_NORM_CASE, "foo", wxPATH_DOS }, | |
342 | { "C:\\Program Files\\wx", wxPATH_NORM_CASE, | |
343 | "c:\\program files\\wx", wxPATH_DOS }, | |
344 | { "C:/Program Files/wx", wxPATH_NORM_ALL | wxPATH_NORM_CASE, | |
345 | "c:\\program files\\wx", wxPATH_DOS }, | |
9a0c5c01 VZ |
346 | { "C:\\Users\\zeitlin", wxPATH_NORM_ALL | wxPATH_NORM_CASE, |
347 | "c:\\users\\zeitlin", wxPATH_DOS }, | |
bf7f7793 RR |
348 | |
349 | // test wxPATH_NORM_ABSOLUTE | |
527587d3 VZ |
350 | { "a/b/", wxPATH_NORM_ABSOLUTE, "CWD/a/b/", wxPATH_UNIX }, |
351 | { "a/b/c.ext", wxPATH_NORM_ABSOLUTE, "CWD/a/b/c.ext", wxPATH_UNIX }, | |
c7099635 | 352 | { "/a", wxPATH_NORM_ABSOLUTE, "/a", wxPATH_UNIX }, |
bf7f7793 RR |
353 | |
354 | // test giving no flags at all to Normalize() | |
c7099635 VZ |
355 | { "a/b/", 0, "a/b/", wxPATH_UNIX }, |
356 | { "a/b/c.ext", 0, "a/b/c.ext", wxPATH_UNIX }, | |
ea6319cb VZ |
357 | { "/a", 0, "/a", wxPATH_UNIX }, |
358 | ||
359 | // test handling dots without wxPATH_NORM_DOTS and wxPATH_NORM_ABSOLUTE | |
360 | // for both existing and non-existent files (this is important under | |
361 | // MSW where GetLongPathName() works only for the former) | |
362 | { "./foo", wxPATH_NORM_LONG, "./foo", wxPATH_UNIX }, | |
363 | { "../foo", wxPATH_NORM_LONG, "../foo", wxPATH_UNIX }, | |
364 | { ".\\test.bkl", wxPATH_NORM_LONG, ".\\test.bkl", wxPATH_DOS }, | |
365 | { ".\\foo", wxPATH_NORM_LONG, ".\\foo", wxPATH_DOS }, | |
366 | { "..\\Makefile.in", wxPATH_NORM_LONG, "..\\Makefile.in", wxPATH_DOS }, | |
367 | { "..\\foo", wxPATH_NORM_LONG, "..\\foo", wxPATH_DOS }, | |
bf7f7793 RR |
368 | }; |
369 | ||
370 | // set the env var ABCDEF | |
9630954d | 371 | wxSetEnv("ABCDEF", "abcdef"); |
bf7f7793 | 372 | |
2264775b | 373 | for ( size_t i = 0; i < WXSIZEOF(tests); i++ ) |
bf7f7793 | 374 | { |
c7099635 VZ |
375 | const FileNameTest& fnt = tests[i]; |
376 | wxFileName fn(fnt.original, fnt.fmt); | |
bf7f7793 RR |
377 | |
378 | // be sure this normalization does not fail | |
a779d809 | 379 | WX_ASSERT_MESSAGE |
2264775b | 380 | ( |
a779d809 | 381 | ("#%d: Normalize(%s) failed", (int)i, fnt.original), |
c7099635 | 382 | fn.Normalize(fnt.flags, cwd, fnt.fmt) |
2264775b | 383 | ); |
bf7f7793 RR |
384 | |
385 | // compare result with expected string | |
527587d3 | 386 | wxString expected(tests[i].expected); |
9630954d VZ |
387 | expected.Replace("HOME/", home); |
388 | expected.Replace("CWD/", cwd); | |
a779d809 VZ |
389 | WX_ASSERT_EQUAL_MESSAGE |
390 | ( | |
391 | ("array element #%d", (int)i), | |
392 | expected, fn.GetFullPath(fnt.fmt) | |
393 | ); | |
bf7f7793 | 394 | } |
525711d7 VZ |
395 | |
396 | // MSW-only test for wxPATH_NORM_LONG: notice that we only run it if short | |
397 | // names generation is not disabled for this system as otherwise the file | |
398 | // MKINST~1 doesn't exist at all and normalizing it fails (it's possible | |
399 | // that we're on a FAT partition in which case the test would still succeed | |
400 | // and also that the registry key was changed recently and didn't take | |
401 | // effect yet but these are marginal cases which we consciously choose to | |
402 | // ignore for now) | |
403 | #ifdef __WXMSW__ | |
404 | long shortNamesDisabled; | |
405 | if ( wxRegKey | |
406 | ( | |
407 | wxRegKey::HKLM, | |
408 | "SYSTEM\\CurrentControlSet\\Control\\FileSystem" | |
409 | ).QueryValue("NtfsDisable8dot3NameCreation", &shortNamesDisabled) && | |
410 | !shortNamesDisabled ) | |
411 | { | |
412 | wxFileName fn("..\\MKINST~1"); | |
413 | CPPUNIT_ASSERT( fn.Normalize(wxPATH_NORM_LONG, cwd) ); | |
414 | CPPUNIT_ASSERT_EQUAL( "..\\mkinstalldirs", fn.GetFullPath() ); | |
415 | } | |
416 | //else: when in doubt, don't run the test | |
417 | #endif // __WXMSW__ | |
bf7f7793 RR |
418 | } |
419 | ||
395f3aa8 FM |
420 | void FileNameTestCase::TestReplace() |
421 | { | |
422 | static const struct FileNameTest | |
423 | { | |
424 | const char *original; | |
425 | const char *env_contents; | |
426 | const char *replace_fmtstring; | |
427 | const char *expected; | |
428 | wxPathFormat fmt; | |
429 | } tests[] = | |
430 | { | |
431 | { "/usr/a/strange path/lib/someFile.ext", "/usr/a/strange path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX }, | |
432 | { "/usr/a/path/lib/someFile.ext", "/usr/a/path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX }, | |
433 | { "/usr/a/path/lib/someFile", "/usr/a/path/", "$%s", "$TEST_VARlib/someFile", wxPATH_UNIX }, | |
434 | { "/usr/a/path/lib/", "/usr/a/path/", "$(%s)", "$(TEST_VAR)lib/", wxPATH_UNIX }, | |
435 | { "/usr/a/path/lib/", "/usr/a/path/", "${{%s}}", "${{TEST_VAR}}lib/", wxPATH_UNIX }, | |
436 | { "/usr/a/path/lib/", "/usr/a/path/", "%s", "TEST_VARlib/", wxPATH_UNIX }, | |
437 | { "/usr/a/path/lib/", "/usr/a/path/", "%s//", "TEST_VAR/lib/", wxPATH_UNIX }, | |
438 | // note: empty directory components are automatically removed by wxFileName thus | |
439 | // using // in the replace format string has no effect | |
440 | ||
441 | { "/usr/../a/path/lib/", "/usr/a/path/", "%s", "/usr/../a/path/lib/", wxPATH_UNIX }, | |
442 | { "/usr/a/path/usr/usr", "/usr", "%s", "TEST_VAR/a/pathTEST_VAR/usr", wxPATH_UNIX }, | |
443 | { "/usr/a/path/usr/usr", "/usr", "$%s", "$TEST_VAR/a/path$TEST_VAR/usr", wxPATH_UNIX }, | |
444 | { "/a/b/c/d", "a/", "%s", "/TEST_VARb/c/d", wxPATH_UNIX }, | |
445 | ||
446 | { "C:\\A\\Strange Path\\lib\\someFile", "C:\\A\\Strange Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN }, | |
447 | { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN }, | |
448 | { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "$(%s)", "$(TEST_VAR)\\lib\\someFile", wxPATH_WIN } | |
449 | }; | |
450 | ||
451 | for ( size_t i = 0; i < WXSIZEOF(tests); i++ ) | |
452 | { | |
453 | const FileNameTest& fnt = tests[i]; | |
454 | wxFileName fn(fnt.original, fnt.fmt); | |
455 | ||
456 | // set the environment variable | |
9630954d | 457 | wxSetEnv("TEST_VAR", fnt.env_contents); |
395f3aa8 FM |
458 | |
459 | // be sure this ReplaceEnvVariable does not fail | |
460 | WX_ASSERT_MESSAGE | |
461 | ( | |
462 | ("#%d: ReplaceEnvVariable(%s) failed", (int)i, fnt.replace_fmtstring), | |
463 | fn.ReplaceEnvVariable("TEST_VAR", fnt.replace_fmtstring, fnt.fmt) | |
464 | ); | |
465 | ||
466 | // compare result with expected string | |
467 | wxString expected(fnt.expected); | |
468 | WX_ASSERT_EQUAL_MESSAGE | |
469 | ( | |
470 | ("array element #%d", (int)i), | |
471 | expected, fn.GetFullPath(fnt.fmt) | |
472 | ); | |
473 | } | |
474 | ||
475 | // now test ReplaceHomeDir | |
476 | ||
477 | wxFileName fn = wxFileName::DirName(wxGetHomeDir()); | |
478 | fn.AppendDir("test1"); | |
479 | fn.AppendDir("test2"); | |
480 | fn.AppendDir("test3"); | |
481 | fn.SetName("some file"); | |
482 | ||
483 | WX_ASSERT_MESSAGE | |
484 | ( | |
485 | ("ReplaceHomeDir(%s) failed", fn.GetFullPath()), | |
486 | fn.ReplaceHomeDir() | |
487 | ); | |
488 | ||
9630954d | 489 | CPPUNIT_ASSERT_EQUAL( wxString("~/test1/test2/test3/some file"), |
395f3aa8 FM |
490 | fn.GetFullPath(wxPATH_UNIX) ); |
491 | } | |
492 | ||
b2edb8f3 VZ |
493 | void FileNameTestCase::TestGetHumanReadable() |
494 | { | |
495 | static const struct TestData | |
496 | { | |
497 | const char *result; | |
58271f42 | 498 | int size; |
b2edb8f3 VZ |
499 | int prec; |
500 | wxSizeConvention conv; | |
501 | } testData[] = | |
502 | { | |
a0752618 VZ |
503 | { "NA", 0, 1, wxSIZE_CONV_TRADITIONAL }, |
504 | { "2.0 KB", 2000, 1, wxSIZE_CONV_TRADITIONAL }, | |
505 | { "1.953 KiB", 2000, 3, wxSIZE_CONV_IEC }, | |
506 | { "2.000 KB", 2000, 3, wxSIZE_CONV_SI }, | |
507 | { "297 KB", 304351, 0, wxSIZE_CONV_TRADITIONAL }, | |
508 | { "304 KB", 304351, 0, wxSIZE_CONV_SI }, | |
b2edb8f3 VZ |
509 | }; |
510 | ||
701aa4d8 FM |
511 | CLocaleSetter loc; // we want to use "C" locale for LC_NUMERIC |
512 | // so that regardless of the system's locale | |
513 | // the decimal point used by GetHumanReadableSize() | |
514 | // is always '.' | |
b2edb8f3 VZ |
515 | for ( unsigned n = 0; n < WXSIZEOF(testData); n++ ) |
516 | { | |
517 | const TestData& td = testData[n]; | |
518 | ||
1fe1aecb FM |
519 | // take care of using the decimal point for the current locale before |
520 | // the actual comparison | |
b2edb8f3 VZ |
521 | CPPUNIT_ASSERT_EQUAL |
522 | ( | |
701aa4d8 | 523 | td.result, |
b2edb8f3 VZ |
524 | wxFileName::GetHumanReadableSize(td.size, "NA", td.prec, td.conv) |
525 | ); | |
526 | } | |
527 | ||
528 | // also test the default convention value | |
701aa4d8 | 529 | CPPUNIT_ASSERT_EQUAL( "1.4 MB", wxFileName::GetHumanReadableSize(1512993, "") ); |
b2edb8f3 VZ |
530 | } |
531 | ||
c08dd08b RN |
532 | void FileNameTestCase::TestStrip() |
533 | { | |
9630954d VZ |
534 | CPPUNIT_ASSERT_EQUAL( "", wxFileName::StripExtension("") ); |
535 | CPPUNIT_ASSERT_EQUAL( ".", wxFileName::StripExtension(".") ); | |
536 | CPPUNIT_ASSERT_EQUAL( ".vimrc", wxFileName::StripExtension(".vimrc") ); | |
537 | CPPUNIT_ASSERT_EQUAL( "bad", wxFileName::StripExtension("bad") ); | |
538 | CPPUNIT_ASSERT_EQUAL( "good", wxFileName::StripExtension("good.wav") ); | |
539 | CPPUNIT_ASSERT_EQUAL( "good.wav", wxFileName::StripExtension("good.wav.wav") ); | |
ff3d9a35 | 540 | } |
60c0dfe5 VZ |
541 | |
542 | #ifdef __WINDOWS__ | |
543 | ||
544 | void FileNameTestCase::TestShortLongPath() | |
545 | { | |
9630954d | 546 | wxFileName fn("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"); |
60c0dfe5 VZ |
547 | |
548 | // incredibly enough, GetLongPath() used to return different results during | |
549 | // the first and subsequent runs, test for this | |
550 | CPPUNIT_ASSERT_EQUAL( fn.GetLongPath(), fn.GetLongPath() ); | |
551 | CPPUNIT_ASSERT_EQUAL( fn.GetShortPath(), fn.GetShortPath() ); | |
552 | } | |
553 | ||
554 | #endif // __WINDOWS__ | |
7b611a3a VZ |
555 | |
556 | void FileNameTestCase::TestUNC() | |
557 | { | |
558 | wxFileName fn("//share/path/name.ext", wxPATH_DOS); | |
559 | CPPUNIT_ASSERT_EQUAL( "share", fn.GetVolume() ); | |
acaa8337 | 560 | CPPUNIT_ASSERT_EQUAL( "\\path", fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) ); |
7b611a3a VZ |
561 | |
562 | fn.Assign("\\\\share2\\path2\\name.ext", wxPATH_DOS); | |
563 | CPPUNIT_ASSERT_EQUAL( "share2", fn.GetVolume() ); | |
acaa8337 | 564 | CPPUNIT_ASSERT_EQUAL( "\\path2", fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) ); |
7b611a3a VZ |
565 | } |
566 | ||
e01a788e VZ |
567 | void FileNameTestCase::TestVolumeUniqueName() |
568 | { | |
569 | wxFileName fn("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\", | |
570 | wxPATH_DOS); | |
571 | CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", | |
572 | fn.GetVolume() ); | |
573 | CPPUNIT_ASSERT_EQUAL( "\\", fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) ); | |
574 | CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\", | |
575 | fn.GetFullPath(wxPATH_DOS) ); | |
576 | ||
577 | fn.Assign("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\" | |
578 | "Program Files\\setup.exe", wxPATH_DOS); | |
579 | CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", | |
580 | fn.GetVolume() ); | |
581 | CPPUNIT_ASSERT_EQUAL( "\\Program Files", | |
582 | fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) ); | |
583 | CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\" | |
584 | "Program Files\\setup.exe", | |
585 | fn.GetFullPath(wxPATH_DOS) ); | |
586 | } | |
1fe1aecb FM |
587 | |
588 | void FileNameTestCase::TestCreateTempFileName() | |
589 | { | |
590 | static const struct TestData | |
591 | { | |
592 | const char *prefix; | |
593 | const char *expectedFolder; | |
d6609db5 | 594 | bool shouldSucceed; |
1fe1aecb FM |
595 | } testData[] = |
596 | { | |
d6609db5 FM |
597 | { "", "$SYSTEM_TEMP", true }, |
598 | { "foo", "$SYSTEM_TEMP", true }, | |
599 | { "..", "$SYSTEM_TEMP", true }, | |
600 | { "../bar", "..", true }, | |
701aa4d8 | 601 | #ifdef __WXMSW__ |
d6609db5 FM |
602 | { "$USER_DOCS_DIR\\", "$USER_DOCS_DIR", true }, |
603 | { "c:\\a\\directory\\which\\does\\not\\exist", "", false }, | |
995202d0 | 604 | #elif defined( __UNIX__ ) |
d6609db5 FM |
605 | { "$USER_DOCS_DIR/", "$USER_DOCS_DIR", true }, |
606 | { "/tmp/foo", "/tmp", true }, | |
607 | { "/tmp/a/directory/which/does/not/exist", "", false }, | |
1fe1aecb FM |
608 | #endif // __UNIX__ |
609 | }; | |
610 | ||
611 | for ( size_t n = 0; n < WXSIZEOF(testData); n++ ) | |
612 | { | |
d6609db5 FM |
613 | wxString prefix = testData[n].prefix; |
614 | prefix.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir()); | |
1fe1aecb | 615 | |
d6609db5 FM |
616 | std::string errDesc = wxString::Format("failed on prefix '%s'", prefix).ToStdString(); |
617 | ||
618 | wxString path = wxFileName::CreateTempFileName(prefix); | |
619 | CPPUNIT_ASSERT_EQUAL_MESSAGE( errDesc, !testData[n].shouldSucceed, path.empty() ); | |
620 | ||
621 | if (testData[n].shouldSucceed) | |
1fe1aecb | 622 | { |
d6609db5 FM |
623 | errDesc += "; path is " + path.ToStdString(); |
624 | ||
1fe1aecb FM |
625 | // test the place where the temp file has been created |
626 | wxString expected = testData[n].expectedFolder; | |
627 | expected.Replace("$SYSTEM_TEMP", wxStandardPaths::Get().GetTempDir()); | |
d6609db5 FM |
628 | expected.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir()); |
629 | CPPUNIT_ASSERT_EQUAL_MESSAGE( errDesc, expected, wxFileName(path).GetPath() ); | |
1fe1aecb FM |
630 | |
631 | // the temporary file is created with full permissions for the current process | |
632 | // so we should always be able to remove it: | |
d6609db5 | 633 | CPPUNIT_ASSERT_MESSAGE( errDesc, wxRemoveFile(path) ); |
1fe1aecb FM |
634 | } |
635 | } | |
636 | } | |
637 | ||
638 | void FileNameTestCase::TestGetTimes() | |
639 | { | |
640 | wxFileName fn(wxFileName::CreateTempFileName("filenametest")); | |
641 | CPPUNIT_ASSERT( fn.IsOk() ); | |
642 | ||
643 | wxDateTime dtAccess, dtMod, dtCreate; | |
644 | CPPUNIT_ASSERT( fn.GetTimes(&dtAccess, &dtMod, &dtCreate) ); | |
645 | ||
646 | // make sure all retrieved dates are equal to the current date&time | |
647 | // with an accuracy up to 1 minute | |
648 | CPPUNIT_ASSERT(dtCreate.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1))); | |
649 | CPPUNIT_ASSERT(dtMod.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1))); | |
650 | CPPUNIT_ASSERT(dtAccess.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1))); | |
651 | } | |
901504c3 VZ |
652 | |
653 | void FileNameTestCase::TestExists() | |
654 | { | |
655 | wxFileName fn(wxFileName::CreateTempFileName("filenametest")); | |
656 | CPPUNIT_ASSERT( fn.IsOk() ); | |
657 | ||
658 | CPPUNIT_ASSERT( fn.FileExists() ); | |
659 | CPPUNIT_ASSERT( !wxFileName::DirExists(fn.GetFullPath()) ); | |
996d3fe3 | 660 | CPPUNIT_ASSERT( fn.Exists() ); |
901504c3 VZ |
661 | |
662 | wxFileName dirTemp(wxFileName::DirName(wxFileName::GetTempDir())); | |
663 | CPPUNIT_ASSERT( !dirTemp.FileExists() ); | |
664 | CPPUNIT_ASSERT( dirTemp.DirExists() ); | |
996d3fe3 | 665 | CPPUNIT_ASSERT( dirTemp.Exists() ); |
901504c3 VZ |
666 | |
667 | #ifdef __UNIX__ | |
668 | CPPUNIT_ASSERT( !wxFileName::FileExists("/dev/null") ); | |
669 | CPPUNIT_ASSERT( !wxFileName::DirExists("/dev/null") ); | |
996d3fe3 | 670 | CPPUNIT_ASSERT( wxFileName::Exists("/dev/null") ); |
901504c3 VZ |
671 | #endif // __UNIX__ |
672 | } |