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