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