]>
git.saurik.com Git - wxWidgets.git/blob - tests/filename/filenametest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/filename/filename.cpp
3 // Purpose: wxFileName unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2004 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/filename.h"
25 #include "wx/filefn.h"
28 #include "wx/msw/registry.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 static struct TestFileNameInfo
49 { "", "", "", "", "", false, wxPATH_UNIX
},
50 { "", "", "", "", "", false, wxPATH_DOS
},
51 { "", "", "", "", "", false, wxPATH_VMS
},
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
},
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 { "\\\\?\\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
},
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
81 { "\\\\server\\foo.bar", "server", "\\", "foo", "bar", true, wxPATH_DOS
},
82 { "\\\\server\\dir\\foo.bar", "server", "\\dir", "foo", "bar", true, wxPATH_DOS
},
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
87 { "c:\\aaa\\bbb\\ccc", "c", "\\aaa\\bbb", "ccc", "", true, wxPATH_DOS
},
88 { "c:\\\\aaa\\bbb\\ccc", "c", "\\\\aaa\\bbb", "ccc", "", true, wxPATH_DOS
},
90 // wxFileName support for Mac file names is broken currently
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
},
103 // NB: on Windows they have the same effect of the \\server\\ strings
104 // (see the note above)
105 { "device:[dir1.dir2.dir3]file.txt", "device", "dir1.dir2.dir3", "file", "txt", true, wxPATH_VMS
},
107 { "file.txt", "", "", "file", "txt", false, wxPATH_VMS
},
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 class FileNameTestCase
: public CppUnit::TestCase
117 FileNameTestCase() { }
120 CPPUNIT_TEST_SUITE( FileNameTestCase
);
121 CPPUNIT_TEST( TestConstruction
);
122 CPPUNIT_TEST( TestComparison
);
123 CPPUNIT_TEST( TestSplit
);
124 CPPUNIT_TEST( TestSetPath
);
125 CPPUNIT_TEST( TestStrip
);
126 CPPUNIT_TEST( TestNormalize
);
127 CPPUNIT_TEST( TestReplace
);
129 CPPUNIT_TEST( TestShortLongPath
);
130 #endif // __WINDOWS__
131 CPPUNIT_TEST( TestUNC
);
132 CPPUNIT_TEST( TestVolumeUniqueName
);
133 CPPUNIT_TEST_SUITE_END();
135 void TestConstruction();
136 void TestComparison();
140 void TestNormalize();
143 void TestShortLongPath();
144 #endif // __WINDOWS__
146 void TestVolumeUniqueName();
148 DECLARE_NO_COPY_CLASS(FileNameTestCase
)
151 // register in the unnamed registry so that these tests are run by default
152 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase
);
154 // also include in it's own registry so that these tests can be run alone
155 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase
, "FileNameTestCase" );
157 void FileNameTestCase::TestConstruction()
159 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
161 const TestFileNameInfo
& fni
= filenames
[n
];
163 wxFileName
fn(fni
.fullname
, fni
.format
);
165 // the original full name could contain consecutive [back]slashes,
166 // squeeze them except for the double backslash in the beginning in
167 // Windows filenames where it has special meaning
168 wxString fullnameOrig
;
169 if ( fni
.format
== wxPATH_DOS
)
171 // copy the backslashes at beginning unchanged
172 const char *p
= fni
.fullname
;
174 fullnameOrig
+= *p
++;
176 // replace consecutive slashes with single ones in the rest
177 for ( char chPrev
= '\0'; *p
; p
++ )
179 if ( *p
== '\\' && chPrev
== '\\' )
183 fullnameOrig
+= chPrev
;
188 fullnameOrig
= fni
.fullname
;
191 fullnameOrig
.Replace("//", "/");
194 wxString fullname
= fn
.GetFullPath(fni
.format
);
195 CPPUNIT_ASSERT_EQUAL( fullnameOrig
, fullname
);
197 // notice that we use a dummy working directory to ensure that paths
198 // with "../.." in them could be normalized, otherwise this would fail
199 // if the test is run from root directory or its direct subdirectory
200 CPPUNIT_ASSERT_MESSAGE
202 (const char *)wxString::Format("Normalize(%s) failed", fni
.fullname
).mb_str(),
203 fn
.Normalize(wxPATH_NORM_ALL
, "/foo/bar/baz", fni
.format
)
206 if ( *fni
.volume
&& *fni
.path
)
208 // check that specifying the volume separately or as part of the
209 // path doesn't make any difference
210 wxString pathWithVolume
= fni
.volume
;
211 pathWithVolume
+= wxFileName::GetVolumeSeparator(fni
.format
);
212 pathWithVolume
+= fni
.path
;
214 CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume
,
224 fn
.AssignDir(wxEmptyString
);
225 CPPUNIT_ASSERT( !fn
.IsOk() );
227 fn
.Assign(wxEmptyString
);
228 CPPUNIT_ASSERT( !fn
.IsOk() );
230 fn
.Assign(wxEmptyString
, wxEmptyString
);
231 CPPUNIT_ASSERT( !fn
.IsOk() );
233 fn
.Assign(wxEmptyString
, wxEmptyString
, wxEmptyString
);
234 CPPUNIT_ASSERT( !fn
.IsOk() );
236 fn
.Assign(wxEmptyString
, wxEmptyString
, wxEmptyString
, wxEmptyString
);
237 CPPUNIT_ASSERT( !fn
.IsOk() );
240 void FileNameTestCase::TestComparison()
242 wxFileName
fn1(wxT("/tmp/file1"));
243 wxFileName
fn2(wxT("/tmp/dir2/../file2"));
246 CPPUNIT_ASSERT_EQUAL(fn1
.GetPath(), fn2
.GetPath());
249 void FileNameTestCase::TestSplit()
251 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
253 const TestFileNameInfo
& fni
= filenames
[n
];
254 wxString volume
, path
, name
, ext
;
255 wxFileName::SplitPath(fni
.fullname
,
256 &volume
, &path
, &name
, &ext
, fni
.format
);
258 CPPUNIT_ASSERT_EQUAL( wxString(fni
.volume
), volume
);
259 CPPUNIT_ASSERT_EQUAL( wxString(fni
.path
), path
);
260 CPPUNIT_ASSERT_EQUAL( wxString(fni
.name
), name
);
261 CPPUNIT_ASSERT_EQUAL( wxString(fni
.ext
), ext
);
264 // special case of empty extension
265 wxFileName
fn("foo.");
266 CPPUNIT_ASSERT_EQUAL( wxString("foo."), fn
.GetFullPath() );
269 void FileNameTestCase::TestSetPath()
271 wxFileName
fn("d:\\test\\foo.bar", wxPATH_DOS
);
272 fn
.SetPath("c:\\temp", wxPATH_DOS
);
273 CPPUNIT_ASSERT( fn
.SameAs(wxFileName("c:\\temp\\foo.bar", wxPATH_DOS
)) );
275 fn
= wxFileName("/usr/bin/ls", wxPATH_UNIX
);
276 fn
.SetPath("/usr/local/bin", wxPATH_UNIX
);
277 CPPUNIT_ASSERT( fn
.SameAs(wxFileName("/usr/local/bin/ls", wxPATH_UNIX
)) );
280 void FileNameTestCase::TestNormalize()
282 // prepare some data to be used later
283 wxString sep
= wxFileName::GetPathSeparator();
284 wxString cwd
= wxGetCwd();
285 wxString home
= wxGetUserHome();
287 cwd
.Replace(sep
, wxT("/"));
288 if (cwd
.Last() != wxT('/'))
290 home
.Replace(sep
, wxT("/"));
291 if (home
.Last() != wxT('/'))
294 // since we will always be testing paths using the wxPATH_UNIX
295 // format, we need to remove the volume, if present
296 if (home
.Contains(wxT(':')))
297 home
= home
.AfterFirst(wxT(':'));
298 if (cwd
.Contains(wxT(':')))
299 cwd
= cwd
.AfterFirst(wxT(':'));
301 static const struct FileNameTest
303 const char *original
;
305 const char *expected
;
309 // test wxPATH_NORM_ENV_VARS
311 { "%ABCDEF%/g/h/i", wxPATH_NORM_ENV_VARS
, "abcdef/g/h/i", wxPATH_UNIX
},
313 { "$(ABCDEF)/g/h/i", wxPATH_NORM_ENV_VARS
, "abcdef/g/h/i", wxPATH_UNIX
},
316 // test wxPATH_NORM_DOTS
317 { "a/.././b/c/../../", wxPATH_NORM_DOTS
, "", wxPATH_UNIX
},
318 { "./", wxPATH_NORM_DOTS
, "", wxPATH_UNIX
},
319 { "b/../", wxPATH_NORM_DOTS
, "", wxPATH_UNIX
},
321 // test wxPATH_NORM_TILDE: notice that ~ is only interpreted specially
322 // when it is the first character in the file name
323 { "/a/b/~", wxPATH_NORM_TILDE
, "/a/b/~", wxPATH_UNIX
},
324 { "/~/a/b", wxPATH_NORM_TILDE
, "/~/a/b", wxPATH_UNIX
},
325 { "~/a/b", wxPATH_NORM_TILDE
, "HOME/a/b", wxPATH_UNIX
},
327 // test wxPATH_NORM_CASE
328 { "Foo", wxPATH_NORM_CASE
, "Foo", wxPATH_UNIX
},
329 { "Foo", wxPATH_NORM_CASE
, "foo", wxPATH_DOS
},
330 { "C:\\Program Files\\wx", wxPATH_NORM_CASE
,
331 "c:\\program files\\wx", wxPATH_DOS
},
332 { "C:/Program Files/wx", wxPATH_NORM_ALL
| wxPATH_NORM_CASE
,
333 "c:\\program files\\wx", wxPATH_DOS
},
334 { "C:\\Users\\zeitlin", wxPATH_NORM_ALL
| wxPATH_NORM_CASE
,
335 "c:\\users\\zeitlin", wxPATH_DOS
},
337 // test wxPATH_NORM_ABSOLUTE
338 { "a/b/", wxPATH_NORM_ABSOLUTE
, "CWD/a/b/", wxPATH_UNIX
},
339 { "a/b/c.ext", wxPATH_NORM_ABSOLUTE
, "CWD/a/b/c.ext", wxPATH_UNIX
},
340 { "/a", wxPATH_NORM_ABSOLUTE
, "/a", wxPATH_UNIX
},
342 // test giving no flags at all to Normalize()
343 { "a/b/", 0, "a/b/", wxPATH_UNIX
},
344 { "a/b/c.ext", 0, "a/b/c.ext", wxPATH_UNIX
},
345 { "/a", 0, "/a", wxPATH_UNIX
},
347 // test handling dots without wxPATH_NORM_DOTS and wxPATH_NORM_ABSOLUTE
348 // for both existing and non-existent files (this is important under
349 // MSW where GetLongPathName() works only for the former)
350 { "./foo", wxPATH_NORM_LONG
, "./foo", wxPATH_UNIX
},
351 { "../foo", wxPATH_NORM_LONG
, "../foo", wxPATH_UNIX
},
352 { ".\\test.bkl", wxPATH_NORM_LONG
, ".\\test.bkl", wxPATH_DOS
},
353 { ".\\foo", wxPATH_NORM_LONG
, ".\\foo", wxPATH_DOS
},
354 { "..\\Makefile.in", wxPATH_NORM_LONG
, "..\\Makefile.in", wxPATH_DOS
},
355 { "..\\foo", wxPATH_NORM_LONG
, "..\\foo", wxPATH_DOS
},
358 // set the env var ABCDEF
359 wxSetEnv("ABCDEF", "abcdef");
361 for ( size_t i
= 0; i
< WXSIZEOF(tests
); i
++ )
363 const FileNameTest
& fnt
= tests
[i
];
364 wxFileName
fn(fnt
.original
, fnt
.fmt
);
366 // be sure this normalization does not fail
369 ("#%d: Normalize(%s) failed", (int)i
, fnt
.original
),
370 fn
.Normalize(fnt
.flags
, cwd
, fnt
.fmt
)
373 // compare result with expected string
374 wxString
expected(tests
[i
].expected
);
375 expected
.Replace("HOME/", home
);
376 expected
.Replace("CWD/", cwd
);
377 WX_ASSERT_EQUAL_MESSAGE
379 ("array element #%d", (int)i
),
380 expected
, fn
.GetFullPath(fnt
.fmt
)
384 // MSW-only test for wxPATH_NORM_LONG: notice that we only run it if short
385 // names generation is not disabled for this system as otherwise the file
386 // MKINST~1 doesn't exist at all and normalizing it fails (it's possible
387 // that we're on a FAT partition in which case the test would still succeed
388 // and also that the registry key was changed recently and didn't take
389 // effect yet but these are marginal cases which we consciously choose to
392 long shortNamesDisabled
;
396 "SYSTEM\\CurrentControlSet\\Control\\FileSystem"
397 ).QueryValue("NtfsDisable8dot3NameCreation", &shortNamesDisabled
) &&
398 !shortNamesDisabled
)
400 wxFileName
fn("..\\MKINST~1");
401 CPPUNIT_ASSERT( fn
.Normalize(wxPATH_NORM_LONG
, cwd
) );
402 CPPUNIT_ASSERT_EQUAL( "..\\mkinstalldirs", fn
.GetFullPath() );
404 //else: when in doubt, don't run the test
408 void FileNameTestCase::TestReplace()
410 static const struct FileNameTest
412 const char *original
;
413 const char *env_contents
;
414 const char *replace_fmtstring
;
415 const char *expected
;
419 { "/usr/a/strange path/lib/someFile.ext", "/usr/a/strange path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX
},
420 { "/usr/a/path/lib/someFile.ext", "/usr/a/path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX
},
421 { "/usr/a/path/lib/someFile", "/usr/a/path/", "$%s", "$TEST_VARlib/someFile", wxPATH_UNIX
},
422 { "/usr/a/path/lib/", "/usr/a/path/", "$(%s)", "$(TEST_VAR)lib/", wxPATH_UNIX
},
423 { "/usr/a/path/lib/", "/usr/a/path/", "${{%s}}", "${{TEST_VAR}}lib/", wxPATH_UNIX
},
424 { "/usr/a/path/lib/", "/usr/a/path/", "%s", "TEST_VARlib/", wxPATH_UNIX
},
425 { "/usr/a/path/lib/", "/usr/a/path/", "%s//", "TEST_VAR/lib/", wxPATH_UNIX
},
426 // note: empty directory components are automatically removed by wxFileName thus
427 // using // in the replace format string has no effect
429 { "/usr/../a/path/lib/", "/usr/a/path/", "%s", "/usr/../a/path/lib/", wxPATH_UNIX
},
430 { "/usr/a/path/usr/usr", "/usr", "%s", "TEST_VAR/a/pathTEST_VAR/usr", wxPATH_UNIX
},
431 { "/usr/a/path/usr/usr", "/usr", "$%s", "$TEST_VAR/a/path$TEST_VAR/usr", wxPATH_UNIX
},
432 { "/a/b/c/d", "a/", "%s", "/TEST_VARb/c/d", wxPATH_UNIX
},
434 { "C:\\A\\Strange Path\\lib\\someFile", "C:\\A\\Strange Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN
},
435 { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN
},
436 { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "$(%s)", "$(TEST_VAR)\\lib\\someFile", wxPATH_WIN
}
439 for ( size_t i
= 0; i
< WXSIZEOF(tests
); i
++ )
441 const FileNameTest
& fnt
= tests
[i
];
442 wxFileName
fn(fnt
.original
, fnt
.fmt
);
444 // set the environment variable
445 wxSetEnv("TEST_VAR", fnt
.env_contents
);
447 // be sure this ReplaceEnvVariable does not fail
450 ("#%d: ReplaceEnvVariable(%s) failed", (int)i
, fnt
.replace_fmtstring
),
451 fn
.ReplaceEnvVariable("TEST_VAR", fnt
.replace_fmtstring
, fnt
.fmt
)
454 // compare result with expected string
455 wxString
expected(fnt
.expected
);
456 WX_ASSERT_EQUAL_MESSAGE
458 ("array element #%d", (int)i
),
459 expected
, fn
.GetFullPath(fnt
.fmt
)
463 // now test ReplaceHomeDir
465 wxFileName fn
= wxFileName::DirName(wxGetHomeDir());
466 fn
.AppendDir("test1");
467 fn
.AppendDir("test2");
468 fn
.AppendDir("test3");
469 fn
.SetName("some file");
473 ("ReplaceHomeDir(%s) failed", fn
.GetFullPath()),
477 CPPUNIT_ASSERT_EQUAL( wxString("~/test1/test2/test3/some file"),
478 fn
.GetFullPath(wxPATH_UNIX
) );
481 void FileNameTestCase::TestStrip()
483 CPPUNIT_ASSERT_EQUAL( "", wxFileName::StripExtension("") );
484 CPPUNIT_ASSERT_EQUAL( ".", wxFileName::StripExtension(".") );
485 CPPUNIT_ASSERT_EQUAL( ".vimrc", wxFileName::StripExtension(".vimrc") );
486 CPPUNIT_ASSERT_EQUAL( "bad", wxFileName::StripExtension("bad") );
487 CPPUNIT_ASSERT_EQUAL( "good", wxFileName::StripExtension("good.wav") );
488 CPPUNIT_ASSERT_EQUAL( "good.wav", wxFileName::StripExtension("good.wav.wav") );
493 void FileNameTestCase::TestShortLongPath()
495 wxFileName
fn("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe");
497 // incredibly enough, GetLongPath() used to return different results during
498 // the first and subsequent runs, test for this
499 CPPUNIT_ASSERT_EQUAL( fn
.GetLongPath(), fn
.GetLongPath() );
500 CPPUNIT_ASSERT_EQUAL( fn
.GetShortPath(), fn
.GetShortPath() );
503 #endif // __WINDOWS__
505 void FileNameTestCase::TestUNC()
507 wxFileName
fn("//share/path/name.ext", wxPATH_DOS
);
508 CPPUNIT_ASSERT_EQUAL( "share", fn
.GetVolume() );
509 CPPUNIT_ASSERT_EQUAL( "\\path", fn
.GetPath(wxPATH_NO_SEPARATOR
, wxPATH_DOS
) );
511 fn
.Assign("\\\\share2\\path2\\name.ext", wxPATH_DOS
);
512 CPPUNIT_ASSERT_EQUAL( "share2", fn
.GetVolume() );
513 CPPUNIT_ASSERT_EQUAL( "\\path2", fn
.GetPath(wxPATH_NO_SEPARATOR
, wxPATH_DOS
) );
516 void FileNameTestCase::TestVolumeUniqueName()
518 wxFileName
fn("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\",
520 CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}",
522 CPPUNIT_ASSERT_EQUAL( "\\", fn
.GetPath(wxPATH_NO_SEPARATOR
, wxPATH_DOS
) );
523 CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\",
524 fn
.GetFullPath(wxPATH_DOS
) );
526 fn
.Assign("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\"
527 "Program Files\\setup.exe", wxPATH_DOS
);
528 CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}",
530 CPPUNIT_ASSERT_EQUAL( "\\Program Files",
531 fn
.GetPath(wxPATH_NO_SEPARATOR
, wxPATH_DOS
) );
532 CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\"
533 "Program Files\\setup.exe",
534 fn
.GetFullPath(wxPATH_DOS
) );