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