1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/filename/filename.cpp
3 // Purpose: wxFileName unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2004 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/filename.h"
25 #include "wx/filefn.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 // define stream inserter for wxFileName to use it in CPPUNIT_ASSERT_EQUAL()
32 inline std
::ostream
& operator<<(std
::ostream
& o
, const wxFileName
& fn
)
34 return o
<< fn
.GetFullPath();
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 static struct FileNameInfo
43 const wxChar
*fullname
;
53 { _T(""), _(""), _(""), _(""), _(""), false, wxPATH_UNIX
},
54 { _T(""), _(""), _(""), _(""), _(""), false, wxPATH_DOS
},
55 { _T(""), _(""), _(""), _(""), _(""), false, wxPATH_VMS
},
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
},
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
},
75 // NB: when using the wxFileName::GetLongPath() function on these two strings,
76 // the program will hang various seconds. All those time is taken by the
77 // call to the win32 API GetLongPathName()...
78 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
79 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
82 // wxFileName support for Mac file names is broken currently
85 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC
},
86 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC
},
87 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC
},
88 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC
},
89 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
90 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
94 // NB: on Windows they have the same effect of the \\server\\ strings
95 // (see the note above)
96 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS
},
97 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS
},
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 class FileNameTestCase
: public CppUnit
::TestCase
107 FileNameTestCase() { }
110 CPPUNIT_TEST_SUITE( FileNameTestCase
);
111 CPPUNIT_TEST( TestConstruction
);
112 CPPUNIT_TEST( TestComparison
);
113 CPPUNIT_TEST( TestSplit
);
114 CPPUNIT_TEST( TestSetPath
);
115 CPPUNIT_TEST( TestStrip
);
116 CPPUNIT_TEST( TestNormalize
);
118 CPPUNIT_TEST( TestShortLongPath
);
119 #endif // __WINDOWS__
120 CPPUNIT_TEST_SUITE_END();
122 void TestConstruction();
123 void TestComparison();
127 void TestNormalize();
129 void TestShortLongPath();
130 #endif // __WINDOWS__
132 DECLARE_NO_COPY_CLASS(FileNameTestCase
)
135 // register in the unnamed registry so that these tests are run by default
136 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase
);
138 // also include in it's own registry so that these tests can be run alone
139 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase
, "FileNameTestCase" );
141 void FileNameTestCase
::TestConstruction()
143 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
145 const FileNameInfo
& fni
= filenames
[n
];
147 wxFileName
fn(fni
.fullname
, fni
.format
);
149 wxString fullname
= fn
.GetFullPath(fni
.format
);
150 CPPUNIT_ASSERT_EQUAL( wxString(fni
.fullname
), fullname
);
152 // notice that we use a dummy working directory to ensure that paths
153 // with "../.." in them could be normalized, otherwise this would fail
154 // if the test is run from root directory or its direct subdirectory
155 CPPUNIT_ASSERT_MESSAGE
157 (const char *)wxString
::Format(_T("Normalize(%s) failed"), fni
.fullname
).mb_str(),
158 fn
.Normalize(wxPATH_NORM_ALL
, _T("/foo/bar/baz"), fni
.format
)
161 if ( *fni
.volume
&& *fni
.path
)
163 // check that specifying the volume separately or as part of the
164 // path doesn't make any difference
165 wxString pathWithVolume
= fni
.volume
;
166 pathWithVolume
+= wxFileName
::GetVolumeSeparator(fni
.format
);
167 pathWithVolume
+= fni
.path
;
169 CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume
,
179 fn
.AssignDir(wxEmptyString
);
180 CPPUNIT_ASSERT( !fn
.IsOk() );
182 fn
.Assign(wxEmptyString
);
183 CPPUNIT_ASSERT( !fn
.IsOk() );
185 fn
.Assign(wxEmptyString
, wxEmptyString
);
186 CPPUNIT_ASSERT( !fn
.IsOk() );
188 fn
.Assign(wxEmptyString
, wxEmptyString
, wxEmptyString
);
189 CPPUNIT_ASSERT( !fn
.IsOk() );
191 fn
.Assign(wxEmptyString
, wxEmptyString
, wxEmptyString
, wxEmptyString
);
192 CPPUNIT_ASSERT( !fn
.IsOk() );
195 void FileNameTestCase
::TestComparison()
197 wxFileName
fn1(wxT("/tmp/file1"));
198 wxFileName
fn2(wxT("/tmp/dir2/../file2"));
201 CPPUNIT_ASSERT_EQUAL(fn1
.GetPath(), fn2
.GetPath());
204 void FileNameTestCase
::TestSplit()
206 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
208 const FileNameInfo
& fni
= filenames
[n
];
209 wxString volume
, path
, name
, ext
;
210 wxFileName
::SplitPath(fni
.fullname
,
211 &volume
, &path
, &name
, &ext
, fni
.format
);
213 CPPUNIT_ASSERT_EQUAL( wxString(fni
.volume
), volume
);
214 CPPUNIT_ASSERT_EQUAL( wxString(fni
.path
), path
);
215 CPPUNIT_ASSERT_EQUAL( wxString(fni
.name
), name
);
216 CPPUNIT_ASSERT_EQUAL( wxString(fni
.ext
), ext
);
219 // special case of empty extension
220 wxFileName
fn(_T("foo."));
221 CPPUNIT_ASSERT_EQUAL( wxString(_T("foo.")), fn
.GetFullPath() );
224 void FileNameTestCase
::TestSetPath()
226 wxFileName
fn(_T("d:\\test\\foo.bar"), wxPATH_DOS
);
227 fn
.SetPath(_T("c:\\temp"), wxPATH_DOS
);
228 CPPUNIT_ASSERT( fn
.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS
)) );
230 fn
= wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX
);
231 fn
.SetPath(_T("/usr/local/bin"), wxPATH_UNIX
);
232 CPPUNIT_ASSERT( fn
.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX
)) );
235 void FileNameTestCase
::TestNormalize()
237 // prepare some data to be used later
238 wxString sep
= wxFileName
::GetPathSeparator();
239 wxString cwd
= wxGetCwd();
240 wxString home
= wxGetUserHome();
242 cwd
.Replace(sep
, wxT("/"));
243 if (cwd
.Last() != wxT('/'))
245 home
.Replace(sep
, wxT("/"));
246 if (home
.Last() != wxT('/'))
249 // since we will always be testing paths using the wxPATH_UNIX
250 // format, we need to remove the volume, if present
251 if (home
.Contains(wxT(':')))
252 home
= home
.AfterFirst(wxT(':'));
253 if (cwd
.Contains(wxT(':')))
254 cwd
= cwd
.AfterFirst(wxT(':'));
256 static struct FileNameTest
258 const wxChar
*original
;
263 // test wxPATH_NORM_ENV_VARS
265 { wxT("%ABCDEF%/g/h/i"), wxPATH_NORM_ENV_VARS
, wxT("abcdef/g/h/i") },
267 { wxT("$(ABCDEF)/g/h/i"), wxPATH_NORM_ENV_VARS
, wxT("abcdef/g/h/i") },
270 // test wxPATH_NORM_DOTS
271 { wxT("a/.././b/c/../../"), wxPATH_NORM_DOTS
, wxT("") },
273 // test wxPATH_NORM_TILDE
274 // NB: do the tilde expansion also under Windows to test if it works there too
275 { wxT("/a/b/~"), wxPATH_NORM_TILDE
, wxT("/a/b/~") },
276 { wxT("/~/a/b"), wxPATH_NORM_TILDE
, home
+ wxT("a/b") },
277 { wxT("~/a/b"), wxPATH_NORM_TILDE
, home
+ wxT("a/b") },
279 // test wxPATH_NORM_ABSOLUTE
280 { wxT("a/b/"), wxPATH_NORM_ABSOLUTE
, cwd
+ wxT("a/b/") },
281 { wxT("a/b/c.ext"), wxPATH_NORM_ABSOLUTE
, cwd
+ wxT("a/b/c.ext") },
282 { wxT("/a"), wxPATH_NORM_ABSOLUTE
, wxT("/a") },
284 // test giving no flags at all to Normalize()
285 { wxT("a/b/"), 0, wxT("a/b/") },
286 { wxT("a/b/c.ext"), 0, wxT("a/b/c.ext") },
287 { wxT("/a"), 0, wxT("/a") }
290 // set the env var ABCDEF
291 wxSetEnv(_T("ABCDEF"), _T("abcdef"));
293 for ( size_t i
= 0; i
< WXSIZEOF(tests
); i
++ )
295 wxFileName
fn(tests
[i
].original
, wxPATH_UNIX
);
297 // be sure this normalization does not fail
298 CPPUNIT_ASSERT_MESSAGE
300 (const char *)wxString
::Format(_T("Normalize(%s) failed"), tests
[i
].original
).mb_str(),
301 fn
.Normalize(tests
[i
].flags
, cwd
, wxPATH_UNIX
)
304 // compare result with expected string
305 CPPUNIT_ASSERT_EQUAL( tests
[i
].expected
, fn
.GetFullPath(wxPATH_UNIX
) );
309 wxString
wxTestStripExtension(wxString szFile
)
311 wxStripExtension(szFile
);
315 void FileNameTestCase
::TestStrip()
318 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T("")) );
321 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".")) );
322 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".wav")) );
323 CPPUNIT_ASSERT_EQUAL( wxString(_T("good")), wxTestStripExtension(_T("good.wav")) );
324 CPPUNIT_ASSERT_EQUAL( wxString(_T("good.wav")), wxTestStripExtension(_T("good.wav.wav")) );
329 void FileNameTestCase
::TestShortLongPath()
331 wxFileName
fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"));
333 // incredibly enough, GetLongPath() used to return different results during
334 // the first and subsequent runs, test for this
335 CPPUNIT_ASSERT_EQUAL( fn
.GetLongPath(), fn
.GetLongPath() );
336 CPPUNIT_ASSERT_EQUAL( fn
.GetShortPath(), fn
.GetShortPath() );
339 #endif // __WINDOWS__