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