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