]>
Commit | Line | Data |
---|---|---|
f095b1fc VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/filename/filename.cpp | |
3 | // Purpose: wxFileName unit test | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2004-07-25 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2004 Vadim Zeitlin | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
8899b155 | 14 | #include "testprec.h" |
f095b1fc VZ |
15 | |
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
21 | #endif // WX_PRECOMP | |
22 | ||
23 | #include "wx/filename.h" | |
c08dd08b | 24 | #include "wx/filefn.h" |
f095b1fc | 25 | |
f095b1fc VZ |
26 | // ---------------------------------------------------------------------------- |
27 | // test data | |
28 | // ---------------------------------------------------------------------------- | |
29 | ||
30 | static struct FileNameInfo | |
31 | { | |
32 | const wxChar *fullname; | |
33 | const wxChar *volume; | |
34 | const wxChar *path; | |
35 | const wxChar *name; | |
36 | const wxChar *ext; | |
37 | bool isAbsolute; | |
38 | wxPathFormat format; | |
39 | } filenames[] = | |
40 | { | |
41 | // Unix file names | |
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 }, | |
51 | ||
52 | // Windows file names | |
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 }, | |
60 | ||
61 | // wxFileName support for Mac file names is broken currently | |
62 | #if 0 | |
63 | // Mac file names | |
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 }, | |
70 | #endif // 0 | |
71 | ||
72 | // VMS file names | |
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 }, | |
75 | }; | |
76 | ||
77 | // ---------------------------------------------------------------------------- | |
78 | // test class | |
79 | // ---------------------------------------------------------------------------- | |
80 | ||
81 | class FileNameTestCase : public CppUnit::TestCase | |
82 | { | |
83 | public: | |
84 | FileNameTestCase() { } | |
85 | ||
86 | private: | |
87 | CPPUNIT_TEST_SUITE( FileNameTestCase ); | |
88 | CPPUNIT_TEST( TestConstruction ); | |
33366127 | 89 | CPPUNIT_TEST( TestComparison ); |
f095b1fc | 90 | CPPUNIT_TEST( TestSplit ); |
4524a24b | 91 | CPPUNIT_TEST( TestSetPath ); |
c08dd08b | 92 | CPPUNIT_TEST( TestStrip ); |
60c0dfe5 VZ |
93 | #ifdef __WINDOWS__ |
94 | CPPUNIT_TEST( TestShortLongPath ); | |
95 | #endif // __WINDOWS__ | |
f095b1fc VZ |
96 | CPPUNIT_TEST_SUITE_END(); |
97 | ||
98 | void TestConstruction(); | |
33366127 | 99 | void TestComparison(); |
f095b1fc VZ |
100 | void TestSplit(); |
101 | void TestSetPath(); | |
c08dd08b | 102 | void TestStrip(); |
60c0dfe5 VZ |
103 | #ifdef __WINDOWS__ |
104 | void TestShortLongPath(); | |
105 | #endif // __WINDOWS__ | |
f095b1fc VZ |
106 | |
107 | DECLARE_NO_COPY_CLASS(FileNameTestCase) | |
108 | }; | |
109 | ||
110 | // register in the unnamed registry so that these tests are run by default | |
111 | CPPUNIT_TEST_SUITE_REGISTRATION( FileNameTestCase ); | |
112 | ||
113 | // also include in it's own registry so that these tests can be run alone | |
114 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FileNameTestCase, "FileNameTestCase" ); | |
115 | ||
116 | void FileNameTestCase::TestConstruction() | |
117 | { | |
118 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
119 | { | |
120 | const FileNameInfo& fni = filenames[n]; | |
121 | ||
122 | wxFileName fn(fni.fullname, fni.format); | |
123 | ||
124 | wxString fullname = fn.GetFullPath(fni.format); | |
125 | CPPUNIT_ASSERT( fullname == fni.fullname ); | |
126 | ||
127 | CPPUNIT_ASSERT( fn.Normalize(wxPATH_NORM_ALL, _T(""), fni.format) ); | |
4c2deb19 VZ |
128 | |
129 | if ( *fni.volume && *fni.path ) | |
130 | { | |
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; | |
136 | ||
137 | CPPUNIT_ASSERT( fn == wxFileName(pathWithVolume, | |
138 | fni.name, | |
139 | fni.ext, | |
140 | fni.format) ); | |
141 | } | |
f095b1fc VZ |
142 | } |
143 | } | |
144 | ||
33366127 VZ |
145 | void FileNameTestCase::TestComparison() |
146 | { | |
147 | wxFileName fn1(wxT("/tmp")); | |
148 | wxFileName fn2(wxT("/tmp/")); | |
149 | assert(fn1.SameAs(fn2)); | |
150 | } | |
151 | ||
f095b1fc VZ |
152 | void FileNameTestCase::TestSplit() |
153 | { | |
154 | for ( size_t n = 0; n < WXSIZEOF(filenames); n++ ) | |
155 | { | |
156 | const FileNameInfo& fni = filenames[n]; | |
157 | wxString volume, path, name, ext; | |
158 | wxFileName::SplitPath(fni.fullname, | |
159 | &volume, &path, &name, &ext, fni.format); | |
160 | ||
161 | CPPUNIT_ASSERT( volume == fni.volume ); | |
162 | CPPUNIT_ASSERT( path == fni.path ); | |
163 | CPPUNIT_ASSERT( name == fni.name ); | |
164 | CPPUNIT_ASSERT( ext == fni.ext ); | |
165 | } | |
dfecbee5 VZ |
166 | |
167 | // special case of empty extension | |
168 | wxFileName fn(_T("foo.")); | |
169 | CPPUNIT_ASSERT( fn.GetFullPath() == _T("foo.") ); | |
f095b1fc VZ |
170 | } |
171 | ||
4524a24b VZ |
172 | void FileNameTestCase::TestSetPath() |
173 | { | |
174 | wxFileName fn(_T("d:\\test\\foo.bar"), wxPATH_DOS); | |
67050b6d VS |
175 | fn.SetPath(_T("c:\\temp"), wxPATH_DOS); |
176 | CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("c:\\temp\\foo.bar"), wxPATH_DOS)) ); | |
4524a24b | 177 | |
67050b6d VS |
178 | fn = wxFileName(_T("/usr/bin/ls"), wxPATH_UNIX); |
179 | fn.SetPath(_T("/usr/local/bin"), wxPATH_UNIX); | |
180 | CPPUNIT_ASSERT( fn.SameAs(wxFileName(_T("/usr/local/bin/ls"), wxPATH_UNIX)) ); | |
4524a24b VZ |
181 | } |
182 | ||
ff3d9a35 | 183 | wxString wxTestStripExtension(wxString szFile) |
c08dd08b RN |
184 | { |
185 | wxStripExtension(szFile); | |
186 | return szFile; | |
187 | } | |
188 | ||
189 | void FileNameTestCase::TestStrip() | |
190 | { | |
191 | //test a crash | |
ff3d9a35 | 192 | CPPUNIT_ASSERT( wxTestStripExtension( _T("") ) == _T("") ); |
c08dd08b RN |
193 | |
194 | //others | |
ff3d9a35 RN |
195 | CPPUNIT_ASSERT( wxTestStripExtension( _T(".") ) == _T("") ); |
196 | CPPUNIT_ASSERT( wxTestStripExtension( _T(".wav") ) == _T("") ); | |
197 | CPPUNIT_ASSERT( wxTestStripExtension( _T("good.wav") ) == _T("good") ); | |
198 | CPPUNIT_ASSERT( wxTestStripExtension( _T("good.wav.wav") ) == _T("good.wav") ); | |
199 | } | |
60c0dfe5 VZ |
200 | |
201 | #ifdef __WINDOWS__ | |
202 | ||
203 | void FileNameTestCase::TestShortLongPath() | |
204 | { | |
3f302e75 | 205 | wxFileName fn(_T("C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe")); |
60c0dfe5 VZ |
206 | |
207 | // incredibly enough, GetLongPath() used to return different results during | |
208 | // the first and subsequent runs, test for this | |
209 | CPPUNIT_ASSERT_EQUAL( fn.GetLongPath(), fn.GetLongPath() ); | |
210 | CPPUNIT_ASSERT_EQUAL( fn.GetShortPath(), fn.GetShortPath() ); | |
211 | } | |
212 | ||
213 | #endif // __WINDOWS__ |