]>
Commit | Line | Data |
---|---|---|
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 | // notice that we use a dummy working directory to ensure that paths | |
148 | // with "../.." in them could be normalized, otherwise this would fail | |
149 | // if the test is run from root directory or its direct subdirectory | |
150 | CPPUNIT_ASSERT_MESSAGE | |
151 | ( | |
152 | wxString::Format("Normalize(%s) failed", fni.fullname).c_str(), | |
153 | fn.Normalize(wxPATH_NORM_ALL, _T("/foo/bar/baz"), fni.format) | |
154 | ); | |
155 | ||
156 | if ( *fni.volume && *fni.path ) | |
157 | { | |
158 | // check that specifying the volume separately or as part of the | |
159 | // path doesn't make any difference | |
160 | wxString pathWithVolume = fni.volume; | |
161 | pathWithVolume += wxFileName::GetVolumeSeparator(fni.format); | |
162 | pathWithVolume += fni.path; | |
163 | ||
164 | CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume, | |
165 | fni.name, | |
166 | fni.ext, | |
167 | fni.format), fn ); | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | void FileNameTestCase::TestComparison() | |
173 | { | |
174 | wxFileName fn1(wxT("/tmp/file1")); | |
175 | wxFileName fn2(wxT("/tmp/dir2/../file2")); | |
176 | fn1.Normalize(); | |
177 | fn2.Normalize(); | |
178 | CPPUNIT_ASSERT_EQUAL(fn1.GetPath(), fn2.GetPath()); | |
179 | } | |
180 | ||
181 | void FileNameTestCase::TestSplit() | |
182 | { | |
183 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
184 | { | |
185 | const FileNameInfo& fni = filenames[n]; | |
186 | wxString volume, path, name, ext; | |
187 | wxFileName::SplitPath(fni.fullname, | |
188 | &volume, &path, &name, &ext, fni.format); | |
189 | ||
190 | CPPUNIT_ASSERT_EQUAL( wxString(fni.volume), volume ); | |
191 | CPPUNIT_ASSERT_EQUAL( wxString(fni.path), path ); | |
192 | CPPUNIT_ASSERT_EQUAL( wxString(fni.name), name ); | |
193 | CPPUNIT_ASSERT_EQUAL( wxString(fni.ext), ext ); | |
194 | } | |
195 | ||
196 | // special case of empty extension | |
197 | wxFileName fn(_T("foo.")); | |
198 | CPPUNIT_ASSERT_EQUAL( wxString(_T("foo.")), fn.GetFullPath() ); | |
199 | } | |
200 | ||
201 | void FileNameTestCase::TestSetPath() | |
202 | { | |
203 | wxFileName fn(_T("d:\\test\\foo.bar"), wxPATH_DOS); | |
204 | fn.SetPath(_T("c:\\temp"), wxPATH_DOS); | |
205 | CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS)) ); | |
206 | ||
207 | fn = wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX); | |
208 | fn.SetPath(_T("/usr/local/bin"), wxPATH_UNIX); | |
209 | CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX)) ); | |
210 | } | |
211 | ||
212 | void FileNameTestCase::TestNormalize() | |
213 | { | |
214 | // prepare some data to be used later | |
215 | wxString sep = wxFileName::GetPathSeparator(); | |
216 | wxString cwd = wxGetCwd(); | |
217 | wxString home = wxGetUserHome(); | |
218 | ||
219 | cwd.Replace(sep, wxT("/")); | |
220 | if (cwd.Last() != wxT('/')) | |
221 | cwd += wxT('/'); | |
222 | home.Replace(sep, wxT("/")); | |
223 | if (home.Last() != wxT('/')) | |
224 | home += wxT('/'); | |
225 | ||
226 | // since we will always be testing paths using the wxPATH_UNIX | |
227 | // format, we need to remove the volume, if present | |
228 | if (home.Contains(wxT(':'))) | |
229 | home = home.AfterFirst(wxT(':')); | |
230 | if (cwd.Contains(wxT(':'))) | |
231 | cwd = cwd.AfterFirst(wxT(':')); | |
232 | ||
233 | static struct FileNameTest | |
234 | { | |
235 | const wxChar *original; | |
236 | int flags; | |
237 | wxString expected; | |
238 | } tests[] = | |
239 | { | |
240 | // test wxPATH_NORM_ENV_VARS | |
241 | #ifdef __WXMSW__ | |
242 | { wxT("%ABCDEF%/g/h/i"), wxPATH_NORM_ENV_VARS, wxT("abcdef/g/h/i") }, | |
243 | #else | |
244 | { wxT("$(ABCDEF)/g/h/i"), wxPATH_NORM_ENV_VARS, wxT("abcdef/g/h/i") }, | |
245 | #endif | |
246 | ||
247 | // test wxPATH_NORM_DOTS | |
248 | { wxT("a/.././b/c/../../"), wxPATH_NORM_DOTS, wxT("") }, | |
249 | ||
250 | // test wxPATH_NORM_TILDE | |
251 | // NB: do the tilde expansion also under Windows to test if it works there too | |
252 | { wxT("/a/b/~"), wxPATH_NORM_TILDE, wxT("/a/b/~") }, | |
253 | { wxT("/~/a/b"), wxPATH_NORM_TILDE, home + wxT("a/b") }, | |
254 | { wxT("~/a/b"), wxPATH_NORM_TILDE, home + wxT("a/b") }, | |
255 | ||
256 | // test wxPATH_NORM_ABSOLUTE | |
257 | { wxT("a/b/"), wxPATH_NORM_ABSOLUTE, cwd + wxT("a/b/") }, | |
258 | { wxT("a/b/c.ext"), wxPATH_NORM_ABSOLUTE, cwd + wxT("a/b/c.ext") }, | |
259 | { wxT("/a"), wxPATH_NORM_ABSOLUTE, wxT("/a") }, | |
260 | ||
261 | // test giving no flags at all to Normalize() | |
262 | { wxT("a/b/"), 0, wxT("a/b/") }, | |
263 | { wxT("a/b/c.ext"), 0, wxT("a/b/c.ext") }, | |
264 | { wxT("/a"), 0, wxT("/a") } | |
265 | }; | |
266 | ||
267 | // set the env var ABCDEF | |
268 | wxSetEnv(_T("ABCDEF"), _T("abcdef")); | |
269 | ||
270 | for ( size_t i = 0; i < WXSIZEOF(tests); i++ ) | |
271 | { | |
272 | wxFileName fn(tests[i].original, wxPATH_UNIX); | |
273 | ||
274 | // be sure this normalization does not fail | |
275 | CPPUNIT_ASSERT_MESSAGE | |
276 | ( | |
277 | wxString::Format("Normalize(%s) failed", tests[i].original).c_str(), | |
278 | fn.Normalize(tests[i].flags, cwd, wxPATH_UNIX) | |
279 | ); | |
280 | ||
281 | // compare result with expected string | |
282 | CPPUNIT_ASSERT_EQUAL( tests[i].expected, fn.GetFullPath(wxPATH_UNIX) ); | |
283 | } | |
284 | } | |
285 | ||
286 | wxString wxTestStripExtension(wxString szFile) | |
287 | { | |
288 | wxStripExtension(szFile); | |
289 | return szFile; | |
290 | } | |
291 | ||
292 | void FileNameTestCase::TestStrip() | |
293 | { | |
294 | //test a crash | |
295 | CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T("")) ); | |
296 | ||
297 | //others | |
298 | CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".")) ); | |
299 | CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".wav")) ); | |
300 | CPPUNIT_ASSERT_EQUAL( wxString(_T("good")), wxTestStripExtension(_T("good.wav")) ); | |
301 | CPPUNIT_ASSERT_EQUAL( wxString(_T("good.wav")), wxTestStripExtension(_T("good.wav.wav")) ); | |
302 | } | |
303 | ||
304 | #ifdef __WINDOWS__ | |
305 | ||
306 | void FileNameTestCase::TestShortLongPath() | |
307 | { | |
308 | wxFileName fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe")); | |
309 | ||
310 | // incredibly enough, GetLongPath() used to return different results during | |
311 | // the first and subsequent runs, test for this | |
312 | CPPUNIT_ASSERT_EQUAL( fn.GetLongPath(), fn.GetLongPath() ); | |
313 | CPPUNIT_ASSERT_EQUAL( fn.GetShortPath(), fn.GetShortPath() ); | |
314 | } | |
315 | ||
316 | #endif // __WINDOWS__ |