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