Remove temporary files created by wxFileName unit test case.
[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 #include "wx/stdpaths.h"
27 #include "wx/scopeguard.h"
28
29 #ifdef __WXMSW__
30 #include "wx/msw/registry.h"
31 #endif // __WXMSW__
32
33 #include "testfile.h"
34
35 // ----------------------------------------------------------------------------
36 // test data
37 // ----------------------------------------------------------------------------
38
39 static struct TestFileNameInfo
40 {
41 const char *fullname;
42 const char *volume;
43 const char *path;
44 const char *name;
45 const char *ext;
46 bool isAbsolute;
47 wxPathFormat format;
48 } filenames[] =
49 {
50 // the empty string
51 { "", "", "", "", "", false, wxPATH_UNIX },
52 { "", "", "", "", "", false, wxPATH_DOS },
53 { "", "", "", "", "", false, wxPATH_VMS },
54
55 // Unix file names
56 { "/usr/bin/ls", "", "/usr/bin", "ls", "", true, wxPATH_UNIX },
57 { "/usr/bin/", "", "/usr/bin", "", "", true, wxPATH_UNIX },
58 { "~/.zshrc", "", "~", ".zshrc", "", true, wxPATH_UNIX },
59 { "../../foo", "", "../..", "foo", "", false, wxPATH_UNIX },
60 { "foo.bar", "", "", "foo", "bar", false, wxPATH_UNIX },
61 { "~/foo.bar", "", "~", "foo", "bar", true, wxPATH_UNIX },
62 { "~user/foo.bar", "", "~user", "foo", "bar", true, wxPATH_UNIX },
63 { "~user/", "", "~user", "", "", true, wxPATH_UNIX },
64 { "/foo", "", "/", "foo", "", true, wxPATH_UNIX },
65 { "Mahogany-0.60/foo.bar", "", "Mahogany-0.60", "foo", "bar", false, wxPATH_UNIX },
66 { "/tmp/wxwin.tar.bz", "", "/tmp", "wxwin.tar", "bz", true, wxPATH_UNIX },
67
68 // Windows file names
69 { "foo.bar", "", "", "foo", "bar", false, wxPATH_DOS },
70 { "\\foo.bar", "", "\\", "foo", "bar", false, wxPATH_DOS },
71 { "c:foo.bar", "c", "", "foo", "bar", false, wxPATH_DOS },
72 { "c:\\foo.bar", "c", "\\", "foo", "bar", true, wxPATH_DOS },
73 { "c:\\Windows\\command.com", "c", "\\Windows", "command", "com", true, wxPATH_DOS },
74 { "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\",
75 "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", "\\", "", "", true, wxPATH_DOS },
76 { "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\Program Files\\setup.exe",
77 "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", "\\Program Files", "setup", "exe", 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 CPPUNIT_TEST( TestGetHumanReadable );
131 #ifdef __WINDOWS__
132 CPPUNIT_TEST( TestShortLongPath );
133 #endif // __WINDOWS__
134 CPPUNIT_TEST( TestUNC );
135 CPPUNIT_TEST( TestVolumeUniqueName );
136 CPPUNIT_TEST( TestCreateTempFileName );
137 CPPUNIT_TEST( TestGetTimes );
138 CPPUNIT_TEST( TestExists );
139 CPPUNIT_TEST_SUITE_END();
140
141 void TestConstruction();
142 void TestComparison();
143 void TestSplit();
144 void TestSetPath();
145 void TestStrip();
146 void TestNormalize();
147 void TestReplace();
148 void TestGetHumanReadable();
149 #ifdef __WINDOWS__
150 void TestShortLongPath();
151 #endif // __WINDOWS__
152 void TestUNC();
153 void TestVolumeUniqueName();
154 void TestCreateTempFileName();
155 void TestGetTimes();
156 void TestExists();
157
158 DECLARE_NO_COPY_CLASS(FileNameTestCase)
159 };
160
161 // register in the unnamed registry so that these tests are run by default
162 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase );
163
164 // also include in its own registry so that these tests can be run alone
165 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" );
166
167 void FileNameTestCase::TestConstruction()
168 {
169 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
170 {
171 const TestFileNameInfo& fni = filenames[n];
172
173 wxFileName fn(fni.fullname, fni.format);
174
175 // the original full name could contain consecutive [back]slashes,
176 // squeeze them except for the double backslash in the beginning in
177 // Windows filenames where it has special meaning
178 wxString fullnameOrig;
179 if ( fni.format == wxPATH_DOS )
180 {
181 // copy the backslashes at beginning unchanged
182 const char *p = fni.fullname;
183 while ( *p == '\\' )
184 fullnameOrig += *p++;
185
186 // replace consecutive slashes with single ones in the rest
187 for ( char chPrev = '\0'; *p; p++ )
188 {
189 if ( *p == '\\' && chPrev == '\\' )
190 continue;
191
192 chPrev = *p;
193 fullnameOrig += chPrev;
194 }
195 }
196 else // !wxPATH_DOS
197 {
198 fullnameOrig = fni.fullname;
199 }
200
201 fullnameOrig.Replace("//", "/");
202
203
204 wxString fullname = fn.GetFullPath(fni.format);
205 CPPUNIT_ASSERT_EQUAL( fullnameOrig, fullname );
206
207 // notice that we use a dummy working directory to ensure that paths
208 // with "../.." in them could be normalized, otherwise this would fail
209 // if the test is run from root directory or its direct subdirectory
210 CPPUNIT_ASSERT_MESSAGE
211 (
212 (const char *)wxString::Format("Normalize(%s) failed", fni.fullname).mb_str(),
213 fn.Normalize(wxPATH_NORM_ALL, "/foo/bar/baz", fni.format)
214 );
215
216 if ( *fni.volume && *fni.path )
217 {
218 // check that specifying the volume separately or as part of the
219 // path doesn't make any difference
220 wxString pathWithVolume = fni.volume;
221 pathWithVolume += wxFileName::GetVolumeSeparator(fni.format);
222 pathWithVolume += fni.path;
223
224 CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume,
225 fni.name,
226 fni.ext,
227 fni.format), fn );
228 }
229 }
230
231 wxFileName fn;
232
233 // empty strings
234 fn.AssignDir(wxEmptyString);
235 CPPUNIT_ASSERT( !fn.IsOk() );
236
237 fn.Assign(wxEmptyString);
238 CPPUNIT_ASSERT( !fn.IsOk() );
239
240 fn.Assign(wxEmptyString, wxEmptyString);
241 CPPUNIT_ASSERT( !fn.IsOk() );
242
243 fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString);
244 CPPUNIT_ASSERT( !fn.IsOk() );
245
246 fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString, wxEmptyString);
247 CPPUNIT_ASSERT( !fn.IsOk() );
248 }
249
250 void FileNameTestCase::TestComparison()
251 {
252 wxFileName fn1(wxT("/tmp/file1"));
253 wxFileName fn2(wxT("/tmp/dir2/../file2"));
254 fn1.Normalize();
255 fn2.Normalize();
256 CPPUNIT_ASSERT_EQUAL(fn1.GetPath(), fn2.GetPath());
257 }
258
259 void FileNameTestCase::TestSplit()
260 {
261 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
262 {
263 const TestFileNameInfo& fni = filenames[n];
264 wxString volume, path, name, ext;
265 wxFileName::SplitPath(fni.fullname,
266 &volume, &path, &name, &ext, fni.format);
267
268 CPPUNIT_ASSERT_EQUAL( wxString(fni.volume), volume );
269 CPPUNIT_ASSERT_EQUAL( wxString(fni.path), path );
270 CPPUNIT_ASSERT_EQUAL( wxString(fni.name), name );
271 CPPUNIT_ASSERT_EQUAL( wxString(fni.ext), ext );
272 }
273
274 // special case of empty extension
275 wxFileName fn("foo.");
276 CPPUNIT_ASSERT_EQUAL( wxString("foo."), fn.GetFullPath() );
277 }
278
279 void FileNameTestCase::TestSetPath()
280 {
281 wxFileName fn("d:\\test\\foo.bar", wxPATH_DOS);
282 fn.SetPath("c:\\temp", wxPATH_DOS);
283 CPPUNIT_ASSERT( fn.SameAs(wxFileName("c:\\temp\\foo.bar", wxPATH_DOS)) );
284
285 fn = wxFileName("/usr/bin/ls", wxPATH_UNIX);
286 fn.SetPath("/usr/local/bin", wxPATH_UNIX);
287 CPPUNIT_ASSERT( fn.SameAs(wxFileName("/usr/local/bin/ls", wxPATH_UNIX)) );
288 }
289
290 void FileNameTestCase::TestNormalize()
291 {
292 // prepare some data to be used later
293 wxString sep = wxFileName::GetPathSeparator();
294 wxString cwd = wxGetCwd();
295 wxString home = wxGetUserHome();
296
297 cwd.Replace(sep, wxT("/"));
298 if (cwd.Last() != wxT('/'))
299 cwd += wxT('/');
300 home.Replace(sep, wxT("/"));
301 if (home.Last() != wxT('/'))
302 home += wxT('/');
303
304 // since we will always be testing paths using the wxPATH_UNIX
305 // format, we need to remove the volume, if present
306 if (home.Contains(wxT(':')))
307 home = home.AfterFirst(wxT(':'));
308 if (cwd.Contains(wxT(':')))
309 cwd = cwd.AfterFirst(wxT(':'));
310
311 static const struct FileNameTest
312 {
313 const char *original;
314 int flags;
315 const char *expected;
316 wxPathFormat fmt;
317 } tests[] =
318 {
319 // test wxPATH_NORM_ENV_VARS
320 #ifdef __WXMSW__
321 { "%ABCDEF%/g/h/i", wxPATH_NORM_ENV_VARS, "abcdef/g/h/i", wxPATH_UNIX },
322 #else
323 { "$(ABCDEF)/g/h/i", wxPATH_NORM_ENV_VARS, "abcdef/g/h/i", wxPATH_UNIX },
324 #endif
325
326 // test wxPATH_NORM_DOTS
327 { "a/.././b/c/../../", wxPATH_NORM_DOTS, "", wxPATH_UNIX },
328 { "", wxPATH_NORM_DOTS, "", wxPATH_UNIX },
329 { "./foo", wxPATH_NORM_DOTS, "foo", wxPATH_UNIX },
330 { "b/../bar", wxPATH_NORM_DOTS, "bar", wxPATH_UNIX },
331 { "c/../../quux", wxPATH_NORM_DOTS, "../quux", wxPATH_UNIX },
332 { "/c/../../quux", wxPATH_NORM_DOTS, "/quux", wxPATH_UNIX },
333
334 // test wxPATH_NORM_TILDE: notice that ~ is only interpreted specially
335 // when it is the first character in the file name
336 { "/a/b/~", wxPATH_NORM_TILDE, "/a/b/~", wxPATH_UNIX },
337 { "/~/a/b", wxPATH_NORM_TILDE, "/~/a/b", wxPATH_UNIX },
338 { "~/a/b", wxPATH_NORM_TILDE, "HOME/a/b", wxPATH_UNIX },
339
340 // test wxPATH_NORM_CASE
341 { "Foo", wxPATH_NORM_CASE, "Foo", wxPATH_UNIX },
342 { "Foo", wxPATH_NORM_CASE, "foo", wxPATH_DOS },
343 { "C:\\Program Files\\wx", wxPATH_NORM_CASE,
344 "c:\\program files\\wx", wxPATH_DOS },
345 { "C:/Program Files/wx", wxPATH_NORM_ALL | wxPATH_NORM_CASE,
346 "c:\\program files\\wx", wxPATH_DOS },
347 { "C:\\Users\\zeitlin", wxPATH_NORM_ALL | wxPATH_NORM_CASE,
348 "c:\\users\\zeitlin", wxPATH_DOS },
349
350 // test wxPATH_NORM_ABSOLUTE
351 { "a/b/", wxPATH_NORM_ABSOLUTE, "CWD/a/b/", wxPATH_UNIX },
352 { "a/b/c.ext", wxPATH_NORM_ABSOLUTE, "CWD/a/b/c.ext", wxPATH_UNIX },
353 { "/a", wxPATH_NORM_ABSOLUTE, "/a", wxPATH_UNIX },
354
355 // test giving no flags at all to Normalize()
356 { "a/b/", 0, "a/b/", wxPATH_UNIX },
357 { "a/b/c.ext", 0, "a/b/c.ext", wxPATH_UNIX },
358 { "/a", 0, "/a", wxPATH_UNIX },
359
360 // test handling dots without wxPATH_NORM_DOTS and wxPATH_NORM_ABSOLUTE
361 // for both existing and non-existent files (this is important under
362 // MSW where GetLongPathName() works only for the former)
363 { "./foo", wxPATH_NORM_LONG, "./foo", wxPATH_UNIX },
364 { "../foo", wxPATH_NORM_LONG, "../foo", wxPATH_UNIX },
365 { ".\\test.bkl", wxPATH_NORM_LONG, ".\\test.bkl", wxPATH_DOS },
366 { ".\\foo", wxPATH_NORM_LONG, ".\\foo", wxPATH_DOS },
367 { "..\\Makefile.in", wxPATH_NORM_LONG, "..\\Makefile.in", wxPATH_DOS },
368 { "..\\foo", wxPATH_NORM_LONG, "..\\foo", wxPATH_DOS },
369 };
370
371 // set the env var ABCDEF
372 wxSetEnv("ABCDEF", "abcdef");
373
374 for ( size_t i = 0; i < WXSIZEOF(tests); i++ )
375 {
376 const FileNameTest& fnt = tests[i];
377 wxFileName fn(fnt.original, fnt.fmt);
378
379 // be sure this normalization does not fail
380 WX_ASSERT_MESSAGE
381 (
382 ("#%d: Normalize(%s) failed", (int)i, fnt.original),
383 fn.Normalize(fnt.flags, cwd, fnt.fmt)
384 );
385
386 // compare result with expected string
387 wxString expected(tests[i].expected);
388 expected.Replace("HOME/", home);
389 expected.Replace("CWD/", cwd);
390 WX_ASSERT_EQUAL_MESSAGE
391 (
392 ("array element #%d", (int)i),
393 expected, fn.GetFullPath(fnt.fmt)
394 );
395 }
396
397 // MSW-only test for wxPATH_NORM_LONG: notice that we only run it if short
398 // names generation is not disabled for this system as otherwise the file
399 // MKINST~1 doesn't exist at all and normalizing it fails (it's possible
400 // that we're on a FAT partition in which case the test would still succeed
401 // and also that the registry key was changed recently and didn't take
402 // effect yet but these are marginal cases which we consciously choose to
403 // ignore for now)
404 #ifdef __WXMSW__
405 long shortNamesDisabled;
406 if ( wxRegKey
407 (
408 wxRegKey::HKLM,
409 "SYSTEM\\CurrentControlSet\\Control\\FileSystem"
410 ).QueryValue("NtfsDisable8dot3NameCreation", &shortNamesDisabled) &&
411 !shortNamesDisabled )
412 {
413 wxFileName fn("..\\MKINST~1");
414 CPPUNIT_ASSERT( fn.Normalize(wxPATH_NORM_LONG, cwd) );
415 CPPUNIT_ASSERT_EQUAL( "..\\mkinstalldirs", fn.GetFullPath() );
416 }
417 //else: when in doubt, don't run the test
418 #endif // __WXMSW__
419 }
420
421 void FileNameTestCase::TestReplace()
422 {
423 static const struct FileNameTest
424 {
425 const char *original;
426 const char *env_contents;
427 const char *replace_fmtstring;
428 const char *expected;
429 wxPathFormat fmt;
430 } tests[] =
431 {
432 { "/usr/a/strange path/lib/someFile.ext", "/usr/a/strange path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX },
433 { "/usr/a/path/lib/someFile.ext", "/usr/a/path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX },
434 { "/usr/a/path/lib/someFile", "/usr/a/path/", "$%s", "$TEST_VARlib/someFile", wxPATH_UNIX },
435 { "/usr/a/path/lib/", "/usr/a/path/", "$(%s)", "$(TEST_VAR)lib/", wxPATH_UNIX },
436 { "/usr/a/path/lib/", "/usr/a/path/", "${{%s}}", "${{TEST_VAR}}lib/", wxPATH_UNIX },
437 { "/usr/a/path/lib/", "/usr/a/path/", "%s", "TEST_VARlib/", wxPATH_UNIX },
438 { "/usr/a/path/lib/", "/usr/a/path/", "%s//", "TEST_VAR/lib/", wxPATH_UNIX },
439 // note: empty directory components are automatically removed by wxFileName thus
440 // using // in the replace format string has no effect
441
442 { "/usr/../a/path/lib/", "/usr/a/path/", "%s", "/usr/../a/path/lib/", wxPATH_UNIX },
443 { "/usr/a/path/usr/usr", "/usr", "%s", "TEST_VAR/a/pathTEST_VAR/usr", wxPATH_UNIX },
444 { "/usr/a/path/usr/usr", "/usr", "$%s", "$TEST_VAR/a/path$TEST_VAR/usr", wxPATH_UNIX },
445 { "/a/b/c/d", "a/", "%s", "/TEST_VARb/c/d", wxPATH_UNIX },
446
447 { "C:\\A\\Strange Path\\lib\\someFile", "C:\\A\\Strange Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN },
448 { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN },
449 { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "$(%s)", "$(TEST_VAR)\\lib\\someFile", wxPATH_WIN }
450 };
451
452 for ( size_t i = 0; i < WXSIZEOF(tests); i++ )
453 {
454 const FileNameTest& fnt = tests[i];
455 wxFileName fn(fnt.original, fnt.fmt);
456
457 // set the environment variable
458 wxSetEnv("TEST_VAR", fnt.env_contents);
459
460 // be sure this ReplaceEnvVariable does not fail
461 WX_ASSERT_MESSAGE
462 (
463 ("#%d: ReplaceEnvVariable(%s) failed", (int)i, fnt.replace_fmtstring),
464 fn.ReplaceEnvVariable("TEST_VAR", fnt.replace_fmtstring, fnt.fmt)
465 );
466
467 // compare result with expected string
468 wxString expected(fnt.expected);
469 WX_ASSERT_EQUAL_MESSAGE
470 (
471 ("array element #%d", (int)i),
472 expected, fn.GetFullPath(fnt.fmt)
473 );
474 }
475
476 // now test ReplaceHomeDir
477
478 wxFileName fn = wxFileName::DirName(wxGetHomeDir());
479 fn.AppendDir("test1");
480 fn.AppendDir("test2");
481 fn.AppendDir("test3");
482 fn.SetName("some file");
483
484 WX_ASSERT_MESSAGE
485 (
486 ("ReplaceHomeDir(%s) failed", fn.GetFullPath()),
487 fn.ReplaceHomeDir()
488 );
489
490 CPPUNIT_ASSERT_EQUAL( wxString("~/test1/test2/test3/some file"),
491 fn.GetFullPath(wxPATH_UNIX) );
492 }
493
494 void FileNameTestCase::TestGetHumanReadable()
495 {
496 static const struct TestData
497 {
498 const char *result;
499 int size;
500 int prec;
501 wxSizeConvention conv;
502 } testData[] =
503 {
504 { "NA", 0, 1, wxSIZE_CONV_TRADITIONAL },
505 { "2.0 KB", 2000, 1, wxSIZE_CONV_TRADITIONAL },
506 { "1.953 KiB", 2000, 3, wxSIZE_CONV_IEC },
507 { "2.000 KB", 2000, 3, wxSIZE_CONV_SI },
508 { "297 KB", 304351, 0, wxSIZE_CONV_TRADITIONAL },
509 { "304 KB", 304351, 0, wxSIZE_CONV_SI },
510 };
511
512 CLocaleSetter loc; // we want to use "C" locale for LC_NUMERIC
513 // so that regardless of the system's locale
514 // the decimal point used by GetHumanReadableSize()
515 // is always '.'
516 for ( unsigned n = 0; n < WXSIZEOF(testData); n++ )
517 {
518 const TestData& td = testData[n];
519
520 // take care of using the decimal point for the current locale before
521 // the actual comparison
522 CPPUNIT_ASSERT_EQUAL
523 (
524 td.result,
525 wxFileName::GetHumanReadableSize(td.size, "NA", td.prec, td.conv)
526 );
527 }
528
529 // also test the default convention value
530 CPPUNIT_ASSERT_EQUAL( "1.4 MB", wxFileName::GetHumanReadableSize(1512993, "") );
531 }
532
533 void FileNameTestCase::TestStrip()
534 {
535 CPPUNIT_ASSERT_EQUAL( "", wxFileName::StripExtension("") );
536 CPPUNIT_ASSERT_EQUAL( ".", wxFileName::StripExtension(".") );
537 CPPUNIT_ASSERT_EQUAL( ".vimrc", wxFileName::StripExtension(".vimrc") );
538 CPPUNIT_ASSERT_EQUAL( "bad", wxFileName::StripExtension("bad") );
539 CPPUNIT_ASSERT_EQUAL( "good", wxFileName::StripExtension("good.wav") );
540 CPPUNIT_ASSERT_EQUAL( "good.wav", wxFileName::StripExtension("good.wav.wav") );
541 }
542
543 #ifdef __WINDOWS__
544
545 void FileNameTestCase::TestShortLongPath()
546 {
547 wxFileName fn("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe");
548
549 // incredibly enough, GetLongPath() used to return different results during
550 // the first and subsequent runs, test for this
551 CPPUNIT_ASSERT_EQUAL( fn.GetLongPath(), fn.GetLongPath() );
552 CPPUNIT_ASSERT_EQUAL( fn.GetShortPath(), fn.GetShortPath() );
553 }
554
555 #endif // __WINDOWS__
556
557 void FileNameTestCase::TestUNC()
558 {
559 wxFileName fn("//share/path/name.ext", wxPATH_DOS);
560 CPPUNIT_ASSERT_EQUAL( "share", fn.GetVolume() );
561 CPPUNIT_ASSERT_EQUAL( "\\path", fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) );
562
563 fn.Assign("\\\\share2\\path2\\name.ext", wxPATH_DOS);
564 CPPUNIT_ASSERT_EQUAL( "share2", fn.GetVolume() );
565 CPPUNIT_ASSERT_EQUAL( "\\path2", fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) );
566 }
567
568 void FileNameTestCase::TestVolumeUniqueName()
569 {
570 wxFileName fn("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\",
571 wxPATH_DOS);
572 CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}",
573 fn.GetVolume() );
574 CPPUNIT_ASSERT_EQUAL( "\\", fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) );
575 CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\",
576 fn.GetFullPath(wxPATH_DOS) );
577
578 fn.Assign("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\"
579 "Program Files\\setup.exe", wxPATH_DOS);
580 CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}",
581 fn.GetVolume() );
582 CPPUNIT_ASSERT_EQUAL( "\\Program Files",
583 fn.GetPath(wxPATH_NO_SEPARATOR, wxPATH_DOS) );
584 CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\"
585 "Program Files\\setup.exe",
586 fn.GetFullPath(wxPATH_DOS) );
587 }
588
589 void FileNameTestCase::TestCreateTempFileName()
590 {
591 static const struct TestData
592 {
593 const char *prefix;
594 const char *expectedFolder;
595 bool shouldSucceed;
596 } testData[] =
597 {
598 { "", "$SYSTEM_TEMP", true },
599 { "foo", "$SYSTEM_TEMP", true },
600 { "..", "$SYSTEM_TEMP", true },
601 { "../bar", "..", true },
602 #ifdef __WXMSW__
603 { "$USER_DOCS_DIR\\", "$USER_DOCS_DIR", true },
604 { "c:\\a\\directory\\which\\does\\not\\exist", "", false },
605 #elif defined( __UNIX__ )
606 { "$USER_DOCS_DIR/", "$USER_DOCS_DIR", true },
607 { "/tmp/foo", "/tmp", true },
608 { "/tmp/a/directory/which/does/not/exist", "", false },
609 #endif // __UNIX__
610 };
611
612 for ( size_t n = 0; n < WXSIZEOF(testData); n++ )
613 {
614 wxString prefix = testData[n].prefix;
615 prefix.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir());
616
617 std::string errDesc = wxString::Format("failed on prefix '%s'", prefix).ToStdString();
618
619 wxString path = wxFileName::CreateTempFileName(prefix);
620 CPPUNIT_ASSERT_EQUAL_MESSAGE( errDesc, !testData[n].shouldSucceed, path.empty() );
621
622 if (testData[n].shouldSucceed)
623 {
624 errDesc += "; path is " + path.ToStdString();
625
626 // test the place where the temp file has been created
627 wxString expected = testData[n].expectedFolder;
628 expected.Replace("$SYSTEM_TEMP", wxStandardPaths::Get().GetTempDir());
629 expected.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir());
630 CPPUNIT_ASSERT_EQUAL_MESSAGE( errDesc, expected, wxFileName(path).GetPath() );
631
632 // the temporary file is created with full permissions for the current process
633 // so we should always be able to remove it:
634 CPPUNIT_ASSERT_MESSAGE( errDesc, wxRemoveFile(path) );
635 }
636 }
637 }
638
639 void FileNameTestCase::TestGetTimes()
640 {
641 wxFileName fn(wxFileName::CreateTempFileName("filenametest"));
642 CPPUNIT_ASSERT( fn.IsOk() );
643 wxON_BLOCK_EXIT1( wxRemoveFile, fn.GetFullPath() );
644
645 wxDateTime dtAccess, dtMod, dtCreate;
646 CPPUNIT_ASSERT( fn.GetTimes(&dtAccess, &dtMod, &dtCreate) );
647
648 // make sure all retrieved dates are equal to the current date&time
649 // with an accuracy up to 1 minute
650 CPPUNIT_ASSERT(dtCreate.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1)));
651 CPPUNIT_ASSERT(dtMod.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1)));
652 CPPUNIT_ASSERT(dtAccess.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1)));
653 }
654
655 void FileNameTestCase::TestExists()
656 {
657 wxFileName fn(wxFileName::CreateTempFileName("filenametest"));
658 CPPUNIT_ASSERT( fn.IsOk() );
659 wxON_BLOCK_EXIT1( wxRemoveFile, fn.GetFullPath() );
660
661 CPPUNIT_ASSERT( fn.FileExists() );
662 CPPUNIT_ASSERT( !wxFileName::DirExists(fn.GetFullPath()) );
663 CPPUNIT_ASSERT( fn.Exists() );
664
665 wxFileName dirTemp(wxFileName::DirName(wxFileName::GetTempDir()));
666 CPPUNIT_ASSERT( !dirTemp.FileExists() );
667 CPPUNIT_ASSERT( dirTemp.DirExists() );
668 CPPUNIT_ASSERT( dirTemp.Exists() );
669
670 #ifdef __UNIX__
671 CPPUNIT_ASSERT( !wxFileName::FileExists("/dev/null") );
672 CPPUNIT_ASSERT( !wxFileName::DirExists("/dev/null") );
673 CPPUNIT_ASSERT( wxFileName::Exists("/dev/null") );
674 #endif // __UNIX__
675 }