]>
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 static struct FileNameInfo
33 const wxChar
*fullname
;
43 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX
},
44 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX
},
45 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX
},
46 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX
},
47 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
48 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX
},
49 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX
},
50 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
51 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX
},
54 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
55 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS
},
56 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
57 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
58 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS
},
60 // NB: when using the wxFileName::GetLongPath() function on these two strings,
61 // the program will hang various seconds. All those time is taken by the
62 // call to the win32 API GetLongPathName()...
63 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
64 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
67 // wxFileName support for Mac file names is broken currently
70 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC
},
71 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC
},
72 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC
},
73 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC
},
74 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
75 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
79 // NB: on Windows they have the same effect of the \\server\\ strings
80 // (see the note above)
81 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS
},
82 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS
},
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 class FileNameTestCase
: public CppUnit::TestCase
92 FileNameTestCase() { }
95 CPPUNIT_TEST_SUITE( FileNameTestCase
);
96 CPPUNIT_TEST( TestConstruction
);
97 CPPUNIT_TEST( TestComparison
);
98 CPPUNIT_TEST( TestSplit
);
99 CPPUNIT_TEST( TestSetPath
);
100 CPPUNIT_TEST( TestStrip
);
101 CPPUNIT_TEST( TestNormalize
);
103 CPPUNIT_TEST( TestShortLongPath
);
104 #endif // __WINDOWS__
105 CPPUNIT_TEST_SUITE_END();
107 void TestConstruction();
108 void TestComparison();
112 void TestNormalize();
114 void TestShortLongPath();
115 #endif // __WINDOWS__
117 DECLARE_NO_COPY_CLASS(FileNameTestCase
)
120 // register in the unnamed registry so that these tests are run by default
121 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase
);
123 // also include in it's own registry so that these tests can be run alone
124 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase
, "FileNameTestCase" );
126 void FileNameTestCase::TestConstruction()
128 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
130 const FileNameInfo
& fni
= filenames
[n
];
132 wxFileName
fn(fni
.fullname
, fni
.format
);
134 wxString fullname
= fn
.GetFullPath(fni
.format
);
135 CPPUNIT_ASSERT( fullname
== fni
.fullname
);
137 CPPUNIT_ASSERT( fn
.Normalize(wxPATH_NORM_ALL
, _T(""), fni
.format
) );
139 if ( *fni
.volume
&& *fni
.path
)
141 // check that specifying the volume separately or as part of the
142 // path doesn't make any difference
143 wxString pathWithVolume
= fni
.volume
;
144 pathWithVolume
+= wxFileName::GetVolumeSeparator(fni
.format
);
145 pathWithVolume
+= fni
.path
;
147 CPPUNIT_ASSERT( fn
== wxFileName(pathWithVolume
,
155 void FileNameTestCase::TestComparison()
157 wxFileName
fn1(wxT("/tmp/file1"));
158 wxFileName
fn2(wxT("/tmp/dir2/../file2"));
161 CPPUNIT_ASSERT(fn1
.GetPath() == fn2
.GetPath());
164 void FileNameTestCase::TestSplit()
166 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
168 const FileNameInfo
& fni
= filenames
[n
];
169 wxString volume
, path
, name
, ext
;
170 wxFileName::SplitPath(fni
.fullname
,
171 &volume
, &path
, &name
, &ext
, fni
.format
);
173 CPPUNIT_ASSERT( volume
== fni
.volume
);
174 CPPUNIT_ASSERT( path
== fni
.path
);
175 CPPUNIT_ASSERT( name
== fni
.name
);
176 CPPUNIT_ASSERT( ext
== fni
.ext
);
179 // special case of empty extension
180 wxFileName
fn(_T("foo."));
181 CPPUNIT_ASSERT( fn
.GetFullPath() == _T("foo.") );
184 void FileNameTestCase::TestSetPath()
186 wxFileName
fn(_T("d:\\test\\foo.bar"), wxPATH_DOS
);
187 fn
.SetPath(_T("c:\\temp"), wxPATH_DOS
);
188 CPPUNIT_ASSERT( fn
.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS
)) );
190 fn
= wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX
);
191 fn
.SetPath(_T("/usr/local/bin"), wxPATH_UNIX
);
192 CPPUNIT_ASSERT( fn
.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX
)) );
195 void FileNameTestCase::TestNormalize()
197 // prepare some data to be used later
198 wxString sep
= wxFileName::GetPathSeparator();
199 wxString cwd
= wxGetCwd();
200 wxString home
= wxGetUserHome();
202 cwd
.Replace(sep
, wxT("/"));
203 if (cwd
.Last() != wxT('/'))
205 home
.Replace(sep
, wxT("/"));
206 if (home
.Last() != wxT('/'))
209 // since we will always be testing paths using the wxPATH_UNIX
210 // format, we need to remove the volume, if present
211 if (home
.Contains(wxT(':')))
212 home
= home
.AfterFirst(wxT(':'));
213 if (cwd
.Contains(wxT(':')))
214 cwd
= cwd
.AfterFirst(wxT(':'));
216 static struct FileNameTest
223 // test wxPATH_NORM_ENV_VARS
225 { wxT("%ABCDEF%/g/h/i"), wxPATH_NORM_ENV_VARS
, wxT("abcdef/g/h/i") },
227 { wxT("$(ABCDEF)/g/h/i"), wxPATH_NORM_ENV_VARS
, wxT("abcdef/g/h/i") },
230 // test wxPATH_NORM_DOTS
231 { wxT("a/.././b/c/../../"), wxPATH_NORM_DOTS
, wxT("") },
233 // test wxPATH_NORM_TILDE
234 // NB: do the tilde expansion also under Windows to test if it works there too
235 { wxT("/a/b/~"), wxPATH_NORM_TILDE
, wxT("/a/b/~") },
236 { wxT("/~/a/b"), wxPATH_NORM_TILDE
, home
+ wxT("a/b") },
237 { wxT("~/a/b"), wxPATH_NORM_TILDE
, home
+ wxT("a/b") },
239 // test wxPATH_NORM_ABSOLUTE
240 { wxT("a/b/"), wxPATH_NORM_ABSOLUTE
, cwd
+ wxT("a/b/") },
241 { wxT("a/b/c.ext"), wxPATH_NORM_ABSOLUTE
, cwd
+ wxT("a/b/c.ext") },
242 { wxT("/a"), wxPATH_NORM_ABSOLUTE
, wxT("/a") },
244 // test giving no flags at all to Normalize()
245 { wxT("a/b/"), 0, wxT("a/b/") },
246 { wxT("a/b/c.ext"), 0, wxT("a/b/c.ext") },
247 { wxT("/a"), 0, wxT("/a") }
250 // set the env var ABCDEF
251 wxSetEnv(_T("ABCDEF"), _T("abcdef"));
253 for (size_t i
=0; i
< WXSIZEOF(tests
); i
++)
255 wxFileName
fn(tests
[i
].original
, wxPATH_UNIX
);
257 // be sure this normalization does not fail
258 CPPUNIT_ASSERT( fn
.Normalize(tests
[i
].flags
, cwd
, wxPATH_UNIX
) );
260 // compare result with expected string
261 CPPUNIT_ASSERT_EQUAL( tests
[i
].expected
, fn
.GetFullPath(wxPATH_UNIX
) );
265 wxString
wxTestStripExtension(wxString szFile
)
267 wxStripExtension(szFile
);
271 void FileNameTestCase::TestStrip()
274 CPPUNIT_ASSERT( wxTestStripExtension( _T("") ) == _T("") );
277 CPPUNIT_ASSERT( wxTestStripExtension( _T(".") ) == _T("") );
278 CPPUNIT_ASSERT( wxTestStripExtension( _T(".wav") ) == _T("") );
279 CPPUNIT_ASSERT( wxTestStripExtension( _T("good.wav") ) == _T("good") );
280 CPPUNIT_ASSERT( wxTestStripExtension( _T("good.wav.wav") ) == _T("good.wav") );
285 void FileNameTestCase::TestShortLongPath()
287 wxFileName
fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"));
289 // incredibly enough, GetLongPath() used to return different results during
290 // the first and subsequent runs, test for this
291 CPPUNIT_ASSERT_EQUAL( fn
.GetLongPath(), fn
.GetLongPath() );
292 CPPUNIT_ASSERT_EQUAL( fn
.GetShortPath(), fn
.GetShortPath() );
295 #endif // __WINDOWS__