Upstream tarball 20080823
[amule.git] / unittests / tests / PathTest.cpp
blobaa4af4b09dd37697340002baba002e9a903e782c
1 #include <muleunit/test.h>
3 #include <common/Path.h>
4 #include <common/StringFunctions.h>
6 #include <wx/filename.h>
8 using namespace muleunit;
10 struct STestStr
12 //! Specifies the "source" of the string (user/fs)
13 const bool fromFS;
14 //! Input filename
15 const wxString input;
16 //! The expected result.
17 const wxString expected;
20 const STestStr g_fromFSTests[] = {
21 // From filesystem
22 { true, wxConvFileName->cMB2WC("\xe1\x62\x63"), wxT("\xe1\x62\x63") },
23 { true, wxConvFileName->cMB2WC("\xe6\xf8\xe5"), wxT("\xe6\xf8\xe5") },
24 { true, wxConvFileName->cMB2WC("\xd8\xa7\xd9\x84\xd8\xb9"), wxT("\u0627\u0644\u0639") },
26 // From User
27 { false, wxT("\u0627\u0644\u0639"), wxT("\u0627\u0644\u0639") }
30 enum UsesEncoding {
31 UE_Unknown,
32 UE_Broken,
33 UE_NonBroken
36 wxString GetExpectedString(const wxString& src)
38 // Determine how the filenames are expected to be mangled
39 static UsesEncoding encoding = UE_Unknown;
41 if (encoding == UE_Unknown) {
42 wxCharBuffer fn = wxConvFile.cWC2MB(wxT("\u0627\u0644\u0639"));
44 if (fn) {
45 encoding = UE_NonBroken;
46 } else {
47 encoding = UE_Broken;
51 if ((encoding == UE_Broken) && !wxConvFile.cWC2MB(src)) {
52 // See CPath::CPath for rationale ...
53 wxCharBuffer fn = wxConvUTF8.cWC2MB(src);
54 return wxConvFile.cMB2WC(fn);
55 } else {
56 return src;
61 wxString StringFrom(const CPath& prt)
63 return prt.GetPrintableString();
67 DECLARE_SIMPLE(CPath)
69 TEST(CPath, DefaultConstructor)
71 CPath tmp;
73 ASSERT_FALSE(tmp.IsOk());
74 ASSERT_EQUALS(tmp, CPath());
76 ASSERT_FALSE(tmp.FileExists());
77 ASSERT_FALSE(tmp.DirExists());
79 ASSERT_EQUALS(wxEmptyString, tmp.GetRaw());
80 ASSERT_EQUALS(wxEmptyString, tmp.GetPrintable());
81 ASSERT_EQUALS(CPath(), tmp.GetPath());
82 ASSERT_EQUALS(CPath(), tmp.GetFullName());
84 ASSERT_EQUALS(wxEmptyString, tmp.GetPrintableString());
88 TEST(CPath, PathConstructor)
91 CPath tmp(wxT(""));
93 ASSERT_FALSE(tmp.IsOk());
94 ASSERT_EQUALS(tmp, CPath());
98 for (size_t i = 0; i < ArraySize(g_fromFSTests); ++i) {
99 const wxString input = g_fromFSTests[i].input;
100 const wxString result = g_fromFSTests[i].expected;
102 ASSERT_TRUE(result.Length());
103 ASSERT_TRUE(input.Length());
105 CPath tmp(input);
107 ASSERT_TRUE(tmp.IsOk());
108 ASSERT_EQUALS(tmp, CPath(input));
110 ASSERT_EQUALS(tmp.GetRaw(), ::GetExpectedString(input));
111 ASSERT_EQUALS(tmp.GetPrintable(), result);
113 ASSERT_EQUALS(tmp.GetFullName(), tmp);
114 ASSERT_EQUALS(tmp.GetPath(), CPath());
119 TEST(CPath, CopyConstructor)
121 const wxChar* tmpPath = wxT("foobar.tgz");
124 CPath a(tmpPath);
126 ASSERT_TRUE(a.IsOk());
127 ASSERT_EQUALS(a.GetRaw(), tmpPath);
128 ASSERT_EQUALS(a.GetPrintable(), tmpPath);
129 ASSERT_EQUALS(a.GetPath(), CPath());
130 ASSERT_EQUALS(a.GetFullName(), CPath(tmpPath));
132 CPath b(a);
133 ASSERT_TRUE(b.IsOk());
134 ASSERT_EQUALS(b.GetRaw(), tmpPath);
135 ASSERT_EQUALS(b.GetPrintable(), tmpPath);
136 ASSERT_EQUALS(b.GetPath(), CPath());
137 ASSERT_EQUALS(b.GetFullName(), CPath(tmpPath));
139 ASSERT_EQUALS(a, b);
143 CPath a(tmpPath);
145 ASSERT_TRUE(a.IsOk());
146 ASSERT_EQUALS(a.GetRaw(), tmpPath);
147 ASSERT_EQUALS(a.GetPrintable(), tmpPath);
148 ASSERT_EQUALS(a.GetPath(), CPath());
149 ASSERT_EQUALS(a.GetFullName(), CPath(tmpPath));
151 CPath b(a);
152 ASSERT_TRUE(b.IsOk());
153 ASSERT_EQUALS(b.GetRaw(), tmpPath);
154 ASSERT_EQUALS(b.GetPrintable(), tmpPath);
155 ASSERT_EQUALS(b.GetPath(), CPath());
156 ASSERT_EQUALS(b.GetFullName(), CPath(tmpPath));
158 ASSERT_EQUALS(a, b);
162 CPath a;
163 CPath b(a);
165 ASSERT_EQUALS(a, b);
166 ASSERT_FALSE(a.IsOk());
167 ASSERT_FALSE(b.IsOk());
172 TEST(CPath, Operators)
174 const wxChar* tmpPath1 = wxT("foobar.tgz");
175 const wxChar* tmpPath2 = wxT("barfoo.tar");
178 CPath a, b;
179 ASSERT_EQUALS(a, b);
180 ASSERT_FALSE(a.IsOk());
181 ASSERT_FALSE(b.IsOk());
183 a = CPath(tmpPath1);
184 ASSERT_EQUALS(a.GetRaw(), tmpPath1);
185 ASSERT_EQUALS(a.GetPrintable(), tmpPath1);
186 ASSERT_EQUALS(a.GetPath(), CPath());
187 ASSERT_EQUALS(a.GetFullName(), CPath(tmpPath1));
188 ASSERT_TRUE(a.IsOk());
189 ASSERT_TRUE(a != b);
191 b = a;
192 ASSERT_EQUALS(b.GetRaw(), tmpPath1);
193 ASSERT_EQUALS(b.GetPrintable(), tmpPath1);
194 ASSERT_EQUALS(b.GetPath(), CPath());
195 ASSERT_EQUALS(b.GetFullName(), CPath(tmpPath1));
196 ASSERT_TRUE(a.IsOk());
197 ASSERT_TRUE(b.IsOk());
198 ASSERT_EQUALS(a, b);
200 a = CPath(tmpPath2);
201 ASSERT_TRUE(a.IsOk());
202 ASSERT_TRUE(b.IsOk());
203 ASSERT_TRUE(a != b);
205 ASSERT_EQUALS(a.GetRaw(), tmpPath2);
206 ASSERT_EQUALS(a.GetPrintable(), tmpPath2);
207 ASSERT_EQUALS(a.GetPath(), CPath());
208 ASSERT_EQUALS(a.GetFullName(), CPath(tmpPath2));
209 ASSERT_EQUALS(b.GetRaw(), tmpPath1);
210 ASSERT_EQUALS(b.GetPrintable(), tmpPath1);
211 ASSERT_EQUALS(b.GetPath(), CPath());
212 ASSERT_EQUALS(b.GetFullName(), CPath(tmpPath1));
215 // See note in CPath::operator==
216 #if 0
218 CPath a = CPath(wxT("/home/amule"));
219 CPath b = CPath(wxT("/hone/amule/"));
221 ASSERT_EQUALS(a, b);
223 #endif
225 // TODO: Less than
229 /** Return the path normalized for the current platform. */
230 CPath Norm(wxString str)
232 str.Replace(wxT("/"), wxString(wxFileName::GetPathSeparator()));
234 return CPath(str);
238 TEST(CPath, JoinPaths)
240 const CPath expected1 = Norm(wxT("/home/amule/"));
241 const CPath expected2 = Norm(wxT("/home/amule"));
243 // Note: Just a few checks, as ::JoinPaths is tested in StringFunctionTests.cpp
244 ASSERT_EQUALS(expected1, Norm(wxT("/home")).JoinPaths(Norm(wxT("amule/"))));
245 ASSERT_EQUALS(expected1, Norm(wxT("/home")).JoinPaths(Norm(wxT("/amule/"))));
246 ASSERT_EQUALS(expected1, Norm(wxT("/home/")).JoinPaths(Norm(wxT("/amule/"))));
247 ASSERT_EQUALS(expected1, Norm(wxT("/home/")).JoinPaths(Norm(wxT("amule/"))));
249 ASSERT_EQUALS(expected2, Norm(wxT("/home")).JoinPaths(Norm(wxT("amule"))));
250 ASSERT_EQUALS(expected2, Norm(wxT("/home")).JoinPaths(Norm(wxT("/amule"))));
251 ASSERT_EQUALS(expected2, Norm(wxT("/home/")).JoinPaths(Norm(wxT("/amule"))));
252 ASSERT_EQUALS(expected2, Norm(wxT("/home/")).JoinPaths(Norm(wxT("amule"))));
254 ASSERT_EQUALS(expected1, expected1.JoinPaths(CPath()));
255 ASSERT_EQUALS(expected1, CPath().JoinPaths(expected1));
257 ASSERT_EQUALS(expected2, expected2.JoinPaths(CPath()));
258 ASSERT_EQUALS(expected2, CPath().JoinPaths(expected2));
262 TEST(CPath, StartsWith)
264 const CPath test = Norm(wxT("/home/amule/"));
266 ASSERT_FALSE(test.StartsWith(CPath()));
267 ASSERT_FALSE(CPath().StartsWith(test));
269 ASSERT_TRUE(test.StartsWith(Norm(wxT("/"))));
270 ASSERT_TRUE(test.StartsWith(Norm(wxT("/home"))));
271 ASSERT_TRUE(test.StartsWith(Norm(wxT("/home/"))));
272 ASSERT_TRUE(test.StartsWith(Norm(wxT("/home/amule"))));
273 ASSERT_TRUE(test.StartsWith(Norm(wxT("/home/amule/"))));
275 ASSERT_FALSE(test.StartsWith(Norm(wxT(""))));
276 ASSERT_FALSE(test.StartsWith(Norm(wxT("~"))));
277 ASSERT_FALSE(test.StartsWith(Norm(wxT("/ho"))));
278 ASSERT_FALSE(test.StartsWith(Norm(wxT("/home/am"))));
279 ASSERT_FALSE(test.StartsWith(Norm(wxT("/home/amule2"))));
280 ASSERT_FALSE(test.StartsWith(Norm(wxT("/home/amule/foo"))));
284 TEST(CPath, IsSameDir)
286 ASSERT_TRUE(Norm(wxT("/root")).IsSameDir(Norm(wxT("/root"))));
287 ASSERT_TRUE(Norm(wxT("/root/")).IsSameDir(Norm(wxT("/root"))));
288 ASSERT_TRUE(Norm(wxT("/root")).IsSameDir(Norm(wxT("/root/"))));
289 ASSERT_TRUE(Norm(wxT("/root/")).IsSameDir(Norm(wxT("/root/"))));
291 ASSERT_FALSE(CPath().IsSameDir(Norm(wxT("/"))));
292 ASSERT_FALSE(Norm(wxT("/")).IsSameDir(CPath()));
294 ASSERT_FALSE(Norm(wxT("/root")).IsSameDir(Norm(wxT("/home"))));
298 TEST(CPath, GetPath_FullName)
301 ASSERT_EQUALS(CPath().GetPath(), CPath());
302 ASSERT_EQUALS(CPath().GetFullName(), CPath());
306 const CPath path = Norm(wxT("/home/mule/"));
308 ASSERT_EQUALS(path.GetPath(), Norm(wxT("/home/mule")));
309 ASSERT_EQUALS(path.GetFullName(), CPath());
313 const CPath path = Norm(wxT("/home/mule"));
315 ASSERT_EQUALS(path.GetPath(), Norm(wxT("/home")));
316 ASSERT_EQUALS(path.GetFullName(), Norm(wxT("mule")));
320 const CPath path = Norm(wxT("mule"));
322 ASSERT_EQUALS(path.GetPath(), CPath());
323 ASSERT_EQUALS(path.GetFullName(), Norm(wxT("mule")));
327 const CPath path = Norm(wxT("mule.ext"));
329 ASSERT_EQUALS(path.GetPath(), CPath());
330 ASSERT_EQUALS(path.GetFullName(), Norm(wxT("mule.ext")));
335 TEST(CPath, Cleanup)
337 const CPath initial = CPath(wxT(" /a\"b*c* <d>?e|\\:f "));
339 ASSERT_EQUALS(initial.Cleanup(false, false), Norm(wxT("\%20a\"b*c*\%20<d>?e|\\:f\%20")));
340 ASSERT_EQUALS(initial.Cleanup(false, true), Norm(wxT("\%20abc\%20def\%20")));
341 ASSERT_EQUALS(initial.Cleanup(true, false), Norm(wxT(" a\"b*c* <d>?e|\\:f ")));
342 ASSERT_EQUALS(initial.Cleanup(true, true), Norm(wxT(" abc def ")));
346 TEST(CPath, AddPostFix)
348 ASSERT_EQUALS(Norm(wxT("/foo.bar")).AddPostfix(wxT("_1")), Norm(wxT("/foo_1.bar")));
349 ASSERT_EQUALS(Norm(wxT("/foo.bar")).AddPostfix(wxT("")), Norm(wxT("/foo.bar")));
350 ASSERT_EQUALS(Norm(wxT("/.bar")).AddPostfix(wxT("_1")), Norm(wxT("/.bar_1")));
351 ASSERT_EQUALS(Norm(wxT("/")).AddPostfix(wxT("_1")), Norm(wxT("/_1")));
355 TEST(CPath, Extensions)
357 const CPath initial = Norm(wxT("/home/amule.foo.bar"));
359 ASSERT_EQUALS(Norm(wxT("/home/amule.foo")), initial.RemoveExt());
360 ASSERT_EQUALS(Norm(wxT("/home/amule")), initial.RemoveExt().RemoveExt());
361 ASSERT_EQUALS(Norm(wxT("/home/amule")), initial.RemoveExt().RemoveExt().RemoveExt());
362 ASSERT_EQUALS(Norm(wxT("/home/amule")), initial.RemoveAllExt());
364 ASSERT_EQUALS(wxT("bar"), initial.GetExt());
365 ASSERT_EQUALS(wxT("foo"), initial.RemoveExt().GetExt());
366 ASSERT_EQUALS(wxEmptyString, initial.RemoveExt().RemoveExt().GetExt());
367 ASSERT_EQUALS(wxEmptyString, initial.RemoveAllExt().GetExt());
369 ASSERT_EQUALS(Norm(wxT("/home/amule.foo.bar")), initial.AppendExt(wxT("")));
370 ASSERT_EQUALS(Norm(wxT("/home/amule.foo.bar.zod")), initial.AppendExt(wxT("zod")));
371 ASSERT_EQUALS(Norm(wxT("/home/amule.foo.bar.zod")), initial.AppendExt(wxT(".zod")));
372 ASSERT_EQUALS(Norm(wxT("/home/amule.zod")), initial.RemoveAllExt().AppendExt(wxT("zod")));
373 ASSERT_EQUALS(Norm(wxT("/home/amule.zod")), initial.RemoveAllExt().AppendExt(wxT(".zod")));