]>
git.saurik.com Git - wxWidgets.git/blob - tests/filename/filenametest.cpp
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 // 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
152 wxString::Format("Normalize(%s) failed", fni
.fullname
).c_str(),
153 fn
.Normalize(wxPATH_NORM_ALL
, _T("/foo/bar/baz"), fni
.format
)
156 if ( *fni
.volume
&& *fni
.path
)
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
;
164 CPPUNIT_ASSERT_EQUAL( wxFileName(pathWithVolume
,
172 void FileNameTestCase::TestComparison()
174 wxFileName
fn1(wxT("/tmp/file1"));
175 wxFileName
fn2(wxT("/tmp/dir2/../file2"));
178 CPPUNIT_ASSERT_EQUAL(fn1
.GetPath(), fn2
.GetPath());
181 void FileNameTestCase::TestSplit()
183 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
185 const FileNameInfo
& fni
= filenames
[n
];
186 wxString volume
, path
, name
, ext
;
187 wxFileName::SplitPath(fni
.fullname
,
188 &volume
, &path
, &name
, &ext
, fni
.format
);
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
);
196 // special case of empty extension
197 wxFileName
fn(_T("foo."));
198 CPPUNIT_ASSERT_EQUAL( wxString(_T("foo.")), fn
.GetFullPath() );
201 void FileNameTestCase::TestSetPath()
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
)) );
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
)) );
212 void FileNameTestCase::TestNormalize()
214 // prepare some data to be used later
215 wxString sep
= wxFileName::GetPathSeparator();
216 wxString cwd
= wxGetCwd();
217 wxString home
= wxGetUserHome();
219 cwd
.Replace(sep
, wxT("/"));
220 if (cwd
.Last() != wxT('/'))
222 home
.Replace(sep
, wxT("/"));
223 if (home
.Last() != wxT('/'))
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(':'));
233 static struct FileNameTest
235 const wxChar
*original
;
240 // test wxPATH_NORM_ENV_VARS
242 { wxT("%ABCDEF%/g/h/i"), wxPATH_NORM_ENV_VARS
, wxT("abcdef/g/h/i") },
244 { wxT("$(ABCDEF)/g/h/i"), wxPATH_NORM_ENV_VARS
, wxT("abcdef/g/h/i") },
247 // test wxPATH_NORM_DOTS
248 { wxT("a/.././b/c/../../"), wxPATH_NORM_DOTS
, wxT("") },
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") },
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") },
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") }
267 // set the env var ABCDEF
268 wxSetEnv(_T("ABCDEF"), _T("abcdef"));
270 for ( size_t i
= 0; i
< WXSIZEOF(tests
); i
++ )
272 wxFileName
fn(tests
[i
].original
, wxPATH_UNIX
);
274 // be sure this normalization does not fail
275 CPPUNIT_ASSERT_MESSAGE
277 wxString::Format("Normalize(%s) failed", tests
[i
].original
).c_str(),
278 fn
.Normalize(tests
[i
].flags
, cwd
, wxPATH_UNIX
)
281 // compare result with expected string
282 CPPUNIT_ASSERT_EQUAL( tests
[i
].expected
, fn
.GetFullPath(wxPATH_UNIX
) );
286 wxString
wxTestStripExtension(wxString szFile
)
288 wxStripExtension(szFile
);
292 void FileNameTestCase::TestStrip()
295 CPPUNIT_ASSERT_EQUAL( wxString(_T("")), wxTestStripExtension(_T("")) );
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")) );
306 void FileNameTestCase::TestShortLongPath()
308 wxFileName
fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"));
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() );
316 #endif // __WINDOWS__