]> git.saurik.com Git - wxWidgets.git/blame - tests/filename/filenametest.cpp
fix comparison in Normalize() test
[wxWidgets.git] / tests / filename / filenametest.cpp
CommitLineData
f095b1fc
VZ
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
8899b155 14#include "testprec.h"
f095b1fc
VZ
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
f7c69b90 21 #include "wx/utils.h"
f095b1fc
VZ
22#endif // WX_PRECOMP
23
24#include "wx/filename.h"
c08dd08b 25#include "wx/filefn.h"
f095b1fc 26
2264775b
VZ
27// ----------------------------------------------------------------------------
28// local functions
29// ----------------------------------------------------------------------------
30
31// define stream inserter for wxFileName to use it in CPPUNIT_ASSERT_EQUAL()
32inline std::ostream& operator<<(std::ostream& o, const wxFileName& fn)
33{
34 return o << fn.GetFullPath();
35}
36
f095b1fc
VZ
37// ----------------------------------------------------------------------------
38// test data
39// ----------------------------------------------------------------------------
40
5fed01a9 41static struct TestFileNameInfo
f095b1fc
VZ
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{
69858116 52 // the empty string
e7b54c07
VS
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 },
69858116 56
f095b1fc
VZ
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 },
bf7f7793 74
ea6319cb 75#if 0
c7099635
VZ
76 // NB: when using the wxFileName::GetLongPath() function on these two
77 // strings, the program will hang for several seconds blocking inside
78 // Win32 GetLongPathName() function
f095b1fc
VZ
79 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS },
80 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS },
ea6319cb 81#endif
f095b1fc 82
34841b21
VZ
83 // consecutive [back]slashes should be treated as single occurrences of
84 // them and not interpreted as share names if there is a volume name
85 { _T("c:\\aaa\\bbb\\ccc"), _T("c"), _T("\\aaa\\bbb"), _T("ccc"), _T(""), true, wxPATH_DOS },
9b9596de 86 { _T("c:\\\\aaa\\bbb\\ccc"), _T("c"), _T("\\\\aaa\\bbb"), _T("ccc"), _T(""), true, wxPATH_DOS },
bf7f7793 87
f095b1fc
VZ
88 // wxFileName support for Mac file names is broken currently
89#if 0
90 // Mac file names
91 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC },
92 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC },
93 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC },
94 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC },
95 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC },
96 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC },
97#endif // 0
98
ea6319cb 99#if 0
f095b1fc 100 // VMS file names
bf7f7793
RR
101 // NB: on Windows they have the same effect of the \\server\\ strings
102 // (see the note above)
f095b1fc 103 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS },
ea6319cb 104#endif
f095b1fc
VZ
105 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS },
106};
107
108// ----------------------------------------------------------------------------
109// test class
110// ----------------------------------------------------------------------------
111
112class FileNameTestCase : public CppUnit::TestCase
113{
114public:
115 FileNameTestCase() { }
116
117private:
118 CPPUNIT_TEST_SUITE( FileNameTestCase );
119 CPPUNIT_TEST( TestConstruction );
33366127 120 CPPUNIT_TEST( TestComparison );
f095b1fc 121 CPPUNIT_TEST( TestSplit );
4524a24b 122 CPPUNIT_TEST( TestSetPath );
c08dd08b 123 CPPUNIT_TEST( TestStrip );
bf7f7793 124 CPPUNIT_TEST( TestNormalize );
60c0dfe5
VZ
125#ifdef __WINDOWS__
126 CPPUNIT_TEST( TestShortLongPath );
127#endif // __WINDOWS__
f095b1fc
VZ
128 CPPUNIT_TEST_SUITE_END();
129
130 void TestConstruction();
33366127 131 void TestComparison();
f095b1fc
VZ
132 void TestSplit();
133 void TestSetPath();
c08dd08b 134 void TestStrip();
bf7f7793 135 void TestNormalize();
60c0dfe5
VZ
136#ifdef __WINDOWS__
137 void TestShortLongPath();
138#endif // __WINDOWS__
f095b1fc
VZ
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 {
5fed01a9 153 const TestFileNameInfo& fni = filenames[n];
f095b1fc
VZ
154
155 wxFileName fn(fni.fullname, fni.format);
156
9b9596de
VZ
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 wxChar *p = fni.fullname;
165 while ( *p == _T('\\') )
166 fullnameOrig += *p++;
167
168 // replace consecutive slashes with single ones in the rest
169 for ( wxChar chPrev = _T('\0'); *p; p++ )
170 {
171 if ( *p == _T('\\') && chPrev == _T('\\') )
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(_T("//"), _T("/"));
184
185
f095b1fc 186 wxString fullname = fn.GetFullPath(fni.format);
9b9596de 187 CPPUNIT_ASSERT_EQUAL( fullnameOrig, fullname );
f095b1fc 188
ab8576b4
VZ
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
2264775b
VZ
192 CPPUNIT_ASSERT_MESSAGE
193 (
b51abd73 194 (const char *)wxString::Format(_T("Normalize(%s) failed"), fni.fullname).mb_str(),
c2173965 195 fn.Normalize(wxPATH_NORM_ALL, _T("/foo/bar/baz"), fni.format)
2264775b 196 );
4c2deb19
VZ
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
2264775b 206 CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume,
4c2deb19
VZ
207 fni.name,
208 fni.ext,
2264775b 209 fni.format), fn );
4c2deb19 210 }
f095b1fc 211 }
69858116
VZ
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() );
f095b1fc
VZ
230}
231
33366127
VZ
232void FileNameTestCase::TestComparison()
233{
523cd68a
VZ
234 wxFileName fn1(wxT("/tmp/file1"));
235 wxFileName fn2(wxT("/tmp/dir2/../file2"));
236 fn1.Normalize();
237 fn2.Normalize();
2264775b 238 CPPUNIT_ASSERT_EQUAL(fn1.GetPath(), fn2.GetPath());
33366127
VZ
239}
240
f095b1fc
VZ
241void FileNameTestCase::TestSplit()
242{
243 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
244 {
5fed01a9 245 const TestFileNameInfo& fni = filenames[n];
f095b1fc
VZ
246 wxString volume, path, name, ext;
247 wxFileName::SplitPath(fni.fullname,
248 &volume, &path, &name, &ext, fni.format);
249
2264775b
VZ
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 );
f095b1fc 254 }
dfecbee5
VZ
255
256 // special case of empty extension
257 wxFileName fn(_T("foo."));
2264775b 258 CPPUNIT_ASSERT_EQUAL( wxString(_T("foo.")), fn.GetFullPath() );
f095b1fc
VZ
259}
260
4524a24b
VZ
261void FileNameTestCase::TestSetPath()
262{
263 wxFileName fn(_T("d:\\test\\foo.bar"), wxPATH_DOS);
67050b6d
VS
264 fn.SetPath(_T("c:\\temp"), wxPATH_DOS);
265 CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS)) );
4524a24b 266
67050b6d
VS
267 fn = wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX);
268 fn.SetPath(_T("/usr/local/bin"), wxPATH_UNIX);
269 CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX)) );
4524a24b
VZ
270}
271
bf7f7793
RR
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
8e083702 293 static const struct FileNameTest
bf7f7793 294 {
527587d3 295 const char *original;
bf7f7793 296 int flags;
527587d3 297 const char *expected;
c7099635 298 wxPathFormat fmt;
bf7f7793
RR
299 } tests[] =
300 {
301 // test wxPATH_NORM_ENV_VARS
302#ifdef __WXMSW__
c7099635 303 { "%ABCDEF%/g/h/i", wxPATH_NORM_ENV_VARS, "abcdef/g/h/i", wxPATH_UNIX },
bf7f7793 304#else
c7099635 305 { "$(ABCDEF)/g/h/i", wxPATH_NORM_ENV_VARS, "abcdef/g/h/i", wxPATH_UNIX },
bf7f7793
RR
306#endif
307
308 // test wxPATH_NORM_DOTS
c7099635 309 { "a/.././b/c/../../", wxPATH_NORM_DOTS, "", wxPATH_UNIX },
bf7f7793
RR
310
311 // test wxPATH_NORM_TILDE
312 // NB: do the tilde expansion also under Windows to test if it works there too
c7099635 313 { "/a/b/~", wxPATH_NORM_TILDE, "/a/b/~", wxPATH_UNIX },
527587d3
VZ
314 { "/~/a/b", wxPATH_NORM_TILDE, "HOME/a/b", wxPATH_UNIX },
315 { "~/a/b", wxPATH_NORM_TILDE, "HOME/a/b", wxPATH_UNIX },
c7099635
VZ
316
317 // test wxPATH_NORM_CASE
318 { "Foo", wxPATH_NORM_CASE, "Foo", wxPATH_UNIX },
319 { "Foo", wxPATH_NORM_CASE, "foo", wxPATH_DOS },
320 { "C:\\Program Files\\wx", wxPATH_NORM_CASE,
321 "c:\\program files\\wx", wxPATH_DOS },
322 { "C:/Program Files/wx", wxPATH_NORM_ALL | wxPATH_NORM_CASE,
323 "c:\\program files\\wx", wxPATH_DOS },
9a0c5c01
VZ
324 { "C:\\Users\\zeitlin", wxPATH_NORM_ALL | wxPATH_NORM_CASE,
325 "c:\\users\\zeitlin", wxPATH_DOS },
bf7f7793
RR
326
327 // test wxPATH_NORM_ABSOLUTE
527587d3
VZ
328 { "a/b/", wxPATH_NORM_ABSOLUTE, "CWD/a/b/", wxPATH_UNIX },
329 { "a/b/c.ext", wxPATH_NORM_ABSOLUTE, "CWD/a/b/c.ext", wxPATH_UNIX },
c7099635 330 { "/a", wxPATH_NORM_ABSOLUTE, "/a", wxPATH_UNIX },
bf7f7793
RR
331
332 // test giving no flags at all to Normalize()
c7099635
VZ
333 { "a/b/", 0, "a/b/", wxPATH_UNIX },
334 { "a/b/c.ext", 0, "a/b/c.ext", wxPATH_UNIX },
ea6319cb
VZ
335 { "/a", 0, "/a", wxPATH_UNIX },
336
337 // test handling dots without wxPATH_NORM_DOTS and wxPATH_NORM_ABSOLUTE
338 // for both existing and non-existent files (this is important under
339 // MSW where GetLongPathName() works only for the former)
340 { "./foo", wxPATH_NORM_LONG, "./foo", wxPATH_UNIX },
341 { "../foo", wxPATH_NORM_LONG, "../foo", wxPATH_UNIX },
342 { ".\\test.bkl", wxPATH_NORM_LONG, ".\\test.bkl", wxPATH_DOS },
343 { ".\\foo", wxPATH_NORM_LONG, ".\\foo", wxPATH_DOS },
344 { "..\\Makefile.in", wxPATH_NORM_LONG, "..\\Makefile.in", wxPATH_DOS },
345 { "..\\foo", wxPATH_NORM_LONG, "..\\foo", wxPATH_DOS },
346#ifdef __WXMSW__
347 { "..\\MKINST~1", wxPATH_NORM_LONG, "..\\mkinstalldirs", wxPATH_DOS },
348#endif
bf7f7793
RR
349 };
350
351 // set the env var ABCDEF
352 wxSetEnv(_T("ABCDEF"), _T("abcdef"));
353
2264775b 354 for ( size_t i = 0; i < WXSIZEOF(tests); i++ )
bf7f7793 355 {
c7099635
VZ
356 const FileNameTest& fnt = tests[i];
357 wxFileName fn(fnt.original, fnt.fmt);
bf7f7793
RR
358
359 // be sure this normalization does not fail
a779d809 360 WX_ASSERT_MESSAGE
2264775b 361 (
a779d809 362 ("#%d: Normalize(%s) failed", (int)i, fnt.original),
c7099635 363 fn.Normalize(fnt.flags, cwd, fnt.fmt)
2264775b 364 );
bf7f7793
RR
365
366 // compare result with expected string
527587d3
VZ
367 wxString expected(tests[i].expected);
368 expected.Replace(_T("HOME/"), home);
369 expected.Replace(_T("CWD/"), cwd);
a779d809
VZ
370 WX_ASSERT_EQUAL_MESSAGE
371 (
372 ("array element #%d", (int)i),
373 expected, fn.GetFullPath(fnt.fmt)
374 );
bf7f7793
RR
375 }
376}
377
ff3d9a35 378wxString wxTestStripExtension(wxString szFile)
c08dd08b
RN
379{
380 wxStripExtension(szFile);
381 return szFile;
382}
383
384void FileNameTestCase::TestStrip()
385{
386 //test a crash
2264775b 387 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T("")) );
c08dd08b
RN
388
389 //others
2264775b
VZ
390 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".")) );
391 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".wav")) );
392 CPPUNIT_ASSERT_EQUAL( wxString(_T("good")), wxTestStripExtension(_T("good.wav")) );
393 CPPUNIT_ASSERT_EQUAL( wxString(_T("good.wav")), wxTestStripExtension(_T("good.wav.wav")) );
ff3d9a35 394}
60c0dfe5
VZ
395
396#ifdef __WINDOWS__
397
398void FileNameTestCase::TestShortLongPath()
399{
3f302e75 400 wxFileName fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"));
60c0dfe5
VZ
401
402 // incredibly enough, GetLongPath() used to return different results during
403 // the first and subsequent runs, test for this
404 CPPUNIT_ASSERT_EQUAL( fn.GetLongPath(), fn.GetLongPath() );
405 CPPUNIT_ASSERT_EQUAL( fn.GetShortPath(), fn.GetShortPath() );
406}
407
408#endif // __WINDOWS__