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
.GetPrintable();
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());
107 TEST(CPath
, PathConstructor
)
112 ASSERT_FALSE(tmp
.IsOk());
113 ASSERT_EQUALS(tmp
, CPath());
117 for (size_t i
= 0; i
< ArraySize(g_fromFSTests
); ++i
) {
118 const wxString input
= g_fromFSTests
[i
].input
;
119 const wxString result
= g_fromFSTests
[i
].expected
;
121 ASSERT_TRUE(result
.Length());
122 ASSERT_TRUE(input
.Length());
126 ASSERT_TRUE(tmp
.IsOk());
127 ASSERT_EQUALS(tmp
, CPath(input
));
129 ASSERT_EQUALS(tmp
.GetRaw(), ::GetExpectedString(input
));
130 ASSERT_EQUALS(tmp
.GetPrintable(), result
);
132 ASSERT_EQUALS(tmp
.GetFullName(), tmp
);
133 ASSERT_EQUALS(tmp
.GetPath(), CPath());
138 TEST(CPath
, CopyConstructor
)
140 const wxChar
* tmpPath
= wxT("foobar.tgz");
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
));
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
));
164 ASSERT_TRUE(a
.IsOk());
165 ASSERT_EQUALS(a
.GetRaw(), tmpPath
);
166 ASSERT_EQUALS(a
.GetPrintable(), tmpPath
);
167 ASSERT_EQUALS(a
.GetPath(), CPath());
168 ASSERT_EQUALS(a
.GetFullName(), CPath(tmpPath
));
171 ASSERT_TRUE(b
.IsOk());
172 ASSERT_EQUALS(b
.GetRaw(), tmpPath
);
173 ASSERT_EQUALS(b
.GetPrintable(), tmpPath
);
174 ASSERT_EQUALS(b
.GetPath(), CPath());
175 ASSERT_EQUALS(b
.GetFullName(), CPath(tmpPath
));
185 ASSERT_FALSE(a
.IsOk());
186 ASSERT_FALSE(b
.IsOk());
191 TEST(CPath
, Operators
)
193 const wxChar
* tmpPath1
= wxT("foobar.tgz");
194 const wxChar
* tmpPath2
= wxT("barfoo.tar");
199 ASSERT_FALSE(a
.IsOk());
200 ASSERT_FALSE(b
.IsOk());
203 ASSERT_EQUALS(a
.GetRaw(), tmpPath1
);
204 ASSERT_EQUALS(a
.GetPrintable(), tmpPath1
);
205 ASSERT_EQUALS(a
.GetPath(), CPath());
206 ASSERT_EQUALS(a
.GetFullName(), CPath(tmpPath1
));
207 ASSERT_TRUE(a
.IsOk());
211 ASSERT_EQUALS(b
.GetRaw(), tmpPath1
);
212 ASSERT_EQUALS(b
.GetPrintable(), tmpPath1
);
213 ASSERT_EQUALS(b
.GetPath(), CPath());
214 ASSERT_EQUALS(b
.GetFullName(), CPath(tmpPath1
));
215 ASSERT_TRUE(a
.IsOk());
216 ASSERT_TRUE(b
.IsOk());
220 ASSERT_TRUE(a
.IsOk());
221 ASSERT_TRUE(b
.IsOk());
224 ASSERT_EQUALS(a
.GetRaw(), tmpPath2
);
225 ASSERT_EQUALS(a
.GetPrintable(), tmpPath2
);
226 ASSERT_EQUALS(a
.GetPath(), CPath());
227 ASSERT_EQUALS(a
.GetFullName(), CPath(tmpPath2
));
228 ASSERT_EQUALS(b
.GetRaw(), tmpPath1
);
229 ASSERT_EQUALS(b
.GetPrintable(), tmpPath1
);
230 ASSERT_EQUALS(b
.GetPath(), CPath());
231 ASSERT_EQUALS(b
.GetFullName(), CPath(tmpPath1
));
234 // See note in CPath::operator==
237 CPath a
= CPath(wxT("/home/amule"));
238 CPath b
= CPath(wxT("/hone/amule/"));
248 /** Return the path normalized for the current platform. */
249 CPath
Norm(wxString str
)
251 str
.Replace(wxT("/"), wxString(wxFileName::GetPathSeparator()));
257 TEST(CPath
, JoinPaths
)
259 const CPath expected1
= Norm(wxT("/home/amule/"));
260 const CPath expected2
= Norm(wxT("/home/amule"));
262 // Note: Just a few checks, as ::JoinPaths is tested above
263 ASSERT_EQUALS(expected1
, Norm(wxT("/home")).JoinPaths(Norm(wxT("amule/"))));
264 ASSERT_EQUALS(expected1
, Norm(wxT("/home")).JoinPaths(Norm(wxT("/amule/"))));
265 ASSERT_EQUALS(expected1
, Norm(wxT("/home/")).JoinPaths(Norm(wxT("/amule/"))));
266 ASSERT_EQUALS(expected1
, Norm(wxT("/home/")).JoinPaths(Norm(wxT("amule/"))));
268 ASSERT_EQUALS(expected2
, Norm(wxT("/home")).JoinPaths(Norm(wxT("amule"))));
269 ASSERT_EQUALS(expected2
, 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"))));
273 ASSERT_EQUALS(expected1
, expected1
.JoinPaths(CPath()));
274 ASSERT_EQUALS(expected1
, CPath().JoinPaths(expected1
));
276 ASSERT_EQUALS(expected2
, expected2
.JoinPaths(CPath()));
277 ASSERT_EQUALS(expected2
, CPath().JoinPaths(expected2
));
281 TEST(CPath
, StartsWith
)
283 const CPath test
= Norm(wxT("/home/amule/"));
285 ASSERT_FALSE(test
.StartsWith(CPath()));
286 ASSERT_FALSE(CPath().StartsWith(test
));
288 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/"))));
289 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/home"))));
290 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/home/"))));
291 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/home/amule"))));
292 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/home/amule/"))));
294 ASSERT_FALSE(test
.StartsWith(Norm(wxT(""))));
295 ASSERT_FALSE(test
.StartsWith(Norm(wxT("~"))));
296 ASSERT_FALSE(test
.StartsWith(Norm(wxT("/ho"))));
297 ASSERT_FALSE(test
.StartsWith(Norm(wxT("/home/am"))));
298 ASSERT_FALSE(test
.StartsWith(Norm(wxT("/home/amule2"))));
299 ASSERT_FALSE(test
.StartsWith(Norm(wxT("/home/amule/foo"))));
303 TEST(CPath
, IsSameDir
)
305 ASSERT_TRUE(Norm(wxT("/root")).IsSameDir(Norm(wxT("/root"))));
306 ASSERT_TRUE(Norm(wxT("/root/")).IsSameDir(Norm(wxT("/root"))));
307 ASSERT_TRUE(Norm(wxT("/root")).IsSameDir(Norm(wxT("/root/"))));
308 ASSERT_TRUE(Norm(wxT("/root/")).IsSameDir(Norm(wxT("/root/"))));
310 ASSERT_FALSE(CPath().IsSameDir(Norm(wxT("/"))));
311 ASSERT_FALSE(Norm(wxT("/")).IsSameDir(CPath()));
313 ASSERT_FALSE(Norm(wxT("/root")).IsSameDir(Norm(wxT("/home"))));
317 TEST(CPath
, GetPath_FullName
)
320 ASSERT_EQUALS(CPath().GetPath(), CPath());
321 ASSERT_EQUALS(CPath().GetFullName(), CPath());
325 const CPath path
= Norm(wxT("/home/mule/"));
327 ASSERT_EQUALS(path
.GetPath(), Norm(wxT("/home/mule")));
328 ASSERT_EQUALS(path
.GetFullName(), CPath());
332 const CPath path
= Norm(wxT("/home/mule"));
334 ASSERT_EQUALS(path
.GetPath(), Norm(wxT("/home")));
335 ASSERT_EQUALS(path
.GetFullName(), Norm(wxT("mule")));
339 const CPath path
= Norm(wxT("mule"));
341 ASSERT_EQUALS(path
.GetPath(), CPath());
342 ASSERT_EQUALS(path
.GetFullName(), Norm(wxT("mule")));
346 const CPath path
= Norm(wxT("mule.ext"));
348 ASSERT_EQUALS(path
.GetPath(), CPath());
349 ASSERT_EQUALS(path
.GetFullName(), Norm(wxT("mule.ext")));
356 const CPath initial
= CPath(wxT(" /a\"b*c* <d>?e|\\:f "));
358 ASSERT_EQUALS(initial
.Cleanup(false, false), Norm(wxT("\%20a\"b*c*\%20<d>?e|\\:f\%20")));
359 ASSERT_EQUALS(initial
.Cleanup(false, true), Norm(wxT("\%20abc\%20def\%20")));
360 ASSERT_EQUALS(initial
.Cleanup(true, false), Norm(wxT(" a\"b*c* <d>?e|\\:f ")));
361 ASSERT_EQUALS(initial
.Cleanup(true, true), Norm(wxT(" abc def ")));
365 TEST(CPath
, AddPostFix
)
367 ASSERT_EQUALS(Norm(wxT("/foo.bar")).AddPostfix(wxT("_1")), Norm(wxT("/foo_1.bar")));
368 ASSERT_EQUALS(Norm(wxT("/foo.bar")).AddPostfix(wxT("")), Norm(wxT("/foo.bar")));
369 ASSERT_EQUALS(Norm(wxT("/.bar")).AddPostfix(wxT("_1")), Norm(wxT("/.bar_1")));
370 ASSERT_EQUALS(Norm(wxT("/")).AddPostfix(wxT("_1")), Norm(wxT("/_1")));
374 TEST(CPath
, Extensions
)
376 const CPath initial
= Norm(wxT("/home/amule.foo.bar"));
378 ASSERT_EQUALS(Norm(wxT("/home/amule.foo")), initial
.RemoveExt());
379 ASSERT_EQUALS(Norm(wxT("/home/amule")), initial
.RemoveExt().RemoveExt());
380 ASSERT_EQUALS(Norm(wxT("/home/amule")), initial
.RemoveExt().RemoveExt().RemoveExt());
381 ASSERT_EQUALS(Norm(wxT("/home/amule")), initial
.RemoveAllExt());
383 ASSERT_EQUALS(wxT("bar"), initial
.GetExt());
384 ASSERT_EQUALS(wxT("foo"), initial
.RemoveExt().GetExt());
385 ASSERT_EQUALS(wxEmptyString
, initial
.RemoveExt().RemoveExt().GetExt());
386 ASSERT_EQUALS(wxEmptyString
, initial
.RemoveAllExt().GetExt());
388 ASSERT_EQUALS(Norm(wxT("/home/amule.foo.bar")), initial
.AppendExt(wxT("")));
389 ASSERT_EQUALS(Norm(wxT("/home/amule.foo.bar.zod")), initial
.AppendExt(wxT("zod")));
390 ASSERT_EQUALS(Norm(wxT("/home/amule.foo.bar.zod")), initial
.AppendExt(wxT(".zod")));
391 ASSERT_EQUALS(Norm(wxT("/home/amule.zod")), initial
.RemoveAllExt().AppendExt(wxT("zod")));
392 ASSERT_EQUALS(Norm(wxT("/home/amule.zod")), initial
.RemoveAllExt().AppendExt(wxT(".zod")));
395 TEST(CPath
, TruncatePath
)
397 const CPath testPath
= Norm(wxT("/home/amule/truncate"));
399 ASSERT_EQUALS(Norm(wxT("/home/amule/truncate")).GetPrintable(), testPath
.TruncatePath(21));
400 ASSERT_EQUALS(Norm(wxT("/home/amule/truncate")).GetPrintable(), testPath
.TruncatePath(20));
401 ASSERT_EQUALS(Norm(wxT("/home/amule/tr[...]")).GetPrintable(), testPath
.TruncatePath(19));
402 ASSERT_EQUALS(Norm(wxT("/h[...]")).GetPrintable(), testPath
.TruncatePath(7));
403 ASSERT_EQUALS(Norm(wxT("/[...]")).GetPrintable(), testPath
.TruncatePath(6));
404 ASSERT_EQUALS(wxEmptyString
, testPath
.TruncatePath(5));
405 ASSERT_EQUALS(wxEmptyString
, testPath
.TruncatePath(4));
407 ASSERT_EQUALS(Norm(wxT("/home/amule/truncate")).GetPrintable(), testPath
.TruncatePath(21, true));
408 ASSERT_EQUALS(Norm(wxT("/home/amule/truncate")).GetPrintable(), testPath
.TruncatePath(20, true));
409 ASSERT_EQUALS(Norm(wxT("[...]amule/truncate")).GetPrintable(), testPath
.TruncatePath(19, true));
410 ASSERT_EQUALS(Norm(wxT("[...]e/truncate")).GetPrintable(), testPath
.TruncatePath(15, true));
411 ASSERT_EQUALS(wxT("truncate"), testPath
.TruncatePath(14, true));
412 ASSERT_EQUALS(wxT("truncate"), testPath
.TruncatePath(13, true));
413 ASSERT_EQUALS(wxT("truncate"), testPath
.TruncatePath(9, true));
414 ASSERT_EQUALS(wxT("truncate"), testPath
.TruncatePath(8, true));
415 ASSERT_EQUALS(wxT("tr[...]"), testPath
.TruncatePath(7, true));
416 ASSERT_EQUALS(wxT("t[...]"), testPath
.TruncatePath(6, true));
417 ASSERT_EQUALS(wxEmptyString
, testPath
.TruncatePath(5, true));