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