]>
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
},
58 { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
59 { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS
},
61 // wxFileName support for Mac file names is broken currently
64 { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC
},
65 { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC
},
66 { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC
},
67 { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC
},
68 { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
69 { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC
},
73 { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS
},
74 { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS
},
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 class FileNameTestCase
: public CppUnit::TestCase
84 FileNameTestCase() { }
87 CPPUNIT_TEST_SUITE( FileNameTestCase
);
88 CPPUNIT_TEST( TestConstruction
);
89 CPPUNIT_TEST( TestComparison
);
90 CPPUNIT_TEST( TestSplit
);
91 CPPUNIT_TEST( TestSetPath
);
92 CPPUNIT_TEST( TestStrip
);
94 CPPUNIT_TEST( TestShortLongPath
);
96 CPPUNIT_TEST_SUITE_END();
98 void TestConstruction();
99 void TestComparison();
104 void TestShortLongPath();
105 #endif // __WINDOWS__
107 DECLARE_NO_COPY_CLASS(FileNameTestCase
)
110 // register in the unnamed registry so that these tests are run by default
111 CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase
);
113 // also include in it's own registry so that these tests can be run alone
114 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase
, "FileNameTestCase" );
116 void FileNameTestCase::TestConstruction()
118 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
120 const FileNameInfo
& fni
= filenames
[n
];
122 wxFileName
fn(fni
.fullname
, fni
.format
);
124 wxString fullname
= fn
.GetFullPath(fni
.format
);
125 CPPUNIT_ASSERT( fullname
== fni
.fullname
);
127 CPPUNIT_ASSERT( fn
.Normalize(wxPATH_NORM_ALL
, _T(""), fni
.format
) );
129 if ( *fni
.volume
&& *fni
.path
)
131 // check that specifying the volume separately or as part of the
132 // path doesn't make any difference
133 wxString pathWithVolume
= fni
.volume
;
134 pathWithVolume
+= wxFileName::GetVolumeSeparator(fni
.format
);
135 pathWithVolume
+= fni
.path
;
137 CPPUNIT_ASSERT( fn
== wxFileName(pathWithVolume
,
145 void FileNameTestCase::TestComparison()
147 wxFileName
fn1(wxT("/tmp/file1"));
148 wxFileName
fn2(wxT("/tmp/dir2/../file2"));
151 CPPUNIT_ASSERT(fn1
.GetPath() == fn2
.GetPath());
155 void FileNameTestCase::TestSplit()
157 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
159 const FileNameInfo
& fni
= filenames
[n
];
160 wxString volume
, path
, name
, ext
;
161 wxFileName::SplitPath(fni
.fullname
,
162 &volume
, &path
, &name
, &ext
, fni
.format
);
164 CPPUNIT_ASSERT( volume
== fni
.volume
);
165 CPPUNIT_ASSERT( path
== fni
.path
);
166 CPPUNIT_ASSERT( name
== fni
.name
);
167 CPPUNIT_ASSERT( ext
== fni
.ext
);
170 // special case of empty extension
171 wxFileName
fn(_T("foo."));
172 CPPUNIT_ASSERT( fn
.GetFullPath() == _T("foo.") );
175 void FileNameTestCase::TestSetPath()
177 wxFileName
fn(_T("d:\\test\\foo.bar"), wxPATH_DOS
);
178 fn
.SetPath(_T("c:\\temp"), wxPATH_DOS
);
179 CPPUNIT_ASSERT( fn
.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS
)) );
181 fn
= wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX
);
182 fn
.SetPath(_T("/usr/local/bin"), wxPATH_UNIX
);
183 CPPUNIT_ASSERT( fn
.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX
)) );
186 wxString
wxTestStripExtension(wxString szFile
)
188 wxStripExtension(szFile
);
192 void FileNameTestCase::TestStrip()
195 CPPUNIT_ASSERT( wxTestStripExtension( _T("") ) == _T("") );
198 CPPUNIT_ASSERT( wxTestStripExtension( _T(".") ) == _T("") );
199 CPPUNIT_ASSERT( wxTestStripExtension( _T(".wav") ) == _T("") );
200 CPPUNIT_ASSERT( wxTestStripExtension( _T("good.wav") ) == _T("good") );
201 CPPUNIT_ASSERT( wxTestStripExtension( _T("good.wav.wav") ) == _T("good.wav") );
206 void FileNameTestCase::TestShortLongPath()
208 wxFileName
fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe"));
210 // incredibly enough, GetLongPath() used to return different results during
211 // the first and subsequent runs, test for this
212 CPPUNIT_ASSERT_EQUAL( fn
.GetLongPath(), fn
.GetLongPath() );
213 CPPUNIT_ASSERT_EQUAL( fn
.GetShortPath(), fn
.GetShortPath() );
216 #endif // __WINDOWS__