]>
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 // ----------------------------------------------------------------------------
23 #include "wx/filename.h"
24 #include "wx/filefn.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 static struct FileNameInfo
32 const wxChar
*fullname
;
42 { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX
},
43 { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX
},
44 { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX
},
45 { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX
},
46 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
47 { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX
},
48 { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX
},
49 { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX
},
50 { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX
},
53 { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
54 { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS
},
55 { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS
},
56 { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
57 { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS
},
59 // NB: when using the wxFileName::GetLongPath() function on these two strings,
60 // the program will hang various seconds. All those time is taken by the
61 // call to the win32 API GetLongPathName()...
62 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
63 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
66 // wxFileName support for Mac file names is broken currently
69 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC
},
70 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC
},
71 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC
},
72 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC
},
73 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
74 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
78 // NB: on Windows they have the same effect of the \\server\\ strings
79 // (see the note above)
80 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS
},
81 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS
},
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 class FileNameTestCase
: public CppUnit::TestCase
91 FileNameTestCase() { }
94 CPPUNIT_TEST_SUITE( FileNameTestCase
);
95 CPPUNIT_TEST( TestConstruction
);
96 CPPUNIT_TEST( TestComparison
);
97 CPPUNIT_TEST( TestSplit
);
98 CPPUNIT_TEST( TestSetPath
);
99 CPPUNIT_TEST( TestStrip
);
100 CPPUNIT_TEST( TestNormalize
);
102 CPPUNIT_TEST( TestShortLongPath
);
103 #endif // __WINDOWS__
104 CPPUNIT_TEST_SUITE_END();
106 void TestConstruction();
107 void TestComparison();
111 void TestNormalize();
113 void TestShortLongPath();
114 #endif // __WINDOWS__
116 DECLARE_NO_COPY_CLASS(FileNameTestCase
)
119 // register in the unnamed registry so that these tests are run by default
120 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase
);
122 // also include in it's own registry so that these tests can be run alone
123 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase
, "FileNameTestCase" );
125 void FileNameTestCase::TestConstruction()
127 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
129 const FileNameInfo
& fni
= filenames
[n
];
131 wxFileName
fn(fni
.fullname
, fni
.format
);
133 wxString fullname
= fn
.GetFullPath(fni
.format
);
134 CPPUNIT_ASSERT( fullname
== fni
.fullname
);
136 CPPUNIT_ASSERT( fn
.Normalize(wxPATH_NORM_ALL
, _T(""), fni
.format
) );
138 if ( *fni
.volume
&& *fni
.path
)
140 // check that specifying the volume separately or as part of the
141 // path doesn't make any difference
142 wxString pathWithVolume
= fni
.volume
;
143 pathWithVolume
+= wxFileName::GetVolumeSeparator(fni
.format
);
144 pathWithVolume
+= fni
.path
;
146 CPPUNIT_ASSERT( fn
== wxFileName(pathWithVolume
,
154 void FileNameTestCase::TestComparison()
156 wxFileName
fn1(wxT("/tmp/file1"));
157 wxFileName
fn2(wxT("/tmp/dir2/../file2"));
160 CPPUNIT_ASSERT(fn1
.GetPath() == fn2
.GetPath());
163 void FileNameTestCase::TestSplit()
165 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
167 const FileNameInfo
& fni
= filenames
[n
];
168 wxString volume
, path
, name
, ext
;
169 wxFileName::SplitPath(fni
.fullname
,
170 &volume
, &path
, &name
, &ext
, fni
.format
);
172 CPPUNIT_ASSERT( volume
== fni
.volume
);
173 CPPUNIT_ASSERT( path
== fni
.path
);
174 CPPUNIT_ASSERT( name
== fni
.name
);
175 CPPUNIT_ASSERT( ext
== fni
.ext
);
178 // special case of empty extension
179 wxFileName
fn(_T("foo."));
180 CPPUNIT_ASSERT( fn
.GetFullPath() == _T("foo.") );
183 void FileNameTestCase::TestSetPath()
185 wxFileName
fn(_T("d:\\test\\foo.bar"), wxPATH_DOS
);
186 fn
.SetPath(_T("c:\\temp"), wxPATH_DOS
);
187 CPPUNIT_ASSERT( fn
.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS
)) );
189 fn
= wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX
);
190 fn
.SetPath(_T("/usr/local/bin"), wxPATH_UNIX
);
191 CPPUNIT_ASSERT( fn
.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX
)) );
194 void FileNameTestCase::TestNormalize()
196 // prepare some data to be used later
197 wxString sep
= wxFileName::GetPathSeparator();
198 wxString cwd
= wxGetCwd();
199 wxString home
= wxGetUserHome();
201 cwd
.Replace(sep
, wxT("/"));
202 if (cwd
.Last() != wxT('/'))
204 home
.Replace(sep
, wxT("/"));
205 if (home
.Last() != wxT('/'))
208 // since we will always be testing paths using the wxPATH_UNIX
209 // format, we need to remove the volume, if present
210 if (home
.Contains(wxT(':')))
211 home
= home
.AfterFirst(wxT(':'));
212 if (cwd
.Contains(wxT(':')))
213 cwd
= cwd
.AfterFirst(wxT(':'));
215 static struct FileNameTest
222 // test wxPATH_NORM_ENV_VARS
224 { wxT("%ABCDEF%/g/h/i"), wxPATH_NORM_ENV_VARS
, wxT("abcdef/g/h/i") },
226 { wxT("$(ABCDEF)/g/h/i"), wxPATH_NORM_ENV_VARS
, wxT("abcdef/g/h/i") },
229 // test wxPATH_NORM_DOTS
230 { wxT("a/.././b/c/../../"), wxPATH_NORM_DOTS
, wxT("") },
232 // test wxPATH_NORM_TILDE
233 // NB: do the tilde expansion also under Windows to test if it works there too
234 { wxT("/a/b/~"), wxPATH_NORM_TILDE
, wxT("/a/b/~") },
235 { wxT("/~/a/b"), wxPATH_NORM_TILDE
, home
+ wxT("a/b") },
236 { wxT("~/a/b"), wxPATH_NORM_TILDE
, home
+ wxT("a/b") },
238 // test wxPATH_NORM_ABSOLUTE
239 { wxT("a/b/"), wxPATH_NORM_ABSOLUTE
, cwd
+ wxT("a/b/") },
240 { wxT("a/b/c.ext"), wxPATH_NORM_ABSOLUTE
, cwd
+ wxT("a/b/c.ext") },
241 { wxT("/a"), wxPATH_NORM_ABSOLUTE
, wxT("/a") },
243 // test giving no flags at all to Normalize()
244 { wxT("a/b/"), 0, wxT("a/b/") },
245 { wxT("a/b/c.ext"), 0, wxT("a/b/c.ext") },
246 { wxT("/a"), 0, wxT("/a") }
249 // set the env var ABCDEF
250 wxSetEnv(_T("ABCDEF"), _T("abcdef"));
252 for (size_t i
=0; i
< WXSIZEOF(tests
); i
++)
254 wxFileName
fn(tests
[i
].original
, wxPATH_UNIX
);
256 // be sure this normalization does not fail
257 CPPUNIT_ASSERT( fn
.Normalize(tests
[i
].flags
, cwd
, wxPATH_UNIX
) );
259 // compare result with expected string
260 CPPUNIT_ASSERT_EQUAL( tests
[i
].expected
, fn
.GetFullPath(wxPATH_UNIX
) );
264 wxString
wxTestStripExtension(wxString szFile
)
266 wxStripExtension(szFile
);
270 void FileNameTestCase::TestStrip()
273 CPPUNIT_ASSERT( wxTestStripExtension( _T("") ) == _T("") );
276 CPPUNIT_ASSERT( wxTestStripExtension( _T(".") ) == _T("") );
277 CPPUNIT_ASSERT( wxTestStripExtension( _T(".wav") ) == _T("") );
278 CPPUNIT_ASSERT( wxTestStripExtension( _T("good.wav") ) == _T("good") );
279 CPPUNIT_ASSERT( wxTestStripExtension( _T("good.wav.wav") ) == _T("good.wav") );
284 void FileNameTestCase::TestShortLongPath()
286 wxFileName
fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"));
288 // incredibly enough, GetLongPath() used to return different results during
289 // the first and subsequent runs, test for this
290 CPPUNIT_ASSERT_EQUAL( fn
.GetLongPath(), fn
.GetLongPath() );
291 CPPUNIT_ASSERT_EQUAL( fn
.GetShortPath(), fn
.GetShortPath() );
294 #endif // __WINDOWS__