]>
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"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 // define stream inserter for wxFileName to use it in CPPUNIT_ASSERT_EQUAL()
32 inline std::ostream
& operator<<(std::ostream
& o
, const wxFileName
& fn
)
34 return o
<< fn
.GetFullPath();
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 static struct FileNameInfo
43 const wxChar
*fullname
;
53 { _T(""), _T(""), _T(""), _T(""), _T(""), false, wxPATH_UNIX
},
54 { _T(""), _T(""), _T(""), _T(""), _T(""), false, wxPATH_DOS
},
55 { _T(""), _T(""), _T(""), _T(""), _T(""), false, wxPATH_VMS
},
58 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX
},
59 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX
},
60 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX
},
61 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX
},
62 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
63 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX
},
64 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX
},
65 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
66 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX
},
69 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
70 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS
},
71 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
72 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
73 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS
},
75 // NB: when using the wxFileName::GetLongPath() function on these two
76 // strings, the program will hang for several seconds blocking inside
77 // Win32 GetLongPathName() function
78 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
79 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
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 { _T("c:\\aaa\\bbb\\ccc"), _T("c"), _T("\\aaa\\bbb"), _T("ccc"), _T(""), true, wxPATH_DOS
},
84 { _T("c:\\\\aaa\\bbb\\ccc"), _T("c"), _T("\\\\aaa\\bbb"), _T("ccc"), _T(""), true, wxPATH_DOS
},
86 // wxFileName support for Mac file names is broken currently
89 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC
},
90 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC
},
91 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC
},
92 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC
},
93 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
94 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
98 // NB: on Windows they have the same effect of the \\server\\ strings
99 // (see the note above)
100 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS
},
101 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS
},
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 class FileNameTestCase
: public CppUnit::TestCase
111 FileNameTestCase() { }
114 CPPUNIT_TEST_SUITE( FileNameTestCase
);
115 CPPUNIT_TEST( TestConstruction
);
116 CPPUNIT_TEST( TestComparison
);
117 CPPUNIT_TEST( TestSplit
);
118 CPPUNIT_TEST( TestSetPath
);
119 CPPUNIT_TEST( TestStrip
);
120 CPPUNIT_TEST( TestNormalize
);
122 CPPUNIT_TEST( TestShortLongPath
);
123 #endif // __WINDOWS__
124 CPPUNIT_TEST_SUITE_END();
126 void TestConstruction();
127 void TestComparison();
131 void TestNormalize();
133 void TestShortLongPath();
134 #endif // __WINDOWS__
136 DECLARE_NO_COPY_CLASS(FileNameTestCase
)
139 // register in the unnamed registry so that these tests are run by default
140 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase
);
142 // also include in it's own registry so that these tests can be run alone
143 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase
, "FileNameTestCase" );
145 void FileNameTestCase::TestConstruction()
147 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
149 const FileNameInfo
& fni
= filenames
[n
];
151 wxFileName
fn(fni
.fullname
, fni
.format
);
153 // the original full name could contain consecutive [back]slashes,
154 // squeeze them except for the double backslash in the beginning in
155 // Windows filenames where it has special meaning
156 wxString fullnameOrig
;
157 if ( fni
.format
== wxPATH_DOS
)
159 // copy the backslashes at beginning unchanged
160 const wxChar
*p
= fni
.fullname
;
161 while ( *p
== _T('\\') )
162 fullnameOrig
+= *p
++;
164 // replace consecutive slashes with single ones in the rest
165 for ( wxChar chPrev
= _T('\0'); *p
; p
++ )
167 if ( *p
== _T('\\') && chPrev
== _T('\\') )
171 fullnameOrig
+= chPrev
;
176 fullnameOrig
= fni
.fullname
;
179 fullnameOrig
.Replace(_T("//"), _T("/"));
182 wxString fullname
= fn
.GetFullPath(fni
.format
);
183 CPPUNIT_ASSERT_EQUAL( fullnameOrig
, fullname
);
185 // notice that we use a dummy working directory to ensure that paths
186 // with "../.." in them could be normalized, otherwise this would fail
187 // if the test is run from root directory or its direct subdirectory
188 CPPUNIT_ASSERT_MESSAGE
190 (const char *)wxString::Format(_T("Normalize(%s) failed"), fni
.fullname
).mb_str(),
191 fn
.Normalize(wxPATH_NORM_ALL
, _T("/foo/bar/baz"), fni
.format
)
194 if ( *fni
.volume
&& *fni
.path
)
196 // check that specifying the volume separately or as part of the
197 // path doesn't make any difference
198 wxString pathWithVolume
= fni
.volume
;
199 pathWithVolume
+= wxFileName::GetVolumeSeparator(fni
.format
);
200 pathWithVolume
+= fni
.path
;
202 CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume
,
212 fn
.AssignDir(wxEmptyString
);
213 CPPUNIT_ASSERT( !fn
.IsOk() );
215 fn
.Assign(wxEmptyString
);
216 CPPUNIT_ASSERT( !fn
.IsOk() );
218 fn
.Assign(wxEmptyString
, wxEmptyString
);
219 CPPUNIT_ASSERT( !fn
.IsOk() );
221 fn
.Assign(wxEmptyString
, wxEmptyString
, wxEmptyString
);
222 CPPUNIT_ASSERT( !fn
.IsOk() );
224 fn
.Assign(wxEmptyString
, wxEmptyString
, wxEmptyString
, wxEmptyString
);
225 CPPUNIT_ASSERT( !fn
.IsOk() );
228 void FileNameTestCase::TestComparison()
230 wxFileName
fn1(wxT("/tmp/file1"));
231 wxFileName
fn2(wxT("/tmp/dir2/../file2"));
234 CPPUNIT_ASSERT_EQUAL(fn1
.GetPath(), fn2
.GetPath());
237 void FileNameTestCase::TestSplit()
239 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
241 const FileNameInfo
& fni
= filenames
[n
];
242 wxString volume
, path
, name
, ext
;
243 wxFileName::SplitPath(fni
.fullname
,
244 &volume
, &path
, &name
, &ext
, fni
.format
);
246 CPPUNIT_ASSERT_EQUAL( wxString(fni
.volume
), volume
);
247 CPPUNIT_ASSERT_EQUAL( wxString(fni
.path
), path
);
248 CPPUNIT_ASSERT_EQUAL( wxString(fni
.name
), name
);
249 CPPUNIT_ASSERT_EQUAL( wxString(fni
.ext
), ext
);
252 // special case of empty extension
253 wxFileName
fn(_T("foo."));
254 CPPUNIT_ASSERT_EQUAL( wxString(_T("foo.")), fn
.GetFullPath() );
257 void FileNameTestCase::TestSetPath()
259 wxFileName
fn(_T("d:\\test\\foo.bar"), wxPATH_DOS
);
260 fn
.SetPath(_T("c:\\temp"), wxPATH_DOS
);
261 CPPUNIT_ASSERT( fn
.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS
)) );
263 fn
= wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX
);
264 fn
.SetPath(_T("/usr/local/bin"), wxPATH_UNIX
);
265 CPPUNIT_ASSERT( fn
.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX
)) );
268 void FileNameTestCase::TestNormalize()
270 // prepare some data to be used later
271 wxString sep
= wxFileName::GetPathSeparator();
272 wxString cwd
= wxGetCwd();
273 wxString home
= wxGetUserHome();
275 cwd
.Replace(sep
, wxT("/"));
276 if (cwd
.Last() != wxT('/'))
278 home
.Replace(sep
, wxT("/"));
279 if (home
.Last() != wxT('/'))
282 // since we will always be testing paths using the wxPATH_UNIX
283 // format, we need to remove the volume, if present
284 if (home
.Contains(wxT(':')))
285 home
= home
.AfterFirst(wxT(':'));
286 if (cwd
.Contains(wxT(':')))
287 cwd
= cwd
.AfterFirst(wxT(':'));
289 static const struct FileNameTest
297 // test wxPATH_NORM_ENV_VARS
299 { "%ABCDEF%/g/h/i", wxPATH_NORM_ENV_VARS
, "abcdef/g/h/i", wxPATH_UNIX
},
301 { "$(ABCDEF)/g/h/i", wxPATH_NORM_ENV_VARS
, "abcdef/g/h/i", wxPATH_UNIX
},
304 // test wxPATH_NORM_DOTS
305 { "a/.././b/c/../../", wxPATH_NORM_DOTS
, "", wxPATH_UNIX
},
307 // test wxPATH_NORM_TILDE
308 // NB: do the tilde expansion also under Windows to test if it works there too
309 { "/a/b/~", wxPATH_NORM_TILDE
, "/a/b/~", wxPATH_UNIX
},
310 { "/~/a/b", wxPATH_NORM_TILDE
, home
+ "a/b", wxPATH_UNIX
},
311 { "~/a/b", wxPATH_NORM_TILDE
, home
+ "a/b", wxPATH_UNIX
},
313 // test wxPATH_NORM_CASE
314 { "Foo", wxPATH_NORM_CASE
, "Foo", wxPATH_UNIX
},
315 { "Foo", wxPATH_NORM_CASE
, "foo", wxPATH_DOS
},
316 { "C:\\Program Files\\wx", wxPATH_NORM_CASE
,
317 "c:\\program files\\wx", wxPATH_DOS
},
318 { "C:/Program Files/wx", wxPATH_NORM_ALL
| wxPATH_NORM_CASE
,
319 "c:\\program files\\wx", wxPATH_DOS
},
320 { "C:\\Users\\zeitlin", wxPATH_NORM_ALL
| wxPATH_NORM_CASE
,
321 "c:\\users\\zeitlin", wxPATH_DOS
},
323 // test wxPATH_NORM_ABSOLUTE
324 { "a/b/", wxPATH_NORM_ABSOLUTE
, cwd
+ "a/b/", wxPATH_UNIX
},
325 { "a/b/c.ext", wxPATH_NORM_ABSOLUTE
, cwd
+ "a/b/c.ext", wxPATH_UNIX
},
326 { "/a", wxPATH_NORM_ABSOLUTE
, "/a", wxPATH_UNIX
},
328 // test giving no flags at all to Normalize()
329 { "a/b/", 0, "a/b/", wxPATH_UNIX
},
330 { "a/b/c.ext", 0, "a/b/c.ext", wxPATH_UNIX
},
331 { "/a", 0, "/a", wxPATH_UNIX
}
334 // set the env var ABCDEF
335 wxSetEnv(_T("ABCDEF"), _T("abcdef"));
337 for ( size_t i
= 0; i
< WXSIZEOF(tests
); i
++ )
339 const FileNameTest
& fnt
= tests
[i
];
340 wxFileName
fn(fnt
.original
, fnt
.fmt
);
342 // be sure this normalization does not fail
343 CPPUNIT_ASSERT_MESSAGE
345 (const char *)wxString::Format(_T("Normalize(%s) failed"), fnt
.original
).mb_str(),
346 fn
.Normalize(fnt
.flags
, cwd
, fnt
.fmt
)
349 // compare result with expected string
350 CPPUNIT_ASSERT_EQUAL( fnt
.expected
, fn
.GetFullPath(fnt
.fmt
) );
354 wxString
wxTestStripExtension(wxString szFile
)
356 wxStripExtension(szFile
);
360 void FileNameTestCase::TestStrip()
363 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T("")) );
366 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".")) );
367 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".wav")) );
368 CPPUNIT_ASSERT_EQUAL( wxString(_T("good")), wxTestStripExtension(_T("good.wav")) );
369 CPPUNIT_ASSERT_EQUAL( wxString(_T("good.wav")), wxTestStripExtension(_T("good.wav.wav")) );
374 void FileNameTestCase::TestShortLongPath()
376 wxFileName
fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"));
378 // incredibly enough, GetLongPath() used to return different results during
379 // the first and subsequent runs, test for this
380 CPPUNIT_ASSERT_EQUAL( fn
.GetLongPath(), fn
.GetLongPath() );
381 CPPUNIT_ASSERT_EQUAL( fn
.GetShortPath(), fn
.GetShortPath() );
384 #endif // __WINDOWS__