]>
git.saurik.com Git - wxWidgets.git/blob - tests/filename/filenametest.cpp
e87c50a66b74e17c2247a380db42a31ee81e5aeb
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("/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
},
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
},
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
},
77 // wxFileName support for Mac file names is broken currently
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
},
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
},
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 class FileNameTestCase
: public CppUnit::TestCase
102 FileNameTestCase() { }
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
);
113 CPPUNIT_TEST( TestShortLongPath
);
114 #endif // __WINDOWS__
115 CPPUNIT_TEST_SUITE_END();
117 void TestConstruction();
118 void TestComparison();
122 void TestNormalize();
124 void TestShortLongPath();
125 #endif // __WINDOWS__
127 DECLARE_NO_COPY_CLASS(FileNameTestCase
)
130 // register in the unnamed registry so that these tests are run by default
131 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase
);
133 // also include in it's own registry so that these tests can be run alone
134 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase
, "FileNameTestCase" );
136 void FileNameTestCase::TestConstruction()
138 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
140 const FileNameInfo
& fni
= filenames
[n
];
142 wxFileName
fn(fni
.fullname
, fni
.format
);
144 wxString fullname
= fn
.GetFullPath(fni
.format
);
145 CPPUNIT_ASSERT_EQUAL( wxString(fni
.fullname
), fullname
);
147 CPPUNIT_ASSERT_MESSAGE
149 wxString::Format("Normalize(%s) failed", fni
.fullname
).c_str(),
150 fn
.Normalize(wxPATH_NORM_ALL
, _T(""), fni
.format
)
153 if ( *fni
.volume
&& *fni
.path
)
155 // check that specifying the volume separately or as part of the
156 // path doesn't make any difference
157 wxString pathWithVolume
= fni
.volume
;
158 pathWithVolume
+= wxFileName::GetVolumeSeparator(fni
.format
);
159 pathWithVolume
+= fni
.path
;
161 CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume
,
169 void FileNameTestCase::TestComparison()
171 wxFileName
fn1(wxT("/tmp/file1"));
172 wxFileName
fn2(wxT("/tmp/dir2/../file2"));
175 CPPUNIT_ASSERT_EQUAL(fn1
.GetPath(), fn2
.GetPath());
178 void FileNameTestCase::TestSplit()
180 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
182 const FileNameInfo
& fni
= filenames
[n
];
183 wxString volume
, path
, name
, ext
;
184 wxFileName::SplitPath(fni
.fullname
,
185 &volume
, &path
, &name
, &ext
, fni
.format
);
187 CPPUNIT_ASSERT_EQUAL( wxString(fni
.volume
), volume
);
188 CPPUNIT_ASSERT_EQUAL( wxString(fni
.path
), path
);
189 CPPUNIT_ASSERT_EQUAL( wxString(fni
.name
), name
);
190 CPPUNIT_ASSERT_EQUAL( wxString(fni
.ext
), ext
);
193 // special case of empty extension
194 wxFileName
fn(_T("foo."));
195 CPPUNIT_ASSERT_EQUAL( wxString(_T("foo.")), fn
.GetFullPath() );
198 void FileNameTestCase::TestSetPath()
200 wxFileName
fn(_T("d:\\test\\foo.bar"), wxPATH_DOS
);
201 fn
.SetPath(_T("c:\\temp"), wxPATH_DOS
);
202 CPPUNIT_ASSERT( fn
.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS
)) );
204 fn
= wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX
);
205 fn
.SetPath(_T("/usr/local/bin"), wxPATH_UNIX
);
206 CPPUNIT_ASSERT( fn
.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX
)) );
209 void FileNameTestCase::TestNormalize()
211 // prepare some data to be used later
212 wxString sep
= wxFileName::GetPathSeparator();
213 wxString cwd
= wxGetCwd();
214 wxString home
= wxGetUserHome();
216 cwd
.Replace(sep
, wxT("/"));
217 if (cwd
.Last() != wxT('/'))
219 home
.Replace(sep
, wxT("/"));
220 if (home
.Last() != wxT('/'))
223 // since we will always be testing paths using the wxPATH_UNIX
224 // format, we need to remove the volume, if present
225 if (home
.Contains(wxT(':')))
226 home
= home
.AfterFirst(wxT(':'));
227 if (cwd
.Contains(wxT(':')))
228 cwd
= cwd
.AfterFirst(wxT(':'));
230 static struct FileNameTest
232 const wxChar
*original
;
237 // test wxPATH_NORM_ENV_VARS
239 { wxT("%ABCDEF%/g/h/i"), wxPATH_NORM_ENV_VARS
, wxT("abcdef/g/h/i") },
241 { wxT("$(ABCDEF)/g/h/i"), wxPATH_NORM_ENV_VARS
, wxT("abcdef/g/h/i") },
244 // test wxPATH_NORM_DOTS
245 { wxT("a/.././b/c/../../"), wxPATH_NORM_DOTS
, wxT("") },
247 // test wxPATH_NORM_TILDE
248 // NB: do the tilde expansion also under Windows to test if it works there too
249 { wxT("/a/b/~"), wxPATH_NORM_TILDE
, wxT("/a/b/~") },
250 { wxT("/~/a/b"), wxPATH_NORM_TILDE
, home
+ wxT("a/b") },
251 { wxT("~/a/b"), wxPATH_NORM_TILDE
, home
+ wxT("a/b") },
253 // test wxPATH_NORM_ABSOLUTE
254 { wxT("a/b/"), wxPATH_NORM_ABSOLUTE
, cwd
+ wxT("a/b/") },
255 { wxT("a/b/c.ext"), wxPATH_NORM_ABSOLUTE
, cwd
+ wxT("a/b/c.ext") },
256 { wxT("/a"), wxPATH_NORM_ABSOLUTE
, wxT("/a") },
258 // test giving no flags at all to Normalize()
259 { wxT("a/b/"), 0, wxT("a/b/") },
260 { wxT("a/b/c.ext"), 0, wxT("a/b/c.ext") },
261 { wxT("/a"), 0, wxT("/a") }
264 // set the env var ABCDEF
265 wxSetEnv(_T("ABCDEF"), _T("abcdef"));
267 for ( size_t i
= 0; i
< WXSIZEOF(tests
); i
++ )
269 wxFileName
fn(tests
[i
].original
, wxPATH_UNIX
);
271 // be sure this normalization does not fail
272 CPPUNIT_ASSERT_MESSAGE
274 wxString::Format("Normalize(%s) failed", tests
[i
].original
).c_str(),
275 fn
.Normalize(tests
[i
].flags
, cwd
, wxPATH_UNIX
)
278 // compare result with expected string
279 CPPUNIT_ASSERT_EQUAL( tests
[i
].expected
, fn
.GetFullPath(wxPATH_UNIX
) );
283 wxString
wxTestStripExtension(wxString szFile
)
285 wxStripExtension(szFile
);
289 void FileNameTestCase::TestStrip()
292 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T("")) );
295 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".")) );
296 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T(".wav")) );
297 CPPUNIT_ASSERT_EQUAL( wxString(_T("good")), wxTestStripExtension(_T("good.wav")) );
298 CPPUNIT_ASSERT_EQUAL( wxString(_T("good.wav")), wxTestStripExtension(_T("good.wav.wav")) );
303 void FileNameTestCase::TestShortLongPath()
305 wxFileName
fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"));
307 // incredibly enough, GetLongPath() used to return different results during
308 // the first and subsequent runs, test for this
309 CPPUNIT_ASSERT_EQUAL( fn
.GetLongPath(), fn
.GetLongPath() );
310 CPPUNIT_ASSERT_EQUAL( fn
.GetShortPath(), fn
.GetShortPath() );
313 #endif // __WINDOWS__