1 #include <muleunit/test.h>
3 #include <common/Path.h>
4 #include <common/StringFunctions.h>
6 #include <wx/filename.h>
8 using namespace muleunit
;
12 //! Specifies the "source" of the string (user/fs)
16 //! The expected result.
17 const wxString expected
;
20 const STestStr g_fromFSTests
[] = {
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") },
27 { false, wxT("\u0627\u0644\u0639"), wxT("\u0627\u0644\u0639") }
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"));
45 encoding
= UE_NonBroken
;
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
);
61 wxString
StringFrom(const CPath
& prt
)
63 return prt
.GetPrintableString();
67 DECLARE_SIMPLE(GenericPathFunctions
)
69 TEST(GenericPathFunctions
, JoinPaths
)
71 const wxString seps
= wxFileName::GetPathSeparators();
72 const wxString sep
= wxFileName::GetPathSeparator();
74 for (size_t i
= 0; i
< seps
.Length(); ++i
) {
75 const wxString cur_sep
= seps
.Mid(i
, 1);
77 ASSERT_EQUALS(wxT("a") + sep
+ wxT("b"), JoinPaths(wxT("a"), wxT("b")));
78 ASSERT_EQUALS(wxT("a") + sep
+ wxT("b"), JoinPaths(wxT("a") + cur_sep
, wxT("b")));
79 ASSERT_EQUALS(wxT("a") + sep
+ wxT("b"), JoinPaths(wxT("a"), cur_sep
+ wxT("b")));
80 ASSERT_EQUALS(wxT("a") + sep
+ wxT("b"), JoinPaths(wxT("a") + cur_sep
, cur_sep
+ wxT("b")));
81 ASSERT_EQUALS(wxT("a"), JoinPaths(wxT("a"), wxEmptyString
));
82 ASSERT_EQUALS(wxT("b"), JoinPaths(wxEmptyString
, wxT("b")));
83 ASSERT_EQUALS(wxEmptyString
, JoinPaths(wxEmptyString
, wxEmptyString
));
90 TEST(CPath
, DefaultConstructor
)
94 ASSERT_FALSE(tmp
.IsOk());
95 ASSERT_EQUALS(tmp
, CPath());
97 ASSERT_FALSE(tmp
.FileExists());
98 ASSERT_FALSE(tmp
.DirExists());
100 ASSERT_EQUALS(wxEmptyString
, tmp
.GetRaw());
101 ASSERT_EQUALS(wxEmptyString
, tmp
.GetPrintable());
102 ASSERT_EQUALS(CPath(), tmp
.GetPath());
103 ASSERT_EQUALS(CPath(), tmp
.GetFullName());
105 ASSERT_EQUALS(wxEmptyString
, tmp
.GetPrintableString());
109 TEST(CPath
, PathConstructor
)
114 ASSERT_FALSE(tmp
.IsOk());
115 ASSERT_EQUALS(tmp
, CPath());
119 for (size_t i
= 0; i
< ArraySize(g_fromFSTests
); ++i
) {
120 const wxString input
= g_fromFSTests
[i
].input
;
121 const wxString result
= g_fromFSTests
[i
].expected
;
123 ASSERT_TRUE(result
.Length());
124 ASSERT_TRUE(input
.Length());
128 ASSERT_TRUE(tmp
.IsOk());
129 ASSERT_EQUALS(tmp
, CPath(input
));
131 ASSERT_EQUALS(tmp
.GetRaw(), ::GetExpectedString(input
));
132 ASSERT_EQUALS(tmp
.GetPrintable(), result
);
134 ASSERT_EQUALS(tmp
.GetFullName(), tmp
);
135 ASSERT_EQUALS(tmp
.GetPath(), CPath());
140 TEST(CPath
, CopyConstructor
)
142 const wxChar
* tmpPath
= wxT("foobar.tgz");
147 ASSERT_TRUE(a
.IsOk());
148 ASSERT_EQUALS(a
.GetRaw(), tmpPath
);
149 ASSERT_EQUALS(a
.GetPrintable(), tmpPath
);
150 ASSERT_EQUALS(a
.GetPath(), CPath());
151 ASSERT_EQUALS(a
.GetFullName(), CPath(tmpPath
));
154 ASSERT_TRUE(b
.IsOk());
155 ASSERT_EQUALS(b
.GetRaw(), tmpPath
);
156 ASSERT_EQUALS(b
.GetPrintable(), tmpPath
);
157 ASSERT_EQUALS(b
.GetPath(), CPath());
158 ASSERT_EQUALS(b
.GetFullName(), CPath(tmpPath
));
166 ASSERT_TRUE(a
.IsOk());
167 ASSERT_EQUALS(a
.GetRaw(), tmpPath
);
168 ASSERT_EQUALS(a
.GetPrintable(), tmpPath
);
169 ASSERT_EQUALS(a
.GetPath(), CPath());
170 ASSERT_EQUALS(a
.GetFullName(), CPath(tmpPath
));
173 ASSERT_TRUE(b
.IsOk());
174 ASSERT_EQUALS(b
.GetRaw(), tmpPath
);
175 ASSERT_EQUALS(b
.GetPrintable(), tmpPath
);
176 ASSERT_EQUALS(b
.GetPath(), CPath());
177 ASSERT_EQUALS(b
.GetFullName(), CPath(tmpPath
));
187 ASSERT_FALSE(a
.IsOk());
188 ASSERT_FALSE(b
.IsOk());
193 TEST(CPath
, Operators
)
195 const wxChar
* tmpPath1
= wxT("foobar.tgz");
196 const wxChar
* tmpPath2
= wxT("barfoo.tar");
201 ASSERT_FALSE(a
.IsOk());
202 ASSERT_FALSE(b
.IsOk());
205 ASSERT_EQUALS(a
.GetRaw(), tmpPath1
);
206 ASSERT_EQUALS(a
.GetPrintable(), tmpPath1
);
207 ASSERT_EQUALS(a
.GetPath(), CPath());
208 ASSERT_EQUALS(a
.GetFullName(), CPath(tmpPath1
));
209 ASSERT_TRUE(a
.IsOk());
213 ASSERT_EQUALS(b
.GetRaw(), tmpPath1
);
214 ASSERT_EQUALS(b
.GetPrintable(), tmpPath1
);
215 ASSERT_EQUALS(b
.GetPath(), CPath());
216 ASSERT_EQUALS(b
.GetFullName(), CPath(tmpPath1
));
217 ASSERT_TRUE(a
.IsOk());
218 ASSERT_TRUE(b
.IsOk());
222 ASSERT_TRUE(a
.IsOk());
223 ASSERT_TRUE(b
.IsOk());
226 ASSERT_EQUALS(a
.GetRaw(), tmpPath2
);
227 ASSERT_EQUALS(a
.GetPrintable(), tmpPath2
);
228 ASSERT_EQUALS(a
.GetPath(), CPath());
229 ASSERT_EQUALS(a
.GetFullName(), CPath(tmpPath2
));
230 ASSERT_EQUALS(b
.GetRaw(), tmpPath1
);
231 ASSERT_EQUALS(b
.GetPrintable(), tmpPath1
);
232 ASSERT_EQUALS(b
.GetPath(), CPath());
233 ASSERT_EQUALS(b
.GetFullName(), CPath(tmpPath1
));
236 // See note in CPath::operator==
239 CPath a
= CPath(wxT("/home/amule"));
240 CPath b
= CPath(wxT("/hone/amule/"));
250 /** Return the path normalized for the current platform. */
251 CPath
Norm(wxString str
)
253 str
.Replace(wxT("/"), wxString(wxFileName::GetPathSeparator()));
259 TEST(CPath
, JoinPaths
)
261 const CPath expected1
= Norm(wxT("/home/amule/"));
262 const CPath expected2
= Norm(wxT("/home/amule"));
264 // Note: Just a few checks, as ::JoinPaths is tested above
265 ASSERT_EQUALS(expected1
, Norm(wxT("/home")).JoinPaths(Norm(wxT("amule/"))));
266 ASSERT_EQUALS(expected1
, Norm(wxT("/home")).JoinPaths(Norm(wxT("/amule/"))));
267 ASSERT_EQUALS(expected1
, Norm(wxT("/home/")).JoinPaths(Norm(wxT("/amule/"))));
268 ASSERT_EQUALS(expected1
, Norm(wxT("/home/")).JoinPaths(Norm(wxT("amule/"))));
270 ASSERT_EQUALS(expected2
, Norm(wxT("/home")).JoinPaths(Norm(wxT("amule"))));
271 ASSERT_EQUALS(expected2
, Norm(wxT("/home")).JoinPaths(Norm(wxT("/amule"))));
272 ASSERT_EQUALS(expected2
, Norm(wxT("/home/")).JoinPaths(Norm(wxT("/amule"))));
273 ASSERT_EQUALS(expected2
, Norm(wxT("/home/")).JoinPaths(Norm(wxT("amule"))));
275 ASSERT_EQUALS(expected1
, expected1
.JoinPaths(CPath()));
276 ASSERT_EQUALS(expected1
, CPath().JoinPaths(expected1
));
278 ASSERT_EQUALS(expected2
, expected2
.JoinPaths(CPath()));
279 ASSERT_EQUALS(expected2
, CPath().JoinPaths(expected2
));
283 TEST(CPath
, StartsWith
)
285 const CPath test
= Norm(wxT("/home/amule/"));
287 ASSERT_FALSE(test
.StartsWith(CPath()));
288 ASSERT_FALSE(CPath().StartsWith(test
));
290 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/"))));
291 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/home"))));
292 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/home/"))));
293 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/home/amule"))));
294 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/home/amule/"))));
296 ASSERT_FALSE(test
.StartsWith(Norm(wxT(""))));
297 ASSERT_FALSE(test
.StartsWith(Norm(wxT("~"))));
298 ASSERT_FALSE(test
.StartsWith(Norm(wxT("/ho"))));
299 ASSERT_FALSE(test
.StartsWith(Norm(wxT("/home/am"))));
300 ASSERT_FALSE(test
.StartsWith(Norm(wxT("/home/amule2"))));
301 ASSERT_FALSE(test
.StartsWith(Norm(wxT("/home/amule/foo"))));
305 TEST(CPath
, IsSameDir
)
307 ASSERT_TRUE(Norm(wxT("/root")).IsSameDir(Norm(wxT("/root"))));
308 ASSERT_TRUE(Norm(wxT("/root/")).IsSameDir(Norm(wxT("/root"))));
309 ASSERT_TRUE(Norm(wxT("/root")).IsSameDir(Norm(wxT("/root/"))));
310 ASSERT_TRUE(Norm(wxT("/root/")).IsSameDir(Norm(wxT("/root/"))));
312 ASSERT_FALSE(CPath().IsSameDir(Norm(wxT("/"))));
313 ASSERT_FALSE(Norm(wxT("/")).IsSameDir(CPath()));
315 ASSERT_FALSE(Norm(wxT("/root")).IsSameDir(Norm(wxT("/home"))));
319 TEST(CPath
, GetPath_FullName
)
322 ASSERT_EQUALS(CPath().GetPath(), CPath());
323 ASSERT_EQUALS(CPath().GetFullName(), CPath());
327 const CPath path
= Norm(wxT("/home/mule/"));
329 ASSERT_EQUALS(path
.GetPath(), Norm(wxT("/home/mule")));
330 ASSERT_EQUALS(path
.GetFullName(), CPath());
334 const CPath path
= Norm(wxT("/home/mule"));
336 ASSERT_EQUALS(path
.GetPath(), Norm(wxT("/home")));
337 ASSERT_EQUALS(path
.GetFullName(), Norm(wxT("mule")));
341 const CPath path
= Norm(wxT("mule"));
343 ASSERT_EQUALS(path
.GetPath(), CPath());
344 ASSERT_EQUALS(path
.GetFullName(), Norm(wxT("mule")));
348 const CPath path
= Norm(wxT("mule.ext"));
350 ASSERT_EQUALS(path
.GetPath(), CPath());
351 ASSERT_EQUALS(path
.GetFullName(), Norm(wxT("mule.ext")));
358 const CPath initial
= CPath(wxT(" /a\"b*c* <d>?e|\\:f "));
360 ASSERT_EQUALS(initial
.Cleanup(false, false), Norm(wxT("\%20a\"b*c*\%20<d>?e|\\:f\%20")));
361 ASSERT_EQUALS(initial
.Cleanup(false, true), Norm(wxT("\%20abc\%20def\%20")));
362 ASSERT_EQUALS(initial
.Cleanup(true, false), Norm(wxT(" a\"b*c* <d>?e|\\:f ")));
363 ASSERT_EQUALS(initial
.Cleanup(true, true), Norm(wxT(" abc def ")));
367 TEST(CPath
, AddPostFix
)
369 ASSERT_EQUALS(Norm(wxT("/foo.bar")).AddPostfix(wxT("_1")), Norm(wxT("/foo_1.bar")));
370 ASSERT_EQUALS(Norm(wxT("/foo.bar")).AddPostfix(wxT("")), Norm(wxT("/foo.bar")));
371 ASSERT_EQUALS(Norm(wxT("/.bar")).AddPostfix(wxT("_1")), Norm(wxT("/.bar_1")));
372 ASSERT_EQUALS(Norm(wxT("/")).AddPostfix(wxT("_1")), Norm(wxT("/_1")));
376 TEST(CPath
, Extensions
)
378 const CPath initial
= Norm(wxT("/home/amule.foo.bar"));
380 ASSERT_EQUALS(Norm(wxT("/home/amule.foo")), initial
.RemoveExt());
381 ASSERT_EQUALS(Norm(wxT("/home/amule")), initial
.RemoveExt().RemoveExt());
382 ASSERT_EQUALS(Norm(wxT("/home/amule")), initial
.RemoveExt().RemoveExt().RemoveExt());
383 ASSERT_EQUALS(Norm(wxT("/home/amule")), initial
.RemoveAllExt());
385 ASSERT_EQUALS(wxT("bar"), initial
.GetExt());
386 ASSERT_EQUALS(wxT("foo"), initial
.RemoveExt().GetExt());
387 ASSERT_EQUALS(wxEmptyString
, initial
.RemoveExt().RemoveExt().GetExt());
388 ASSERT_EQUALS(wxEmptyString
, initial
.RemoveAllExt().GetExt());
390 ASSERT_EQUALS(Norm(wxT("/home/amule.foo.bar")), initial
.AppendExt(wxT("")));
391 ASSERT_EQUALS(Norm(wxT("/home/amule.foo.bar.zod")), initial
.AppendExt(wxT("zod")));
392 ASSERT_EQUALS(Norm(wxT("/home/amule.foo.bar.zod")), initial
.AppendExt(wxT(".zod")));
393 ASSERT_EQUALS(Norm(wxT("/home/amule.zod")), initial
.RemoveAllExt().AppendExt(wxT("zod")));
394 ASSERT_EQUALS(Norm(wxT("/home/amule.zod")), initial
.RemoveAllExt().AppendExt(wxT(".zod")));
397 TEST(CPath
, TruncatePath
)
399 const CPath testPath
= Norm(wxT("/home/amule/truncate"));
401 ASSERT_EQUALS(Norm(wxT("/home/amule/truncate")).GetPrintable(), testPath
.TruncatePath(21));
402 ASSERT_EQUALS(Norm(wxT("/home/amule/truncate")).GetPrintable(), testPath
.TruncatePath(20));
403 ASSERT_EQUALS(Norm(wxT("/home/amule/tr[...]")).GetPrintable(), testPath
.TruncatePath(19));
404 ASSERT_EQUALS(Norm(wxT("/h[...]")).GetPrintable(), testPath
.TruncatePath(7));
405 ASSERT_EQUALS(Norm(wxT("/[...]")).GetPrintable(), testPath
.TruncatePath(6));
406 ASSERT_EQUALS(wxEmptyString
, testPath
.TruncatePath(5));
407 ASSERT_EQUALS(wxEmptyString
, testPath
.TruncatePath(4));
409 ASSERT_EQUALS(Norm(wxT("/home/amule/truncate")).GetPrintable(), testPath
.TruncatePath(21, true));
410 ASSERT_EQUALS(Norm(wxT("/home/amule/truncate")).GetPrintable(), testPath
.TruncatePath(20, true));
411 ASSERT_EQUALS(Norm(wxT("[...]amule/truncate")).GetPrintable(), testPath
.TruncatePath(19, true));
412 ASSERT_EQUALS(Norm(wxT("[...]e/truncate")).GetPrintable(), testPath
.TruncatePath(15, true));
413 ASSERT_EQUALS(wxT("truncate"), testPath
.TruncatePath(14, true));
414 ASSERT_EQUALS(wxT("truncate"), testPath
.TruncatePath(13, true));
415 ASSERT_EQUALS(wxT("truncate"), testPath
.TruncatePath(9, true));
416 ASSERT_EQUALS(wxT("truncate"), testPath
.TruncatePath(8, true));
417 ASSERT_EQUALS(wxT("tr[...]"), testPath
.TruncatePath(7, true));
418 ASSERT_EQUALS(wxT("t[...]"), testPath
.TruncatePath(6, true));
419 ASSERT_EQUALS(wxEmptyString
, testPath
.TruncatePath(5, true));