1 // Copyright (c) 2008 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.
4
5 #include "base/basictypes.h"
6 #include "base/file_path.h"
7 #include "base/file_util.h"
8 #include "base/string_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
11
12 // This macro helps avoid wrapped lines in the test structs.
13 #define FPL(x) FILE_PATH_LITERAL(x)
14
15 struct UnaryTestData {
16 const FilePath::CharType* input;
17 const FilePath::CharType* expected;
18 };
19
20 struct UnaryBooleanTestData {
21 const FilePath::CharType* input;
22 bool expected;
23 };
24
25 struct BinaryTestData {
26 const FilePath::CharType* inputs[2];
27 const FilePath::CharType* expected;
28 };
29
30 struct BinaryBooleanTestData {
31 const FilePath::CharType* inputs[2];
32 bool expected;
33 };
34
35 struct BinaryIntTestData {
36 const FilePath::CharType* inputs[2];
37 int expected;
38 };
39
40 // file_util winds up using autoreleased objects on the Mac, so this needs
41 // to be a PlatformTest
42 class FilePathTest : public PlatformTest {
43 protected:
SetUp()44 virtual void SetUp() {
45 PlatformTest::SetUp();
46 }
TearDown()47 virtual void TearDown() {
48 PlatformTest::TearDown();
49 }
50 };
51
TEST_F(FilePathTest,DirName)52 TEST_F(FilePathTest, DirName) {
53 const struct UnaryTestData cases[] = {
54 { FPL(""), FPL(".") },
55 { FPL("aa"), FPL(".") },
56 { FPL("/aa/bb"), FPL("/aa") },
57 { FPL("/aa/bb/"), FPL("/aa") },
58 { FPL("/aa/bb//"), FPL("/aa") },
59 { FPL("/aa/bb/ccc"), FPL("/aa/bb") },
60 { FPL("/aa"), FPL("/") },
61 { FPL("/aa/"), FPL("/") },
62 { FPL("/"), FPL("/") },
63 { FPL("//"), FPL("//") },
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//"), FPL("aa") },
70 { FPL("aa//bb/"), FPL("aa") },
71 { FPL("aa//bb"), FPL("aa") },
72 { FPL("//aa/bb"), FPL("//aa") },
73 { FPL("//aa/"), FPL("//") },
74 { FPL("//aa"), FPL("//") },
75 { FPL("0:"), FPL(".") },
76 { FPL("@:"), FPL(".") },
77 { FPL("[:"), FPL(".") },
78 { FPL("`:"), FPL(".") },
79 { FPL("{:"), FPL(".") },
80 { FPL("\xB3:"), FPL(".") },
81 { FPL("\xC5:"), FPL(".") },
82 #if defined(OS_WIN)
83 { FPL("\x0143:"), FPL(".") },
84 #endif // OS_WIN
85 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
86 { FPL("c:"), FPL("c:") },
87 { FPL("C:"), FPL("C:") },
88 { FPL("A:"), FPL("A:") },
89 { FPL("Z:"), FPL("Z:") },
90 { FPL("a:"), FPL("a:") },
91 { FPL("z:"), FPL("z:") },
92 { FPL("c:aa"), FPL("c:") },
93 { FPL("c:/"), FPL("c:/") },
94 { FPL("c://"), FPL("c://") },
95 { FPL("c:///"), FPL("c:/") },
96 { FPL("c:/aa"), FPL("c:/") },
97 { FPL("c:/aa/"), FPL("c:/") },
98 { FPL("c:/aa/bb"), FPL("c:/aa") },
99 { FPL("c:aa/bb"), FPL("c:aa") },
100 #endif // FILE_PATH_USES_DRIVE_LETTERS
101 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
102 { FPL("\\aa\\bb"), FPL("\\aa") },
103 { FPL("\\aa\\bb\\"), FPL("\\aa") },
104 { FPL("\\aa\\bb\\\\"), FPL("\\aa") },
105 { FPL("\\aa\\bb\\ccc"), FPL("\\aa\\bb") },
106 { FPL("\\aa"), FPL("\\") },
107 { FPL("\\aa\\"), FPL("\\") },
108 { FPL("\\"), FPL("\\") },
109 { FPL("\\\\"), FPL("\\\\") },
110 { FPL("\\\\\\"), FPL("\\") },
111 { FPL("aa\\"), FPL(".") },
112 { FPL("aa\\bb"), FPL("aa") },
113 { FPL("aa\\bb\\"), FPL("aa") },
114 { FPL("aa\\bb\\\\"), FPL("aa") },
115 { FPL("aa\\\\bb\\\\"), FPL("aa") },
116 { FPL("aa\\\\bb\\"), FPL("aa") },
117 { FPL("aa\\\\bb"), FPL("aa") },
118 { FPL("\\\\aa\\bb"), FPL("\\\\aa") },
119 { FPL("\\\\aa\\"), FPL("\\\\") },
120 { FPL("\\\\aa"), FPL("\\\\") },
121 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
122 { FPL("c:\\"), FPL("c:\\") },
123 { FPL("c:\\\\"), FPL("c:\\\\") },
124 { FPL("c:\\\\\\"), FPL("c:\\") },
125 { FPL("c:\\aa"), FPL("c:\\") },
126 { FPL("c:\\aa\\"), FPL("c:\\") },
127 { FPL("c:\\aa\\bb"), FPL("c:\\aa") },
128 { FPL("c:aa\\bb"), FPL("c:aa") },
129 #endif // FILE_PATH_USES_DRIVE_LETTERS
130 #endif // FILE_PATH_USES_WIN_SEPARATORS
131 };
132
133 for (size_t i = 0; i < arraysize(cases); ++i) {
134 FilePath input(cases[i].input);
135 FilePath observed = input.DirName();
136 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
137 "i: " << i << ", input: " << input.value();
138 }
139 }
140
TEST_F(FilePathTest,BaseName)141 TEST_F(FilePathTest, BaseName) {
142 const struct UnaryTestData cases[] = {
143 { FPL(""), FPL("") },
144 { FPL("aa"), FPL("aa") },
145 { FPL("/aa/bb"), FPL("bb") },
146 { FPL("/aa/bb/"), FPL("bb") },
147 { FPL("/aa/bb//"), FPL("bb") },
148 { FPL("/aa/bb/ccc"), FPL("ccc") },
149 { FPL("/aa"), FPL("aa") },
150 { FPL("/"), FPL("/") },
151 { FPL("//"), FPL("//") },
152 { FPL("///"), FPL("/") },
153 { FPL("aa/"), FPL("aa") },
154 { FPL("aa/bb"), FPL("bb") },
155 { FPL("aa/bb/"), FPL("bb") },
156 { FPL("aa/bb//"), FPL("bb") },
157 { FPL("aa//bb//"), FPL("bb") },
158 { FPL("aa//bb/"), FPL("bb") },
159 { FPL("aa//bb"), FPL("bb") },
160 { FPL("//aa/bb"), FPL("bb") },
161 { FPL("//aa/"), FPL("aa") },
162 { FPL("//aa"), FPL("aa") },
163 { FPL("0:"), FPL("0:") },
164 { FPL("@:"), FPL("@:") },
165 { FPL("[:"), FPL("[:") },
166 { FPL("`:"), FPL("`:") },
167 { FPL("{:"), FPL("{:") },
168 { FPL("\xB3:"), FPL("\xB3:") },
169 { FPL("\xC5:"), FPL("\xC5:") },
170 #if defined(OS_WIN)
171 { FPL("\x0143:"), FPL("\x0143:") },
172 #endif // OS_WIN
173 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
174 { FPL("c:"), FPL("") },
175 { FPL("C:"), FPL("") },
176 { FPL("A:"), FPL("") },
177 { FPL("Z:"), FPL("") },
178 { FPL("a:"), FPL("") },
179 { FPL("z:"), FPL("") },
180 { FPL("c:aa"), FPL("aa") },
181 { FPL("c:/"), FPL("/") },
182 { FPL("c://"), FPL("//") },
183 { FPL("c:///"), FPL("/") },
184 { FPL("c:/aa"), FPL("aa") },
185 { FPL("c:/aa/"), FPL("aa") },
186 { FPL("c:/aa/bb"), FPL("bb") },
187 { FPL("c:aa/bb"), FPL("bb") },
188 #endif // FILE_PATH_USES_DRIVE_LETTERS
189 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
190 { FPL("\\aa\\bb"), FPL("bb") },
191 { FPL("\\aa\\bb\\"), FPL("bb") },
192 { FPL("\\aa\\bb\\\\"), FPL("bb") },
193 { FPL("\\aa\\bb\\ccc"), FPL("ccc") },
194 { FPL("\\aa"), FPL("aa") },
195 { FPL("\\"), FPL("\\") },
196 { FPL("\\\\"), FPL("\\\\") },
197 { FPL("\\\\\\"), FPL("\\") },
198 { FPL("aa\\"), FPL("aa") },
199 { FPL("aa\\bb"), FPL("bb") },
200 { FPL("aa\\bb\\"), FPL("bb") },
201 { FPL("aa\\bb\\\\"), FPL("bb") },
202 { FPL("aa\\\\bb\\\\"), FPL("bb") },
203 { FPL("aa\\\\bb\\"), FPL("bb") },
204 { FPL("aa\\\\bb"), FPL("bb") },
205 { FPL("\\\\aa\\bb"), FPL("bb") },
206 { FPL("\\\\aa\\"), FPL("aa") },
207 { FPL("\\\\aa"), FPL("aa") },
208 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
209 { FPL("c:\\"), FPL("\\") },
210 { FPL("c:\\\\"), FPL("\\\\") },
211 { FPL("c:\\\\\\"), FPL("\\") },
212 { FPL("c:\\aa"), FPL("aa") },
213 { FPL("c:\\aa\\"), FPL("aa") },
214 { FPL("c:\\aa\\bb"), FPL("bb") },
215 { FPL("c:aa\\bb"), FPL("bb") },
216 #endif // FILE_PATH_USES_DRIVE_LETTERS
217 #endif // FILE_PATH_USES_WIN_SEPARATORS
218 };
219
220 for (size_t i = 0; i < arraysize(cases); ++i) {
221 FilePath input(cases[i].input);
222 FilePath observed = input.BaseName();
223 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
224 "i: " << i << ", input: " << input.value();
225 }
226 }
227
TEST_F(FilePathTest,Append)228 TEST_F(FilePathTest, Append) {
229 const struct BinaryTestData cases[] = {
230 { { FPL(""), FPL("cc") }, FPL("cc") },
231 { { FPL("."), FPL("ff") }, FPL("ff") },
232 { { FPL("/"), FPL("cc") }, FPL("/cc") },
233 { { FPL("/aa"), FPL("") }, FPL("/aa") },
234 { { FPL("/aa/"), FPL("") }, FPL("/aa") },
235 { { FPL("//aa"), FPL("") }, FPL("//aa") },
236 { { FPL("//aa/"), FPL("") }, FPL("//aa") },
237 { { FPL("//"), FPL("aa") }, FPL("//aa") },
238 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
239 { { FPL("c:"), FPL("a") }, FPL("c:a") },
240 { { FPL("c:"), FPL("") }, FPL("c:") },
241 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
242 { { FPL("c://"), FPL("a") }, FPL("c://a") },
243 { { FPL("c:///"), FPL("a") }, FPL("c:/a") },
244 #endif // FILE_PATH_USES_DRIVE_LETTERS
245 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
246 // Append introduces the default separator character, so these test cases
247 // need to be defined with different expected results on platforms that use
248 // different default separator characters.
249 { { FPL("\\"), FPL("cc") }, FPL("\\cc") },
250 { { FPL("\\aa"), FPL("") }, FPL("\\aa") },
251 { { FPL("\\aa\\"), FPL("") }, FPL("\\aa") },
252 { { FPL("\\\\aa"), FPL("") }, FPL("\\\\aa") },
253 { { FPL("\\\\aa\\"), FPL("") }, FPL("\\\\aa") },
254 { { FPL("\\\\"), FPL("aa") }, FPL("\\\\aa") },
255 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb\\cc") },
256 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb\\cc") },
257 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb\\cc") },
258 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb\\cc") },
259 { { FPL("a/b"), FPL("c") }, FPL("a/b\\c") },
260 { { FPL("a/b/"), FPL("c") }, FPL("a/b\\c") },
261 { { FPL("//aa"), FPL("bb") }, FPL("//aa\\bb") },
262 { { FPL("//aa/"), FPL("bb") }, FPL("//aa\\bb") },
263 { { FPL("\\aa\\bb"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
264 { { FPL("\\aa\\bb\\"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
265 { { FPL("aa\\bb\\"), FPL("cc") }, FPL("aa\\bb\\cc") },
266 { { FPL("aa\\bb"), FPL("cc") }, FPL("aa\\bb\\cc") },
267 { { FPL("a\\b"), FPL("c") }, FPL("a\\b\\c") },
268 { { FPL("a\\b\\"), FPL("c") }, FPL("a\\b\\c") },
269 { { FPL("\\\\aa"), FPL("bb") }, FPL("\\\\aa\\bb") },
270 { { FPL("\\\\aa\\"), FPL("bb") }, FPL("\\\\aa\\bb") },
271 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
272 { { FPL("c:\\"), FPL("a") }, FPL("c:\\a") },
273 { { FPL("c:\\\\"), FPL("a") }, FPL("c:\\\\a") },
274 { { FPL("c:\\\\\\"), FPL("a") }, FPL("c:\\a") },
275 { { FPL("c:\\"), FPL("") }, FPL("c:\\") },
276 { { FPL("c:\\a"), FPL("b") }, FPL("c:\\a\\b") },
277 { { FPL("c:\\a\\"), FPL("b") }, FPL("c:\\a\\b") },
278 #endif // FILE_PATH_USES_DRIVE_LETTERS
279 #else // FILE_PATH_USES_WIN_SEPARATORS
280 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb/cc") },
281 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb/cc") },
282 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb/cc") },
283 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb/cc") },
284 { { FPL("a/b"), FPL("c") }, FPL("a/b/c") },
285 { { FPL("a/b/"), FPL("c") }, FPL("a/b/c") },
286 { { FPL("//aa"), FPL("bb") }, FPL("//aa/bb") },
287 { { FPL("//aa/"), FPL("bb") }, FPL("//aa/bb") },
288 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
289 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
290 { { FPL("c:/"), FPL("") }, FPL("c:/") },
291 { { FPL("c:/a"), FPL("b") }, FPL("c:/a/b") },
292 { { FPL("c:/a/"), FPL("b") }, FPL("c:/a/b") },
293 #endif // FILE_PATH_USES_DRIVE_LETTERS
294 #endif // FILE_PATH_USES_WIN_SEPARATORS
295 };
296
297 for (size_t i = 0; i < arraysize(cases); ++i) {
298 FilePath root(cases[i].inputs[0]);
299 FilePath::StringType leaf(cases[i].inputs[1]);
300 FilePath observed_str = root.Append(leaf);
301 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
302 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
303 FilePath observed_path = root.Append(FilePath(leaf));
304 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) <<
305 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
306
307 // TODO(erikkay): It would be nice to have a unicode test append value to
308 // handle the case when AppendASCII is passed UTF8
309 #if defined(OS_WIN)
310 std::string ascii = WideToASCII(leaf);
311 #elif defined(OS_POSIX)
312 std::string ascii = leaf;
313 #endif
314 observed_str = root.AppendASCII(ascii);
315 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
316 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
317 }
318 }
319
TEST_F(FilePathTest,StripTrailingSeparators)320 TEST_F(FilePathTest, StripTrailingSeparators) {
321 const struct UnaryTestData cases[] = {
322 { FPL(""), FPL("") },
323 { FPL("/"), FPL("/") },
324 { FPL("//"), FPL("//") },
325 { FPL("///"), FPL("/") },
326 { FPL("////"), FPL("/") },
327 { FPL("a/"), FPL("a") },
328 { FPL("a//"), FPL("a") },
329 { FPL("a///"), FPL("a") },
330 { FPL("a////"), FPL("a") },
331 { FPL("/a"), FPL("/a") },
332 { FPL("/a/"), FPL("/a") },
333 { FPL("/a//"), FPL("/a") },
334 { FPL("/a///"), FPL("/a") },
335 { FPL("/a////"), FPL("/a") },
336 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
337 { FPL("c:"), FPL("c:") },
338 { FPL("c:/"), FPL("c:/") },
339 { FPL("c://"), FPL("c://") },
340 { FPL("c:///"), FPL("c:/") },
341 { FPL("c:////"), FPL("c:/") },
342 { FPL("c:/a"), FPL("c:/a") },
343 { FPL("c:/a/"), FPL("c:/a") },
344 { FPL("c:/a//"), FPL("c:/a") },
345 { FPL("c:/a///"), FPL("c:/a") },
346 { FPL("c:/a////"), FPL("c:/a") },
347 #endif // FILE_PATH_USES_DRIVE_LETTERS
348 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
349 { FPL("\\"), FPL("\\") },
350 { FPL("\\\\"), FPL("\\\\") },
351 { FPL("\\\\\\"), FPL("\\") },
352 { FPL("\\\\\\\\"), FPL("\\") },
353 { FPL("a\\"), FPL("a") },
354 { FPL("a\\\\"), FPL("a") },
355 { FPL("a\\\\\\"), FPL("a") },
356 { FPL("a\\\\\\\\"), FPL("a") },
357 { FPL("\\a"), FPL("\\a") },
358 { FPL("\\a\\"), FPL("\\a") },
359 { FPL("\\a\\\\"), FPL("\\a") },
360 { FPL("\\a\\\\\\"), FPL("\\a") },
361 { FPL("\\a\\\\\\\\"), FPL("\\a") },
362 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
363 { FPL("c:\\"), FPL("c:\\") },
364 { FPL("c:\\\\"), FPL("c:\\\\") },
365 { FPL("c:\\\\\\"), FPL("c:\\") },
366 { FPL("c:\\\\\\\\"), FPL("c:\\") },
367 { FPL("c:\\a"), FPL("c:\\a") },
368 { FPL("c:\\a\\"), FPL("c:\\a") },
369 { FPL("c:\\a\\\\"), FPL("c:\\a") },
370 { FPL("c:\\a\\\\\\"), FPL("c:\\a") },
371 { FPL("c:\\a\\\\\\\\"), FPL("c:\\a") },
372 #endif // FILE_PATH_USES_DRIVE_LETTERS
373 #endif // FILE_PATH_USES_WIN_SEPARATORS
374 };
375
376 for (size_t i = 0; i < arraysize(cases); ++i) {
377 FilePath input(cases[i].input);
378 FilePath observed = input.StripTrailingSeparators();
379 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
380 "i: " << i << ", input: " << input.value();
381 }
382 }
383
TEST_F(FilePathTest,IsAbsolute)384 TEST_F(FilePathTest, IsAbsolute) {
385 const struct UnaryBooleanTestData cases[] = {
386 { FPL(""), false },
387 { FPL("a"), false },
388 { FPL("c:"), false },
389 { FPL("c:a"), false },
390 { FPL("a/b"), false },
391 { FPL("//"), true },
392 { FPL("//a"), true },
393 { FPL("c:a/b"), false },
394 { FPL("?:/a"), false },
395 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
396 { FPL("/"), false },
397 { FPL("/a"), false },
398 { FPL("/."), false },
399 { FPL("/.."), false },
400 { FPL("c:/"), true },
401 { FPL("c:/a"), true },
402 { FPL("c:/."), true },
403 { FPL("c:/.."), true },
404 { FPL("C:/a"), true },
405 { FPL("d:/a"), true },
406 #else // FILE_PATH_USES_DRIVE_LETTERS
407 { FPL("/"), true },
408 { FPL("/a"), true },
409 { FPL("/."), true },
410 { FPL("/.."), true },
411 { FPL("c:/"), false },
412 #endif // FILE_PATH_USES_DRIVE_LETTERS
413 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
414 { FPL("a\\b"), false },
415 { FPL("\\\\"), true },
416 { FPL("\\\\a"), true },
417 { FPL("a\\b"), false },
418 { FPL("\\\\"), true },
419 { FPL("//a"), true },
420 { FPL("c:a\\b"), false },
421 { FPL("?:\\a"), false },
422 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
423 { FPL("\\"), false },
424 { FPL("\\a"), false },
425 { FPL("\\."), false },
426 { FPL("\\.."), false },
427 { FPL("c:\\"), true },
428 { FPL("c:\\"), true },
429 { FPL("c:\\a"), true },
430 { FPL("c:\\."), true },
431 { FPL("c:\\.."), true },
432 { FPL("C:\\a"), true },
433 { FPL("d:\\a"), true },
434 #else // FILE_PATH_USES_DRIVE_LETTERS
435 { FPL("\\"), true },
436 { FPL("\\a"), true },
437 { FPL("\\."), true },
438 { FPL("\\.."), true },
439 { FPL("c:\\"), false },
440 #endif // FILE_PATH_USES_DRIVE_LETTERS
441 #endif // FILE_PATH_USES_WIN_SEPARATORS
442 };
443
444 for (size_t i = 0; i < arraysize(cases); ++i) {
445 FilePath input(cases[i].input);
446 bool observed = input.IsAbsolute();
447 EXPECT_EQ(cases[i].expected, observed) <<
448 "i: " << i << ", input: " << input.value();
449 }
450 }
451
TEST_F(FilePathTest,PathComponentsTest)452 TEST_F(FilePathTest, PathComponentsTest) {
453 const struct UnaryTestData cases[] = {
454 { FPL("//foo/bar/baz/"), FPL("|//|foo|bar|baz")},
455 { FPL("///"), FPL("|/")},
456 { FPL("/foo//bar//baz/"), FPL("|/|foo|bar|baz")},
457 { FPL("/foo/bar/baz/"), FPL("|/|foo|bar|baz")},
458 { FPL("/foo/bar/baz//"), FPL("|/|foo|bar|baz")},
459 { FPL("/foo/bar/baz///"), FPL("|/|foo|bar|baz")},
460 { FPL("/foo/bar/baz"), FPL("|/|foo|bar|baz")},
461 { FPL("/foo/bar.bot/baz.txt"), FPL("|/|foo|bar.bot|baz.txt")},
462 { FPL("//foo//bar/baz"), FPL("|//|foo|bar|baz")},
463 { FPL("/"), FPL("|/")},
464 { FPL("foo"), FPL("|foo")},
465 { FPL(""), FPL("")},
466 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
467 { FPL("e:/foo"), FPL("|e:|/|foo")},
468 { FPL("e:/"), FPL("|e:|/")},
469 { FPL("e:"), FPL("|e:")},
470 #endif // FILE_PATH_USES_DRIVE_LETTERS
471 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
472 { FPL("../foo"), FPL("|..|foo")},
473 { FPL("./foo"), FPL("|foo")},
474 { FPL("../foo/bar/"), FPL("|..|foo|bar") },
475 { FPL("\\\\foo\\bar\\baz\\"), FPL("|\\\\|foo|bar|baz")},
476 { FPL("\\\\\\"), FPL("|\\")},
477 { FPL("\\foo\\\\bar\\\\baz\\"), FPL("|\\|foo|bar|baz")},
478 { FPL("\\foo\\bar\\baz\\"), FPL("|\\|foo|bar|baz")},
479 { FPL("\\foo\\bar\\baz\\\\"), FPL("|\\|foo|bar|baz")},
480 { FPL("\\foo\\bar\\baz\\\\\\"), FPL("|\\|foo|bar|baz")},
481 { FPL("\\foo\\bar\\baz"), FPL("|\\|foo|bar|baz")},
482 { FPL("\\foo\\bar/baz\\\\\\"), FPL("|\\|foo|bar|baz")},
483 { FPL("/foo\\bar\\baz"), FPL("|/|foo|bar|baz")},
484 { FPL("\\foo\\bar.bot\\baz.txt"), FPL("|\\|foo|bar.bot|baz.txt")},
485 { FPL("\\\\foo\\\\bar\\baz"), FPL("|\\\\|foo|bar|baz")},
486 { FPL("\\"), FPL("|\\")},
487 #endif // FILE_PATH_USES_WIN_SEPARATORS
488 };
489
490 for (size_t i = 0; i < arraysize(cases); ++i) {
491 FilePath input(cases[i].input);
492 std::vector<FilePath::StringType> comps;
493 input.GetComponents(&comps);
494
495 FilePath::StringType observed;
496 for (size_t j = 0; j < comps.size(); ++j) {
497 observed.append(FILE_PATH_LITERAL("|"), 1);
498 observed.append(comps[j]);
499 }
500 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed) <<
501 "i: " << i << ", input: " << input.value();
502 }
503 }
504
TEST_F(FilePathTest,IsParentTest)505 TEST_F(FilePathTest, IsParentTest) {
506 const struct BinaryBooleanTestData cases[] = {
507 { { FPL("/"), FPL("/foo/bar/baz") }, true},
508 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, true},
509 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, true},
510 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, true},
511 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, false},
512 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, false},
513 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, false},
514 { { FPL("/foo/bar"), FPL("/foo/bar") }, false},
515 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
516 { { FPL("foo/bar"), FPL("foo/bar/baz") }, true},
517 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, false},
518 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, false},
519 { { FPL(""), FPL("foo") }, false},
520 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
521 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, true},
522 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, true},
523 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, true},
524 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, false},
525 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, false},
526 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, true},
527 { { FPL("c:"), FPL("c:/foo/bar/baz") }, true},
528 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
529 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, false},
530 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
531 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, false},
532 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, false},
533 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, false},
534 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, false},
535 #endif // FILE_PATH_USES_DRIVE_LETTERS
536 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
537 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, true},
538 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, true},
539 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, true},
540 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, true},
541 { { FPL(""), FPL("\\foo\\bar\\baz") }, false},
542 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, false},
543 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, false},
544 #endif // FILE_PATH_USES_WIN_SEPARATORS
545 };
546
547 for (size_t i = 0; i < arraysize(cases); ++i) {
548 FilePath parent(cases[i].inputs[0]);
549 FilePath child(cases[i].inputs[1]);
550
551 EXPECT_EQ(parent.IsParent(child), cases[i].expected) <<
552 "i: " << i << ", parent: " << parent.value() << ", child: " <<
553 child.value();
554 }
555 }
556
TEST_F(FilePathTest,AppendRelativePathTest)557 TEST_F(FilePathTest, AppendRelativePathTest) {
558 const struct BinaryTestData cases[] = {
559 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
560 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo\\bar\\baz")},
561 #else // FILE_PATH_USES_WIN_SEPARATORS
562 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo/bar/baz")},
563 #endif // FILE_PATH_USES_WIN_SEPARATORS
564 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, FPL("baz")},
565 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, FPL("baz")},
566 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, FPL("baz")},
567 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, FPL("")},
568 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, FPL("")},
569 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, FPL("")},
570 { { FPL("/foo/bar"), FPL("/foo/bar") }, FPL("")},
571 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, FPL("")},
572 { { FPL("foo/bar"), FPL("foo/bar/baz") }, FPL("baz")},
573 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, FPL("")},
574 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, FPL("")},
575 { { FPL(""), FPL("foo") }, FPL("")},
576 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
577 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, FPL("baz")},
578 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, FPL("baz")},
579 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, FPL("baz")},
580 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, FPL("")},
581 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, FPL("")},
582 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
583 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
584 // TODO(akalin): Figure out how to handle the corner case in the
585 // commented-out test case below. Appending to an empty path gives
586 // /foo\bar\baz but appending to a nonempty path "blah" gives
587 // blah\foo\bar\baz.
588 // { { FPL("c:"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
589 #endif // FILE_PATH_USES_WIN_SEPARATORS
590 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
591 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, FPL("")},
592 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
593 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, FPL("")},
594 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, FPL("")},
595 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, FPL("")},
596 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, FPL("")},
597 #endif // FILE_PATH_USES_DRIVE_LETTERS
598 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
599 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
600 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
601 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, FPL("baz")},
602 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, FPL("foo\\bar\\baz")},
603 { { FPL(""), FPL("\\foo\\bar\\baz") }, FPL("")},
604 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, FPL("")},
605 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, FPL("")},
606 #endif // FILE_PATH_USES_WIN_SEPARATORS
607 };
608
609 const FilePath base(FPL("blah"));
610
611 for (size_t i = 0; i < arraysize(cases); ++i) {
612 FilePath parent(cases[i].inputs[0]);
613 FilePath child(cases[i].inputs[1]);
614 {
615 FilePath result;
616 bool success = parent.AppendRelativePath(child, &result);
617 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
618 "i: " << i << ", parent: " << parent.value() << ", child: " <<
619 child.value();
620 EXPECT_STREQ(cases[i].expected, result.value().c_str()) <<
621 "i: " << i << ", parent: " << parent.value() << ", child: " <<
622 child.value();
623 }
624 {
625 FilePath result(base);
626 bool success = parent.AppendRelativePath(child, &result);
627 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
628 "i: " << i << ", parent: " << parent.value() << ", child: " <<
629 child.value();
630 EXPECT_EQ(base.Append(cases[i].expected).value(), result.value()) <<
631 "i: " << i << ", parent: " << parent.value() << ", child: " <<
632 child.value();
633 }
634 }
635 }
636
TEST_F(FilePathTest,EqualityTest)637 TEST_F(FilePathTest, EqualityTest) {
638 const struct BinaryBooleanTestData cases[] = {
639 { { FPL("/foo/bar/baz"), FPL("/foo/bar/baz") }, true},
640 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, false},
641 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
642 { { FPL("//foo/bar/"), FPL("//foo/bar/") }, true},
643 { { FPL("/foo/bar"), FPL("/foo2/bar") }, false},
644 { { FPL("/foo/bar.txt"), FPL("/foo/bar") }, false},
645 { { FPL("foo/bar"), FPL("foo/bar") }, true},
646 { { FPL("foo/bar"), FPL("foo/bar/baz") }, false},
647 { { FPL(""), FPL("foo") }, false},
648 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
649 { { FPL("c:/foo/bar"), FPL("c:/foo/bar") }, true},
650 { { FPL("E:/foo/bar"), FPL("e:/foo/bar") }, true},
651 { { FPL("f:/foo/bar"), FPL("F:/foo/bar") }, true},
652 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar") }, false},
653 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar") }, false},
654 { { FPL("c:/"), FPL("c:/") }, true},
655 { { FPL("c:"), FPL("c:") }, true},
656 { { FPL("c:/foo/bar"), FPL("d:/foo/bar") }, false},
657 { { FPL("c:/foo/bar"), FPL("D:/foo/bar") }, false},
658 { { FPL("C:/foo/bar"), FPL("d:/foo/bar") }, false},
659 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar") }, false},
660 #endif // FILE_PATH_USES_DRIVE_LETTERS
661 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
662 { { FPL("\\foo\\bar"), FPL("\\foo\\bar") }, true},
663 { { FPL("\\foo/bar"), FPL("\\foo/bar") }, true},
664 { { FPL("\\foo/bar"), FPL("\\foo\bar") }, false},
665 { { FPL("\\"), FPL("\\") }, true},
666 { { FPL("\\"), FPL("/") }, false},
667 { { FPL(""), FPL("\\") }, false},
668 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar") }, false},
669 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2") }, false},
670 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
671 { { FPL("c:\\foo\\bar"), FPL("c:\\foo\\bar") }, true},
672 { { FPL("E:\\foo\\bar"), FPL("e:\\foo\\bar") }, true},
673 { { FPL("f:\\foo\\bar"), FPL("F:\\foo/bar") }, false},
674 #endif // FILE_PATH_USES_DRIVE_LETTERS
675 #endif // FILE_PATH_USES_WIN_SEPARATORS
676 };
677
678 for (size_t i = 0; i < arraysize(cases); ++i) {
679 FilePath a(cases[i].inputs[0]);
680 FilePath b(cases[i].inputs[1]);
681
682 EXPECT_EQ(a == b, cases[i].expected) <<
683 "equality i: " << i << ", a: " << a.value() << ", b: " <<
684 b.value();
685 }
686
687 for (size_t i = 0; i < arraysize(cases); ++i) {
688 FilePath a(cases[i].inputs[0]);
689 FilePath b(cases[i].inputs[1]);
690
691 EXPECT_EQ(a != b, !cases[i].expected) <<
692 "inequality i: " << i << ", a: " << a.value() << ", b: " <<
693 b.value();
694 }
695 }
696
TEST_F(FilePathTest,Extension)697 TEST_F(FilePathTest, Extension) {
698 FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
699
700 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
701 EXPECT_EQ(jpg.Extension(), FILE_PATH_LITERAL(".jpg"));
702
703 FilePath base = jpg.BaseName().RemoveExtension();
704 EXPECT_EQ(base.value(), FILE_PATH_LITERAL("foo"));
705
706 FilePath path_no_ext = base_dir.Append(base);
707 EXPECT_EQ(jpg.RemoveExtension().value(), path_no_ext.value());
708
709 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value());
710 EXPECT_EQ(path_no_ext.Extension(), FILE_PATH_LITERAL(""));
711 }
712
TEST_F(FilePathTest,Extension2)713 TEST_F(FilePathTest, Extension2) {
714 const struct UnaryTestData cases[] = {
715 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
716 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
717 { FPL("C:\\a\\b\\c."), FPL(".") },
718 { FPL("C:\\a\\b\\c"), FPL("") },
719 { FPL("C:\\a\\b\\"), FPL("") },
720 { FPL("C:\\a\\b.\\"), FPL(".") },
721 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
722 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
723 { FPL("C:\\foo.bar\\.."), FPL("") },
724 { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
725 #endif
726 { FPL("/foo/bar/baz.ext"), FPL(".ext") },
727 { FPL("/foo/bar/baz."), FPL(".") },
728 { FPL("/foo/bar/baz.."), FPL(".") },
729 { FPL("/foo/bar/baz"), FPL("") },
730 { FPL("/foo/bar/"), FPL("") },
731 { FPL("/foo/bar./"), FPL(".") },
732 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
733 { FPL("."), FPL("") },
734 { FPL(".."), FPL("") },
735 { FPL("./foo"), FPL("") },
736 { FPL("./foo.ext"), FPL(".ext") },
737 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
738 { FPL("/foo.bar////"), FPL(".bar") },
739 { FPL("/foo.bar/.."), FPL("") },
740 { FPL("/foo.bar/..////"), FPL("") },
741 };
742 for (unsigned int i = 0; i < arraysize(cases); ++i) {
743 FilePath path(cases[i].input);
744 FilePath::StringType extension = path.Extension();
745 EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
746 ", path: " << path.value();
747 }
748 }
749
TEST_F(FilePathTest,InsertBeforeExtension)750 TEST_F(FilePathTest, InsertBeforeExtension) {
751 const struct BinaryTestData cases[] = {
752 { { FPL(""), FPL("") }, FPL("") },
753 { { FPL(""), FPL("txt") }, FPL("") },
754 { { FPL("."), FPL("txt") }, FPL("") },
755 { { FPL(".."), FPL("txt") }, FPL("") },
756 { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
757 { { FPL("."), FPL("") }, FPL(".") },
758 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
759 { { FPL("foo"), FPL("txt") }, FPL("footxt") },
760 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
761 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
762 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
763 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
764 { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
765 { { FPL("foo"), FPL("") }, FPL("foo") },
766 { { FPL("foo"), FPL(".") }, FPL("foo.") },
767 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
768 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
769 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
770 { { FPL("\\"), FPL("") }, FPL("\\") },
771 { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
772 { { FPL("\\."), FPL("txt") }, FPL("") },
773 { { FPL("\\.."), FPL("txt") }, FPL("") },
774 { { FPL("\\."), FPL("") }, FPL("\\.") },
775 { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
776 FPL("C:\\bar\\footxt.dll") },
777 { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
778 FPL("C:\\bar.baz\\foodlltxt") },
779 { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
780 FPL("C:\\bar.baz\\footxt.dll") },
781 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
782 FPL("C:\\bar.baz\\foo.dlltxt.exe") },
783 { { FPL("C:\\bar.baz\\foo"), FPL("") },
784 FPL("C:\\bar.baz\\foo") },
785 { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
786 FPL("C:\\bar.baz\\foo.exe") },
787 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
788 FPL("C:\\bar.baz\\foo.dll.exe") },
789 { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
790 FPL("C:\\bar\\baz\\foo (1).exe") },
791 { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
792 { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
793 #endif
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("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
800 { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
801 { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
802 { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
803 FPL("/bar.baz/foo.dlltxt.exe") },
804 { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
805 { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
806 { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
807 { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
808 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
809 };
810 for (unsigned int i = 0; i < arraysize(cases); ++i) {
811 FilePath path(cases[i].inputs[0]);
812 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
813 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
814 ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
815 }
816 }
817
TEST_F(FilePathTest,ReplaceExtension)818 TEST_F(FilePathTest, ReplaceExtension) {
819 const struct BinaryTestData cases[] = {
820 { { FPL(""), FPL("") }, FPL("") },
821 { { FPL(""), FPL("txt") }, FPL("") },
822 { { FPL("."), FPL("txt") }, FPL("") },
823 { { FPL(".."), FPL("txt") }, FPL("") },
824 { { FPL("."), FPL("") }, FPL("") },
825 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
826 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
827 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
828 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
829 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
830 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
831 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
832 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
833 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
834 { { FPL("foo.dll"), FPL("") }, FPL("foo") },
835 { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
836 { { FPL("foo"), FPL("") }, FPL("foo") },
837 { { FPL("foo"), FPL(".") }, FPL("foo") },
838 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
839 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
840 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
841 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
842 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
843 #endif
844 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
845 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
846 };
847 for (unsigned int i = 0; i < arraysize(cases); ++i) {
848 FilePath path(cases[i].inputs[0]);
849 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
850 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
851 ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
852 }
853 }
854
TEST_F(FilePathTest,MatchesExtension)855 TEST_F(FilePathTest, MatchesExtension) {
856 const struct BinaryBooleanTestData cases[] = {
857 { { FPL("foo"), FPL("") }, true},
858 { { FPL("foo"), FPL(".") }, false},
859 { { FPL("foo."), FPL("") }, false},
860 { { FPL("foo."), FPL(".") }, true},
861 { { FPL("foo.txt"), FPL(".dll") }, false},
862 { { FPL("foo.txt"), FPL(".txt") }, true},
863 { { FPL("foo.txt.dll"), FPL(".txt") }, false},
864 { { FPL("foo.txt.dll"), FPL(".dll") }, true},
865 { { FPL("foo.TXT"), FPL(".txt") }, true},
866 { { FPL("foo.txt"), FPL(".TXT") }, true},
867 { { FPL("foo.tXt"), FPL(".txt") }, true},
868 { { FPL("foo.txt"), FPL(".tXt") }, true},
869 { { FPL("foo.tXt"), FPL(".TXT") }, true},
870 { { FPL("foo.tXt"), FPL(".tXt") }, true},
871 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
872 { { FPL("c:/foo.txt.dll"), FPL(".txt") }, false},
873 { { FPL("c:/foo.txt"), FPL(".txt") }, true},
874 #endif // FILE_PATH_USES_DRIVE_LETTERS
875 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
876 { { FPL("c:\\bar\\foo.txt.dll"), FPL(".txt") }, false},
877 { { FPL("c:\\bar\\foo.txt"), FPL(".txt") }, true},
878 #endif // FILE_PATH_USES_DRIVE_LETTERS
879 { { FPL("/bar/foo.txt.dll"), FPL(".txt") }, false},
880 { { FPL("/bar/foo.txt"), FPL(".txt") }, true},
881 #if defined(OS_WIN) || defined(OS_MACOSX)
882 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
883 { { FPL("foo.\u00E4\u00F6\u00FC"), FPL(".\u00E4\u00F6\u00FC") }, true},
884 { { FPL("foo.\u00C4\u00D6\u00DC"), FPL(".\u00E4\u00F6\u00FC") }, true},
885 // C with circumflex: direct comparison, and upper case vs. lower case
886 { { FPL("foo.\u0109"), FPL(".\u0109") }, true},
887 { { FPL("foo.\u0108"), FPL(".\u0109") }, true},
888 #endif
889 };
890
891 for (size_t i = 0; i < arraysize(cases); ++i) {
892 FilePath path(cases[i].inputs[0]);
893 FilePath::StringType ext(cases[i].inputs[1]);
894
895 EXPECT_EQ(cases[i].expected, path.MatchesExtension(ext)) <<
896 "i: " << i << ", path: " << path.value() << ", ext: " << ext;
897 }
898 }
899
TEST_F(FilePathTest,CompareIgnoreCase)900 TEST_F(FilePathTest, CompareIgnoreCase) {
901 const struct BinaryIntTestData cases[] = {
902 { { FPL("foo"), FPL("foo") }, 0},
903 { { FPL("FOO"), FPL("foo") }, 0},
904 { { FPL("foo.ext"), FPL("foo.ext") }, 0},
905 { { FPL("FOO.EXT"), FPL("foo.ext") }, 0},
906 { { FPL("Foo.Ext"), FPL("foo.ext") }, 0},
907 { { FPL("foO"), FPL("foo") }, 0},
908 { { FPL("foo"), FPL("foO") }, 0},
909 { { FPL("fOo"), FPL("foo") }, 0},
910 { { FPL("foo"), FPL("fOo") }, 0},
911 { { FPL("bar"), FPL("foo") }, -1},
912 { { FPL("foo"), FPL("bar") }, 1},
913 { { FPL("BAR"), FPL("foo") }, -1},
914 { { FPL("FOO"), FPL("bar") }, 1},
915 { { FPL("bar"), FPL("FOO") }, -1},
916 { { FPL("foo"), FPL("BAR") }, 1},
917 { { FPL("BAR"), FPL("FOO") }, -1},
918 { { FPL("FOO"), FPL("BAR") }, 1},
919 // German "Eszett" (lower case and the new-fangled upper case)
920 // Note that uc(<lowercase eszett>) => "SS", NOT <uppercase eszett>!
921 // However, neither Windows nor Mac OSX converts these.
922 // (or even have glyphs for <uppercase eszett>)
923 { { FPL("\u00DF"), FPL("\u00DF") }, 0},
924 { { FPL("\u1E9E"), FPL("\u1E9E") }, 0},
925 { { FPL("\u00DF"), FPL("\u1E9E") }, -1},
926 { { FPL("SS"), FPL("\u00DF") }, -1},
927 { { FPL("SS"), FPL("\u1E9E") }, -1},
928 #if defined(OS_WIN) || defined(OS_MACOSX)
929 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
930 { { FPL("\u00E4\u00F6\u00FC"), FPL("\u00E4\u00F6\u00FC") }, 0},
931 { { FPL("\u00C4\u00D6\u00DC"), FPL("\u00E4\u00F6\u00FC") }, 0},
932 // C with circumflex: direct comparison, and upper case vs. lower case
933 { { FPL("\u0109"), FPL("\u0109") }, 0},
934 { { FPL("\u0108"), FPL("\u0109") }, 0},
935 // Cyrillic letter SHA: direct comparison, and upper case vs. lower case
936 { { FPL("\u0428"), FPL("\u0428") }, 0},
937 { { FPL("\u0428"), FPL("\u0448") }, 0},
938 // Greek letter DELTA: direct comparison, and upper case vs. lower case
939 { { FPL("\u0394"), FPL("\u0394") }, 0},
940 { { FPL("\u0394"), FPL("\u03B4") }, 0},
941 // Japanese full-width A: direct comparison, and upper case vs. lower case
942 // Note that full-width and standard characters are considered different.
943 { { FPL("\uFF21"), FPL("\uFF21") }, 0},
944 { { FPL("\uFF21"), FPL("\uFF41") }, 0},
945 { { FPL("A"), FPL("\uFF21") }, -1},
946 { { FPL("A"), FPL("\uFF41") }, -1},
947 { { FPL("a"), FPL("\uFF21") }, -1},
948 { { FPL("a"), FPL("\uFF41") }, -1},
949 #endif
950 #if defined(OS_MACOSX)
951 // Codepoints > 0x1000
952 // Georgian letter DON: direct comparison, and upper case vs. lower case
953 { { FPL("\u10A3"), FPL("\u10A3") }, 0},
954 { { FPL("\u10A3"), FPL("\u10D3") }, 0},
955 // Combining characters vs. pre-composed characters, upper and lower case
956 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E31\u1E77\u1E53n") }, 0},
957 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("kuon") }, 1},
958 { { FPL("kuon"), FPL("k\u0301u\u032Do\u0304\u0301n") }, -1},
959 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("KUON") }, 1},
960 { { FPL("KUON"), FPL("K\u0301U\u032DO\u0304\u0301N") }, -1},
961 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("KUON") }, 1},
962 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("\u1E31\u1E77\u1E53n") }, 0},
963 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E30\u1E76\u1E52n") }, 0},
964 { { FPL("k\u0301u\u032Do\u0304\u0302n"), FPL("\u1E30\u1E76\u1E52n") }, 1},
965 #endif
966 };
967
968 for (size_t i = 0; i < arraysize(cases); ++i) {
969 FilePath::StringType s1(cases[i].inputs[0]);
970 FilePath::StringType s2(cases[i].inputs[1]);
971 int result = FilePath::CompareIgnoreCase(s1, s2);
972 EXPECT_EQ(cases[i].expected, result) <<
973 "i: " << i << ", s1: " << s1 << ", s2: " << s2;
974 }
975 }
976
TEST_F(FilePathTest,ReferencesParent)977 TEST_F(FilePathTest, ReferencesParent) {
978 const struct UnaryBooleanTestData cases[] = {
979 { FPL("."), false },
980 { FPL(".."), true },
981 { FPL("a.."), false },
982 { FPL("..a"), false },
983 { FPL("../"), true },
984 { FPL("/.."), true },
985 { FPL("/../"), true },
986 { FPL("/a../"), false },
987 { FPL("/..a/"), false },
988 { FPL("//.."), true },
989 { FPL("..//"), true },
990 { FPL("//..//"), true },
991 { FPL("a//..//c"), true },
992 { FPL("../b/c"), true },
993 { FPL("/../b/c"), true },
994 { FPL("a/b/.."), true },
995 { FPL("a/b/../"), true },
996 { FPL("a/../c"), true },
997 { FPL("a/b/c"), false },
998 };
999
1000 for (size_t i = 0; i < arraysize(cases); ++i) {
1001 FilePath input(cases[i].input);
1002 bool observed = input.ReferencesParent();
1003 EXPECT_EQ(cases[i].expected, observed) <<
1004 "i: " << i << ", input: " << input.value();
1005 }
1006 }
1007