]>
Commit | Line | Data |
---|---|---|
f095b1fc VZ |
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 | ||
8899b155 | 14 | #include "testprec.h" |
f095b1fc VZ |
15 | |
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
f7c69b90 | 21 | #include "wx/utils.h" |
f095b1fc VZ |
22 | #endif // WX_PRECOMP |
23 | ||
24 | #include "wx/filename.h" | |
c08dd08b | 25 | #include "wx/filefn.h" |
f095b1fc | 26 | |
2264775b VZ |
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 | ||
f095b1fc VZ |
37 | // ---------------------------------------------------------------------------- |
38 | // test data | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
5fed01a9 | 41 | static struct TestFileNameInfo |
f095b1fc VZ |
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 | { | |
69858116 | 52 | // the empty string |
e7b54c07 VS |
53 | { _T(""), _T(""), _T(""), _T(""), _T(""), false, wxPATH_UNIX }, |
54 | { _T(""), _T(""), _T(""), _T(""), _T(""), false, wxPATH_DOS }, | |
55 | { _T(""), _T(""), _T(""), _T(""), _T(""), false, wxPATH_VMS }, | |
69858116 | 56 | |
f095b1fc VZ |
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 }, | |
bf7f7793 | 74 | |
ea6319cb | 75 | #if 0 |
c7099635 VZ |
76 | // NB: when using the wxFileName::GetLongPath() function on these two |
77 | // strings, the program will hang for several seconds blocking inside | |
78 | // Win32 GetLongPathName() function | |
f095b1fc VZ |
79 | { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS }, |
80 | { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS }, | |
ea6319cb | 81 | #endif |
f095b1fc | 82 | |
34841b21 VZ |
83 | // consecutive [back]slashes should be treated as single occurrences of |
84 | // them and not interpreted as share names if there is a volume name | |
85 | { _T("c:\\aaa\\bbb\\ccc"), _T("c"), _T("\\aaa\\bbb"), _T("ccc"), _T(""), true, wxPATH_DOS }, | |
9b9596de | 86 | { _T("c:\\\\aaa\\bbb\\ccc"), _T("c"), _T("\\\\aaa\\bbb"), _T("ccc"), _T(""), true, wxPATH_DOS }, |
bf7f7793 | 87 | |
f095b1fc VZ |
88 | // wxFileName support for Mac file names is broken currently |
89 | #if 0 | |
90 | // Mac file names | |
91 | { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC }, | |
92 | { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC }, | |
93 | { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC }, | |
94 | { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC }, | |
95 | { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC }, | |
96 | { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC }, | |
97 | #endif // 0 | |
98 | ||
ea6319cb | 99 | #if 0 |
f095b1fc | 100 | // VMS file names |
bf7f7793 RR |
101 | // NB: on Windows they have the same effect of the \\server\\ strings |
102 | // (see the note above) | |
f095b1fc | 103 | { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS }, |
ea6319cb | 104 | #endif |
f095b1fc VZ |
105 | { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS }, |
106 | }; | |
107 | ||
108 | // ---------------------------------------------------------------------------- | |
109 | // test class | |
110 | // ---------------------------------------------------------------------------- | |
111 | ||
112 | class FileNameTestCase : public CppUnit::TestCase | |
113 | { | |
114 | public: | |
115 | FileNameTestCase() { } | |
116 | ||
117 | private: | |
118 | CPPUNIT_TEST_SUITE( FileNameTestCase ); | |
119 | CPPUNIT_TEST( TestConstruction ); | |
33366127 | 120 | CPPUNIT_TEST( TestComparison ); |
f095b1fc | 121 | CPPUNIT_TEST( TestSplit ); |
4524a24b | 122 | CPPUNIT_TEST( TestSetPath ); |
395f3aa8 | 123 | #if WXWIN_COMPATIBILITY_2_8 |
c08dd08b | 124 | CPPUNIT_TEST( TestStrip ); |
395f3aa8 | 125 | #endif |
bf7f7793 | 126 | CPPUNIT_TEST( TestNormalize ); |
395f3aa8 | 127 | CPPUNIT_TEST( TestReplace ); |
60c0dfe5 VZ |
128 | #ifdef __WINDOWS__ |
129 | CPPUNIT_TEST( TestShortLongPath ); | |
130 | #endif // __WINDOWS__ | |
f095b1fc VZ |
131 | CPPUNIT_TEST_SUITE_END(); |
132 | ||
133 | void TestConstruction(); | |
33366127 | 134 | void TestComparison(); |
f095b1fc VZ |
135 | void TestSplit(); |
136 | void TestSetPath(); | |
395f3aa8 | 137 | #if WXWIN_COMPATIBILITY_2_8 |
c08dd08b | 138 | void TestStrip(); |
395f3aa8 | 139 | #endif |
bf7f7793 | 140 | void TestNormalize(); |
395f3aa8 | 141 | void TestReplace(); |
60c0dfe5 VZ |
142 | #ifdef __WINDOWS__ |
143 | void TestShortLongPath(); | |
144 | #endif // __WINDOWS__ | |
f095b1fc VZ |
145 | |
146 | DECLARE_NO_COPY_CLASS(FileNameTestCase) | |
147 | }; | |
148 | ||
149 | // register in the unnamed registry so that these tests are run by default | |
150 | CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase ); | |
151 | ||
152 | // also include in it's own registry so that these tests can be run alone | |
153 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" ); | |
154 | ||
155 | void FileNameTestCase::TestConstruction() | |
156 | { | |
157 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
158 | { | |
5fed01a9 | 159 | const TestFileNameInfo& fni = filenames[n]; |
f095b1fc VZ |
160 | |
161 | wxFileName fn(fni.fullname, fni.format); | |
162 | ||
9b9596de VZ |
163 | // the original full name could contain consecutive [back]slashes, |
164 | // squeeze them except for the double backslash in the beginning in | |
165 | // Windows filenames where it has special meaning | |
166 | wxString fullnameOrig; | |
167 | if ( fni.format == wxPATH_DOS ) | |
168 | { | |
169 | // copy the backslashes at beginning unchanged | |
170 | const wxChar *p = fni.fullname; | |
171 | while ( *p == _T('\\') ) | |
172 | fullnameOrig += *p++; | |
173 | ||
174 | // replace consecutive slashes with single ones in the rest | |
175 | for ( wxChar chPrev = _T('\0'); *p; p++ ) | |
176 | { | |
177 | if ( *p == _T('\\') && chPrev == _T('\\') ) | |
178 | continue; | |
179 | ||
180 | chPrev = *p; | |
181 | fullnameOrig += chPrev; | |
182 | } | |
183 | } | |
184 | else // !wxPATH_DOS | |
185 | { | |
186 | fullnameOrig = fni.fullname; | |
187 | } | |
188 | ||
189 | fullnameOrig.Replace(_T("//"), _T("/")); | |
190 | ||
191 | ||
f095b1fc | 192 | wxString fullname = fn.GetFullPath(fni.format); |
9b9596de | 193 | CPPUNIT_ASSERT_EQUAL( fullnameOrig, fullname ); |
f095b1fc | 194 | |
ab8576b4 VZ |
195 | // notice that we use a dummy working directory to ensure that paths |
196 | // with "../.." in them could be normalized, otherwise this would fail | |
197 | // if the test is run from root directory or its direct subdirectory | |
2264775b VZ |
198 | CPPUNIT_ASSERT_MESSAGE |
199 | ( | |
b51abd73 | 200 | (const char *)wxString::Format(_T("Normalize(%s) failed"), fni.fullname).mb_str(), |
c2173965 | 201 | fn.Normalize(wxPATH_NORM_ALL, _T("/foo/bar/baz"), fni.format) |
2264775b | 202 | ); |
4c2deb19 VZ |
203 | |
204 | if ( *fni.volume && *fni.path ) | |
205 | { | |
206 | // check that specifying the volume separately or as part of the | |
207 | // path doesn't make any difference | |
208 | wxString pathWithVolume = fni.volume; | |
209 | pathWithVolume += wxFileName::GetVolumeSeparator(fni.format); | |
210 | pathWithVolume += fni.path; | |
211 | ||
2264775b | 212 | CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume, |
4c2deb19 VZ |
213 | fni.name, |
214 | fni.ext, | |
2264775b | 215 | fni.format), fn ); |
4c2deb19 | 216 | } |
f095b1fc | 217 | } |
69858116 VZ |
218 | |
219 | wxFileName fn; | |
220 | ||
221 | // empty strings | |
222 | fn.AssignDir(wxEmptyString); | |
223 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
224 | ||
225 | fn.Assign(wxEmptyString); | |
226 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
227 | ||
228 | fn.Assign(wxEmptyString, wxEmptyString); | |
229 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
230 | ||
231 | fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString); | |
232 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
233 | ||
234 | fn.Assign(wxEmptyString, wxEmptyString, wxEmptyString, wxEmptyString); | |
235 | CPPUNIT_ASSERT( !fn.IsOk() ); | |
f095b1fc VZ |
236 | } |
237 | ||
33366127 VZ |
238 | void FileNameTestCase::TestComparison() |
239 | { | |
523cd68a VZ |
240 | wxFileName fn1(wxT("/tmp/file1")); |
241 | wxFileName fn2(wxT("/tmp/dir2/../file2")); | |
242 | fn1.Normalize(); | |
243 | fn2.Normalize(); | |
2264775b | 244 | CPPUNIT_ASSERT_EQUAL(fn1.GetPath(), fn2.GetPath()); |
33366127 VZ |
245 | } |
246 | ||
f095b1fc VZ |
247 | void FileNameTestCase::TestSplit() |
248 | { | |
249 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
250 | { | |
5fed01a9 | 251 | const TestFileNameInfo& fni = filenames[n]; |
f095b1fc VZ |
252 | wxString volume, path, name, ext; |
253 | wxFileName::SplitPath(fni.fullname, | |
254 | &volume, &path, &name, &ext, fni.format); | |
255 | ||
2264775b VZ |
256 | CPPUNIT_ASSERT_EQUAL( wxString(fni.volume), volume ); |
257 | CPPUNIT_ASSERT_EQUAL( wxString(fni.path), path ); | |
258 | CPPUNIT_ASSERT_EQUAL( wxString(fni.name), name ); | |
259 | CPPUNIT_ASSERT_EQUAL( wxString(fni.ext), ext ); | |
f095b1fc | 260 | } |
dfecbee5 VZ |
261 | |
262 | // special case of empty extension | |
263 | wxFileName fn(_T("foo.")); | |
2264775b | 264 | CPPUNIT_ASSERT_EQUAL( wxString(_T("foo.")), fn.GetFullPath() ); |
f095b1fc VZ |
265 | } |
266 | ||
4524a24b VZ |
267 | void FileNameTestCase::TestSetPath() |
268 | { | |
269 | wxFileName fn(_T("d:\\test\\foo.bar"), wxPATH_DOS); | |
67050b6d VS |
270 | fn.SetPath(_T("c:\\temp"), wxPATH_DOS); |
271 | CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS)) ); | |
4524a24b | 272 | |
67050b6d VS |
273 | fn = wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX); |
274 | fn.SetPath(_T("/usr/local/bin"), wxPATH_UNIX); | |
275 | CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX)) ); | |
4524a24b VZ |
276 | } |
277 | ||
bf7f7793 RR |
278 | void FileNameTestCase::TestNormalize() |
279 | { | |
280 | // prepare some data to be used later | |
281 | wxString sep = wxFileName::GetPathSeparator(); | |
282 | wxString cwd = wxGetCwd(); | |
283 | wxString home = wxGetUserHome(); | |
284 | ||
285 | cwd.Replace(sep, wxT("/")); | |
286 | if (cwd.Last() != wxT('/')) | |
287 | cwd += wxT('/'); | |
288 | home.Replace(sep, wxT("/")); | |
289 | if (home.Last() != wxT('/')) | |
290 | home += wxT('/'); | |
291 | ||
292 | // since we will always be testing paths using the wxPATH_UNIX | |
293 | // format, we need to remove the volume, if present | |
294 | if (home.Contains(wxT(':'))) | |
295 | home = home.AfterFirst(wxT(':')); | |
296 | if (cwd.Contains(wxT(':'))) | |
297 | cwd = cwd.AfterFirst(wxT(':')); | |
298 | ||
8e083702 | 299 | static const struct FileNameTest |
bf7f7793 | 300 | { |
527587d3 | 301 | const char *original; |
bf7f7793 | 302 | int flags; |
527587d3 | 303 | const char *expected; |
c7099635 | 304 | wxPathFormat fmt; |
bf7f7793 RR |
305 | } tests[] = |
306 | { | |
307 | // test wxPATH_NORM_ENV_VARS | |
308 | #ifdef __WXMSW__ | |
c7099635 | 309 | { "%ABCDEF%/g/h/i", wxPATH_NORM_ENV_VARS, "abcdef/g/h/i", wxPATH_UNIX }, |
bf7f7793 | 310 | #else |
c7099635 | 311 | { "$(ABCDEF)/g/h/i", wxPATH_NORM_ENV_VARS, "abcdef/g/h/i", wxPATH_UNIX }, |
bf7f7793 RR |
312 | #endif |
313 | ||
314 | // test wxPATH_NORM_DOTS | |
c7099635 | 315 | { "a/.././b/c/../../", wxPATH_NORM_DOTS, "", wxPATH_UNIX }, |
bf7f7793 RR |
316 | |
317 | // test wxPATH_NORM_TILDE | |
318 | // NB: do the tilde expansion also under Windows to test if it works there too | |
c7099635 | 319 | { "/a/b/~", wxPATH_NORM_TILDE, "/a/b/~", wxPATH_UNIX }, |
527587d3 VZ |
320 | { "/~/a/b", wxPATH_NORM_TILDE, "HOME/a/b", wxPATH_UNIX }, |
321 | { "~/a/b", wxPATH_NORM_TILDE, "HOME/a/b", wxPATH_UNIX }, | |
c7099635 VZ |
322 | |
323 | // test wxPATH_NORM_CASE | |
324 | { "Foo", wxPATH_NORM_CASE, "Foo", wxPATH_UNIX }, | |
325 | { "Foo", wxPATH_NORM_CASE, "foo", wxPATH_DOS }, | |
326 | { "C:\\Program Files\\wx", wxPATH_NORM_CASE, | |
327 | "c:\\program files\\wx", wxPATH_DOS }, | |
328 | { "C:/Program Files/wx", wxPATH_NORM_ALL | wxPATH_NORM_CASE, | |
329 | "c:\\program files\\wx", wxPATH_DOS }, | |
9a0c5c01 VZ |
330 | { "C:\\Users\\zeitlin", wxPATH_NORM_ALL | wxPATH_NORM_CASE, |
331 | "c:\\users\\zeitlin", wxPATH_DOS }, | |
bf7f7793 RR |
332 | |
333 | // test wxPATH_NORM_ABSOLUTE | |
527587d3 VZ |
334 | { "a/b/", wxPATH_NORM_ABSOLUTE, "CWD/a/b/", wxPATH_UNIX }, |
335 | { "a/b/c.ext", wxPATH_NORM_ABSOLUTE, "CWD/a/b/c.ext", wxPATH_UNIX }, | |
c7099635 | 336 | { "/a", wxPATH_NORM_ABSOLUTE, "/a", wxPATH_UNIX }, |
bf7f7793 RR |
337 | |
338 | // test giving no flags at all to Normalize() | |
c7099635 VZ |
339 | { "a/b/", 0, "a/b/", wxPATH_UNIX }, |
340 | { "a/b/c.ext", 0, "a/b/c.ext", wxPATH_UNIX }, | |
ea6319cb VZ |
341 | { "/a", 0, "/a", wxPATH_UNIX }, |
342 | ||
343 | // test handling dots without wxPATH_NORM_DOTS and wxPATH_NORM_ABSOLUTE | |
344 | // for both existing and non-existent files (this is important under | |
345 | // MSW where GetLongPathName() works only for the former) | |
346 | { "./foo", wxPATH_NORM_LONG, "./foo", wxPATH_UNIX }, | |
347 | { "../foo", wxPATH_NORM_LONG, "../foo", wxPATH_UNIX }, | |
348 | { ".\\test.bkl", wxPATH_NORM_LONG, ".\\test.bkl", wxPATH_DOS }, | |
349 | { ".\\foo", wxPATH_NORM_LONG, ".\\foo", wxPATH_DOS }, | |
350 | { "..\\Makefile.in", wxPATH_NORM_LONG, "..\\Makefile.in", wxPATH_DOS }, | |
351 | { "..\\foo", wxPATH_NORM_LONG, "..\\foo", wxPATH_DOS }, | |
352 | #ifdef __WXMSW__ | |
353 | { "..\\MKINST~1", wxPATH_NORM_LONG, "..\\mkinstalldirs", wxPATH_DOS }, | |
354 | #endif | |
bf7f7793 RR |
355 | }; |
356 | ||
357 | // set the env var ABCDEF | |
358 | wxSetEnv(_T("ABCDEF"), _T("abcdef")); | |
359 | ||
2264775b | 360 | for ( size_t i = 0; i < WXSIZEOF(tests); i++ ) |
bf7f7793 | 361 | { |
c7099635 VZ |
362 | const FileNameTest& fnt = tests[i]; |
363 | wxFileName fn(fnt.original, fnt.fmt); | |
bf7f7793 RR |
364 | |
365 | // be sure this normalization does not fail | |
a779d809 | 366 | WX_ASSERT_MESSAGE |
2264775b | 367 | ( |
a779d809 | 368 | ("#%d: Normalize(%s) failed", (int)i, fnt.original), |
c7099635 | 369 | fn.Normalize(fnt.flags, cwd, fnt.fmt) |
2264775b | 370 | ); |
bf7f7793 RR |
371 | |
372 | // compare result with expected string | |
527587d3 VZ |
373 | wxString expected(tests[i].expected); |
374 | expected.Replace(_T("HOME/"), home); | |
375 | expected.Replace(_T("CWD/"), cwd); | |
a779d809 VZ |
376 | WX_ASSERT_EQUAL_MESSAGE |
377 | ( | |
378 | ("array element #%d", (int)i), | |
379 | expected, fn.GetFullPath(fnt.fmt) | |
380 | ); | |
bf7f7793 RR |
381 | } |
382 | } | |
383 | ||
395f3aa8 FM |
384 | void FileNameTestCase::TestReplace() |
385 | { | |
386 | static const struct FileNameTest | |
387 | { | |
388 | const char *original; | |
389 | const char *env_contents; | |
390 | const char *replace_fmtstring; | |
391 | const char *expected; | |
392 | wxPathFormat fmt; | |
393 | } tests[] = | |
394 | { | |
395 | { "/usr/a/strange path/lib/someFile.ext", "/usr/a/strange path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX }, | |
396 | { "/usr/a/path/lib/someFile.ext", "/usr/a/path", "$%s", "$TEST_VAR/lib/someFile.ext", wxPATH_UNIX }, | |
397 | { "/usr/a/path/lib/someFile", "/usr/a/path/", "$%s", "$TEST_VARlib/someFile", wxPATH_UNIX }, | |
398 | { "/usr/a/path/lib/", "/usr/a/path/", "$(%s)", "$(TEST_VAR)lib/", wxPATH_UNIX }, | |
399 | { "/usr/a/path/lib/", "/usr/a/path/", "${{%s}}", "${{TEST_VAR}}lib/", wxPATH_UNIX }, | |
400 | { "/usr/a/path/lib/", "/usr/a/path/", "%s", "TEST_VARlib/", wxPATH_UNIX }, | |
401 | { "/usr/a/path/lib/", "/usr/a/path/", "%s//", "TEST_VAR/lib/", wxPATH_UNIX }, | |
402 | // note: empty directory components are automatically removed by wxFileName thus | |
403 | // using // in the replace format string has no effect | |
404 | ||
405 | { "/usr/../a/path/lib/", "/usr/a/path/", "%s", "/usr/../a/path/lib/", wxPATH_UNIX }, | |
406 | { "/usr/a/path/usr/usr", "/usr", "%s", "TEST_VAR/a/pathTEST_VAR/usr", wxPATH_UNIX }, | |
407 | { "/usr/a/path/usr/usr", "/usr", "$%s", "$TEST_VAR/a/path$TEST_VAR/usr", wxPATH_UNIX }, | |
408 | { "/a/b/c/d", "a/", "%s", "/TEST_VARb/c/d", wxPATH_UNIX }, | |
409 | ||
410 | { "C:\\A\\Strange Path\\lib\\someFile", "C:\\A\\Strange Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN }, | |
411 | { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "%%%s%%", "%TEST_VAR%\\lib\\someFile", wxPATH_WIN }, | |
412 | { "C:\\A\\Path\\lib\\someFile", "C:\\A\\Path", "$(%s)", "$(TEST_VAR)\\lib\\someFile", wxPATH_WIN } | |
413 | }; | |
414 | ||
415 | for ( size_t i = 0; i < WXSIZEOF(tests); i++ ) | |
416 | { | |
417 | const FileNameTest& fnt = tests[i]; | |
418 | wxFileName fn(fnt.original, fnt.fmt); | |
419 | ||
420 | // set the environment variable | |
421 | wxSetEnv(_T("TEST_VAR"), fnt.env_contents); | |
422 | ||
423 | // be sure this ReplaceEnvVariable does not fail | |
424 | WX_ASSERT_MESSAGE | |
425 | ( | |
426 | ("#%d: ReplaceEnvVariable(%s) failed", (int)i, fnt.replace_fmtstring), | |
427 | fn.ReplaceEnvVariable("TEST_VAR", fnt.replace_fmtstring, fnt.fmt) | |
428 | ); | |
429 | ||
430 | // compare result with expected string | |
431 | wxString expected(fnt.expected); | |
432 | WX_ASSERT_EQUAL_MESSAGE | |
433 | ( | |
434 | ("array element #%d", (int)i), | |
435 | expected, fn.GetFullPath(fnt.fmt) | |
436 | ); | |
437 | } | |
438 | ||
439 | // now test ReplaceHomeDir | |
440 | ||
441 | wxFileName fn = wxFileName::DirName(wxGetHomeDir()); | |
442 | fn.AppendDir("test1"); | |
443 | fn.AppendDir("test2"); | |
444 | fn.AppendDir("test3"); | |
445 | fn.SetName("some file"); | |
446 | ||
447 | WX_ASSERT_MESSAGE | |
448 | ( | |
449 | ("ReplaceHomeDir(%s) failed", fn.GetFullPath()), | |
450 | fn.ReplaceHomeDir() | |
451 | ); | |
452 | ||
453 | CPPUNIT_ASSERT_EQUAL( wxString(_T("~/test1/test2/test3/some file")), | |
454 | fn.GetFullPath(wxPATH_UNIX) ); | |
455 | } | |
456 | ||
457 | #if WXWIN_COMPATIBILITY_2_8 | |
b3f76d40 VZ |
458 | |
459 | #ifdef __VISUALC__ | |
460 | // disable warning about using deprecated wxStripExtension() | |
461 | #pragma warning(disable:4996) | |
462 | #endif | |
463 | ||
ff3d9a35 | 464 | wxString wxTestStripExtension(wxString szFile) |
c08dd08b RN |
465 | { |
466 | wxStripExtension(szFile); | |
467 | return szFile; | |
468 | } | |
469 | ||
b3f76d40 VZ |
470 | #ifdef __VISUALC__ |
471 | #pragma warning(default:4996) | |
472 | #endif | |
473 | ||
c08dd08b RN |
474 | void FileNameTestCase::TestStrip() |
475 | { | |
476 | //test a crash | |
2264775b | 477 | CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T("")) ); |
c08dd08b RN |
478 | |
479 | //others | |
2264775b VZ |
480 | CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".")) ); |
481 | CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".wav")) ); | |
482 | CPPUNIT_ASSERT_EQUAL( wxString(_T("good")), wxTestStripExtension(_T("good.wav")) ); | |
483 | CPPUNIT_ASSERT_EQUAL( wxString(_T("good.wav")), wxTestStripExtension(_T("good.wav.wav")) ); | |
ff3d9a35 | 484 | } |
b3f76d40 | 485 | #endif // WXWIN_COMPATIBILITY_2_8 |
60c0dfe5 VZ |
486 | |
487 | #ifdef __WINDOWS__ | |
488 | ||
489 | void FileNameTestCase::TestShortLongPath() | |
490 | { | |
3f302e75 | 491 | wxFileName fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe")); |
60c0dfe5 VZ |
492 | |
493 | // incredibly enough, GetLongPath() used to return different results during | |
494 | // the first and subsequent runs, test for this | |
495 | CPPUNIT_ASSERT_EQUAL( fn.GetLongPath(), fn.GetLongPath() ); | |
496 | CPPUNIT_ASSERT_EQUAL( fn.GetShortPath(), fn.GetShortPath() ); | |
497 | } | |
498 | ||
499 | #endif // __WINDOWS__ |