]>
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"
26 #include "wx/stdpaths.h"
29 #include "wx/msw/registry.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 static struct TestFileNameInfo
50 { "", "", "", "", "", false, wxPATH_UNIX
},
51 { "", "", "", "", "", false, wxPATH_DOS
},
52 { "", "", "", "", "", false, wxPATH_VMS
},
55 { "/usr/bin/ls", "", "/usr/bin", "ls", "", true, wxPATH_UNIX
},
56 { "/usr/bin/", "", "/usr/bin", "", "", true, wxPATH_UNIX
},
57 { "~/.zshrc", "", "~", ".zshrc", "", true, wxPATH_UNIX
},
58 { "../../foo", "", "../..", "foo", "", false, wxPATH_UNIX
},
59 { "foo.bar", "", "", "foo", "bar", false, wxPATH_UNIX
},
60 { "~/foo.bar", "", "~", "foo", "bar", true, wxPATH_UNIX
},
61 { "~user/foo.bar", "", "~user", "foo", "bar", true, wxPATH_UNIX
},
62 { "~user/", "", "~user", "", "", true, wxPATH_UNIX
},
63 { "/foo", "", "/", "foo", "", true, wxPATH_UNIX
},
64 { "Mahogany-0.60/foo.bar", "", "Mahogany-0.60", "foo", "bar", false, wxPATH_UNIX
},
65 { "/tmp/wxwin.tar.bz", "", "/tmp", "wxwin.tar", "bz", true, wxPATH_UNIX
},
68 { "foo.bar", "", "", "foo", "bar", false, wxPATH_DOS
},
69 { "\\foo.bar", "", "\\", "foo", "bar", false, wxPATH_DOS
},
70 { "c:foo.bar", "c", "", "foo", "bar", false, wxPATH_DOS
},
71 { "c:\\foo.bar", "c", "\\", "foo", "bar", true, wxPATH_DOS
},
72 { "c:\\Windows\\command.com", "c", "\\Windows", "command", "com", true, wxPATH_DOS
},
73 { "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\",
74 "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", "\\", "", "", true, wxPATH_DOS
},
75 { "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\Program Files\\setup.exe",
76 "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}", "\\Program Files", "setup", "exe", true, wxPATH_DOS
},
79 // NB: when using the wxFileName::GetLongPath() function on these two
80 // strings, the program will hang for several seconds blocking inside
81 // Win32 GetLongPathName() function
82 { "\\\\server\\foo.bar", "server", "\\", "foo", "bar", true, wxPATH_DOS
},
83 { "\\\\server\\dir\\foo.bar", "server", "\\dir", "foo", "bar", true, wxPATH_DOS
},
86 // consecutive [back]slashes should be treated as single occurrences of
87 // them and not interpreted as share names if there is a volume name
88 { "c:\\aaa\\bbb\\ccc", "c", "\\aaa\\bbb", "ccc", "", true, wxPATH_DOS
},
89 { "c:\\\\aaa\\bbb\\ccc", "c", "\\\\aaa\\bbb", "ccc", "", true, wxPATH_DOS
},
91 // wxFileName support for Mac file names is broken currently
94 { "Volume:Dir:File", "Volume", "Dir", "File", "", true, wxPATH_MAC
},
95 { "Volume:Dir:Subdir:File", "Volume", "Dir:Subdir", "File", "", true, wxPATH_MAC
},
96 { "Volume:", "Volume", "", "", "", true, wxPATH_MAC
},
97 { ":Dir:File", "", "Dir", "File", "", false, wxPATH_MAC
},
98 { ":File.Ext", "", "", "File", ".Ext", false, wxPATH_MAC
},
99 { "File.Ext", "", "", "File", ".Ext", false, wxPATH_MAC
},
104 // NB: on Windows they have the same effect of the \\server\\ strings
105 // (see the note above)
106 { "device:[dir1.dir2.dir3]file.txt", "device", "dir1.dir2.dir3", "file", "txt", true, wxPATH_VMS
},
108 { "file.txt", "", "", "file", "txt", false, wxPATH_VMS
},
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 class FileNameTestCase
: public CppUnit::TestCase
118 FileNameTestCase() { }
121 CPPUNIT_TEST_SUITE( FileNameTestCase
);
122 CPPUNIT_TEST( TestConstruction
);
123 CPPUNIT_TEST( TestComparison
);
124 CPPUNIT_TEST( TestSplit
);
125 CPPUNIT_TEST( TestSetPath
);
126 CPPUNIT_TEST( TestStrip
);
127 CPPUNIT_TEST( TestNormalize
);
128 CPPUNIT_TEST( TestReplace
);
129 CPPUNIT_TEST( TestGetHumanReadable
);
131 CPPUNIT_TEST( TestShortLongPath
);
132 #endif // __WINDOWS__
133 CPPUNIT_TEST( TestUNC
);
134 CPPUNIT_TEST( TestVolumeUniqueName
);
135 CPPUNIT_TEST( TestCreateTempFileName
);
136 CPPUNIT_TEST( TestGetTimes
);
137 CPPUNIT_TEST_SUITE_END();
139 void TestConstruction();
140 void TestComparison();
144 void TestNormalize();
146 void TestGetHumanReadable();
148 void TestShortLongPath();
149 #endif // __WINDOWS__
151 void TestVolumeUniqueName();
152 void TestCreateTempFileName();
155 DECLARE_NO_COPY_CLASS(FileNameTestCase
)
158 // register in the unnamed registry so that these tests are run by default
159 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase
);
161 // also include in it's own registry so that these tests can be run alone
162 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase
, "FileNameTestCase" );
164 void FileNameTestCase::TestConstruction()
166 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
168 const TestFileNameInfo
& fni
= filenames
[n
];
170 wxFileName
fn(fni
.fullname
, fni
.format
);
172 // the original full name could contain consecutive [back]slashes,
173 // squeeze them except for the double backslash in the beginning in
174 // Windows filenames where it has special meaning
175 wxString fullnameOrig
;
176 if ( fni
.format
== wxPATH_DOS
)
178 // copy the backslashes at beginning unchanged
179 const char *p
= fni
.fullname
;
181 fullnameOrig
+= *p
++;
183 // replace consecutive slashes with single ones in the rest
184 for ( char chPrev
= '\0'; *p
; p
++ )
186 if ( *p
== '\\' && chPrev
== '\\' )
190 fullnameOrig
+= chPrev
;
195 fullnameOrig
= fni
.fullname
;
198 fullnameOrig
.Replace("//", "/");
201 wxString fullname
= fn
.GetFullPath(fni
.format
);
202 CPPUNIT_ASSERT_EQUAL( fullnameOrig
, fullname
);
204 // notice that we use a dummy working directory to ensure that paths
205 // with "../.." in them could be normalized, otherwise this would fail
206 // if the test is run from root directory or its direct subdirectory
207 CPPUNIT_ASSERT_MESSAGE
209 (const char *)wxString::Format("Normalize(%s) failed", fni
.fullname
).mb_str(),
210 fn
.Normalize(wxPATH_NORM_ALL
, "/foo/bar/baz", fni
.format
)
213 if ( *fni
.volume
&& *fni
.path
)
215 // check that specifying the volume separately or as part of the
216 // path doesn't make any difference
217 wxString pathWithVolume
= fni
.volume
;
218 pathWithVolume
+= wxFileName::GetVolumeSeparator(fni
.format
);
219 pathWithVolume
+= fni
.path
;
221 CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume
,
231 fn
.AssignDir(wxEmptyString
);
232 CPPUNIT_ASSERT( !fn
.IsOk() );
234 fn
.Assign(wxEmptyString
);
235 CPPUNIT_ASSERT( !fn
.IsOk() );
237 fn
.Assign(wxEmptyString
, wxEmptyString
);
238 CPPUNIT_ASSERT( !fn
.IsOk() );
240 fn
.Assign(wxEmptyString
, wxEmptyString
, wxEmptyString
);
241 CPPUNIT_ASSERT( !fn
.IsOk() );
243 fn
.Assign(wxEmptyString
, wxEmptyString
, wxEmptyString
, wxEmptyString
);
244 CPPUNIT_ASSERT( !fn
.IsOk() );
247 void FileNameTestCase::TestComparison()
249 wxFileName
fn1(wxT("/tmp/file1"));
250 wxFileName
fn2(wxT("/tmp/dir2/../file2"));
253 CPPUNIT_ASSERT_EQUAL(fn1
.GetPath(), fn2
.GetPath());
256 void FileNameTestCase::TestSplit()
258 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
260 const TestFileNameInfo
& fni
= filenames
[n
];
261 wxString volume
, path
, name
, ext
;
262 wxFileName::SplitPath(fni
.fullname
,
263 &volume
, &path
, &name
, &ext
, fni
.format
);
265 CPPUNIT_ASSERT_EQUAL( wxString(fni
.volume
), volume
);
266 CPPUNIT_ASSERT_EQUAL( wxString(fni
.path
), path
);
267 CPPUNIT_ASSERT_EQUAL( wxString(fni
.name
), name
);
268 CPPUNIT_ASSERT_EQUAL( wxString(fni
.ext
), ext
);
271 // special case of empty extension
272 wxFileName
fn("foo.");
273 CPPUNIT_ASSERT_EQUAL( wxString("foo."), fn
.GetFullPath() );
276 void FileNameTestCase::TestSetPath()
278 wxFileName
fn("d:\\test\\foo.bar", wxPATH_DOS
);
279 fn
.SetPath("c:\\temp", wxPATH_DOS
);
280 CPPUNIT_ASSERT( fn
.SameAs(wxFileName("c:\\temp\\foo.bar", wxPATH_DOS
)) );
282 fn
= wxFileName("/usr/bin/ls", wxPATH_UNIX
);
283 fn
.SetPath("/usr/local/bin", wxPATH_UNIX
);
284 CPPUNIT_ASSERT( fn
.SameAs(wxFileName("/usr/local/bin/ls", wxPATH_UNIX
)) );
287 void FileNameTestCase::TestNormalize()
289 // prepare some data to be used later
290 wxString sep
= wxFileName::GetPathSeparator();
291 wxString cwd
= wxGetCwd();
292 wxString home
= wxGetUserHome();
294 cwd
.Replace(sep
, wxT("/"));
295 if (cwd
.Last() != wxT('/'))
297 home
.Replace(sep
, wxT("/"));
298 if (home
.Last() != wxT('/'))
301 // since we will always be testing paths using the wxPATH_UNIX
302 // format, we need to remove the volume, if present
303 if (home
.Contains(wxT(':')))
304 home
= home
.AfterFirst(wxT(':'));
305 if (cwd
.Contains(wxT(':')))
306 cwd
= cwd
.AfterFirst(wxT(':'));
308 static const struct FileNameTest
310 const char *original
;
312 const char *expected
;
316 // test wxPATH_NORM_ENV_VARS
318 { "%ABCDEF%/g/h/i", wxPATH_NORM_ENV_VARS
, "abcdef/g/h/i", wxPATH_UNIX
},
320 { "$(ABCDEF)/g/h/i", wxPATH_NORM_ENV_VARS
, "abcdef/g/h/i", wxPATH_UNIX
},
323 // test wxPATH_NORM_DOTS
324 { "a/.././b/c/../../", wxPATH_NORM_DOTS
, "", wxPATH_UNIX
},
325 { "./", wxPATH_NORM_DOTS
, "", wxPATH_UNIX
},
326 { "b/../", wxPATH_NORM_DOTS
, "", wxPATH_UNIX
},
328 // test wxPATH_NORM_TILDE: notice that ~ is only interpreted specially
329 // when it is the first character in the file name
330 { "/a/b/~", wxPATH_NORM_TILDE
, "/a/b/~", wxPATH_UNIX
},
331 { "/~/a/b", wxPATH_NORM_TILDE
, "/~/a/b", wxPATH_UNIX
},
332 { "~/a/b", wxPATH_NORM_TILDE
, "HOME/a/b", wxPATH_UNIX
},
334 // test wxPATH_NORM_CASE
335 { "Foo", wxPATH_NORM_CASE
, "Foo", wxPATH_UNIX
},
336 { "Foo", wxPATH_NORM_CASE
, "foo", wxPATH_DOS
},
337 { "C:\\Program Files\\wx", wxPATH_NORM_CASE
,
338 "c:\\program files\\wx", wxPATH_DOS
},
339 { "C:/Program Files/wx", wxPATH_NORM_ALL
| wxPATH_NORM_CASE
,
340 "c:\\program files\\wx", wxPATH_DOS
},
341 { "C:\\Users\\zeitlin", wxPATH_NORM_ALL
| wxPATH_NORM_CASE
,
342 "c:\\users\\zeitlin", wxPATH_DOS
},
344 // test wxPATH_NORM_ABSOLUTE
345 { "a/b/", wxPATH_NORM_ABSOLUTE
, "CWD/a/b/", wxPATH_UNIX
},
346 { "a/b/c.ext", wxPATH_NORM_ABSOLUTE
, "CWD/a/b/c.ext", wxPATH_UNIX
},
347 { "/a", wxPATH_NORM_ABSOLUTE
, "/a", wxPATH_UNIX
},
349 // test giving no flags at all to Normalize()
350 { "a/b/", 0, "a/b/", wxPATH_UNIX
},
351 { "a/b/c.ext", 0, "a/b/c.ext", wxPATH_UNIX
},
352 { "/a", 0, "/a", wxPATH_UNIX
},
354 // test handling dots without wxPATH_NORM_DOTS and wxPATH_NORM_ABSOLUTE
355 // for both existing and non-existent files (this is important under
356 // MSW where GetLongPathName() works only for the former)
357 { "./foo", wxPATH_NORM_LONG
, "./foo", wxPATH_UNIX
},
358 { "../foo", wxPATH_NORM_LONG
, "../foo", wxPATH_UNIX
},
359 { ".\\test.bkl", wxPATH_NORM_LONG
, ".\\test.bkl", wxPATH_DOS
},
360 { ".\\foo", wxPATH_NORM_LONG
, ".\\foo", wxPATH_DOS
},
361 { "..\\Makefile.in", wxPATH_NORM_LONG
, "..\\Makefile.in", wxPATH_DOS
},
362 { "..\\foo", wxPATH_NORM_LONG
, "..\\foo", wxPATH_DOS
},
365 // set the env var ABCDEF
366 wxSetEnv("ABCDEF", "abcdef");
368 for ( size_t i
= 0; i
< WXSIZEOF(tests
); i
++ )
370 const FileNameTest
& fnt
= tests
[i
];
371 wxFileName
fn(fnt
.original
, fnt
.fmt
);
373 // be sure this normalization does not fail
376 ("#%d: Normalize(%s) failed", (int)i
, fnt
.original
),
377 fn
.Normalize(fnt
.flags
, cwd
, fnt
.fmt
)
380 // compare result with expected string
381 wxString
expected(tests
[i
].expected
);
382 expected
.Replace("HOME/", home
);
383 expected
.Replace("CWD/", cwd
);
384 WX_ASSERT_EQUAL_MESSAGE
386 ("array element #%d", (int)i
),
387 expected
, fn
.GetFullPath(fnt
.fmt
)
391 // MSW-only test for wxPATH_NORM_LONG: notice that we only run it if short
392 // names generation is not disabled for this system as otherwise the file
393 // MKINST~1 doesn't exist at all and normalizing it fails (it's possible
394 // that we're on a FAT partition in which case the test would still succeed
395 // and also that the registry key was changed recently and didn't take
396 // effect yet but these are marginal cases which we consciously choose to
399 long shortNamesDisabled
;
403 "SYSTEM\\CurrentControlSet\\Control\\FileSystem"
404 ).QueryValue("NtfsDisable8dot3NameCreation", &shortNamesDisabled
) &&
405 !shortNamesDisabled
)
407 wxFileName
fn("..\\MKINST~1");
408 CPPUNIT_ASSERT( fn
.Normalize(wxPATH_NORM_LONG
, cwd
) );
409 CPPUNIT_ASSERT_EQUAL( "..\\mkinstalldirs", fn
.GetFullPath() );
411 //else: when in doubt, don't run the test
415 void FileNameTestCase::TestReplace()
417 static const struct FileNameTest
419 const char *original
;
420 const char *env_contents
;
421 const char *replace_fmtstring
;
422 const char *expected
;
426 { "/usr/a/strange path/lib/someFile.ext", "/usr/a/strange path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX
},
427 { "/usr/a/path/lib/someFile.ext", "/usr/a/path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX
},
428 { "/usr/a/path/lib/someFile", "/usr/a/path/", "$%s", "$TEST_VARlib/someFile", wxPATH_UNIX
},
429 { "/usr/a/path/lib/", "/usr/a/path/", "$(%s)", "$(TEST_VAR)lib/", wxPATH_UNIX
},
430 { "/usr/a/path/lib/", "/usr/a/path/", "${{%s}}", "${{TEST_VAR}}lib/", wxPATH_UNIX
},
431 { "/usr/a/path/lib/", "/usr/a/path/", "%s", "TEST_VARlib/", wxPATH_UNIX
},
432 { "/usr/a/path/lib/", "/usr/a/path/", "%s//", "TEST_VAR/lib/", wxPATH_UNIX
},
433 // note: empty directory components are automatically removed by wxFileName thus
434 // using // in the replace format string has no effect
436 { "/usr/../a/path/lib/", "/usr/a/path/", "%s", "/usr/../a/path/lib/", wxPATH_UNIX
},
437 { "/usr/a/path/usr/usr", "/usr", "%s", "TEST_VAR/a/pathTEST_VAR/usr", wxPATH_UNIX
},
438 { "/usr/a/path/usr/usr", "/usr", "$%s", "$TEST_VAR/a/path$TEST_VAR/usr", wxPATH_UNIX
},
439 { "/a/b/c/d", "a/", "%s", "/TEST_VARb/c/d", wxPATH_UNIX
},
441 { "C:\\A\\Strange Path\\lib\\someFile", "C:\\A\\Strange Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN
},
442 { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN
},
443 { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "$(%s)", "$(TEST_VAR)\\lib\\someFile", wxPATH_WIN
}
446 for ( size_t i
= 0; i
< WXSIZEOF(tests
); i
++ )
448 const FileNameTest
& fnt
= tests
[i
];
449 wxFileName
fn(fnt
.original
, fnt
.fmt
);
451 // set the environment variable
452 wxSetEnv("TEST_VAR", fnt
.env_contents
);
454 // be sure this ReplaceEnvVariable does not fail
457 ("#%d: ReplaceEnvVariable(%s) failed", (int)i
, fnt
.replace_fmtstring
),
458 fn
.ReplaceEnvVariable("TEST_VAR", fnt
.replace_fmtstring
, fnt
.fmt
)
461 // compare result with expected string
462 wxString
expected(fnt
.expected
);
463 WX_ASSERT_EQUAL_MESSAGE
465 ("array element #%d", (int)i
),
466 expected
, fn
.GetFullPath(fnt
.fmt
)
470 // now test ReplaceHomeDir
472 wxFileName fn
= wxFileName::DirName(wxGetHomeDir());
473 fn
.AppendDir("test1");
474 fn
.AppendDir("test2");
475 fn
.AppendDir("test3");
476 fn
.SetName("some file");
480 ("ReplaceHomeDir(%s) failed", fn
.GetFullPath()),
484 CPPUNIT_ASSERT_EQUAL( wxString("~/test1/test2/test3/some file"),
485 fn
.GetFullPath(wxPATH_UNIX
) );
488 void FileNameTestCase::TestGetHumanReadable()
490 static const struct TestData
495 wxSizeConvention conv
;
498 { "NA", 0, 1, wxSIZE_CONV_TRADITIONAL
},
499 { "2.0 KB", 2000, 1, wxSIZE_CONV_TRADITIONAL
},
500 { "1.953 KiB", 2000, 3, wxSIZE_CONV_IEC
},
501 { "2.000 KB", 2000, 3, wxSIZE_CONV_SI
},
502 { "297 KB", 304351, 0, wxSIZE_CONV_TRADITIONAL
},
503 { "304 KB", 304351, 0, wxSIZE_CONV_SI
},
506 CLocaleSetter loc
; // we want to use "C" locale for LC_NUMERIC
507 // so that regardless of the system's locale
508 // the decimal point used by GetHumanReadableSize()
510 for ( unsigned n
= 0; n
< WXSIZEOF(testData
); n
++ )
512 const TestData
& td
= testData
[n
];
514 // take care of using the decimal point for the current locale before
515 // the actual comparison
519 wxFileName::GetHumanReadableSize(td
.size
, "NA", td
.prec
, td
.conv
)
523 // also test the default convention value
524 CPPUNIT_ASSERT_EQUAL( "1.4 MB", wxFileName::GetHumanReadableSize(1512993, "") );
527 void FileNameTestCase::TestStrip()
529 CPPUNIT_ASSERT_EQUAL( "", wxFileName::StripExtension("") );
530 CPPUNIT_ASSERT_EQUAL( ".", wxFileName::StripExtension(".") );
531 CPPUNIT_ASSERT_EQUAL( ".vimrc", wxFileName::StripExtension(".vimrc") );
532 CPPUNIT_ASSERT_EQUAL( "bad", wxFileName::StripExtension("bad") );
533 CPPUNIT_ASSERT_EQUAL( "good", wxFileName::StripExtension("good.wav") );
534 CPPUNIT_ASSERT_EQUAL( "good.wav", wxFileName::StripExtension("good.wav.wav") );
539 void FileNameTestCase::TestShortLongPath()
541 wxFileName
fn("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe");
543 // incredibly enough, GetLongPath() used to return different results during
544 // the first and subsequent runs, test for this
545 CPPUNIT_ASSERT_EQUAL( fn
.GetLongPath(), fn
.GetLongPath() );
546 CPPUNIT_ASSERT_EQUAL( fn
.GetShortPath(), fn
.GetShortPath() );
549 #endif // __WINDOWS__
551 void FileNameTestCase::TestUNC()
553 wxFileName
fn("//share/path/name.ext", wxPATH_DOS
);
554 CPPUNIT_ASSERT_EQUAL( "share", fn
.GetVolume() );
555 CPPUNIT_ASSERT_EQUAL( "\\path", fn
.GetPath(wxPATH_NO_SEPARATOR
, wxPATH_DOS
) );
557 fn
.Assign("\\\\share2\\path2\\name.ext", wxPATH_DOS
);
558 CPPUNIT_ASSERT_EQUAL( "share2", fn
.GetVolume() );
559 CPPUNIT_ASSERT_EQUAL( "\\path2", fn
.GetPath(wxPATH_NO_SEPARATOR
, wxPATH_DOS
) );
562 void FileNameTestCase::TestVolumeUniqueName()
564 wxFileName
fn("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\",
566 CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}",
568 CPPUNIT_ASSERT_EQUAL( "\\", fn
.GetPath(wxPATH_NO_SEPARATOR
, wxPATH_DOS
) );
569 CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\",
570 fn
.GetFullPath(wxPATH_DOS
) );
572 fn
.Assign("\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\"
573 "Program Files\\setup.exe", wxPATH_DOS
);
574 CPPUNIT_ASSERT_EQUAL( "Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}",
576 CPPUNIT_ASSERT_EQUAL( "\\Program Files",
577 fn
.GetPath(wxPATH_NO_SEPARATOR
, wxPATH_DOS
) );
578 CPPUNIT_ASSERT_EQUAL( "\\\\?\\Volume{8089d7d7-d0ac-11db-9dd0-806d6172696f}\\"
579 "Program Files\\setup.exe",
580 fn
.GetFullPath(wxPATH_DOS
) );
583 void FileNameTestCase::TestCreateTempFileName()
585 static const struct TestData
588 const char *expectedFolder
;
592 { "", "$SYSTEM_TEMP", true },
593 { "foo", "$SYSTEM_TEMP", true },
594 { "..", "$SYSTEM_TEMP", true },
595 { "../bar", "..", true },
597 { "$USER_DOCS_DIR\\", "$USER_DOCS_DIR", true },
598 { "c:\\a\\directory\\which\\does\\not\\exist", "", false },
599 #elif defined( __UNIX__ )
600 { "$USER_DOCS_DIR/", "$USER_DOCS_DIR", true },
601 { "/tmp/foo", "/tmp", true },
602 { "/tmp/a/directory/which/does/not/exist", "", false },
606 for ( size_t n
= 0; n
< WXSIZEOF(testData
); n
++ )
608 wxString prefix
= testData
[n
].prefix
;
609 prefix
.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir());
611 std::string errDesc
= wxString::Format("failed on prefix '%s'", prefix
).ToStdString();
613 wxString path
= wxFileName::CreateTempFileName(prefix
);
614 CPPUNIT_ASSERT_EQUAL_MESSAGE( errDesc
, !testData
[n
].shouldSucceed
, path
.empty() );
616 if (testData
[n
].shouldSucceed
)
618 errDesc
+= "; path is " + path
.ToStdString();
620 // test the place where the temp file has been created
621 wxString expected
= testData
[n
].expectedFolder
;
622 expected
.Replace("$SYSTEM_TEMP", wxStandardPaths::Get().GetTempDir());
623 expected
.Replace("$USER_DOCS_DIR", wxStandardPaths::Get().GetDocumentsDir());
624 CPPUNIT_ASSERT_EQUAL_MESSAGE( errDesc
, expected
, wxFileName(path
).GetPath() );
626 // the temporary file is created with full permissions for the current process
627 // so we should always be able to remove it:
628 CPPUNIT_ASSERT_MESSAGE( errDesc
, wxRemoveFile(path
) );
633 void FileNameTestCase::TestGetTimes()
635 wxFileName
fn(wxFileName::CreateTempFileName("filenametest"));
636 CPPUNIT_ASSERT( fn
.IsOk() );
638 wxDateTime dtAccess
, dtMod
, dtCreate
;
639 CPPUNIT_ASSERT( fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) );
641 // make sure all retrieved dates are equal to the current date&time
642 // with an accuracy up to 1 minute
643 CPPUNIT_ASSERT(dtCreate
.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1)));
644 CPPUNIT_ASSERT(dtMod
.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1)));
645 CPPUNIT_ASSERT(dtAccess
.IsEqualUpTo(wxDateTime::Now(), wxTimeSpan(0,1)));