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