added test for parsing c:\\aaa\bbb\ccc paths (aaa shouldn't be interpreted as network...
[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 // ----------------------------------------------------------------------------
28 // local functions
29 // ----------------------------------------------------------------------------
30
31 // define stream inserter for wxFileName to use it in CPPUNIT_ASSERT_EQUAL()
32 inline std::ostream& operator<<(std::ostream& o, const wxFileName& fn)
33 {
34 return o << fn.GetFullPath();
35 }
36
37 // ----------------------------------------------------------------------------
38 // test data
39 // ----------------------------------------------------------------------------
40
41 static struct FileNameInfo
42 {
43 const wxChar *fullname;
44 const wxChar *volume;
45 const wxChar *path;
46 const wxChar *name;
47 const wxChar *ext;
48 bool isAbsolute;
49 wxPathFormat format;
50 } filenames[] =
51 {
52 // the empty string
53 { _T(""), _(""), _(""), _(""), _(""), false, wxPATH_UNIX },
54 { _T(""), _(""), _(""), _(""), _(""), false, wxPATH_DOS },
55 { _T(""), _(""), _(""), _(""), _(""), false, wxPATH_VMS },
56
57 // Unix file names
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 },
67
68 // Windows file names
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 },
74
75 // NB: when using the wxFileName::GetLongPath() function on these two strings,
76 // the program will hang various seconds. All those time is taken by the
77 // call to the win32 API GetLongPathName()...
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 },
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 { _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 },
85
86 // wxFileName support for Mac file names is broken currently
87 #if 0
88 // Mac file names
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 },
95 #endif // 0
96
97 // VMS file names
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 },
102 };
103
104 // ----------------------------------------------------------------------------
105 // test class
106 // ----------------------------------------------------------------------------
107
108 class FileNameTestCase : public CppUnit::TestCase
109 {
110 public:
111 FileNameTestCase() { }
112
113 private:
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 );
121 #ifdef __WINDOWS__
122 CPPUNIT_TEST( TestShortLongPath );
123 #endif // __WINDOWS__
124 CPPUNIT_TEST_SUITE_END();
125
126 void TestConstruction();
127 void TestComparison();
128 void TestSplit();
129 void TestSetPath();
130 void TestStrip();
131 void TestNormalize();
132 #ifdef __WINDOWS__
133 void TestShortLongPath();
134 #endif // __WINDOWS__
135
136 DECLARE_NO_COPY_CLASS(FileNameTestCase)
137 };
138
139 // register in the unnamed registry so that these tests are run by default
140 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase );
141
142 // also include in it's own registry so that these tests can be run alone
143 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" );
144
145 void FileNameTestCase::TestConstruction()
146 {
147 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
148 {
149 const FileNameInfo& fni = filenames[n];
150
151 wxFileName fn(fni.fullname, fni.format);
152
153 wxString fullname = fn.GetFullPath(fni.format);
154 CPPUNIT_ASSERT_EQUAL( wxString(fni.fullname), fullname );
155
156 // notice that we use a dummy working directory to ensure that paths
157 // with "../.." in them could be normalized, otherwise this would fail
158 // if the test is run from root directory or its direct subdirectory
159 CPPUNIT_ASSERT_MESSAGE
160 (
161 (const char *)wxString::Format(_T("Normalize(%s) failed"), fni.fullname).mb_str(),
162 fn.Normalize(wxPATH_NORM_ALL, _T("/foo/bar/baz"), fni.format)
163 );
164
165 if ( *fni.volume && *fni.path )
166 {
167 // check that specifying the volume separately or as part of the
168 // path doesn't make any difference
169 wxString pathWithVolume = fni.volume;
170 pathWithVolume += wxFileName::GetVolumeSeparator(fni.format);
171 pathWithVolume += fni.path;
172
173 CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume,
174 fni.name,
175 fni.ext,
176 fni.format), fn );
177 }
178 }
179
180 wxFileName fn;
181
182 // empty strings
183 fn.AssignDir(wxEmptyString);
184 CPPUNIT_ASSERT( !fn.IsOk() );
185
186 fn.Assign(wxEmptyString);
187 CPPUNIT_ASSERT( !fn.IsOk() );
188
189 fn.Assign(wxEmptyString, wxEmptyString);
190 CPPUNIT_ASSERT( !fn.IsOk() );
191
192 fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString);
193 CPPUNIT_ASSERT( !fn.IsOk() );
194
195 fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString, wxEmptyString);
196 CPPUNIT_ASSERT( !fn.IsOk() );
197 }
198
199 void FileNameTestCase::TestComparison()
200 {
201 wxFileName fn1(wxT("/tmp/file1"));
202 wxFileName fn2(wxT("/tmp/dir2/../file2"));
203 fn1.Normalize();
204 fn2.Normalize();
205 CPPUNIT_ASSERT_EQUAL(fn1.GetPath(), fn2.GetPath());
206 }
207
208 void FileNameTestCase::TestSplit()
209 {
210 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
211 {
212 const FileNameInfo& fni = filenames[n];
213 wxString volume, path, name, ext;
214 wxFileName::SplitPath(fni.fullname,
215 &volume, &path, &name, &ext, fni.format);
216
217 CPPUNIT_ASSERT_EQUAL( wxString(fni.volume), volume );
218 CPPUNIT_ASSERT_EQUAL( wxString(fni.path), path );
219 CPPUNIT_ASSERT_EQUAL( wxString(fni.name), name );
220 CPPUNIT_ASSERT_EQUAL( wxString(fni.ext), ext );
221 }
222
223 // special case of empty extension
224 wxFileName fn(_T("foo."));
225 CPPUNIT_ASSERT_EQUAL( wxString(_T("foo.")), fn.GetFullPath() );
226 }
227
228 void FileNameTestCase::TestSetPath()
229 {
230 wxFileName fn(_T("d:\\test\\foo.bar"), wxPATH_DOS);
231 fn.SetPath(_T("c:\\temp"), wxPATH_DOS);
232 CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS)) );
233
234 fn = wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX);
235 fn.SetPath(_T("/usr/local/bin"), wxPATH_UNIX);
236 CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX)) );
237 }
238
239 void FileNameTestCase::TestNormalize()
240 {
241 // prepare some data to be used later
242 wxString sep = wxFileName::GetPathSeparator();
243 wxString cwd = wxGetCwd();
244 wxString home = wxGetUserHome();
245
246 cwd.Replace(sep, wxT("/"));
247 if (cwd.Last() != wxT('/'))
248 cwd += wxT('/');
249 home.Replace(sep, wxT("/"));
250 if (home.Last() != wxT('/'))
251 home += wxT('/');
252
253 // since we will always be testing paths using the wxPATH_UNIX
254 // format, we need to remove the volume, if present
255 if (home.Contains(wxT(':')))
256 home = home.AfterFirst(wxT(':'));
257 if (cwd.Contains(wxT(':')))
258 cwd = cwd.AfterFirst(wxT(':'));
259
260 static struct FileNameTest
261 {
262 const wxChar *original;
263 int flags;
264 wxString expected;
265 } tests[] =
266 {
267 // test wxPATH_NORM_ENV_VARS
268 #ifdef __WXMSW__
269 { wxT("%ABCDEF%/g/h/i"), wxPATH_NORM_ENV_VARS, wxT("abcdef/g/h/i") },
270 #else
271 { wxT("$(ABCDEF)/g/h/i"), wxPATH_NORM_ENV_VARS, wxT("abcdef/g/h/i") },
272 #endif
273
274 // test wxPATH_NORM_DOTS
275 { wxT("a/.././b/c/../../"), wxPATH_NORM_DOTS, wxT("") },
276
277 // test wxPATH_NORM_TILDE
278 // NB: do the tilde expansion also under Windows to test if it works there too
279 { wxT("/a/b/~"), wxPATH_NORM_TILDE, wxT("/a/b/~") },
280 { wxT("/~/a/b"), wxPATH_NORM_TILDE, home + wxT("a/b") },
281 { wxT("~/a/b"), wxPATH_NORM_TILDE, home + wxT("a/b") },
282
283 // test wxPATH_NORM_ABSOLUTE
284 { wxT("a/b/"), wxPATH_NORM_ABSOLUTE, cwd + wxT("a/b/") },
285 { wxT("a/b/c.ext"), wxPATH_NORM_ABSOLUTE, cwd + wxT("a/b/c.ext") },
286 { wxT("/a"), wxPATH_NORM_ABSOLUTE, wxT("/a") },
287
288 // test giving no flags at all to Normalize()
289 { wxT("a/b/"), 0, wxT("a/b/") },
290 { wxT("a/b/c.ext"), 0, wxT("a/b/c.ext") },
291 { wxT("/a"), 0, wxT("/a") }
292 };
293
294 // set the env var ABCDEF
295 wxSetEnv(_T("ABCDEF"), _T("abcdef"));
296
297 for ( size_t i = 0; i < WXSIZEOF(tests); i++ )
298 {
299 wxFileName fn(tests[i].original, wxPATH_UNIX);
300
301 // be sure this normalization does not fail
302 CPPUNIT_ASSERT_MESSAGE
303 (
304 (const char *)wxString::Format(_T("Normalize(%s) failed"), tests[i].original).mb_str(),
305 fn.Normalize(tests[i].flags, cwd, wxPATH_UNIX)
306 );
307
308 // compare result with expected string
309 CPPUNIT_ASSERT_EQUAL( tests[i].expected, fn.GetFullPath(wxPATH_UNIX) );
310 }
311 }
312
313 wxString wxTestStripExtension(wxString szFile)
314 {
315 wxStripExtension(szFile);
316 return szFile;
317 }
318
319 void FileNameTestCase::TestStrip()
320 {
321 //test a crash
322 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T("")) );
323
324 //others
325 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".")) );
326 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".wav")) );
327 CPPUNIT_ASSERT_EQUAL( wxString(_T("good")), wxTestStripExtension(_T("good.wav")) );
328 CPPUNIT_ASSERT_EQUAL( wxString(_T("good.wav")), wxTestStripExtension(_T("good.wav.wav")) );
329 }
330
331 #ifdef __WINDOWS__
332
333 void FileNameTestCase::TestShortLongPath()
334 {
335 wxFileName fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"));
336
337 // incredibly enough, GetLongPath() used to return different results during
338 // the first and subsequent runs, test for this
339 CPPUNIT_ASSERT_EQUAL( fn.GetLongPath(), fn.GetLongPath() );
340 CPPUNIT_ASSERT_EQUAL( fn.GetShortPath(), fn.GetShortPath() );
341 }
342
343 #endif // __WINDOWS__