Move functions for controlling Caps Lock to CapsLockDelegate from SystemTrayDelegate.
[chromium-blink-merge.git] / base / file_path_unittest.cc
blobab2554365216348c0c605ecd5252fd382f262eea
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_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() OVERRIDE {
50 PlatformTest::SetUp();
52 virtual void TearDown() OVERRIDE {
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("") },
756 { FPL("/foo.1234.user.js"), FPL(".user.js") },
757 { FPL("foo.user.js"), FPL(".user.js") },
758 { FPL("/foo.1234.luser.js"), FPL(".js") },
759 { FPL("/user.js"), FPL(".js") },
761 for (unsigned int i = 0; i < arraysize(cases); ++i) {
762 FilePath path(cases[i].input);
763 FilePath::StringType extension = path.Extension();
764 EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
765 ", path: " << path.value();
769 TEST_F(FilePathTest, InsertBeforeExtension) {
770 const struct BinaryTestData cases[] = {
771 { { FPL(""), FPL("") }, FPL("") },
772 { { FPL(""), FPL("txt") }, FPL("") },
773 { { FPL("."), FPL("txt") }, FPL("") },
774 { { FPL(".."), FPL("txt") }, FPL("") },
775 { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
776 { { FPL("."), FPL("") }, FPL(".") },
777 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
778 { { FPL("foo"), FPL("txt") }, FPL("footxt") },
779 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
780 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
781 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
782 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
783 { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
784 { { FPL("foo"), FPL("") }, FPL("foo") },
785 { { FPL("foo"), FPL(".") }, FPL("foo.") },
786 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
787 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
788 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
789 { { FPL("\\"), FPL("") }, FPL("\\") },
790 { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
791 { { FPL("\\."), FPL("txt") }, FPL("") },
792 { { FPL("\\.."), FPL("txt") }, FPL("") },
793 { { FPL("\\."), FPL("") }, FPL("\\.") },
794 { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
795 FPL("C:\\bar\\footxt.dll") },
796 { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
797 FPL("C:\\bar.baz\\foodlltxt") },
798 { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
799 FPL("C:\\bar.baz\\footxt.dll") },
800 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
801 FPL("C:\\bar.baz\\foo.dlltxt.exe") },
802 { { FPL("C:\\bar.baz\\foo"), FPL("") },
803 FPL("C:\\bar.baz\\foo") },
804 { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
805 FPL("C:\\bar.baz\\foo.exe") },
806 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
807 FPL("C:\\bar.baz\\foo.dll.exe") },
808 { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
809 FPL("C:\\bar\\baz\\foo (1).exe") },
810 { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
811 { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
812 #endif
813 { { FPL("/"), FPL("") }, FPL("/") },
814 { { FPL("/"), FPL("txt") }, FPL("/txt") },
815 { { FPL("/."), FPL("txt") }, FPL("") },
816 { { FPL("/.."), FPL("txt") }, FPL("") },
817 { { FPL("/."), FPL("") }, FPL("/.") },
818 { { FPL("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
819 { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
820 { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
821 { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
822 FPL("/bar.baz/foo.dlltxt.exe") },
823 { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
824 { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
825 { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
826 { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
827 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
829 for (unsigned int i = 0; i < arraysize(cases); ++i) {
830 FilePath path(cases[i].inputs[0]);
831 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
832 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
833 ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
837 TEST_F(FilePathTest, RemoveExtension) {
838 const struct UnaryTestData cases[] = {
839 { FPL(""), FPL("") },
840 { FPL("."), FPL(".") },
841 { FPL(".."), FPL("..") },
842 { FPL("foo.dll"), FPL("foo") },
843 { FPL("./foo.dll"), FPL("./foo") },
844 { FPL("foo..dll"), FPL("foo.") },
845 { FPL("foo"), FPL("foo") },
846 { FPL("foo."), FPL("foo") },
847 { FPL("foo.."), FPL("foo.") },
848 { FPL("foo.baz.dll"), FPL("foo.baz") },
849 { FPL("foo.tar.gz"), FPL("foo") },
850 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
851 { FPL("C:\\foo.bar\\foo"), FPL("C:\\foo.bar\\foo") },
852 { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") },
853 #endif
854 { FPL("/foo.bar/foo"), FPL("/foo.bar/foo") },
855 { FPL("/foo.bar/..////"), FPL("/foo.bar/..////") },
857 for (unsigned int i = 0; i < arraysize(cases); ++i) {
858 FilePath path(cases[i].input);
859 FilePath removed = path.RemoveExtension();
860 EXPECT_EQ(cases[i].expected, removed.value()) << "i: " << i <<
861 ", path: " << path.value();
865 TEST_F(FilePathTest, ReplaceExtension) {
866 const struct BinaryTestData cases[] = {
867 { { FPL(""), FPL("") }, FPL("") },
868 { { FPL(""), FPL("txt") }, FPL("") },
869 { { FPL("."), FPL("txt") }, FPL("") },
870 { { FPL(".."), FPL("txt") }, FPL("") },
871 { { FPL("."), FPL("") }, FPL("") },
872 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
873 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.txt") },
874 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
875 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
876 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
877 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
878 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
879 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
880 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
881 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
882 { { FPL("foo.dll"), FPL("") }, FPL("foo") },
883 { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
884 { { FPL("foo"), FPL("") }, FPL("foo") },
885 { { FPL("foo"), FPL(".") }, FPL("foo") },
886 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
887 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
888 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
889 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
890 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
891 #endif
892 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
893 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
895 for (unsigned int i = 0; i < arraysize(cases); ++i) {
896 FilePath path(cases[i].inputs[0]);
897 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
898 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
899 ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
903 TEST_F(FilePathTest, AddExtension) {
904 const struct BinaryTestData cases[] = {
905 { { FPL(""), FPL("") }, FPL("") },
906 { { FPL(""), FPL("txt") }, FPL("") },
907 { { FPL("."), FPL("txt") }, FPL("") },
908 { { FPL(".."), FPL("txt") }, FPL("") },
909 { { FPL("."), FPL("") }, FPL("") },
910 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.dll.txt") },
911 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.dll.txt") },
912 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..dll.txt") },
913 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.dll.txt") },
914 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
915 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
916 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
917 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
918 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.dll.txt") },
919 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.dll.txt") },
920 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
921 { { FPL("foo.dll"), FPL(".") }, FPL("foo.dll") },
922 { { FPL("foo"), FPL("") }, FPL("foo") },
923 { { FPL("foo"), FPL(".") }, FPL("foo") },
924 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
925 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz.dll") },
926 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
927 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
928 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
929 #endif
930 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
931 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
933 for (unsigned int i = 0; i < arraysize(cases); ++i) {
934 FilePath path(cases[i].inputs[0]);
935 FilePath added = path.AddExtension(cases[i].inputs[1]);
936 EXPECT_EQ(cases[i].expected, added.value()) << "i: " << i <<
937 ", path: " << path.value() << ", add: " << cases[i].inputs[1];
941 TEST_F(FilePathTest, MatchesExtension) {
942 const struct BinaryBooleanTestData cases[] = {
943 { { FPL("foo"), FPL("") }, true},
944 { { FPL("foo"), FPL(".") }, false},
945 { { FPL("foo."), FPL("") }, false},
946 { { FPL("foo."), FPL(".") }, true},
947 { { FPL("foo.txt"), FPL(".dll") }, false},
948 { { FPL("foo.txt"), FPL(".txt") }, true},
949 { { FPL("foo.txt.dll"), FPL(".txt") }, false},
950 { { FPL("foo.txt.dll"), FPL(".dll") }, true},
951 { { FPL("foo.TXT"), FPL(".txt") }, true},
952 { { FPL("foo.txt"), FPL(".TXT") }, true},
953 { { FPL("foo.tXt"), FPL(".txt") }, true},
954 { { FPL("foo.txt"), FPL(".tXt") }, true},
955 { { FPL("foo.tXt"), FPL(".TXT") }, true},
956 { { FPL("foo.tXt"), FPL(".tXt") }, true},
957 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
958 { { FPL("c:/foo.txt.dll"), FPL(".txt") }, false},
959 { { FPL("c:/foo.txt"), FPL(".txt") }, true},
960 #endif // FILE_PATH_USES_DRIVE_LETTERS
961 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
962 { { FPL("c:\\bar\\foo.txt.dll"), FPL(".txt") }, false},
963 { { FPL("c:\\bar\\foo.txt"), FPL(".txt") }, true},
964 #endif // FILE_PATH_USES_DRIVE_LETTERS
965 { { FPL("/bar/foo.txt.dll"), FPL(".txt") }, false},
966 { { FPL("/bar/foo.txt"), FPL(".txt") }, true},
967 #if defined(OS_WIN) || defined(OS_MACOSX)
968 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
969 { { FPL("foo.\u00E4\u00F6\u00FC"), FPL(".\u00E4\u00F6\u00FC") }, true},
970 { { FPL("foo.\u00C4\u00D6\u00DC"), FPL(".\u00E4\u00F6\u00FC") }, true},
971 // C with circumflex: direct comparison, and upper case vs. lower case
972 { { FPL("foo.\u0109"), FPL(".\u0109") }, true},
973 { { FPL("foo.\u0108"), FPL(".\u0109") }, true},
974 #endif
977 for (size_t i = 0; i < arraysize(cases); ++i) {
978 FilePath path(cases[i].inputs[0]);
979 FilePath::StringType ext(cases[i].inputs[1]);
981 EXPECT_EQ(cases[i].expected, path.MatchesExtension(ext)) <<
982 "i: " << i << ", path: " << path.value() << ", ext: " << ext;
986 TEST_F(FilePathTest, CompareIgnoreCase) {
987 const struct BinaryIntTestData cases[] = {
988 { { FPL("foo"), FPL("foo") }, 0},
989 { { FPL("FOO"), FPL("foo") }, 0},
990 { { FPL("foo.ext"), FPL("foo.ext") }, 0},
991 { { FPL("FOO.EXT"), FPL("foo.ext") }, 0},
992 { { FPL("Foo.Ext"), FPL("foo.ext") }, 0},
993 { { FPL("foO"), FPL("foo") }, 0},
994 { { FPL("foo"), FPL("foO") }, 0},
995 { { FPL("fOo"), FPL("foo") }, 0},
996 { { FPL("foo"), FPL("fOo") }, 0},
997 { { FPL("bar"), FPL("foo") }, -1},
998 { { FPL("foo"), FPL("bar") }, 1},
999 { { FPL("BAR"), FPL("foo") }, -1},
1000 { { FPL("FOO"), FPL("bar") }, 1},
1001 { { FPL("bar"), FPL("FOO") }, -1},
1002 { { FPL("foo"), FPL("BAR") }, 1},
1003 { { FPL("BAR"), FPL("FOO") }, -1},
1004 { { FPL("FOO"), FPL("BAR") }, 1},
1005 // German "Eszett" (lower case and the new-fangled upper case)
1006 // Note that uc(<lowercase eszett>) => "SS", NOT <uppercase eszett>!
1007 // However, neither Windows nor Mac OSX converts these.
1008 // (or even have glyphs for <uppercase eszett>)
1009 { { FPL("\u00DF"), FPL("\u00DF") }, 0},
1010 { { FPL("\u1E9E"), FPL("\u1E9E") }, 0},
1011 { { FPL("\u00DF"), FPL("\u1E9E") }, -1},
1012 { { FPL("SS"), FPL("\u00DF") }, -1},
1013 { { FPL("SS"), FPL("\u1E9E") }, -1},
1014 #if defined(OS_WIN) || defined(OS_MACOSX)
1015 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
1016 { { FPL("\u00E4\u00F6\u00FC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1017 { { FPL("\u00C4\u00D6\u00DC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1018 // C with circumflex: direct comparison, and upper case vs. lower case
1019 { { FPL("\u0109"), FPL("\u0109") }, 0},
1020 { { FPL("\u0108"), FPL("\u0109") }, 0},
1021 // Cyrillic letter SHA: direct comparison, and upper case vs. lower case
1022 { { FPL("\u0428"), FPL("\u0428") }, 0},
1023 { { FPL("\u0428"), FPL("\u0448") }, 0},
1024 // Greek letter DELTA: direct comparison, and upper case vs. lower case
1025 { { FPL("\u0394"), FPL("\u0394") }, 0},
1026 { { FPL("\u0394"), FPL("\u03B4") }, 0},
1027 // Japanese full-width A: direct comparison, and upper case vs. lower case
1028 // Note that full-width and standard characters are considered different.
1029 { { FPL("\uFF21"), FPL("\uFF21") }, 0},
1030 { { FPL("\uFF21"), FPL("\uFF41") }, 0},
1031 { { FPL("A"), FPL("\uFF21") }, -1},
1032 { { FPL("A"), FPL("\uFF41") }, -1},
1033 { { FPL("a"), FPL("\uFF21") }, -1},
1034 { { FPL("a"), FPL("\uFF41") }, -1},
1035 #endif
1036 #if defined(OS_MACOSX)
1037 // Codepoints > 0x1000
1038 // Georgian letter DON: direct comparison, and upper case vs. lower case
1039 { { FPL("\u10A3"), FPL("\u10A3") }, 0},
1040 { { FPL("\u10A3"), FPL("\u10D3") }, 0},
1041 // Combining characters vs. pre-composed characters, upper and lower case
1042 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1043 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("kuon") }, 1},
1044 { { FPL("kuon"), FPL("k\u0301u\u032Do\u0304\u0301n") }, -1},
1045 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("KUON") }, 1},
1046 { { FPL("KUON"), FPL("K\u0301U\u032DO\u0304\u0301N") }, -1},
1047 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("KUON") }, 1},
1048 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1049 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E30\u1E76\u1E52n") }, 0},
1050 { { FPL("k\u0301u\u032Do\u0304\u0302n"), FPL("\u1E30\u1E76\u1E52n") }, 1},
1051 #endif
1054 for (size_t i = 0; i < arraysize(cases); ++i) {
1055 FilePath::StringType s1(cases[i].inputs[0]);
1056 FilePath::StringType s2(cases[i].inputs[1]);
1057 int result = FilePath::CompareIgnoreCase(s1, s2);
1058 EXPECT_EQ(cases[i].expected, result) <<
1059 "i: " << i << ", s1: " << s1 << ", s2: " << s2;
1063 TEST_F(FilePathTest, ReferencesParent) {
1064 const struct UnaryBooleanTestData cases[] = {
1065 { FPL("."), false },
1066 { FPL(".."), true },
1067 { FPL("a.."), false },
1068 { FPL("..a"), false },
1069 { FPL("../"), true },
1070 { FPL("/.."), true },
1071 { FPL("/../"), true },
1072 { FPL("/a../"), false },
1073 { FPL("/..a/"), false },
1074 { FPL("//.."), true },
1075 { FPL("..//"), true },
1076 { FPL("//..//"), true },
1077 { FPL("a//..//c"), true },
1078 { FPL("../b/c"), true },
1079 { FPL("/../b/c"), true },
1080 { FPL("a/b/.."), true },
1081 { FPL("a/b/../"), true },
1082 { FPL("a/../c"), true },
1083 { FPL("a/b/c"), false },
1086 for (size_t i = 0; i < arraysize(cases); ++i) {
1087 FilePath input(cases[i].input);
1088 bool observed = input.ReferencesParent();
1089 EXPECT_EQ(cases[i].expected, observed) <<
1090 "i: " << i << ", input: " << input.value();
1094 TEST_F(FilePathTest, FromUTF8Unsafe_And_AsUTF8Unsafe) {
1095 const struct UTF8TestData cases[] = {
1096 { FPL("foo.txt"), "foo.txt" },
1097 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them.
1098 { FPL("\u00E0\u00E8\u00F2.txt"), "\xC3\xA0\xC3\xA8\xC3\xB2.txt" },
1099 // Full-width "ABC".
1100 { FPL("\uFF21\uFF22\uFF23.txt"),
1101 "\xEF\xBC\xA1\xEF\xBC\xA2\xEF\xBC\xA3.txt" },
1104 for (size_t i = 0; i < arraysize(cases); ++i) {
1105 // Test FromUTF8Unsafe() works.
1106 FilePath from_utf8 = FilePath::FromUTF8Unsafe(cases[i].utf8);
1107 EXPECT_EQ(cases[i].native, from_utf8.value())
1108 << "i: " << i << ", input: " << cases[i].native;
1109 // Test AsUTF8Unsafe() works.
1110 FilePath from_native = FilePath(cases[i].native);
1111 EXPECT_EQ(cases[i].utf8, from_native.AsUTF8Unsafe())
1112 << "i: " << i << ", input: " << cases[i].native;
1113 // Test the two file paths are identical.
1114 EXPECT_EQ(from_utf8.value(), from_native.value());
1118 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
1119 TEST_F(FilePathTest, NormalizePathSeparators) {
1120 const struct UnaryTestData cases[] = {
1121 { FPL("foo/bar"), FPL("foo\\bar") },
1122 { FPL("foo/bar\\betz"), FPL("foo\\bar\\betz") },
1123 { FPL("foo\\bar"), FPL("foo\\bar") },
1124 { FPL("foo\\bar/betz"), FPL("foo\\bar\\betz") },
1125 { FPL("foo"), FPL("foo") },
1126 // Trailing slashes don't automatically get stripped. That's what
1127 // StripTrailingSeparators() is for.
1128 { FPL("foo\\"), FPL("foo\\") },
1129 { FPL("foo/"), FPL("foo\\") },
1130 { FPL("foo/bar\\"), FPL("foo\\bar\\") },
1131 { FPL("foo\\bar/"), FPL("foo\\bar\\") },
1132 { FPL("foo/bar/"), FPL("foo\\bar\\") },
1133 { FPL("foo\\bar\\"), FPL("foo\\bar\\") },
1134 { FPL("\\foo/bar"), FPL("\\foo\\bar") },
1135 { FPL("/foo\\bar"), FPL("\\foo\\bar") },
1136 { FPL("c:/foo/bar/"), FPL("c:\\foo\\bar\\") },
1137 { FPL("/foo/bar/"), FPL("\\foo\\bar\\") },
1138 { FPL("\\foo\\bar\\"), FPL("\\foo\\bar\\") },
1139 { FPL("c:\\foo/bar"), FPL("c:\\foo\\bar") },
1140 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1141 { FPL("\\\\foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1142 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1143 // This method does not normalize the number of path separators.
1144 { FPL("foo\\\\bar"), FPL("foo\\\\bar") },
1145 { FPL("foo//bar"), FPL("foo\\\\bar") },
1146 { FPL("foo/\\bar"), FPL("foo\\\\bar") },
1147 { FPL("foo\\/bar"), FPL("foo\\\\bar") },
1148 { FPL("///foo\\\\bar"), FPL("\\\\\\foo\\\\bar") },
1149 { FPL("foo//bar///"), FPL("foo\\\\bar\\\\\\") },
1150 { FPL("foo/\\bar/\\"), FPL("foo\\\\bar\\\\") },
1151 { FPL("/\\foo\\/bar"), FPL("\\\\foo\\\\bar") },
1153 for (size_t i = 0; i < arraysize(cases); ++i) {
1154 FilePath input(cases[i].input);
1155 FilePath observed = input.NormalizePathSeparators();
1156 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
1157 "i: " << i << ", input: " << input.value();
1161 #endif