1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/file_util.h"
7 #include "base/files/file_path.h"
8 #include "base/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
12 // This macro helps avoid wrapped lines in the test structs.
13 #define FPL(x) FILE_PATH_LITERAL(x)
15 // This macro constructs strings which can contain NULs.
16 #define FPS(x) FilePath::StringType(FPL(x), arraysize(FPL(x)) - 1)
20 struct UnaryTestData
{
21 const FilePath::CharType
* input
;
22 const FilePath::CharType
* expected
;
25 struct UnaryBooleanTestData
{
26 const FilePath::CharType
* input
;
30 struct BinaryTestData
{
31 const FilePath::CharType
* inputs
[2];
32 const FilePath::CharType
* expected
;
35 struct BinaryBooleanTestData
{
36 const FilePath::CharType
* inputs
[2];
40 struct BinaryIntTestData
{
41 const FilePath::CharType
* inputs
[2];
46 const FilePath::CharType
* native
;
50 // file_util winds up using autoreleased objects on the Mac, so this needs
51 // to be a PlatformTest
52 class FilePathTest
: public PlatformTest
{
54 virtual void SetUp() OVERRIDE
{
55 PlatformTest::SetUp();
57 virtual void TearDown() OVERRIDE
{
58 PlatformTest::TearDown();
62 TEST_F(FilePathTest
, DirName
) {
63 const struct UnaryTestData cases
[] = {
64 { FPL(""), FPL(".") },
65 { FPL("aa"), FPL(".") },
66 { FPL("/aa/bb"), FPL("/aa") },
67 { FPL("/aa/bb/"), FPL("/aa") },
68 { FPL("/aa/bb//"), FPL("/aa") },
69 { FPL("/aa/bb/ccc"), FPL("/aa/bb") },
70 { FPL("/aa"), FPL("/") },
71 { FPL("/aa/"), FPL("/") },
72 { FPL("/"), FPL("/") },
73 { FPL("//"), FPL("//") },
74 { FPL("///"), FPL("/") },
75 { FPL("aa/"), FPL(".") },
76 { FPL("aa/bb"), FPL("aa") },
77 { FPL("aa/bb/"), FPL("aa") },
78 { FPL("aa/bb//"), FPL("aa") },
79 { FPL("aa//bb//"), FPL("aa") },
80 { FPL("aa//bb/"), FPL("aa") },
81 { FPL("aa//bb"), FPL("aa") },
82 { FPL("//aa/bb"), FPL("//aa") },
83 { FPL("//aa/"), FPL("//") },
84 { FPL("//aa"), FPL("//") },
85 { FPL("0:"), FPL(".") },
86 { FPL("@:"), FPL(".") },
87 { FPL("[:"), FPL(".") },
88 { FPL("`:"), FPL(".") },
89 { FPL("{:"), FPL(".") },
90 { FPL("\xB3:"), FPL(".") },
91 { FPL("\xC5:"), FPL(".") },
93 { FPL("\x0143:"), FPL(".") },
95 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
96 { FPL("c:"), FPL("c:") },
97 { FPL("C:"), FPL("C:") },
98 { FPL("A:"), FPL("A:") },
99 { FPL("Z:"), FPL("Z:") },
100 { FPL("a:"), FPL("a:") },
101 { FPL("z:"), FPL("z:") },
102 { FPL("c:aa"), FPL("c:") },
103 { FPL("c:/"), FPL("c:/") },
104 { FPL("c://"), FPL("c://") },
105 { FPL("c:///"), FPL("c:/") },
106 { FPL("c:/aa"), FPL("c:/") },
107 { FPL("c:/aa/"), FPL("c:/") },
108 { FPL("c:/aa/bb"), FPL("c:/aa") },
109 { FPL("c:aa/bb"), FPL("c:aa") },
110 #endif // FILE_PATH_USES_DRIVE_LETTERS
111 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
112 { FPL("\\aa\\bb"), FPL("\\aa") },
113 { FPL("\\aa\\bb\\"), FPL("\\aa") },
114 { FPL("\\aa\\bb\\\\"), FPL("\\aa") },
115 { FPL("\\aa\\bb\\ccc"), FPL("\\aa\\bb") },
116 { FPL("\\aa"), FPL("\\") },
117 { FPL("\\aa\\"), FPL("\\") },
118 { FPL("\\"), FPL("\\") },
119 { FPL("\\\\"), FPL("\\\\") },
120 { FPL("\\\\\\"), FPL("\\") },
121 { FPL("aa\\"), FPL(".") },
122 { FPL("aa\\bb"), FPL("aa") },
123 { FPL("aa\\bb\\"), FPL("aa") },
124 { FPL("aa\\bb\\\\"), FPL("aa") },
125 { FPL("aa\\\\bb\\\\"), FPL("aa") },
126 { FPL("aa\\\\bb\\"), FPL("aa") },
127 { FPL("aa\\\\bb"), FPL("aa") },
128 { FPL("\\\\aa\\bb"), FPL("\\\\aa") },
129 { FPL("\\\\aa\\"), FPL("\\\\") },
130 { FPL("\\\\aa"), FPL("\\\\") },
131 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
132 { FPL("c:\\"), FPL("c:\\") },
133 { FPL("c:\\\\"), FPL("c:\\\\") },
134 { FPL("c:\\\\\\"), FPL("c:\\") },
135 { FPL("c:\\aa"), FPL("c:\\") },
136 { FPL("c:\\aa\\"), FPL("c:\\") },
137 { FPL("c:\\aa\\bb"), FPL("c:\\aa") },
138 { FPL("c:aa\\bb"), FPL("c:aa") },
139 #endif // FILE_PATH_USES_DRIVE_LETTERS
140 #endif // FILE_PATH_USES_WIN_SEPARATORS
143 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
144 FilePath
input(cases
[i
].input
);
145 FilePath observed
= input
.DirName();
146 EXPECT_EQ(FilePath::StringType(cases
[i
].expected
), observed
.value()) <<
147 "i: " << i
<< ", input: " << input
.value();
151 TEST_F(FilePathTest
, BaseName
) {
152 const struct UnaryTestData cases
[] = {
153 { FPL(""), FPL("") },
154 { FPL("aa"), FPL("aa") },
155 { FPL("/aa/bb"), FPL("bb") },
156 { FPL("/aa/bb/"), FPL("bb") },
157 { FPL("/aa/bb//"), FPL("bb") },
158 { FPL("/aa/bb/ccc"), FPL("ccc") },
159 { FPL("/aa"), FPL("aa") },
160 { FPL("/"), FPL("/") },
161 { FPL("//"), FPL("//") },
162 { FPL("///"), FPL("/") },
163 { FPL("aa/"), FPL("aa") },
164 { FPL("aa/bb"), FPL("bb") },
165 { FPL("aa/bb/"), FPL("bb") },
166 { FPL("aa/bb//"), FPL("bb") },
167 { FPL("aa//bb//"), FPL("bb") },
168 { FPL("aa//bb/"), FPL("bb") },
169 { FPL("aa//bb"), FPL("bb") },
170 { FPL("//aa/bb"), FPL("bb") },
171 { FPL("//aa/"), FPL("aa") },
172 { FPL("//aa"), FPL("aa") },
173 { FPL("0:"), FPL("0:") },
174 { FPL("@:"), FPL("@:") },
175 { FPL("[:"), FPL("[:") },
176 { FPL("`:"), FPL("`:") },
177 { FPL("{:"), FPL("{:") },
178 { FPL("\xB3:"), FPL("\xB3:") },
179 { FPL("\xC5:"), FPL("\xC5:") },
181 { FPL("\x0143:"), FPL("\x0143:") },
183 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
184 { FPL("c:"), FPL("") },
185 { FPL("C:"), FPL("") },
186 { FPL("A:"), FPL("") },
187 { FPL("Z:"), FPL("") },
188 { FPL("a:"), FPL("") },
189 { FPL("z:"), FPL("") },
190 { FPL("c:aa"), FPL("aa") },
191 { FPL("c:/"), FPL("/") },
192 { FPL("c://"), FPL("//") },
193 { FPL("c:///"), FPL("/") },
194 { FPL("c:/aa"), FPL("aa") },
195 { FPL("c:/aa/"), FPL("aa") },
196 { FPL("c:/aa/bb"), FPL("bb") },
197 { FPL("c:aa/bb"), FPL("bb") },
198 #endif // FILE_PATH_USES_DRIVE_LETTERS
199 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
200 { FPL("\\aa\\bb"), FPL("bb") },
201 { FPL("\\aa\\bb\\"), FPL("bb") },
202 { FPL("\\aa\\bb\\\\"), FPL("bb") },
203 { FPL("\\aa\\bb\\ccc"), FPL("ccc") },
204 { FPL("\\aa"), FPL("aa") },
205 { FPL("\\"), FPL("\\") },
206 { FPL("\\\\"), FPL("\\\\") },
207 { FPL("\\\\\\"), FPL("\\") },
208 { FPL("aa\\"), FPL("aa") },
209 { FPL("aa\\bb"), FPL("bb") },
210 { FPL("aa\\bb\\"), FPL("bb") },
211 { FPL("aa\\bb\\\\"), FPL("bb") },
212 { FPL("aa\\\\bb\\\\"), FPL("bb") },
213 { FPL("aa\\\\bb\\"), FPL("bb") },
214 { FPL("aa\\\\bb"), FPL("bb") },
215 { FPL("\\\\aa\\bb"), FPL("bb") },
216 { FPL("\\\\aa\\"), FPL("aa") },
217 { FPL("\\\\aa"), FPL("aa") },
218 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
219 { FPL("c:\\"), FPL("\\") },
220 { FPL("c:\\\\"), FPL("\\\\") },
221 { FPL("c:\\\\\\"), FPL("\\") },
222 { FPL("c:\\aa"), FPL("aa") },
223 { FPL("c:\\aa\\"), FPL("aa") },
224 { FPL("c:\\aa\\bb"), FPL("bb") },
225 { FPL("c:aa\\bb"), FPL("bb") },
226 #endif // FILE_PATH_USES_DRIVE_LETTERS
227 #endif // FILE_PATH_USES_WIN_SEPARATORS
230 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
231 FilePath
input(cases
[i
].input
);
232 FilePath observed
= input
.BaseName();
233 EXPECT_EQ(FilePath::StringType(cases
[i
].expected
), observed
.value()) <<
234 "i: " << i
<< ", input: " << input
.value();
238 TEST_F(FilePathTest
, Append
) {
239 const struct BinaryTestData cases
[] = {
240 { { FPL(""), FPL("cc") }, FPL("cc") },
241 { { FPL("."), FPL("ff") }, FPL("ff") },
242 { { FPL("/"), FPL("cc") }, FPL("/cc") },
243 { { FPL("/aa"), FPL("") }, FPL("/aa") },
244 { { FPL("/aa/"), FPL("") }, FPL("/aa") },
245 { { FPL("//aa"), FPL("") }, FPL("//aa") },
246 { { FPL("//aa/"), FPL("") }, FPL("//aa") },
247 { { FPL("//"), FPL("aa") }, FPL("//aa") },
248 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
249 { { FPL("c:"), FPL("a") }, FPL("c:a") },
250 { { FPL("c:"), FPL("") }, FPL("c:") },
251 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
252 { { FPL("c://"), FPL("a") }, FPL("c://a") },
253 { { FPL("c:///"), FPL("a") }, FPL("c:/a") },
254 #endif // FILE_PATH_USES_DRIVE_LETTERS
255 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
256 // Append introduces the default separator character, so these test cases
257 // need to be defined with different expected results on platforms that use
258 // different default separator characters.
259 { { FPL("\\"), FPL("cc") }, FPL("\\cc") },
260 { { FPL("\\aa"), FPL("") }, FPL("\\aa") },
261 { { FPL("\\aa\\"), FPL("") }, FPL("\\aa") },
262 { { FPL("\\\\aa"), FPL("") }, FPL("\\\\aa") },
263 { { FPL("\\\\aa\\"), FPL("") }, FPL("\\\\aa") },
264 { { FPL("\\\\"), FPL("aa") }, FPL("\\\\aa") },
265 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb\\cc") },
266 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb\\cc") },
267 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb\\cc") },
268 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb\\cc") },
269 { { FPL("a/b"), FPL("c") }, FPL("a/b\\c") },
270 { { FPL("a/b/"), FPL("c") }, FPL("a/b\\c") },
271 { { FPL("//aa"), FPL("bb") }, FPL("//aa\\bb") },
272 { { FPL("//aa/"), FPL("bb") }, FPL("//aa\\bb") },
273 { { FPL("\\aa\\bb"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
274 { { FPL("\\aa\\bb\\"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
275 { { FPL("aa\\bb\\"), FPL("cc") }, FPL("aa\\bb\\cc") },
276 { { FPL("aa\\bb"), FPL("cc") }, FPL("aa\\bb\\cc") },
277 { { FPL("a\\b"), FPL("c") }, FPL("a\\b\\c") },
278 { { FPL("a\\b\\"), FPL("c") }, FPL("a\\b\\c") },
279 { { FPL("\\\\aa"), FPL("bb") }, FPL("\\\\aa\\bb") },
280 { { FPL("\\\\aa\\"), FPL("bb") }, FPL("\\\\aa\\bb") },
281 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
282 { { FPL("c:\\"), FPL("a") }, FPL("c:\\a") },
283 { { FPL("c:\\\\"), FPL("a") }, FPL("c:\\\\a") },
284 { { FPL("c:\\\\\\"), FPL("a") }, FPL("c:\\a") },
285 { { FPL("c:\\"), FPL("") }, FPL("c:\\") },
286 { { FPL("c:\\a"), FPL("b") }, FPL("c:\\a\\b") },
287 { { FPL("c:\\a\\"), FPL("b") }, FPL("c:\\a\\b") },
288 #endif // FILE_PATH_USES_DRIVE_LETTERS
289 #else // FILE_PATH_USES_WIN_SEPARATORS
290 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb/cc") },
291 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb/cc") },
292 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb/cc") },
293 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb/cc") },
294 { { FPL("a/b"), FPL("c") }, FPL("a/b/c") },
295 { { FPL("a/b/"), FPL("c") }, FPL("a/b/c") },
296 { { FPL("//aa"), FPL("bb") }, FPL("//aa/bb") },
297 { { FPL("//aa/"), FPL("bb") }, FPL("//aa/bb") },
298 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
299 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
300 { { FPL("c:/"), FPL("") }, FPL("c:/") },
301 { { FPL("c:/a"), FPL("b") }, FPL("c:/a/b") },
302 { { FPL("c:/a/"), FPL("b") }, FPL("c:/a/b") },
303 #endif // FILE_PATH_USES_DRIVE_LETTERS
304 #endif // FILE_PATH_USES_WIN_SEPARATORS
307 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
308 FilePath
root(cases
[i
].inputs
[0]);
309 FilePath::StringType
leaf(cases
[i
].inputs
[1]);
310 FilePath observed_str
= root
.Append(leaf
);
311 EXPECT_EQ(FilePath::StringType(cases
[i
].expected
), observed_str
.value()) <<
312 "i: " << i
<< ", root: " << root
.value() << ", leaf: " << leaf
;
313 FilePath observed_path
= root
.Append(FilePath(leaf
));
314 EXPECT_EQ(FilePath::StringType(cases
[i
].expected
), observed_path
.value()) <<
315 "i: " << i
<< ", root: " << root
.value() << ", leaf: " << leaf
;
317 // TODO(erikkay): It would be nice to have a unicode test append value to
318 // handle the case when AppendASCII is passed UTF8
320 std::string ascii
= WideToUTF8(leaf
);
321 #elif defined(OS_POSIX)
322 std::string ascii
= leaf
;
324 observed_str
= root
.AppendASCII(ascii
);
325 EXPECT_EQ(FilePath::StringType(cases
[i
].expected
), observed_str
.value()) <<
326 "i: " << i
<< ", root: " << root
.value() << ", leaf: " << leaf
;
330 TEST_F(FilePathTest
, StripTrailingSeparators
) {
331 const struct UnaryTestData cases
[] = {
332 { FPL(""), FPL("") },
333 { FPL("/"), FPL("/") },
334 { FPL("//"), FPL("//") },
335 { FPL("///"), FPL("/") },
336 { FPL("////"), FPL("/") },
337 { FPL("a/"), FPL("a") },
338 { FPL("a//"), FPL("a") },
339 { FPL("a///"), FPL("a") },
340 { FPL("a////"), FPL("a") },
341 { FPL("/a"), FPL("/a") },
342 { FPL("/a/"), FPL("/a") },
343 { FPL("/a//"), FPL("/a") },
344 { FPL("/a///"), FPL("/a") },
345 { FPL("/a////"), FPL("/a") },
346 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
347 { FPL("c:"), FPL("c:") },
348 { FPL("c:/"), FPL("c:/") },
349 { FPL("c://"), FPL("c://") },
350 { FPL("c:///"), FPL("c:/") },
351 { FPL("c:////"), FPL("c:/") },
352 { FPL("c:/a"), FPL("c:/a") },
353 { FPL("c:/a/"), FPL("c:/a") },
354 { FPL("c:/a//"), FPL("c:/a") },
355 { FPL("c:/a///"), FPL("c:/a") },
356 { FPL("c:/a////"), FPL("c:/a") },
357 #endif // FILE_PATH_USES_DRIVE_LETTERS
358 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
359 { FPL("\\"), FPL("\\") },
360 { FPL("\\\\"), FPL("\\\\") },
361 { FPL("\\\\\\"), FPL("\\") },
362 { FPL("\\\\\\\\"), FPL("\\") },
363 { FPL("a\\"), FPL("a") },
364 { FPL("a\\\\"), FPL("a") },
365 { FPL("a\\\\\\"), FPL("a") },
366 { FPL("a\\\\\\\\"), FPL("a") },
367 { FPL("\\a"), FPL("\\a") },
368 { FPL("\\a\\"), FPL("\\a") },
369 { FPL("\\a\\\\"), FPL("\\a") },
370 { FPL("\\a\\\\\\"), FPL("\\a") },
371 { FPL("\\a\\\\\\\\"), FPL("\\a") },
372 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
373 { FPL("c:\\"), FPL("c:\\") },
374 { FPL("c:\\\\"), FPL("c:\\\\") },
375 { FPL("c:\\\\\\"), FPL("c:\\") },
376 { FPL("c:\\\\\\\\"), FPL("c:\\") },
377 { FPL("c:\\a"), FPL("c:\\a") },
378 { FPL("c:\\a\\"), FPL("c:\\a") },
379 { FPL("c:\\a\\\\"), FPL("c:\\a") },
380 { FPL("c:\\a\\\\\\"), FPL("c:\\a") },
381 { FPL("c:\\a\\\\\\\\"), FPL("c:\\a") },
382 #endif // FILE_PATH_USES_DRIVE_LETTERS
383 #endif // FILE_PATH_USES_WIN_SEPARATORS
386 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
387 FilePath
input(cases
[i
].input
);
388 FilePath observed
= input
.StripTrailingSeparators();
389 EXPECT_EQ(FilePath::StringType(cases
[i
].expected
), observed
.value()) <<
390 "i: " << i
<< ", input: " << input
.value();
394 TEST_F(FilePathTest
, IsAbsolute
) {
395 const struct UnaryBooleanTestData cases
[] = {
398 { FPL("c:"), false },
399 { FPL("c:a"), false },
400 { FPL("a/b"), false },
402 { FPL("//a"), true },
403 { FPL("c:a/b"), false },
404 { FPL("?:/a"), false },
405 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
407 { FPL("/a"), false },
408 { FPL("/."), false },
409 { FPL("/.."), false },
410 { FPL("c:/"), true },
411 { FPL("c:/a"), true },
412 { FPL("c:/."), true },
413 { FPL("c:/.."), true },
414 { FPL("C:/a"), true },
415 { FPL("d:/a"), true },
416 #else // FILE_PATH_USES_DRIVE_LETTERS
420 { FPL("/.."), true },
421 { FPL("c:/"), false },
422 #endif // FILE_PATH_USES_DRIVE_LETTERS
423 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
424 { FPL("a\\b"), false },
425 { FPL("\\\\"), true },
426 { FPL("\\\\a"), true },
427 { FPL("a\\b"), false },
428 { FPL("\\\\"), true },
429 { FPL("//a"), true },
430 { FPL("c:a\\b"), false },
431 { FPL("?:\\a"), false },
432 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
433 { FPL("\\"), false },
434 { FPL("\\a"), false },
435 { FPL("\\."), false },
436 { FPL("\\.."), false },
437 { FPL("c:\\"), true },
438 { FPL("c:\\"), true },
439 { FPL("c:\\a"), true },
440 { FPL("c:\\."), true },
441 { FPL("c:\\.."), true },
442 { FPL("C:\\a"), true },
443 { FPL("d:\\a"), true },
444 #else // FILE_PATH_USES_DRIVE_LETTERS
446 { FPL("\\a"), true },
447 { FPL("\\."), true },
448 { FPL("\\.."), true },
449 { FPL("c:\\"), false },
450 #endif // FILE_PATH_USES_DRIVE_LETTERS
451 #endif // FILE_PATH_USES_WIN_SEPARATORS
454 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
455 FilePath
input(cases
[i
].input
);
456 bool observed
= input
.IsAbsolute();
457 EXPECT_EQ(cases
[i
].expected
, observed
) <<
458 "i: " << i
<< ", input: " << input
.value();
462 TEST_F(FilePathTest
, PathComponentsTest
) {
463 const struct UnaryTestData cases
[] = {
464 { FPL("//foo/bar/baz/"), FPL("|//|foo|bar|baz")},
465 { FPL("///"), FPL("|/")},
466 { FPL("/foo//bar//baz/"), FPL("|/|foo|bar|baz")},
467 { FPL("/foo/bar/baz/"), FPL("|/|foo|bar|baz")},
468 { FPL("/foo/bar/baz//"), FPL("|/|foo|bar|baz")},
469 { FPL("/foo/bar/baz///"), FPL("|/|foo|bar|baz")},
470 { FPL("/foo/bar/baz"), FPL("|/|foo|bar|baz")},
471 { FPL("/foo/bar.bot/baz.txt"), FPL("|/|foo|bar.bot|baz.txt")},
472 { FPL("//foo//bar/baz"), FPL("|//|foo|bar|baz")},
473 { FPL("/"), FPL("|/")},
474 { FPL("foo"), FPL("|foo")},
476 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
477 { FPL("e:/foo"), FPL("|e:|/|foo")},
478 { FPL("e:/"), FPL("|e:|/")},
479 { FPL("e:"), FPL("|e:")},
480 #endif // FILE_PATH_USES_DRIVE_LETTERS
481 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
482 { FPL("../foo"), FPL("|..|foo")},
483 { FPL("./foo"), FPL("|foo")},
484 { FPL("../foo/bar/"), FPL("|..|foo|bar") },
485 { FPL("\\\\foo\\bar\\baz\\"), FPL("|\\\\|foo|bar|baz")},
486 { FPL("\\\\\\"), FPL("|\\")},
487 { FPL("\\foo\\\\bar\\\\baz\\"), FPL("|\\|foo|bar|baz")},
488 { FPL("\\foo\\bar\\baz\\"), FPL("|\\|foo|bar|baz")},
489 { FPL("\\foo\\bar\\baz\\\\"), FPL("|\\|foo|bar|baz")},
490 { FPL("\\foo\\bar\\baz\\\\\\"), FPL("|\\|foo|bar|baz")},
491 { FPL("\\foo\\bar\\baz"), FPL("|\\|foo|bar|baz")},
492 { FPL("\\foo\\bar/baz\\\\\\"), FPL("|\\|foo|bar|baz")},
493 { FPL("/foo\\bar\\baz"), FPL("|/|foo|bar|baz")},
494 { FPL("\\foo\\bar.bot\\baz.txt"), FPL("|\\|foo|bar.bot|baz.txt")},
495 { FPL("\\\\foo\\\\bar\\baz"), FPL("|\\\\|foo|bar|baz")},
496 { FPL("\\"), FPL("|\\")},
497 #endif // FILE_PATH_USES_WIN_SEPARATORS
500 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
501 FilePath
input(cases
[i
].input
);
502 std::vector
<FilePath::StringType
> comps
;
503 input
.GetComponents(&comps
);
505 FilePath::StringType observed
;
506 for (size_t j
= 0; j
< comps
.size(); ++j
) {
507 observed
.append(FILE_PATH_LITERAL("|"), 1);
508 observed
.append(comps
[j
]);
510 EXPECT_EQ(FilePath::StringType(cases
[i
].expected
), observed
) <<
511 "i: " << i
<< ", input: " << input
.value();
515 TEST_F(FilePathTest
, IsParentTest
) {
516 const struct BinaryBooleanTestData cases
[] = {
517 { { FPL("/"), FPL("/foo/bar/baz") }, true},
518 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, true},
519 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, true},
520 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, true},
521 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, false},
522 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, false},
523 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, false},
524 { { FPL("/foo/bar"), FPL("/foo/bar") }, false},
525 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
526 { { FPL("foo/bar"), FPL("foo/bar/baz") }, true},
527 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, false},
528 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, false},
529 { { FPL(""), FPL("foo") }, false},
530 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
531 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, true},
532 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, true},
533 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, true},
534 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, false},
535 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, false},
536 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, true},
537 { { FPL("c:"), FPL("c:/foo/bar/baz") }, true},
538 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
539 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, false},
540 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
541 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, false},
542 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, false},
543 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, false},
544 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, false},
545 #endif // FILE_PATH_USES_DRIVE_LETTERS
546 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
547 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, true},
548 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, true},
549 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, true},
550 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, true},
551 { { FPL(""), FPL("\\foo\\bar\\baz") }, false},
552 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, false},
553 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, false},
554 #endif // FILE_PATH_USES_WIN_SEPARATORS
557 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
558 FilePath
parent(cases
[i
].inputs
[0]);
559 FilePath
child(cases
[i
].inputs
[1]);
561 EXPECT_EQ(parent
.IsParent(child
), cases
[i
].expected
) <<
562 "i: " << i
<< ", parent: " << parent
.value() << ", child: " <<
567 TEST_F(FilePathTest
, AppendRelativePathTest
) {
568 const struct BinaryTestData cases
[] = {
569 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
570 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo\\bar\\baz")},
571 #else // FILE_PATH_USES_WIN_SEPARATORS
572 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo/bar/baz")},
573 #endif // FILE_PATH_USES_WIN_SEPARATORS
574 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, FPL("baz")},
575 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, FPL("baz")},
576 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, FPL("baz")},
577 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, FPL("")},
578 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, FPL("")},
579 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, FPL("")},
580 { { FPL("/foo/bar"), FPL("/foo/bar") }, FPL("")},
581 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, FPL("")},
582 { { FPL("foo/bar"), FPL("foo/bar/baz") }, FPL("baz")},
583 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, FPL("")},
584 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, FPL("")},
585 { { FPL(""), FPL("foo") }, FPL("")},
586 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
587 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, FPL("baz")},
588 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, FPL("baz")},
589 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, FPL("baz")},
590 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, FPL("")},
591 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, FPL("")},
592 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
593 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
594 // TODO(akalin): Figure out how to handle the corner case in the
595 // commented-out test case below. Appending to an empty path gives
596 // /foo\bar\baz but appending to a nonempty path "blah" gives
598 // { { FPL("c:"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
599 #endif // FILE_PATH_USES_WIN_SEPARATORS
600 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
601 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, FPL("")},
602 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
603 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, FPL("")},
604 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, FPL("")},
605 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, FPL("")},
606 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, FPL("")},
607 #endif // FILE_PATH_USES_DRIVE_LETTERS
608 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
609 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
610 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
611 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, FPL("baz")},
612 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, FPL("foo\\bar\\baz")},
613 { { FPL(""), FPL("\\foo\\bar\\baz") }, FPL("")},
614 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, FPL("")},
615 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, FPL("")},
616 #endif // FILE_PATH_USES_WIN_SEPARATORS
619 const FilePath
base(FPL("blah"));
621 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
622 FilePath
parent(cases
[i
].inputs
[0]);
623 FilePath
child(cases
[i
].inputs
[1]);
626 bool success
= parent
.AppendRelativePath(child
, &result
);
627 EXPECT_EQ(cases
[i
].expected
[0] != '\0', success
) <<
628 "i: " << i
<< ", parent: " << parent
.value() << ", child: " <<
630 EXPECT_STREQ(cases
[i
].expected
, result
.value().c_str()) <<
631 "i: " << i
<< ", parent: " << parent
.value() << ", child: " <<
635 FilePath
result(base
);
636 bool success
= parent
.AppendRelativePath(child
, &result
);
637 EXPECT_EQ(cases
[i
].expected
[0] != '\0', success
) <<
638 "i: " << i
<< ", parent: " << parent
.value() << ", child: " <<
640 EXPECT_EQ(base
.Append(cases
[i
].expected
).value(), result
.value()) <<
641 "i: " << i
<< ", parent: " << parent
.value() << ", child: " <<
647 TEST_F(FilePathTest
, EqualityTest
) {
648 const struct BinaryBooleanTestData cases
[] = {
649 { { FPL("/foo/bar/baz"), FPL("/foo/bar/baz") }, true},
650 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, false},
651 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
652 { { FPL("//foo/bar/"), FPL("//foo/bar/") }, true},
653 { { FPL("/foo/bar"), FPL("/foo2/bar") }, false},
654 { { FPL("/foo/bar.txt"), FPL("/foo/bar") }, false},
655 { { FPL("foo/bar"), FPL("foo/bar") }, true},
656 { { FPL("foo/bar"), FPL("foo/bar/baz") }, false},
657 { { FPL(""), FPL("foo") }, false},
658 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
659 { { FPL("c:/foo/bar"), FPL("c:/foo/bar") }, true},
660 { { FPL("E:/foo/bar"), FPL("e:/foo/bar") }, true},
661 { { FPL("f:/foo/bar"), FPL("F:/foo/bar") }, true},
662 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar") }, false},
663 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar") }, false},
664 { { FPL("c:/"), FPL("c:/") }, true},
665 { { FPL("c:"), FPL("c:") }, true},
666 { { FPL("c:/foo/bar"), FPL("d:/foo/bar") }, false},
667 { { FPL("c:/foo/bar"), FPL("D:/foo/bar") }, false},
668 { { FPL("C:/foo/bar"), FPL("d:/foo/bar") }, false},
669 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar") }, false},
670 #endif // FILE_PATH_USES_DRIVE_LETTERS
671 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
672 { { FPL("\\foo\\bar"), FPL("\\foo\\bar") }, true},
673 { { FPL("\\foo/bar"), FPL("\\foo/bar") }, true},
674 { { FPL("\\foo/bar"), FPL("\\foo\\bar") }, false},
675 { { FPL("\\"), FPL("\\") }, true},
676 { { FPL("\\"), FPL("/") }, false},
677 { { FPL(""), FPL("\\") }, false},
678 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar") }, false},
679 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2") }, false},
680 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
681 { { FPL("c:\\foo\\bar"), FPL("c:\\foo\\bar") }, true},
682 { { FPL("E:\\foo\\bar"), FPL("e:\\foo\\bar") }, true},
683 { { FPL("f:\\foo\\bar"), FPL("F:\\foo/bar") }, false},
684 #endif // FILE_PATH_USES_DRIVE_LETTERS
685 #endif // FILE_PATH_USES_WIN_SEPARATORS
688 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
689 FilePath
a(cases
[i
].inputs
[0]);
690 FilePath
b(cases
[i
].inputs
[1]);
692 EXPECT_EQ(a
== b
, cases
[i
].expected
) <<
693 "equality i: " << i
<< ", a: " << a
.value() << ", b: " <<
697 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
698 FilePath
a(cases
[i
].inputs
[0]);
699 FilePath
b(cases
[i
].inputs
[1]);
701 EXPECT_EQ(a
!= b
, !cases
[i
].expected
) <<
702 "inequality i: " << i
<< ", a: " << a
.value() << ", b: " <<
707 TEST_F(FilePathTest
, Extension
) {
708 FilePath
base_dir(FILE_PATH_LITERAL("base_dir"));
710 FilePath jpg
= base_dir
.Append(FILE_PATH_LITERAL("foo.jpg"));
711 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg
.Extension());
713 FilePath base
= jpg
.BaseName().RemoveExtension();
714 EXPECT_EQ(FILE_PATH_LITERAL("foo"), base
.value());
716 FilePath path_no_ext
= base_dir
.Append(base
);
717 EXPECT_EQ(path_no_ext
.value(), jpg
.RemoveExtension().value());
719 EXPECT_EQ(path_no_ext
.value(), path_no_ext
.RemoveExtension().value());
720 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext
.Extension());
723 TEST_F(FilePathTest
, Extension2
) {
724 const struct UnaryTestData cases
[] = {
725 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
726 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
727 { FPL("C:\\a\\b\\c."), FPL(".") },
728 { FPL("C:\\a\\b\\c"), FPL("") },
729 { FPL("C:\\a\\b\\"), FPL("") },
730 { FPL("C:\\a\\b.\\"), FPL(".") },
731 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
732 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
733 { FPL("C:\\foo.bar\\.."), FPL("") },
734 { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
736 { FPL("/foo/bar/baz.ext"), FPL(".ext") },
737 { FPL("/foo/bar/baz."), FPL(".") },
738 { FPL("/foo/bar/baz.."), FPL(".") },
739 { FPL("/foo/bar/baz"), FPL("") },
740 { FPL("/foo/bar/"), FPL("") },
741 { FPL("/foo/bar./"), FPL(".") },
742 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
743 { FPL("/foo.tar.gz"), FPL(".tar.gz") },
744 { FPL("/foo.tar.Z"), FPL(".tar.Z") },
745 { FPL("/foo.tar.bz2"), FPL(".tar.bz2") },
746 { FPL("/subversion-1.6.12.zip"), FPL(".zip") },
747 { FPL("/foo.1234.gz"), FPL(".1234.gz") },
748 { FPL("/foo.12345.gz"), FPL(".gz") },
749 { FPL("/foo..gz"), FPL(".gz") },
750 { FPL("/foo.1234.tar.gz"), FPL(".tar.gz") },
751 { FPL("/foo.tar.tar.gz"), FPL(".tar.gz") },
752 { FPL("/foo.tar.gz.gz"), FPL(".gz.gz") },
753 { FPL("."), FPL("") },
754 { FPL(".."), FPL("") },
755 { FPL("./foo"), FPL("") },
756 { FPL("./foo.ext"), FPL(".ext") },
757 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
758 { FPL("/foo.bar////"), FPL(".bar") },
759 { FPL("/foo.bar/.."), FPL("") },
760 { FPL("/foo.bar/..////"), FPL("") },
761 { FPL("/foo.1234.user.js"), FPL(".user.js") },
762 { FPL("foo.user.js"), FPL(".user.js") },
763 { FPL("/foo.1234.luser.js"), FPL(".js") },
764 { FPL("/user.js"), FPL(".js") },
766 for (unsigned int i
= 0; i
< arraysize(cases
); ++i
) {
767 FilePath
path(cases
[i
].input
);
768 FilePath::StringType extension
= path
.Extension();
769 EXPECT_STREQ(cases
[i
].expected
, extension
.c_str()) << "i: " << i
<<
770 ", path: " << path
.value();
774 TEST_F(FilePathTest
, InsertBeforeExtension
) {
775 const struct BinaryTestData cases
[] = {
776 { { FPL(""), FPL("") }, FPL("") },
777 { { FPL(""), FPL("txt") }, FPL("") },
778 { { FPL("."), FPL("txt") }, FPL("") },
779 { { FPL(".."), FPL("txt") }, FPL("") },
780 { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
781 { { FPL("."), FPL("") }, FPL(".") },
782 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
783 { { FPL("foo"), FPL("txt") }, FPL("footxt") },
784 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
785 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
786 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
787 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
788 { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
789 { { FPL("foo"), FPL("") }, FPL("foo") },
790 { { FPL("foo"), FPL(".") }, FPL("foo.") },
791 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
792 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
793 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
794 { { FPL("\\"), FPL("") }, FPL("\\") },
795 { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
796 { { FPL("\\."), FPL("txt") }, FPL("") },
797 { { FPL("\\.."), FPL("txt") }, FPL("") },
798 { { FPL("\\."), FPL("") }, FPL("\\.") },
799 { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
800 FPL("C:\\bar\\footxt.dll") },
801 { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
802 FPL("C:\\bar.baz\\foodlltxt") },
803 { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
804 FPL("C:\\bar.baz\\footxt.dll") },
805 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
806 FPL("C:\\bar.baz\\foo.dlltxt.exe") },
807 { { FPL("C:\\bar.baz\\foo"), FPL("") },
808 FPL("C:\\bar.baz\\foo") },
809 { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
810 FPL("C:\\bar.baz\\foo.exe") },
811 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
812 FPL("C:\\bar.baz\\foo.dll.exe") },
813 { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
814 FPL("C:\\bar\\baz\\foo (1).exe") },
815 { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
816 { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
818 { { FPL("/"), FPL("") }, FPL("/") },
819 { { FPL("/"), FPL("txt") }, FPL("/txt") },
820 { { FPL("/."), FPL("txt") }, FPL("") },
821 { { FPL("/.."), FPL("txt") }, FPL("") },
822 { { FPL("/."), FPL("") }, FPL("/.") },
823 { { FPL("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
824 { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
825 { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
826 { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
827 FPL("/bar.baz/foo.dlltxt.exe") },
828 { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
829 { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
830 { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
831 { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
832 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
834 for (unsigned int i
= 0; i
< arraysize(cases
); ++i
) {
835 FilePath
path(cases
[i
].inputs
[0]);
836 FilePath result
= path
.InsertBeforeExtension(cases
[i
].inputs
[1]);
837 EXPECT_EQ(cases
[i
].expected
, result
.value()) << "i: " << i
<<
838 ", path: " << path
.value() << ", insert: " << cases
[i
].inputs
[1];
842 TEST_F(FilePathTest
, RemoveExtension
) {
843 const struct UnaryTestData cases
[] = {
844 { FPL(""), FPL("") },
845 { FPL("."), FPL(".") },
846 { FPL(".."), FPL("..") },
847 { FPL("foo.dll"), FPL("foo") },
848 { FPL("./foo.dll"), FPL("./foo") },
849 { FPL("foo..dll"), FPL("foo.") },
850 { FPL("foo"), FPL("foo") },
851 { FPL("foo."), FPL("foo") },
852 { FPL("foo.."), FPL("foo.") },
853 { FPL("foo.baz.dll"), FPL("foo.baz") },
854 { FPL("foo.tar.gz"), FPL("foo") },
855 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
856 { FPL("C:\\foo.bar\\foo"), FPL("C:\\foo.bar\\foo") },
857 { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") },
859 { FPL("/foo.bar/foo"), FPL("/foo.bar/foo") },
860 { FPL("/foo.bar/..////"), FPL("/foo.bar/..////") },
862 for (unsigned int i
= 0; i
< arraysize(cases
); ++i
) {
863 FilePath
path(cases
[i
].input
);
864 FilePath removed
= path
.RemoveExtension();
865 EXPECT_EQ(cases
[i
].expected
, removed
.value()) << "i: " << i
<<
866 ", path: " << path
.value();
870 TEST_F(FilePathTest
, ReplaceExtension
) {
871 const struct BinaryTestData cases
[] = {
872 { { FPL(""), FPL("") }, FPL("") },
873 { { FPL(""), FPL("txt") }, FPL("") },
874 { { FPL("."), FPL("txt") }, FPL("") },
875 { { FPL(".."), FPL("txt") }, FPL("") },
876 { { FPL("."), FPL("") }, FPL("") },
877 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
878 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.txt") },
879 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
880 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
881 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
882 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
883 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
884 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
885 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
886 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
887 { { FPL("foo.dll"), FPL("") }, FPL("foo") },
888 { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
889 { { FPL("foo"), FPL("") }, FPL("foo") },
890 { { FPL("foo"), FPL(".") }, FPL("foo") },
891 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
892 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
893 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
894 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
895 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
897 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
898 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
900 for (unsigned int i
= 0; i
< arraysize(cases
); ++i
) {
901 FilePath
path(cases
[i
].inputs
[0]);
902 FilePath replaced
= path
.ReplaceExtension(cases
[i
].inputs
[1]);
903 EXPECT_EQ(cases
[i
].expected
, replaced
.value()) << "i: " << i
<<
904 ", path: " << path
.value() << ", replace: " << cases
[i
].inputs
[1];
908 TEST_F(FilePathTest
, AddExtension
) {
909 const struct BinaryTestData cases
[] = {
910 { { FPL(""), FPL("") }, FPL("") },
911 { { FPL(""), FPL("txt") }, FPL("") },
912 { { FPL("."), FPL("txt") }, FPL("") },
913 { { FPL(".."), FPL("txt") }, FPL("") },
914 { { FPL("."), FPL("") }, FPL("") },
915 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.dll.txt") },
916 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.dll.txt") },
917 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..dll.txt") },
918 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.dll.txt") },
919 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
920 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
921 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
922 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
923 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.dll.txt") },
924 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.dll.txt") },
925 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
926 { { FPL("foo.dll"), FPL(".") }, FPL("foo.dll") },
927 { { FPL("foo"), FPL("") }, FPL("foo") },
928 { { FPL("foo"), FPL(".") }, FPL("foo") },
929 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
930 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz.dll") },
931 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
932 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
933 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
935 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
936 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
938 for (unsigned int i
= 0; i
< arraysize(cases
); ++i
) {
939 FilePath
path(cases
[i
].inputs
[0]);
940 FilePath added
= path
.AddExtension(cases
[i
].inputs
[1]);
941 EXPECT_EQ(cases
[i
].expected
, added
.value()) << "i: " << i
<<
942 ", path: " << path
.value() << ", add: " << cases
[i
].inputs
[1];
946 TEST_F(FilePathTest
, MatchesExtension
) {
947 const struct BinaryBooleanTestData cases
[] = {
948 { { FPL("foo"), FPL("") }, true},
949 { { FPL("foo"), FPL(".") }, false},
950 { { FPL("foo."), FPL("") }, false},
951 { { FPL("foo."), FPL(".") }, true},
952 { { FPL("foo.txt"), FPL(".dll") }, false},
953 { { FPL("foo.txt"), FPL(".txt") }, true},
954 { { FPL("foo.txt.dll"), FPL(".txt") }, false},
955 { { FPL("foo.txt.dll"), FPL(".dll") }, true},
956 { { FPL("foo.TXT"), FPL(".txt") }, true},
957 { { FPL("foo.txt"), FPL(".TXT") }, true},
958 { { FPL("foo.tXt"), FPL(".txt") }, true},
959 { { FPL("foo.txt"), FPL(".tXt") }, true},
960 { { FPL("foo.tXt"), FPL(".TXT") }, true},
961 { { FPL("foo.tXt"), FPL(".tXt") }, true},
962 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
963 { { FPL("c:/foo.txt.dll"), FPL(".txt") }, false},
964 { { FPL("c:/foo.txt"), FPL(".txt") }, true},
965 #endif // FILE_PATH_USES_DRIVE_LETTERS
966 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
967 { { FPL("c:\\bar\\foo.txt.dll"), FPL(".txt") }, false},
968 { { FPL("c:\\bar\\foo.txt"), FPL(".txt") }, true},
969 #endif // FILE_PATH_USES_DRIVE_LETTERS
970 { { FPL("/bar/foo.txt.dll"), FPL(".txt") }, false},
971 { { FPL("/bar/foo.txt"), FPL(".txt") }, true},
972 #if defined(OS_WIN) || defined(OS_MACOSX)
973 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
974 { { FPL("foo.\u00E4\u00F6\u00FC"), FPL(".\u00E4\u00F6\u00FC") }, true},
975 { { FPL("foo.\u00C4\u00D6\u00DC"), FPL(".\u00E4\u00F6\u00FC") }, true},
976 // C with circumflex: direct comparison, and upper case vs. lower case
977 { { FPL("foo.\u0109"), FPL(".\u0109") }, true},
978 { { FPL("foo.\u0108"), FPL(".\u0109") }, true},
982 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
983 FilePath
path(cases
[i
].inputs
[0]);
984 FilePath::StringType
ext(cases
[i
].inputs
[1]);
986 EXPECT_EQ(cases
[i
].expected
, path
.MatchesExtension(ext
)) <<
987 "i: " << i
<< ", path: " << path
.value() << ", ext: " << ext
;
991 TEST_F(FilePathTest
, CompareIgnoreCase
) {
992 const struct BinaryIntTestData cases
[] = {
993 { { FPL("foo"), FPL("foo") }, 0},
994 { { FPL("FOO"), FPL("foo") }, 0},
995 { { FPL("foo.ext"), FPL("foo.ext") }, 0},
996 { { FPL("FOO.EXT"), FPL("foo.ext") }, 0},
997 { { FPL("Foo.Ext"), FPL("foo.ext") }, 0},
998 { { FPL("foO"), FPL("foo") }, 0},
999 { { FPL("foo"), FPL("foO") }, 0},
1000 { { FPL("fOo"), FPL("foo") }, 0},
1001 { { FPL("foo"), FPL("fOo") }, 0},
1002 { { FPL("bar"), FPL("foo") }, -1},
1003 { { FPL("foo"), FPL("bar") }, 1},
1004 { { FPL("BAR"), FPL("foo") }, -1},
1005 { { FPL("FOO"), FPL("bar") }, 1},
1006 { { FPL("bar"), FPL("FOO") }, -1},
1007 { { FPL("foo"), FPL("BAR") }, 1},
1008 { { FPL("BAR"), FPL("FOO") }, -1},
1009 { { FPL("FOO"), FPL("BAR") }, 1},
1010 // German "Eszett" (lower case and the new-fangled upper case)
1011 // Note that uc(<lowercase eszett>) => "SS", NOT <uppercase eszett>!
1012 // However, neither Windows nor Mac OSX converts these.
1013 // (or even have glyphs for <uppercase eszett>)
1014 { { FPL("\u00DF"), FPL("\u00DF") }, 0},
1015 { { FPL("\u1E9E"), FPL("\u1E9E") }, 0},
1016 { { FPL("\u00DF"), FPL("\u1E9E") }, -1},
1017 { { FPL("SS"), FPL("\u00DF") }, -1},
1018 { { FPL("SS"), FPL("\u1E9E") }, -1},
1019 #if defined(OS_WIN) || defined(OS_MACOSX)
1020 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
1021 { { FPL("\u00E4\u00F6\u00FC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1022 { { FPL("\u00C4\u00D6\u00DC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1023 // C with circumflex: direct comparison, and upper case vs. lower case
1024 { { FPL("\u0109"), FPL("\u0109") }, 0},
1025 { { FPL("\u0108"), FPL("\u0109") }, 0},
1026 // Cyrillic letter SHA: direct comparison, and upper case vs. lower case
1027 { { FPL("\u0428"), FPL("\u0428") }, 0},
1028 { { FPL("\u0428"), FPL("\u0448") }, 0},
1029 // Greek letter DELTA: direct comparison, and upper case vs. lower case
1030 { { FPL("\u0394"), FPL("\u0394") }, 0},
1031 { { FPL("\u0394"), FPL("\u03B4") }, 0},
1032 // Japanese full-width A: direct comparison, and upper case vs. lower case
1033 // Note that full-width and standard characters are considered different.
1034 { { FPL("\uFF21"), FPL("\uFF21") }, 0},
1035 { { FPL("\uFF21"), FPL("\uFF41") }, 0},
1036 { { FPL("A"), FPL("\uFF21") }, -1},
1037 { { FPL("A"), FPL("\uFF41") }, -1},
1038 { { FPL("a"), FPL("\uFF21") }, -1},
1039 { { FPL("a"), FPL("\uFF41") }, -1},
1041 #if defined(OS_MACOSX)
1042 // Codepoints > 0x1000
1043 // Georgian letter DON: direct comparison, and upper case vs. lower case
1044 { { FPL("\u10A3"), FPL("\u10A3") }, 0},
1045 { { FPL("\u10A3"), FPL("\u10D3") }, 0},
1046 // Combining characters vs. pre-composed characters, upper and lower case
1047 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1048 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("kuon") }, 1},
1049 { { FPL("kuon"), FPL("k\u0301u\u032Do\u0304\u0301n") }, -1},
1050 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("KUON") }, 1},
1051 { { FPL("KUON"), FPL("K\u0301U\u032DO\u0304\u0301N") }, -1},
1052 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("KUON") }, 1},
1053 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1054 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E30\u1E76\u1E52n") }, 0},
1055 { { FPL("k\u0301u\u032Do\u0304\u0302n"), FPL("\u1E30\u1E76\u1E52n") }, 1},
1059 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
1060 FilePath::StringType
s1(cases
[i
].inputs
[0]);
1061 FilePath::StringType
s2(cases
[i
].inputs
[1]);
1062 int result
= FilePath::CompareIgnoreCase(s1
, s2
);
1063 EXPECT_EQ(cases
[i
].expected
, result
) <<
1064 "i: " << i
<< ", s1: " << s1
<< ", s2: " << s2
;
1068 TEST_F(FilePathTest
, ReferencesParent
) {
1069 const struct UnaryBooleanTestData cases
[] = {
1070 { FPL("."), false },
1071 { FPL(".."), true },
1072 { FPL("a.."), false },
1073 { FPL("..a"), false },
1074 { FPL("../"), true },
1075 { FPL("/.."), true },
1076 { FPL("/../"), true },
1077 { FPL("/a../"), false },
1078 { FPL("/..a/"), false },
1079 { FPL("//.."), true },
1080 { FPL("..//"), true },
1081 { FPL("//..//"), true },
1082 { FPL("a//..//c"), true },
1083 { FPL("../b/c"), true },
1084 { FPL("/../b/c"), true },
1085 { FPL("a/b/.."), true },
1086 { FPL("a/b/../"), true },
1087 { FPL("a/../c"), true },
1088 { FPL("a/b/c"), false },
1091 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
1092 FilePath
input(cases
[i
].input
);
1093 bool observed
= input
.ReferencesParent();
1094 EXPECT_EQ(cases
[i
].expected
, observed
) <<
1095 "i: " << i
<< ", input: " << input
.value();
1099 TEST_F(FilePathTest
, FromUTF8Unsafe_And_AsUTF8Unsafe
) {
1100 const struct UTF8TestData cases
[] = {
1101 { FPL("foo.txt"), "foo.txt" },
1102 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them.
1103 { FPL("\u00E0\u00E8\u00F2.txt"), "\xC3\xA0\xC3\xA8\xC3\xB2.txt" },
1104 // Full-width "ABC".
1105 { FPL("\uFF21\uFF22\uFF23.txt"),
1106 "\xEF\xBC\xA1\xEF\xBC\xA2\xEF\xBC\xA3.txt" },
1109 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
1110 // Test FromUTF8Unsafe() works.
1111 FilePath from_utf8
= FilePath::FromUTF8Unsafe(cases
[i
].utf8
);
1112 EXPECT_EQ(cases
[i
].native
, from_utf8
.value())
1113 << "i: " << i
<< ", input: " << cases
[i
].native
;
1114 // Test AsUTF8Unsafe() works.
1115 FilePath from_native
= FilePath(cases
[i
].native
);
1116 EXPECT_EQ(cases
[i
].utf8
, from_native
.AsUTF8Unsafe())
1117 << "i: " << i
<< ", input: " << cases
[i
].native
;
1118 // Test the two file paths are identical.
1119 EXPECT_EQ(from_utf8
.value(), from_native
.value());
1123 TEST_F(FilePathTest
, ConstructWithNUL
) {
1124 // Assert FPS() works.
1125 ASSERT_TRUE(FPS("a\0b").length() == 3);
1127 // Test constructor strips '\0'
1128 FilePath
path(FPS("a\0b"));
1129 EXPECT_TRUE(path
.value().length() == 1);
1130 EXPECT_EQ(path
.value(), FPL("a"));
1133 TEST_F(FilePathTest
, AppendWithNUL
) {
1134 // Assert FPS() works.
1135 ASSERT_TRUE(FPS("b\0b").length() == 3);
1137 // Test Append() strips '\0'
1138 FilePath
path(FPL("a"));
1139 path
= path
.Append(FPS("b\0b"));
1140 EXPECT_TRUE(path
.value().length() == 3);
1141 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
1142 EXPECT_EQ(path
.value(), FPL("a\\b"));
1144 EXPECT_EQ(path
.value(), FPL("a/b"));
1148 TEST_F(FilePathTest
, ReferencesParentWithNUL
) {
1149 // Assert FPS() works.
1150 ASSERT_TRUE(FPS("..\0").length() == 3);
1152 // Test ReferencesParent() doesn't break with "..\0"
1153 FilePath
path(FPS("..\0"));
1154 EXPECT_TRUE(path
.ReferencesParent());
1157 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
1158 TEST_F(FilePathTest
, NormalizePathSeparators
) {
1159 const struct UnaryTestData cases
[] = {
1160 { FPL("foo/bar"), FPL("foo\\bar") },
1161 { FPL("foo/bar\\betz"), FPL("foo\\bar\\betz") },
1162 { FPL("foo\\bar"), FPL("foo\\bar") },
1163 { FPL("foo\\bar/betz"), FPL("foo\\bar\\betz") },
1164 { FPL("foo"), FPL("foo") },
1165 // Trailing slashes don't automatically get stripped. That's what
1166 // StripTrailingSeparators() is for.
1167 { FPL("foo\\"), FPL("foo\\") },
1168 { FPL("foo/"), FPL("foo\\") },
1169 { FPL("foo/bar\\"), FPL("foo\\bar\\") },
1170 { FPL("foo\\bar/"), FPL("foo\\bar\\") },
1171 { FPL("foo/bar/"), FPL("foo\\bar\\") },
1172 { FPL("foo\\bar\\"), FPL("foo\\bar\\") },
1173 { FPL("\\foo/bar"), FPL("\\foo\\bar") },
1174 { FPL("/foo\\bar"), FPL("\\foo\\bar") },
1175 { FPL("c:/foo/bar/"), FPL("c:\\foo\\bar\\") },
1176 { FPL("/foo/bar/"), FPL("\\foo\\bar\\") },
1177 { FPL("\\foo\\bar\\"), FPL("\\foo\\bar\\") },
1178 { FPL("c:\\foo/bar"), FPL("c:\\foo\\bar") },
1179 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1180 { FPL("\\\\foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1181 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1182 // This method does not normalize the number of path separators.
1183 { FPL("foo\\\\bar"), FPL("foo\\\\bar") },
1184 { FPL("foo//bar"), FPL("foo\\\\bar") },
1185 { FPL("foo/\\bar"), FPL("foo\\\\bar") },
1186 { FPL("foo\\/bar"), FPL("foo\\\\bar") },
1187 { FPL("///foo\\\\bar"), FPL("\\\\\\foo\\\\bar") },
1188 { FPL("foo//bar///"), FPL("foo\\\\bar\\\\\\") },
1189 { FPL("foo/\\bar/\\"), FPL("foo\\\\bar\\\\") },
1190 { FPL("/\\foo\\/bar"), FPL("\\\\foo\\\\bar") },
1192 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
1193 FilePath
input(cases
[i
].input
);
1194 FilePath observed
= input
.NormalizePathSeparators();
1195 EXPECT_EQ(FilePath::StringType(cases
[i
].expected
), observed
.value()) <<
1196 "i: " << i
<< ", input: " << input
.value();