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