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