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 * The validity of this test is at least questionable. The result depends on
13 * - the current system locale and character encoding used, and
14 * - the way wxConvFileName reports error conditions and its fallback behaviour.
18 //! Specifies the "source" of the string (user/fs)
22 //! The expected result.
23 const wxString expected;
26 const STestStr g_fromFSTests[] = {
28 { true, wxConvFileName->cMB2WC("\xe1\x62\x63"), wxT("\xe1\x62\x63") },
29 { true, wxConvFileName->cMB2WC("\xe6\xf8\xe5"), wxT("\xe6\xf8\xe5") },
30 { true, wxConvFileName->cMB2WC("\xd8\xa7\xd9\x84\xd8\xb9"), wxT("\u0627\u0644\u0639") },
33 { false, wxT("\u0627\u0644\u0639"), wxT("\u0627\u0644\u0639") }
42 wxString GetExpectedString(const wxString& src)
44 // Determine how the filenames are expected to be mangled
45 static UsesEncoding encoding = UE_Unknown;
47 if (encoding == UE_Unknown) {
48 wxCharBuffer fn = wxConvFile.cWC2MB(wxT("\u0627\u0644\u0639"));
51 encoding = UE_NonBroken;
57 if ((encoding == UE_Broken) && !wxConvFile.cWC2MB(src)) {
58 // See CPath::CPath for rationale ...
59 wxCharBuffer fn = wxConvUTF8.cWC2MB(src);
60 return wxConvFile.cMB2WC(fn);
68 wxString
StringFrom(const CPath
& prt
)
70 return prt
.GetPrintable();
74 DECLARE_SIMPLE(GenericPathFunctions
)
76 TEST(GenericPathFunctions
, JoinPaths
)
78 const wxString seps
= wxFileName::GetPathSeparators();
79 const wxString sep
= wxFileName::GetPathSeparator();
81 for (size_t i
= 0; i
< seps
.Length(); ++i
) {
82 const wxString cur_sep
= seps
.Mid(i
, 1);
84 ASSERT_EQUALS(wxT("a") + sep
+ wxT("b"), JoinPaths(wxT("a"), wxT("b")));
85 ASSERT_EQUALS(wxT("a") + sep
+ wxT("b"), JoinPaths(wxT("a") + cur_sep
, wxT("b")));
86 ASSERT_EQUALS(wxT("a") + sep
+ wxT("b"), JoinPaths(wxT("a"), cur_sep
+ wxT("b")));
87 ASSERT_EQUALS(wxT("a") + sep
+ wxT("b"), JoinPaths(wxT("a") + cur_sep
, cur_sep
+ wxT("b")));
88 ASSERT_EQUALS(wxT("a"), JoinPaths(wxT("a"), wxEmptyString
));
89 ASSERT_EQUALS(wxT("b"), JoinPaths(wxEmptyString
, wxT("b")));
90 ASSERT_EQUALS(wxEmptyString
, JoinPaths(wxEmptyString
, wxEmptyString
));
97 TEST(CPath
, DefaultConstructor
)
101 ASSERT_FALSE(tmp
.IsOk());
102 ASSERT_EQUALS(tmp
, CPath());
104 ASSERT_FALSE(tmp
.FileExists());
105 ASSERT_FALSE(tmp
.DirExists());
107 ASSERT_EQUALS(wxEmptyString
, tmp
.GetRaw());
108 ASSERT_EQUALS(wxEmptyString
, tmp
.GetPrintable());
109 ASSERT_EQUALS(CPath(), tmp
.GetPath());
110 ASSERT_EQUALS(CPath(), tmp
.GetFullName());
114 TEST(CPath
, PathConstructor
)
119 ASSERT_FALSE(tmp
.IsOk());
120 ASSERT_EQUALS(tmp
, CPath());
127 for (size_t i = 0; i < ArraySize(g_fromFSTests); ++i) {
128 const wxString input = g_fromFSTests[i].input;
129 const wxString result = g_fromFSTests[i].expected;
131 ASSERT_TRUE(result.Length());
132 ASSERT_TRUE(input.Length());
136 ASSERT_TRUE(tmp.IsOk());
137 ASSERT_EQUALS(CPath(input), tmp);
139 ASSERT_EQUALS(::GetExpectedString(input), tmp.GetRaw());
140 ASSERT_EQUALS(result, tmp.GetPrintable());
142 ASSERT_EQUALS(tmp, tmp.GetFullName());
143 ASSERT_EQUALS(CPath(), tmp.GetPath());
150 TEST(CPath
, CopyConstructor
)
152 const wxChar
* tmpPath
= wxT("foobar.tgz");
157 ASSERT_TRUE(a
.IsOk());
158 ASSERT_EQUALS(tmpPath
, a
.GetRaw());
159 ASSERT_EQUALS(tmpPath
, a
.GetPrintable());
160 ASSERT_EQUALS(CPath(), a
.GetPath());
161 ASSERT_EQUALS(CPath(tmpPath
), a
.GetFullName());
164 ASSERT_TRUE(b
.IsOk());
165 ASSERT_EQUALS(tmpPath
, b
.GetRaw());
166 ASSERT_EQUALS(tmpPath
, b
.GetPrintable());
167 ASSERT_EQUALS(CPath(), b
.GetPath());
168 ASSERT_EQUALS(CPath(tmpPath
), b
.GetFullName());
178 ASSERT_FALSE(a
.IsOk());
179 ASSERT_FALSE(b
.IsOk());
184 TEST(CPath
, Operators
)
186 const wxChar
* tmpPath1
= wxT("foobar.tgz");
187 const wxChar
* tmpPath2
= wxT("barfoo.tar");
192 ASSERT_FALSE(a
.IsOk());
193 ASSERT_FALSE(b
.IsOk());
196 ASSERT_EQUALS(tmpPath1
, a
.GetRaw());
197 ASSERT_EQUALS(tmpPath1
, a
.GetPrintable());
198 ASSERT_EQUALS(CPath(), a
.GetPath());
199 ASSERT_EQUALS(CPath(tmpPath1
), a
.GetFullName());
200 ASSERT_TRUE(a
.IsOk());
204 ASSERT_EQUALS(tmpPath1
, b
.GetRaw());
205 ASSERT_EQUALS(tmpPath1
, b
.GetPrintable());
206 ASSERT_EQUALS(CPath(), b
.GetPath());
207 ASSERT_EQUALS(CPath(tmpPath1
), b
.GetFullName());
208 ASSERT_TRUE(a
.IsOk());
209 ASSERT_TRUE(b
.IsOk());
213 ASSERT_TRUE(a
.IsOk());
214 ASSERT_TRUE(b
.IsOk());
217 ASSERT_EQUALS(tmpPath2
, a
.GetRaw());
218 ASSERT_EQUALS(tmpPath2
, a
.GetPrintable());
219 ASSERT_EQUALS(CPath(), a
.GetPath());
220 ASSERT_EQUALS(CPath(tmpPath2
), a
.GetFullName());
221 ASSERT_EQUALS(tmpPath1
, b
.GetRaw());
222 ASSERT_EQUALS(tmpPath1
, b
.GetPrintable());
223 ASSERT_EQUALS(CPath(), b
.GetPath());
224 ASSERT_EQUALS(CPath(tmpPath1
), b
.GetFullName());
227 // See note in CPath::operator==
230 CPath a
= CPath(wxT("/home/amule"));
231 CPath b
= CPath(wxT("/hone/amule/"));
241 /** Return the path normalized for the current platform. */
242 CPath
Norm(wxString str
)
244 str
.Replace(wxT("/"), wxString(wxFileName::GetPathSeparator()));
250 TEST(CPath
, JoinPaths
)
252 const CPath expected1
= Norm(wxT("/home/amule/"));
253 const CPath expected2
= Norm(wxT("/home/amule"));
255 // Note: Just a few checks, as ::JoinPaths is tested above
256 ASSERT_EQUALS(expected1
, Norm(wxT("/home")).JoinPaths(Norm(wxT("amule/"))));
257 ASSERT_EQUALS(expected1
, Norm(wxT("/home")).JoinPaths(Norm(wxT("/amule/"))));
258 ASSERT_EQUALS(expected1
, Norm(wxT("/home/")).JoinPaths(Norm(wxT("/amule/"))));
259 ASSERT_EQUALS(expected1
, Norm(wxT("/home/")).JoinPaths(Norm(wxT("amule/"))));
261 ASSERT_EQUALS(expected2
, Norm(wxT("/home")).JoinPaths(Norm(wxT("amule"))));
262 ASSERT_EQUALS(expected2
, Norm(wxT("/home")).JoinPaths(Norm(wxT("/amule"))));
263 ASSERT_EQUALS(expected2
, Norm(wxT("/home/")).JoinPaths(Norm(wxT("/amule"))));
264 ASSERT_EQUALS(expected2
, Norm(wxT("/home/")).JoinPaths(Norm(wxT("amule"))));
266 ASSERT_EQUALS(expected1
, expected1
.JoinPaths(CPath()));
267 ASSERT_EQUALS(expected1
, CPath().JoinPaths(expected1
));
269 ASSERT_EQUALS(expected2
, expected2
.JoinPaths(CPath()));
270 ASSERT_EQUALS(expected2
, CPath().JoinPaths(expected2
));
274 TEST(CPath
, StartsWith
)
276 const CPath test
= Norm(wxT("/home/amule/"));
278 ASSERT_FALSE(test
.StartsWith(CPath()));
279 ASSERT_FALSE(CPath().StartsWith(test
));
281 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/"))));
282 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/home"))));
283 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/home/"))));
284 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/home/amule"))));
285 ASSERT_TRUE(test
.StartsWith(Norm(wxT("/home/amule/"))));
287 ASSERT_FALSE(test
.StartsWith(Norm(wxT(""))));
288 ASSERT_FALSE(test
.StartsWith(Norm(wxT("~"))));
289 ASSERT_FALSE(test
.StartsWith(Norm(wxT("/ho"))));
290 ASSERT_FALSE(test
.StartsWith(Norm(wxT("/home/am"))));
291 ASSERT_FALSE(test
.StartsWith(Norm(wxT("/home/amule2"))));
292 ASSERT_FALSE(test
.StartsWith(Norm(wxT("/home/amule/foo"))));
296 TEST(CPath
, IsSameDir
)
298 ASSERT_TRUE(Norm(wxT("/root")).IsSameDir(Norm(wxT("/root"))));
299 ASSERT_TRUE(Norm(wxT("/root/")).IsSameDir(Norm(wxT("/root"))));
300 ASSERT_TRUE(Norm(wxT("/root")).IsSameDir(Norm(wxT("/root/"))));
301 ASSERT_TRUE(Norm(wxT("/root/")).IsSameDir(Norm(wxT("/root/"))));
303 ASSERT_FALSE(CPath().IsSameDir(Norm(wxT("/"))));
304 ASSERT_FALSE(Norm(wxT("/")).IsSameDir(CPath()));
306 ASSERT_FALSE(Norm(wxT("/root")).IsSameDir(Norm(wxT("/home"))));
310 TEST(CPath
, GetPath_FullName
)
313 ASSERT_EQUALS(CPath(), CPath().GetPath());
314 ASSERT_EQUALS(CPath(), CPath().GetFullName());
318 const CPath path
= Norm(wxT("/home/mule/"));
320 ASSERT_EQUALS(Norm(wxT("/home/mule")), path
.GetPath());
321 ASSERT_EQUALS(CPath(), path
.GetFullName());
325 const CPath path
= Norm(wxT("/home/mule"));
327 ASSERT_EQUALS(Norm(wxT("/home")), path
.GetPath());
328 ASSERT_EQUALS(Norm(wxT("mule")), path
.GetFullName());
332 const CPath path
= Norm(wxT("mule"));
334 ASSERT_EQUALS(CPath(), path
.GetPath());
335 ASSERT_EQUALS(Norm(wxT("mule")), path
.GetFullName());
339 const CPath path
= Norm(wxT("mule.ext"));
341 ASSERT_EQUALS(CPath(), path
.GetPath());
342 ASSERT_EQUALS(Norm(wxT("mule.ext")), path
.GetFullName());
349 const CPath initial
= CPath(wxT(" /a\"b*c* <d>?e|\\:f "));
351 ASSERT_EQUALS(Norm(wxT("\%20a\"b*c*\%20<d>?e|\\:f\%20")), initial
.Cleanup(false, false));
352 ASSERT_EQUALS(Norm(wxT("\%20abc\%20def\%20")), initial
.Cleanup(false, true));
353 ASSERT_EQUALS(Norm(wxT(" a\"b*c* <d>?e|\\:f ")), initial
.Cleanup(true, false));
354 ASSERT_EQUALS(Norm(wxT(" abc def ")), initial
.Cleanup(true, true));
358 TEST(CPath
, AddPostFix
)
360 ASSERT_EQUALS(Norm(wxT("/foo_1.bar")), Norm(wxT("/foo.bar")).AddPostfix(wxT("_1")));
361 ASSERT_EQUALS(Norm(wxT("/foo.bar")), Norm(wxT("/foo.bar")).AddPostfix(wxT("")));
362 ASSERT_EQUALS(Norm(wxT("/.bar_1")), Norm(wxT("/.bar")).AddPostfix(wxT("_1")));
363 ASSERT_EQUALS(Norm(wxT("/_1")), Norm(wxT("/")).AddPostfix(wxT("_1")));
367 TEST(CPath
, Extensions
)
369 const CPath initial
= Norm(wxT("/home/amule.foo.bar"));
371 ASSERT_EQUALS(Norm(wxT("/home/amule.foo")), initial
.RemoveExt());
372 ASSERT_EQUALS(Norm(wxT("/home/amule")), initial
.RemoveExt().RemoveExt());
373 ASSERT_EQUALS(Norm(wxT("/home/amule")), initial
.RemoveExt().RemoveExt().RemoveExt());
374 ASSERT_EQUALS(Norm(wxT("/home/amule")), initial
.RemoveAllExt());
376 ASSERT_EQUALS(wxT("bar"), initial
.GetExt());
377 ASSERT_EQUALS(wxT("foo"), initial
.RemoveExt().GetExt());
378 ASSERT_EQUALS(wxEmptyString
, initial
.RemoveExt().RemoveExt().GetExt());
379 ASSERT_EQUALS(wxEmptyString
, initial
.RemoveAllExt().GetExt());
381 ASSERT_EQUALS(Norm(wxT("/home/amule.foo.bar")), initial
.AppendExt(wxT("")));
382 ASSERT_EQUALS(Norm(wxT("/home/amule.foo.bar.zod")), initial
.AppendExt(wxT("zod")));
383 ASSERT_EQUALS(Norm(wxT("/home/amule.foo.bar.zod")), initial
.AppendExt(wxT(".zod")));
384 ASSERT_EQUALS(Norm(wxT("/home/amule.zod")), initial
.RemoveAllExt().AppendExt(wxT("zod")));
385 ASSERT_EQUALS(Norm(wxT("/home/amule.zod")), initial
.RemoveAllExt().AppendExt(wxT(".zod")));
388 TEST(CPath
, TruncatePath
)
390 const CPath testPath
= Norm(wxT("/home/amule/truncate"));
392 ASSERT_EQUALS(Norm(wxT("/home/amule/truncate")).GetPrintable(), testPath
.TruncatePath(21));
393 ASSERT_EQUALS(Norm(wxT("/home/amule/truncate")).GetPrintable(), testPath
.TruncatePath(20));
394 ASSERT_EQUALS(Norm(wxT("/home/amule/tr[...]")).GetPrintable(), testPath
.TruncatePath(19));
395 ASSERT_EQUALS(Norm(wxT("/h[...]")).GetPrintable(), testPath
.TruncatePath(7));
396 ASSERT_EQUALS(Norm(wxT("/[...]")).GetPrintable(), testPath
.TruncatePath(6));
397 ASSERT_EQUALS(wxEmptyString
, testPath
.TruncatePath(5));
398 ASSERT_EQUALS(wxEmptyString
, testPath
.TruncatePath(4));
400 ASSERT_EQUALS(Norm(wxT("/home/amule/truncate")).GetPrintable(), testPath
.TruncatePath(21, true));
401 ASSERT_EQUALS(Norm(wxT("/home/amule/truncate")).GetPrintable(), testPath
.TruncatePath(20, true));
402 ASSERT_EQUALS(Norm(wxT("[...]amule/truncate")).GetPrintable(), testPath
.TruncatePath(19, true));
403 ASSERT_EQUALS(Norm(wxT("[...]e/truncate")).GetPrintable(), testPath
.TruncatePath(15, true));
404 ASSERT_EQUALS(wxT("truncate"), testPath
.TruncatePath(14, true));
405 ASSERT_EQUALS(wxT("truncate"), testPath
.TruncatePath(13, true));
406 ASSERT_EQUALS(wxT("truncate"), testPath
.TruncatePath(9, true));
407 ASSERT_EQUALS(wxT("truncate"), testPath
.TruncatePath(8, true));
408 ASSERT_EQUALS(wxT("tr[...]"), testPath
.TruncatePath(7, true));
409 ASSERT_EQUALS(wxT("t[...]"), testPath
.TruncatePath(6, true));
410 ASSERT_EQUALS(wxEmptyString
, testPath
.TruncatePath(5, true));