Fix platform conditionalization of NaCl IRT file name
[chromium-blink-merge.git] / base / file_path_unittest.cc
blob9890e8062729653196baf0ed42b512578e25322d
1 // Copyright (c) 2011 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_path.h"
7 #include "base/file_util.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 struct UnaryTestData {
16 const FilePath::CharType* input;
17 const FilePath::CharType* expected;
20 struct UnaryBooleanTestData {
21 const FilePath::CharType* input;
22 bool expected;
25 struct BinaryTestData {
26 const FilePath::CharType* inputs[2];
27 const FilePath::CharType* expected;
30 struct BinaryBooleanTestData {
31 const FilePath::CharType* inputs[2];
32 bool expected;
35 struct BinaryIntTestData {
36 const FilePath::CharType* inputs[2];
37 int expected;
40 struct UTF8TestData {
41 const FilePath::CharType* native;
42 const char* utf8;
45 // file_util winds up using autoreleased objects on the Mac, so this needs
46 // to be a PlatformTest
47 class FilePathTest : public PlatformTest {
48 protected:
49 virtual void SetUp() {
50 PlatformTest::SetUp();
52 virtual void TearDown() {
53 PlatformTest::TearDown();
57 TEST_F(FilePathTest, DirName) {
58 const struct UnaryTestData cases[] = {
59 { FPL(""), FPL(".") },
60 { FPL("aa"), FPL(".") },
61 { FPL("/aa/bb"), FPL("/aa") },
62 { FPL("/aa/bb/"), FPL("/aa") },
63 { FPL("/aa/bb//"), FPL("/aa") },
64 { FPL("/aa/bb/ccc"), FPL("/aa/bb") },
65 { FPL("/aa"), FPL("/") },
66 { FPL("/aa/"), FPL("/") },
67 { FPL("/"), FPL("/") },
68 { FPL("//"), FPL("//") },
69 { FPL("///"), FPL("/") },
70 { FPL("aa/"), FPL(".") },
71 { FPL("aa/bb"), FPL("aa") },
72 { FPL("aa/bb/"), FPL("aa") },
73 { FPL("aa/bb//"), FPL("aa") },
74 { FPL("aa//bb//"), FPL("aa") },
75 { FPL("aa//bb/"), FPL("aa") },
76 { FPL("aa//bb"), FPL("aa") },
77 { FPL("//aa/bb"), FPL("//aa") },
78 { FPL("//aa/"), FPL("//") },
79 { FPL("//aa"), FPL("//") },
80 { FPL("0:"), FPL(".") },
81 { FPL("@:"), FPL(".") },
82 { FPL("[:"), FPL(".") },
83 { FPL("`:"), FPL(".") },
84 { FPL("{:"), FPL(".") },
85 { FPL("\xB3:"), FPL(".") },
86 { FPL("\xC5:"), FPL(".") },
87 #if defined(OS_WIN)
88 { FPL("\x0143:"), FPL(".") },
89 #endif // OS_WIN
90 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
91 { FPL("c:"), FPL("c:") },
92 { FPL("C:"), FPL("C:") },
93 { FPL("A:"), FPL("A:") },
94 { FPL("Z:"), FPL("Z:") },
95 { FPL("a:"), FPL("a:") },
96 { FPL("z:"), FPL("z:") },
97 { FPL("c:aa"), FPL("c:") },
98 { FPL("c:/"), FPL("c:/") },
99 { FPL("c://"), FPL("c://") },
100 { FPL("c:///"), FPL("c:/") },
101 { FPL("c:/aa"), FPL("c:/") },
102 { FPL("c:/aa/"), FPL("c:/") },
103 { FPL("c:/aa/bb"), FPL("c:/aa") },
104 { FPL("c:aa/bb"), FPL("c:aa") },
105 #endif // FILE_PATH_USES_DRIVE_LETTERS
106 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
107 { FPL("\\aa\\bb"), FPL("\\aa") },
108 { FPL("\\aa\\bb\\"), FPL("\\aa") },
109 { FPL("\\aa\\bb\\\\"), FPL("\\aa") },
110 { FPL("\\aa\\bb\\ccc"), FPL("\\aa\\bb") },
111 { FPL("\\aa"), FPL("\\") },
112 { FPL("\\aa\\"), FPL("\\") },
113 { FPL("\\"), FPL("\\") },
114 { FPL("\\\\"), FPL("\\\\") },
115 { FPL("\\\\\\"), FPL("\\") },
116 { FPL("aa\\"), FPL(".") },
117 { FPL("aa\\bb"), FPL("aa") },
118 { FPL("aa\\bb\\"), FPL("aa") },
119 { FPL("aa\\bb\\\\"), FPL("aa") },
120 { FPL("aa\\\\bb\\\\"), FPL("aa") },
121 { FPL("aa\\\\bb\\"), FPL("aa") },
122 { FPL("aa\\\\bb"), FPL("aa") },
123 { FPL("\\\\aa\\bb"), FPL("\\\\aa") },
124 { FPL("\\\\aa\\"), FPL("\\\\") },
125 { FPL("\\\\aa"), FPL("\\\\") },
126 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
127 { FPL("c:\\"), FPL("c:\\") },
128 { FPL("c:\\\\"), FPL("c:\\\\") },
129 { FPL("c:\\\\\\"), FPL("c:\\") },
130 { FPL("c:\\aa"), FPL("c:\\") },
131 { FPL("c:\\aa\\"), FPL("c:\\") },
132 { FPL("c:\\aa\\bb"), FPL("c:\\aa") },
133 { FPL("c:aa\\bb"), FPL("c:aa") },
134 #endif // FILE_PATH_USES_DRIVE_LETTERS
135 #endif // FILE_PATH_USES_WIN_SEPARATORS
138 for (size_t i = 0; i < arraysize(cases); ++i) {
139 FilePath input(cases[i].input);
140 FilePath observed = input.DirName();
141 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
142 "i: " << i << ", input: " << input.value();
146 TEST_F(FilePathTest, BaseName) {
147 const struct UnaryTestData cases[] = {
148 { FPL(""), FPL("") },
149 { FPL("aa"), FPL("aa") },
150 { FPL("/aa/bb"), FPL("bb") },
151 { FPL("/aa/bb/"), FPL("bb") },
152 { FPL("/aa/bb//"), FPL("bb") },
153 { FPL("/aa/bb/ccc"), FPL("ccc") },
154 { FPL("/aa"), FPL("aa") },
155 { FPL("/"), FPL("/") },
156 { FPL("//"), FPL("//") },
157 { FPL("///"), FPL("/") },
158 { FPL("aa/"), FPL("aa") },
159 { FPL("aa/bb"), FPL("bb") },
160 { FPL("aa/bb/"), FPL("bb") },
161 { FPL("aa/bb//"), FPL("bb") },
162 { FPL("aa//bb//"), FPL("bb") },
163 { FPL("aa//bb/"), FPL("bb") },
164 { FPL("aa//bb"), FPL("bb") },
165 { FPL("//aa/bb"), FPL("bb") },
166 { FPL("//aa/"), FPL("aa") },
167 { FPL("//aa"), FPL("aa") },
168 { FPL("0:"), FPL("0:") },
169 { FPL("@:"), FPL("@:") },
170 { FPL("[:"), FPL("[:") },
171 { FPL("`:"), FPL("`:") },
172 { FPL("{:"), FPL("{:") },
173 { FPL("\xB3:"), FPL("\xB3:") },
174 { FPL("\xC5:"), FPL("\xC5:") },
175 #if defined(OS_WIN)
176 { FPL("\x0143:"), FPL("\x0143:") },
177 #endif // OS_WIN
178 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
179 { FPL("c:"), FPL("") },
180 { FPL("C:"), FPL("") },
181 { FPL("A:"), FPL("") },
182 { FPL("Z:"), FPL("") },
183 { FPL("a:"), FPL("") },
184 { FPL("z:"), FPL("") },
185 { FPL("c:aa"), FPL("aa") },
186 { FPL("c:/"), FPL("/") },
187 { FPL("c://"), FPL("//") },
188 { FPL("c:///"), FPL("/") },
189 { FPL("c:/aa"), FPL("aa") },
190 { FPL("c:/aa/"), FPL("aa") },
191 { FPL("c:/aa/bb"), FPL("bb") },
192 { FPL("c:aa/bb"), FPL("bb") },
193 #endif // FILE_PATH_USES_DRIVE_LETTERS
194 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
195 { FPL("\\aa\\bb"), FPL("bb") },
196 { FPL("\\aa\\bb\\"), FPL("bb") },
197 { FPL("\\aa\\bb\\\\"), FPL("bb") },
198 { FPL("\\aa\\bb\\ccc"), FPL("ccc") },
199 { FPL("\\aa"), FPL("aa") },
200 { FPL("\\"), FPL("\\") },
201 { FPL("\\\\"), FPL("\\\\") },
202 { FPL("\\\\\\"), FPL("\\") },
203 { FPL("aa\\"), FPL("aa") },
204 { FPL("aa\\bb"), FPL("bb") },
205 { FPL("aa\\bb\\"), FPL("bb") },
206 { FPL("aa\\bb\\\\"), FPL("bb") },
207 { FPL("aa\\\\bb\\\\"), FPL("bb") },
208 { FPL("aa\\\\bb\\"), FPL("bb") },
209 { FPL("aa\\\\bb"), FPL("bb") },
210 { FPL("\\\\aa\\bb"), FPL("bb") },
211 { FPL("\\\\aa\\"), FPL("aa") },
212 { FPL("\\\\aa"), FPL("aa") },
213 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
214 { FPL("c:\\"), FPL("\\") },
215 { FPL("c:\\\\"), FPL("\\\\") },
216 { FPL("c:\\\\\\"), FPL("\\") },
217 { FPL("c:\\aa"), FPL("aa") },
218 { FPL("c:\\aa\\"), FPL("aa") },
219 { FPL("c:\\aa\\bb"), FPL("bb") },
220 { FPL("c:aa\\bb"), FPL("bb") },
221 #endif // FILE_PATH_USES_DRIVE_LETTERS
222 #endif // FILE_PATH_USES_WIN_SEPARATORS
225 for (size_t i = 0; i < arraysize(cases); ++i) {
226 FilePath input(cases[i].input);
227 FilePath observed = input.BaseName();
228 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
229 "i: " << i << ", input: " << input.value();
233 TEST_F(FilePathTest, Append) {
234 const struct BinaryTestData cases[] = {
235 { { FPL(""), FPL("cc") }, FPL("cc") },
236 { { FPL("."), FPL("ff") }, FPL("ff") },
237 { { FPL("/"), FPL("cc") }, FPL("/cc") },
238 { { FPL("/aa"), FPL("") }, FPL("/aa") },
239 { { FPL("/aa/"), FPL("") }, FPL("/aa") },
240 { { FPL("//aa"), FPL("") }, FPL("//aa") },
241 { { FPL("//aa/"), FPL("") }, FPL("//aa") },
242 { { FPL("//"), FPL("aa") }, FPL("//aa") },
243 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
244 { { FPL("c:"), FPL("a") }, FPL("c:a") },
245 { { FPL("c:"), FPL("") }, FPL("c:") },
246 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
247 { { FPL("c://"), FPL("a") }, FPL("c://a") },
248 { { FPL("c:///"), FPL("a") }, FPL("c:/a") },
249 #endif // FILE_PATH_USES_DRIVE_LETTERS
250 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
251 // Append introduces the default separator character, so these test cases
252 // need to be defined with different expected results on platforms that use
253 // different default separator characters.
254 { { FPL("\\"), FPL("cc") }, FPL("\\cc") },
255 { { FPL("\\aa"), FPL("") }, FPL("\\aa") },
256 { { FPL("\\aa\\"), FPL("") }, FPL("\\aa") },
257 { { FPL("\\\\aa"), FPL("") }, FPL("\\\\aa") },
258 { { FPL("\\\\aa\\"), FPL("") }, FPL("\\\\aa") },
259 { { FPL("\\\\"), FPL("aa") }, FPL("\\\\aa") },
260 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb\\cc") },
261 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb\\cc") },
262 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb\\cc") },
263 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb\\cc") },
264 { { FPL("a/b"), FPL("c") }, FPL("a/b\\c") },
265 { { FPL("a/b/"), FPL("c") }, FPL("a/b\\c") },
266 { { FPL("//aa"), FPL("bb") }, FPL("//aa\\bb") },
267 { { FPL("//aa/"), FPL("bb") }, FPL("//aa\\bb") },
268 { { FPL("\\aa\\bb"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
269 { { FPL("\\aa\\bb\\"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
270 { { FPL("aa\\bb\\"), FPL("cc") }, FPL("aa\\bb\\cc") },
271 { { FPL("aa\\bb"), FPL("cc") }, FPL("aa\\bb\\cc") },
272 { { FPL("a\\b"), FPL("c") }, FPL("a\\b\\c") },
273 { { FPL("a\\b\\"), FPL("c") }, FPL("a\\b\\c") },
274 { { FPL("\\\\aa"), FPL("bb") }, FPL("\\\\aa\\bb") },
275 { { FPL("\\\\aa\\"), FPL("bb") }, FPL("\\\\aa\\bb") },
276 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
277 { { FPL("c:\\"), FPL("a") }, FPL("c:\\a") },
278 { { FPL("c:\\\\"), FPL("a") }, FPL("c:\\\\a") },
279 { { FPL("c:\\\\\\"), FPL("a") }, FPL("c:\\a") },
280 { { FPL("c:\\"), FPL("") }, FPL("c:\\") },
281 { { FPL("c:\\a"), FPL("b") }, FPL("c:\\a\\b") },
282 { { FPL("c:\\a\\"), FPL("b") }, FPL("c:\\a\\b") },
283 #endif // FILE_PATH_USES_DRIVE_LETTERS
284 #else // FILE_PATH_USES_WIN_SEPARATORS
285 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb/cc") },
286 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb/cc") },
287 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb/cc") },
288 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb/cc") },
289 { { FPL("a/b"), FPL("c") }, FPL("a/b/c") },
290 { { FPL("a/b/"), FPL("c") }, FPL("a/b/c") },
291 { { FPL("//aa"), FPL("bb") }, FPL("//aa/bb") },
292 { { FPL("//aa/"), FPL("bb") }, FPL("//aa/bb") },
293 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
294 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
295 { { FPL("c:/"), FPL("") }, FPL("c:/") },
296 { { FPL("c:/a"), FPL("b") }, FPL("c:/a/b") },
297 { { FPL("c:/a/"), FPL("b") }, FPL("c:/a/b") },
298 #endif // FILE_PATH_USES_DRIVE_LETTERS
299 #endif // FILE_PATH_USES_WIN_SEPARATORS
302 for (size_t i = 0; i < arraysize(cases); ++i) {
303 FilePath root(cases[i].inputs[0]);
304 FilePath::StringType leaf(cases[i].inputs[1]);
305 FilePath observed_str = root.Append(leaf);
306 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
307 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
308 FilePath observed_path = root.Append(FilePath(leaf));
309 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) <<
310 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
312 // TODO(erikkay): It would be nice to have a unicode test append value to
313 // handle the case when AppendASCII is passed UTF8
314 #if defined(OS_WIN)
315 std::string ascii = WideToUTF8(leaf);
316 #elif defined(OS_POSIX)
317 std::string ascii = leaf;
318 #endif
319 observed_str = root.AppendASCII(ascii);
320 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
321 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
325 TEST_F(FilePathTest, StripTrailingSeparators) {
326 const struct UnaryTestData cases[] = {
327 { FPL(""), FPL("") },
328 { FPL("/"), FPL("/") },
329 { FPL("//"), FPL("//") },
330 { FPL("///"), FPL("/") },
331 { FPL("////"), FPL("/") },
332 { FPL("a/"), FPL("a") },
333 { FPL("a//"), FPL("a") },
334 { FPL("a///"), FPL("a") },
335 { FPL("a////"), FPL("a") },
336 { FPL("/a"), FPL("/a") },
337 { FPL("/a/"), FPL("/a") },
338 { FPL("/a//"), FPL("/a") },
339 { FPL("/a///"), FPL("/a") },
340 { FPL("/a////"), FPL("/a") },
341 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
342 { FPL("c:"), FPL("c:") },
343 { FPL("c:/"), FPL("c:/") },
344 { FPL("c://"), FPL("c://") },
345 { FPL("c:///"), FPL("c:/") },
346 { FPL("c:////"), FPL("c:/") },
347 { FPL("c:/a"), FPL("c:/a") },
348 { FPL("c:/a/"), FPL("c:/a") },
349 { FPL("c:/a//"), FPL("c:/a") },
350 { FPL("c:/a///"), FPL("c:/a") },
351 { FPL("c:/a////"), FPL("c:/a") },
352 #endif // FILE_PATH_USES_DRIVE_LETTERS
353 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
354 { FPL("\\"), FPL("\\") },
355 { FPL("\\\\"), FPL("\\\\") },
356 { FPL("\\\\\\"), FPL("\\") },
357 { FPL("\\\\\\\\"), FPL("\\") },
358 { FPL("a\\"), FPL("a") },
359 { FPL("a\\\\"), FPL("a") },
360 { FPL("a\\\\\\"), FPL("a") },
361 { FPL("a\\\\\\\\"), FPL("a") },
362 { FPL("\\a"), FPL("\\a") },
363 { FPL("\\a\\"), FPL("\\a") },
364 { FPL("\\a\\\\"), FPL("\\a") },
365 { FPL("\\a\\\\\\"), FPL("\\a") },
366 { FPL("\\a\\\\\\\\"), FPL("\\a") },
367 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
368 { FPL("c:\\"), FPL("c:\\") },
369 { FPL("c:\\\\"), FPL("c:\\\\") },
370 { FPL("c:\\\\\\"), FPL("c:\\") },
371 { FPL("c:\\\\\\\\"), FPL("c:\\") },
372 { FPL("c:\\a"), FPL("c:\\a") },
373 { FPL("c:\\a\\"), FPL("c:\\a") },
374 { FPL("c:\\a\\\\"), FPL("c:\\a") },
375 { FPL("c:\\a\\\\\\"), FPL("c:\\a") },
376 { FPL("c:\\a\\\\\\\\"), FPL("c:\\a") },
377 #endif // FILE_PATH_USES_DRIVE_LETTERS
378 #endif // FILE_PATH_USES_WIN_SEPARATORS
381 for (size_t i = 0; i < arraysize(cases); ++i) {
382 FilePath input(cases[i].input);
383 FilePath observed = input.StripTrailingSeparators();
384 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
385 "i: " << i << ", input: " << input.value();
389 TEST_F(FilePathTest, IsAbsolute) {
390 const struct UnaryBooleanTestData cases[] = {
391 { FPL(""), false },
392 { FPL("a"), false },
393 { FPL("c:"), false },
394 { FPL("c:a"), false },
395 { FPL("a/b"), false },
396 { FPL("//"), true },
397 { FPL("//a"), true },
398 { FPL("c:a/b"), false },
399 { FPL("?:/a"), false },
400 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
401 { FPL("/"), false },
402 { FPL("/a"), false },
403 { FPL("/."), false },
404 { FPL("/.."), false },
405 { FPL("c:/"), true },
406 { FPL("c:/a"), true },
407 { FPL("c:/."), true },
408 { FPL("c:/.."), true },
409 { FPL("C:/a"), true },
410 { FPL("d:/a"), true },
411 #else // FILE_PATH_USES_DRIVE_LETTERS
412 { FPL("/"), true },
413 { FPL("/a"), true },
414 { FPL("/."), true },
415 { FPL("/.."), true },
416 { FPL("c:/"), false },
417 #endif // FILE_PATH_USES_DRIVE_LETTERS
418 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
419 { FPL("a\\b"), false },
420 { FPL("\\\\"), true },
421 { FPL("\\\\a"), true },
422 { FPL("a\\b"), false },
423 { FPL("\\\\"), true },
424 { FPL("//a"), true },
425 { FPL("c:a\\b"), false },
426 { FPL("?:\\a"), false },
427 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
428 { FPL("\\"), false },
429 { FPL("\\a"), false },
430 { FPL("\\."), false },
431 { FPL("\\.."), false },
432 { FPL("c:\\"), true },
433 { FPL("c:\\"), true },
434 { FPL("c:\\a"), true },
435 { FPL("c:\\."), true },
436 { FPL("c:\\.."), true },
437 { FPL("C:\\a"), true },
438 { FPL("d:\\a"), true },
439 #else // FILE_PATH_USES_DRIVE_LETTERS
440 { FPL("\\"), true },
441 { FPL("\\a"), true },
442 { FPL("\\."), true },
443 { FPL("\\.."), true },
444 { FPL("c:\\"), false },
445 #endif // FILE_PATH_USES_DRIVE_LETTERS
446 #endif // FILE_PATH_USES_WIN_SEPARATORS
449 for (size_t i = 0; i < arraysize(cases); ++i) {
450 FilePath input(cases[i].input);
451 bool observed = input.IsAbsolute();
452 EXPECT_EQ(cases[i].expected, observed) <<
453 "i: " << i << ", input: " << input.value();
457 TEST_F(FilePathTest, PathComponentsTest) {
458 const struct UnaryTestData cases[] = {
459 { FPL("//foo/bar/baz/"), FPL("|//|foo|bar|baz")},
460 { FPL("///"), FPL("|/")},
461 { FPL("/foo//bar//baz/"), FPL("|/|foo|bar|baz")},
462 { FPL("/foo/bar/baz/"), FPL("|/|foo|bar|baz")},
463 { FPL("/foo/bar/baz//"), FPL("|/|foo|bar|baz")},
464 { FPL("/foo/bar/baz///"), FPL("|/|foo|bar|baz")},
465 { FPL("/foo/bar/baz"), FPL("|/|foo|bar|baz")},
466 { FPL("/foo/bar.bot/baz.txt"), FPL("|/|foo|bar.bot|baz.txt")},
467 { FPL("//foo//bar/baz"), FPL("|//|foo|bar|baz")},
468 { FPL("/"), FPL("|/")},
469 { FPL("foo"), FPL("|foo")},
470 { FPL(""), FPL("")},
471 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
472 { FPL("e:/foo"), FPL("|e:|/|foo")},
473 { FPL("e:/"), FPL("|e:|/")},
474 { FPL("e:"), FPL("|e:")},
475 #endif // FILE_PATH_USES_DRIVE_LETTERS
476 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
477 { FPL("../foo"), FPL("|..|foo")},
478 { FPL("./foo"), FPL("|foo")},
479 { FPL("../foo/bar/"), FPL("|..|foo|bar") },
480 { FPL("\\\\foo\\bar\\baz\\"), FPL("|\\\\|foo|bar|baz")},
481 { FPL("\\\\\\"), FPL("|\\")},
482 { FPL("\\foo\\\\bar\\\\baz\\"), FPL("|\\|foo|bar|baz")},
483 { FPL("\\foo\\bar\\baz\\"), FPL("|\\|foo|bar|baz")},
484 { FPL("\\foo\\bar\\baz\\\\"), FPL("|\\|foo|bar|baz")},
485 { FPL("\\foo\\bar\\baz\\\\\\"), FPL("|\\|foo|bar|baz")},
486 { FPL("\\foo\\bar\\baz"), FPL("|\\|foo|bar|baz")},
487 { FPL("\\foo\\bar/baz\\\\\\"), FPL("|\\|foo|bar|baz")},
488 { FPL("/foo\\bar\\baz"), FPL("|/|foo|bar|baz")},
489 { FPL("\\foo\\bar.bot\\baz.txt"), FPL("|\\|foo|bar.bot|baz.txt")},
490 { FPL("\\\\foo\\\\bar\\baz"), FPL("|\\\\|foo|bar|baz")},
491 { FPL("\\"), FPL("|\\")},
492 #endif // FILE_PATH_USES_WIN_SEPARATORS
495 for (size_t i = 0; i < arraysize(cases); ++i) {
496 FilePath input(cases[i].input);
497 std::vector<FilePath::StringType> comps;
498 input.GetComponents(&comps);
500 FilePath::StringType observed;
501 for (size_t j = 0; j < comps.size(); ++j) {
502 observed.append(FILE_PATH_LITERAL("|"), 1);
503 observed.append(comps[j]);
505 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed) <<
506 "i: " << i << ", input: " << input.value();
510 TEST_F(FilePathTest, IsParentTest) {
511 const struct BinaryBooleanTestData cases[] = {
512 { { FPL("/"), FPL("/foo/bar/baz") }, true},
513 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, true},
514 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, true},
515 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, true},
516 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, false},
517 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, false},
518 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, false},
519 { { FPL("/foo/bar"), FPL("/foo/bar") }, false},
520 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
521 { { FPL("foo/bar"), FPL("foo/bar/baz") }, true},
522 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, false},
523 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, false},
524 { { FPL(""), FPL("foo") }, false},
525 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
526 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, true},
527 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, true},
528 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, true},
529 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, false},
530 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, false},
531 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, true},
532 { { FPL("c:"), FPL("c:/foo/bar/baz") }, true},
533 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
534 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, false},
535 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
536 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, false},
537 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, false},
538 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, false},
539 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, false},
540 #endif // FILE_PATH_USES_DRIVE_LETTERS
541 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
542 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, true},
543 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, true},
544 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, true},
545 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, true},
546 { { FPL(""), FPL("\\foo\\bar\\baz") }, false},
547 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, false},
548 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, false},
549 #endif // FILE_PATH_USES_WIN_SEPARATORS
552 for (size_t i = 0; i < arraysize(cases); ++i) {
553 FilePath parent(cases[i].inputs[0]);
554 FilePath child(cases[i].inputs[1]);
556 EXPECT_EQ(parent.IsParent(child), cases[i].expected) <<
557 "i: " << i << ", parent: " << parent.value() << ", child: " <<
558 child.value();
562 TEST_F(FilePathTest, AppendRelativePathTest) {
563 const struct BinaryTestData cases[] = {
564 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
565 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo\\bar\\baz")},
566 #else // FILE_PATH_USES_WIN_SEPARATORS
567 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo/bar/baz")},
568 #endif // FILE_PATH_USES_WIN_SEPARATORS
569 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, FPL("baz")},
570 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, FPL("baz")},
571 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, FPL("baz")},
572 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, FPL("")},
573 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, FPL("")},
574 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, FPL("")},
575 { { FPL("/foo/bar"), FPL("/foo/bar") }, FPL("")},
576 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, FPL("")},
577 { { FPL("foo/bar"), FPL("foo/bar/baz") }, FPL("baz")},
578 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, FPL("")},
579 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, FPL("")},
580 { { FPL(""), FPL("foo") }, FPL("")},
581 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
582 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, FPL("baz")},
583 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, FPL("baz")},
584 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, FPL("baz")},
585 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, FPL("")},
586 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, FPL("")},
587 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
588 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
589 // TODO(akalin): Figure out how to handle the corner case in the
590 // commented-out test case below. Appending to an empty path gives
591 // /foo\bar\baz but appending to a nonempty path "blah" gives
592 // blah\foo\bar\baz.
593 // { { FPL("c:"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
594 #endif // FILE_PATH_USES_WIN_SEPARATORS
595 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
596 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, FPL("")},
597 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
598 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, FPL("")},
599 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, FPL("")},
600 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, FPL("")},
601 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, FPL("")},
602 #endif // FILE_PATH_USES_DRIVE_LETTERS
603 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
604 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
605 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
606 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, FPL("baz")},
607 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, FPL("foo\\bar\\baz")},
608 { { FPL(""), FPL("\\foo\\bar\\baz") }, FPL("")},
609 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, FPL("")},
610 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, FPL("")},
611 #endif // FILE_PATH_USES_WIN_SEPARATORS
614 const FilePath base(FPL("blah"));
616 for (size_t i = 0; i < arraysize(cases); ++i) {
617 FilePath parent(cases[i].inputs[0]);
618 FilePath child(cases[i].inputs[1]);
620 FilePath result;
621 bool success = parent.AppendRelativePath(child, &result);
622 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
623 "i: " << i << ", parent: " << parent.value() << ", child: " <<
624 child.value();
625 EXPECT_STREQ(cases[i].expected, result.value().c_str()) <<
626 "i: " << i << ", parent: " << parent.value() << ", child: " <<
627 child.value();
630 FilePath result(base);
631 bool success = parent.AppendRelativePath(child, &result);
632 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
633 "i: " << i << ", parent: " << parent.value() << ", child: " <<
634 child.value();
635 EXPECT_EQ(base.Append(cases[i].expected).value(), result.value()) <<
636 "i: " << i << ", parent: " << parent.value() << ", child: " <<
637 child.value();
642 TEST_F(FilePathTest, EqualityTest) {
643 const struct BinaryBooleanTestData cases[] = {
644 { { FPL("/foo/bar/baz"), FPL("/foo/bar/baz") }, true},
645 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, false},
646 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
647 { { FPL("//foo/bar/"), FPL("//foo/bar/") }, true},
648 { { FPL("/foo/bar"), FPL("/foo2/bar") }, false},
649 { { FPL("/foo/bar.txt"), FPL("/foo/bar") }, false},
650 { { FPL("foo/bar"), FPL("foo/bar") }, true},
651 { { FPL("foo/bar"), FPL("foo/bar/baz") }, false},
652 { { FPL(""), FPL("foo") }, false},
653 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
654 { { FPL("c:/foo/bar"), FPL("c:/foo/bar") }, true},
655 { { FPL("E:/foo/bar"), FPL("e:/foo/bar") }, true},
656 { { FPL("f:/foo/bar"), FPL("F:/foo/bar") }, true},
657 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar") }, false},
658 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar") }, false},
659 { { FPL("c:/"), FPL("c:/") }, true},
660 { { FPL("c:"), FPL("c:") }, true},
661 { { FPL("c:/foo/bar"), FPL("d:/foo/bar") }, false},
662 { { FPL("c:/foo/bar"), FPL("D:/foo/bar") }, false},
663 { { FPL("C:/foo/bar"), FPL("d:/foo/bar") }, false},
664 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar") }, false},
665 #endif // FILE_PATH_USES_DRIVE_LETTERS
666 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
667 { { FPL("\\foo\\bar"), FPL("\\foo\\bar") }, true},
668 { { FPL("\\foo/bar"), FPL("\\foo/bar") }, true},
669 { { FPL("\\foo/bar"), FPL("\\foo\\bar") }, false},
670 { { FPL("\\"), FPL("\\") }, true},
671 { { FPL("\\"), FPL("/") }, false},
672 { { FPL(""), FPL("\\") }, false},
673 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar") }, false},
674 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2") }, false},
675 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
676 { { FPL("c:\\foo\\bar"), FPL("c:\\foo\\bar") }, true},
677 { { FPL("E:\\foo\\bar"), FPL("e:\\foo\\bar") }, true},
678 { { FPL("f:\\foo\\bar"), FPL("F:\\foo/bar") }, false},
679 #endif // FILE_PATH_USES_DRIVE_LETTERS
680 #endif // FILE_PATH_USES_WIN_SEPARATORS
683 for (size_t i = 0; i < arraysize(cases); ++i) {
684 FilePath a(cases[i].inputs[0]);
685 FilePath b(cases[i].inputs[1]);
687 EXPECT_EQ(a == b, cases[i].expected) <<
688 "equality i: " << i << ", a: " << a.value() << ", b: " <<
689 b.value();
692 for (size_t i = 0; i < arraysize(cases); ++i) {
693 FilePath a(cases[i].inputs[0]);
694 FilePath b(cases[i].inputs[1]);
696 EXPECT_EQ(a != b, !cases[i].expected) <<
697 "inequality i: " << i << ", a: " << a.value() << ", b: " <<
698 b.value();
702 TEST_F(FilePathTest, Extension) {
703 FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
705 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
706 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.Extension());
708 FilePath base = jpg.BaseName().RemoveExtension();
709 EXPECT_EQ(FILE_PATH_LITERAL("foo"), base.value());
711 FilePath path_no_ext = base_dir.Append(base);
712 EXPECT_EQ(path_no_ext.value(), jpg.RemoveExtension().value());
714 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value());
715 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.Extension());
718 TEST_F(FilePathTest, Extension2) {
719 const struct UnaryTestData cases[] = {
720 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
721 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
722 { FPL("C:\\a\\b\\c."), FPL(".") },
723 { FPL("C:\\a\\b\\c"), FPL("") },
724 { FPL("C:\\a\\b\\"), FPL("") },
725 { FPL("C:\\a\\b.\\"), FPL(".") },
726 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
727 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
728 { FPL("C:\\foo.bar\\.."), FPL("") },
729 { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
730 #endif
731 { FPL("/foo/bar/baz.ext"), FPL(".ext") },
732 { FPL("/foo/bar/baz."), FPL(".") },
733 { FPL("/foo/bar/baz.."), FPL(".") },
734 { FPL("/foo/bar/baz"), FPL("") },
735 { FPL("/foo/bar/"), FPL("") },
736 { FPL("/foo/bar./"), FPL(".") },
737 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
738 { FPL("/foo.tar.gz"), FPL(".tar.gz") },
739 { FPL("/foo.tar.Z"), FPL(".tar.Z") },
740 { FPL("/foo.tar.bz2"), FPL(".tar.bz2") },
741 { FPL("/subversion-1.6.12.zip"), FPL(".zip") },
742 { FPL("/foo.1234.gz"), FPL(".1234.gz") },
743 { FPL("/foo.12345.gz"), FPL(".gz") },
744 { FPL("/foo..gz"), FPL(".gz") },
745 { FPL("/foo.1234.tar.gz"), FPL(".tar.gz") },
746 { FPL("/foo.tar.tar.gz"), FPL(".tar.gz") },
747 { FPL("/foo.tar.gz.gz"), FPL(".gz.gz") },
748 { FPL("."), FPL("") },
749 { FPL(".."), FPL("") },
750 { FPL("./foo"), FPL("") },
751 { FPL("./foo.ext"), FPL(".ext") },
752 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
753 { FPL("/foo.bar////"), FPL(".bar") },
754 { FPL("/foo.bar/.."), FPL("") },
755 { FPL("/foo.bar/..////"), FPL("") },
757 for (unsigned int i = 0; i < arraysize(cases); ++i) {
758 FilePath path(cases[i].input);
759 FilePath::StringType extension = path.Extension();
760 EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
761 ", path: " << path.value();
765 TEST_F(FilePathTest, InsertBeforeExtension) {
766 const struct BinaryTestData cases[] = {
767 { { FPL(""), FPL("") }, FPL("") },
768 { { FPL(""), FPL("txt") }, FPL("") },
769 { { FPL("."), FPL("txt") }, FPL("") },
770 { { FPL(".."), FPL("txt") }, FPL("") },
771 { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
772 { { FPL("."), FPL("") }, FPL(".") },
773 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
774 { { FPL("foo"), FPL("txt") }, FPL("footxt") },
775 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
776 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
777 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
778 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
779 { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
780 { { FPL("foo"), FPL("") }, FPL("foo") },
781 { { FPL("foo"), FPL(".") }, FPL("foo.") },
782 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
783 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
784 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
785 { { FPL("\\"), FPL("") }, FPL("\\") },
786 { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
787 { { FPL("\\."), FPL("txt") }, FPL("") },
788 { { FPL("\\.."), FPL("txt") }, FPL("") },
789 { { FPL("\\."), FPL("") }, FPL("\\.") },
790 { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
791 FPL("C:\\bar\\footxt.dll") },
792 { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
793 FPL("C:\\bar.baz\\foodlltxt") },
794 { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
795 FPL("C:\\bar.baz\\footxt.dll") },
796 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
797 FPL("C:\\bar.baz\\foo.dlltxt.exe") },
798 { { FPL("C:\\bar.baz\\foo"), FPL("") },
799 FPL("C:\\bar.baz\\foo") },
800 { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
801 FPL("C:\\bar.baz\\foo.exe") },
802 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
803 FPL("C:\\bar.baz\\foo.dll.exe") },
804 { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
805 FPL("C:\\bar\\baz\\foo (1).exe") },
806 { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
807 { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
808 #endif
809 { { FPL("/"), FPL("") }, FPL("/") },
810 { { FPL("/"), FPL("txt") }, FPL("/txt") },
811 { { FPL("/."), FPL("txt") }, FPL("") },
812 { { FPL("/.."), FPL("txt") }, FPL("") },
813 { { FPL("/."), FPL("") }, FPL("/.") },
814 { { FPL("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
815 { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
816 { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
817 { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
818 FPL("/bar.baz/foo.dlltxt.exe") },
819 { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
820 { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
821 { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
822 { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
823 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
825 for (unsigned int i = 0; i < arraysize(cases); ++i) {
826 FilePath path(cases[i].inputs[0]);
827 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
828 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
829 ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
833 TEST_F(FilePathTest, RemoveExtension) {
834 const struct UnaryTestData cases[] = {
835 { FPL(""), FPL("") },
836 { FPL("."), FPL(".") },
837 { FPL(".."), FPL("..") },
838 { FPL("foo.dll"), FPL("foo") },
839 { FPL("./foo.dll"), FPL("./foo") },
840 { FPL("foo..dll"), FPL("foo.") },
841 { FPL("foo"), FPL("foo") },
842 { FPL("foo."), FPL("foo") },
843 { FPL("foo.."), FPL("foo.") },
844 { FPL("foo.baz.dll"), FPL("foo.baz") },
845 { FPL("foo.tar.gz"), FPL("foo") },
846 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
847 { FPL("C:\\foo.bar\\foo"), FPL("C:\\foo.bar\\foo") },
848 { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") },
849 #endif
850 { FPL("/foo.bar/foo"), FPL("/foo.bar/foo") },
851 { FPL("/foo.bar/..////"), FPL("/foo.bar/..////") },
853 for (unsigned int i = 0; i < arraysize(cases); ++i) {
854 FilePath path(cases[i].input);
855 FilePath removed = path.RemoveExtension();
856 EXPECT_EQ(cases[i].expected, removed.value()) << "i: " << i <<
857 ", path: " << path.value();
861 TEST_F(FilePathTest, ReplaceExtension) {
862 const struct BinaryTestData cases[] = {
863 { { FPL(""), FPL("") }, FPL("") },
864 { { FPL(""), FPL("txt") }, FPL("") },
865 { { FPL("."), FPL("txt") }, FPL("") },
866 { { FPL(".."), FPL("txt") }, FPL("") },
867 { { FPL("."), FPL("") }, FPL("") },
868 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
869 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.txt") },
870 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
871 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
872 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
873 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
874 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
875 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
876 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
877 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
878 { { FPL("foo.dll"), FPL("") }, FPL("foo") },
879 { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
880 { { FPL("foo"), FPL("") }, FPL("foo") },
881 { { FPL("foo"), FPL(".") }, FPL("foo") },
882 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
883 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
884 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
885 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
886 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
887 #endif
888 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
889 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
891 for (unsigned int i = 0; i < arraysize(cases); ++i) {
892 FilePath path(cases[i].inputs[0]);
893 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
894 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
895 ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
899 TEST_F(FilePathTest, MatchesExtension) {
900 const struct BinaryBooleanTestData cases[] = {
901 { { FPL("foo"), FPL("") }, true},
902 { { FPL("foo"), FPL(".") }, false},
903 { { FPL("foo."), FPL("") }, false},
904 { { FPL("foo."), FPL(".") }, true},
905 { { FPL("foo.txt"), FPL(".dll") }, false},
906 { { FPL("foo.txt"), FPL(".txt") }, true},
907 { { FPL("foo.txt.dll"), FPL(".txt") }, false},
908 { { FPL("foo.txt.dll"), FPL(".dll") }, true},
909 { { FPL("foo.TXT"), FPL(".txt") }, true},
910 { { FPL("foo.txt"), FPL(".TXT") }, true},
911 { { FPL("foo.tXt"), FPL(".txt") }, true},
912 { { FPL("foo.txt"), FPL(".tXt") }, true},
913 { { FPL("foo.tXt"), FPL(".TXT") }, true},
914 { { FPL("foo.tXt"), FPL(".tXt") }, true},
915 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
916 { { FPL("c:/foo.txt.dll"), FPL(".txt") }, false},
917 { { FPL("c:/foo.txt"), FPL(".txt") }, true},
918 #endif // FILE_PATH_USES_DRIVE_LETTERS
919 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
920 { { FPL("c:\\bar\\foo.txt.dll"), FPL(".txt") }, false},
921 { { FPL("c:\\bar\\foo.txt"), FPL(".txt") }, true},
922 #endif // FILE_PATH_USES_DRIVE_LETTERS
923 { { FPL("/bar/foo.txt.dll"), FPL(".txt") }, false},
924 { { FPL("/bar/foo.txt"), FPL(".txt") }, true},
925 #if defined(OS_WIN) || defined(OS_MACOSX)
926 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
927 { { FPL("foo.\u00E4\u00F6\u00FC"), FPL(".\u00E4\u00F6\u00FC") }, true},
928 { { FPL("foo.\u00C4\u00D6\u00DC"), FPL(".\u00E4\u00F6\u00FC") }, true},
929 // C with circumflex: direct comparison, and upper case vs. lower case
930 { { FPL("foo.\u0109"), FPL(".\u0109") }, true},
931 { { FPL("foo.\u0108"), FPL(".\u0109") }, true},
932 #endif
935 for (size_t i = 0; i < arraysize(cases); ++i) {
936 FilePath path(cases[i].inputs[0]);
937 FilePath::StringType ext(cases[i].inputs[1]);
939 EXPECT_EQ(cases[i].expected, path.MatchesExtension(ext)) <<
940 "i: " << i << ", path: " << path.value() << ", ext: " << ext;
944 TEST_F(FilePathTest, CompareIgnoreCase) {
945 const struct BinaryIntTestData cases[] = {
946 { { FPL("foo"), FPL("foo") }, 0},
947 { { FPL("FOO"), FPL("foo") }, 0},
948 { { FPL("foo.ext"), FPL("foo.ext") }, 0},
949 { { FPL("FOO.EXT"), FPL("foo.ext") }, 0},
950 { { FPL("Foo.Ext"), FPL("foo.ext") }, 0},
951 { { FPL("foO"), FPL("foo") }, 0},
952 { { FPL("foo"), FPL("foO") }, 0},
953 { { FPL("fOo"), FPL("foo") }, 0},
954 { { FPL("foo"), FPL("fOo") }, 0},
955 { { FPL("bar"), FPL("foo") }, -1},
956 { { FPL("foo"), FPL("bar") }, 1},
957 { { FPL("BAR"), FPL("foo") }, -1},
958 { { FPL("FOO"), FPL("bar") }, 1},
959 { { FPL("bar"), FPL("FOO") }, -1},
960 { { FPL("foo"), FPL("BAR") }, 1},
961 { { FPL("BAR"), FPL("FOO") }, -1},
962 { { FPL("FOO"), FPL("BAR") }, 1},
963 // German "Eszett" (lower case and the new-fangled upper case)
964 // Note that uc(<lowercase eszett>) => "SS", NOT <uppercase eszett>!
965 // However, neither Windows nor Mac OSX converts these.
966 // (or even have glyphs for <uppercase eszett>)
967 { { FPL("\u00DF"), FPL("\u00DF") }, 0},
968 { { FPL("\u1E9E"), FPL("\u1E9E") }, 0},
969 { { FPL("\u00DF"), FPL("\u1E9E") }, -1},
970 { { FPL("SS"), FPL("\u00DF") }, -1},
971 { { FPL("SS"), FPL("\u1E9E") }, -1},
972 #if defined(OS_WIN) || defined(OS_MACOSX)
973 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
974 { { FPL("\u00E4\u00F6\u00FC"), FPL("\u00E4\u00F6\u00FC") }, 0},
975 { { FPL("\u00C4\u00D6\u00DC"), FPL("\u00E4\u00F6\u00FC") }, 0},
976 // C with circumflex: direct comparison, and upper case vs. lower case
977 { { FPL("\u0109"), FPL("\u0109") }, 0},
978 { { FPL("\u0108"), FPL("\u0109") }, 0},
979 // Cyrillic letter SHA: direct comparison, and upper case vs. lower case
980 { { FPL("\u0428"), FPL("\u0428") }, 0},
981 { { FPL("\u0428"), FPL("\u0448") }, 0},
982 // Greek letter DELTA: direct comparison, and upper case vs. lower case
983 { { FPL("\u0394"), FPL("\u0394") }, 0},
984 { { FPL("\u0394"), FPL("\u03B4") }, 0},
985 // Japanese full-width A: direct comparison, and upper case vs. lower case
986 // Note that full-width and standard characters are considered different.
987 { { FPL("\uFF21"), FPL("\uFF21") }, 0},
988 { { FPL("\uFF21"), FPL("\uFF41") }, 0},
989 { { FPL("A"), FPL("\uFF21") }, -1},
990 { { FPL("A"), FPL("\uFF41") }, -1},
991 { { FPL("a"), FPL("\uFF21") }, -1},
992 { { FPL("a"), FPL("\uFF41") }, -1},
993 #endif
994 #if defined(OS_MACOSX)
995 // Codepoints > 0x1000
996 // Georgian letter DON: direct comparison, and upper case vs. lower case
997 { { FPL("\u10A3"), FPL("\u10A3") }, 0},
998 { { FPL("\u10A3"), FPL("\u10D3") }, 0},
999 // Combining characters vs. pre-composed characters, upper and lower case
1000 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1001 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("kuon") }, 1},
1002 { { FPL("kuon"), FPL("k\u0301u\u032Do\u0304\u0301n") }, -1},
1003 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("KUON") }, 1},
1004 { { FPL("KUON"), FPL("K\u0301U\u032DO\u0304\u0301N") }, -1},
1005 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("KUON") }, 1},
1006 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1007 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E30\u1E76\u1E52n") }, 0},
1008 { { FPL("k\u0301u\u032Do\u0304\u0302n"), FPL("\u1E30\u1E76\u1E52n") }, 1},
1009 #endif
1012 for (size_t i = 0; i < arraysize(cases); ++i) {
1013 FilePath::StringType s1(cases[i].inputs[0]);
1014 FilePath::StringType s2(cases[i].inputs[1]);
1015 int result = FilePath::CompareIgnoreCase(s1, s2);
1016 EXPECT_EQ(cases[i].expected, result) <<
1017 "i: " << i << ", s1: " << s1 << ", s2: " << s2;
1021 TEST_F(FilePathTest, ReferencesParent) {
1022 const struct UnaryBooleanTestData cases[] = {
1023 { FPL("."), false },
1024 { FPL(".."), true },
1025 { FPL("a.."), false },
1026 { FPL("..a"), false },
1027 { FPL("../"), true },
1028 { FPL("/.."), true },
1029 { FPL("/../"), true },
1030 { FPL("/a../"), false },
1031 { FPL("/..a/"), false },
1032 { FPL("//.."), true },
1033 { FPL("..//"), true },
1034 { FPL("//..//"), true },
1035 { FPL("a//..//c"), true },
1036 { FPL("../b/c"), true },
1037 { FPL("/../b/c"), true },
1038 { FPL("a/b/.."), true },
1039 { FPL("a/b/../"), true },
1040 { FPL("a/../c"), true },
1041 { FPL("a/b/c"), false },
1044 for (size_t i = 0; i < arraysize(cases); ++i) {
1045 FilePath input(cases[i].input);
1046 bool observed = input.ReferencesParent();
1047 EXPECT_EQ(cases[i].expected, observed) <<
1048 "i: " << i << ", input: " << input.value();
1052 TEST_F(FilePathTest, FromUTF8Unsafe_And_AsUTF8Unsafe) {
1053 const struct UTF8TestData cases[] = {
1054 { FPL("foo.txt"), "foo.txt" },
1055 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them.
1056 { FPL("\u00E0\u00E8\u00F2.txt"), "\xC3\xA0\xC3\xA8\xC3\xB2.txt" },
1057 // Full-width "ABC".
1058 { FPL("\uFF21\uFF22\uFF23.txt"),
1059 "\xEF\xBC\xA1\xEF\xBC\xA2\xEF\xBC\xA3.txt" },
1062 for (size_t i = 0; i < arraysize(cases); ++i) {
1063 // Test FromUTF8Unsafe() works.
1064 FilePath from_utf8 = FilePath::FromUTF8Unsafe(cases[i].utf8);
1065 EXPECT_EQ(cases[i].native, from_utf8.value())
1066 << "i: " << i << ", input: " << cases[i].native;
1067 // Test AsUTF8Unsafe() works.
1068 FilePath from_native = FilePath(cases[i].native);
1069 EXPECT_EQ(cases[i].utf8, from_native.AsUTF8Unsafe())
1070 << "i: " << i << ", input: " << cases[i].native;
1071 // Test the two file paths are identical.
1072 EXPECT_EQ(from_utf8.value(), from_native.value());
1076 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
1077 TEST_F(FilePathTest, NormalizeWindowsPathSeparators) {
1078 const struct UnaryTestData cases[] = {
1079 { FPL("foo/bar"), FPL("foo\\bar") },
1080 { FPL("foo/bar\\betz"), FPL("foo\\bar\\betz") },
1081 { FPL("foo\\bar"), FPL("foo\\bar") },
1082 { FPL("foo\\bar/betz"), FPL("foo\\bar\\betz") },
1083 { FPL("foo"), FPL("foo") },
1084 // Trailing slashes don't automatically get stripped. That's what
1085 // StripTrailingSeparators() is for.
1086 { FPL("foo\\"), FPL("foo\\") },
1087 { FPL("foo/"), FPL("foo\\") },
1088 { FPL("foo/bar\\"), FPL("foo\\bar\\") },
1089 { FPL("foo\\bar/"), FPL("foo\\bar\\") },
1090 { FPL("foo/bar/"), FPL("foo\\bar\\") },
1091 { FPL("foo\\bar\\"), FPL("foo\\bar\\") },
1092 { FPL("\\foo/bar"), FPL("\\foo\\bar") },
1093 { FPL("/foo\\bar"), FPL("\\foo\\bar") },
1094 { FPL("c:/foo/bar/"), FPL("c:\\foo\\bar\\") },
1095 { FPL("/foo/bar/"), FPL("\\foo\\bar\\") },
1096 { FPL("\\foo\\bar\\"), FPL("\\foo\\bar\\") },
1097 { FPL("c:\\foo/bar"), FPL("c:\\foo\\bar") },
1098 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1099 { FPL("\\\\foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1100 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1101 // This method does not normalize the number of path separators.
1102 { FPL("foo\\\\bar"), FPL("foo\\\\bar") },
1103 { FPL("foo//bar"), FPL("foo\\\\bar") },
1104 { FPL("foo/\\bar"), FPL("foo\\\\bar") },
1105 { FPL("foo\\/bar"), FPL("foo\\\\bar") },
1106 { FPL("///foo\\\\bar"), FPL("\\\\\\foo\\\\bar") },
1107 { FPL("foo//bar///"), FPL("foo\\\\bar\\\\\\") },
1108 { FPL("foo/\\bar/\\"), FPL("foo\\\\bar\\\\") },
1109 { FPL("/\\foo\\/bar"), FPL("\\\\foo\\\\bar") },
1111 for (size_t i = 0; i < arraysize(cases); ++i) {
1112 FilePath input(cases[i].input);
1113 FilePath observed = input.NormalizeWindowsPathSeparators();
1114 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
1115 "i: " << i << ", input: " << input.value();
1119 #endif