1// Copyright (c) 2010 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/utf_string_conversions.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
15struct UnaryTestData {
16  const FilePath::CharType* input;
17  const FilePath::CharType* expected;
18};
19
20struct UnaryBooleanTestData {
21  const FilePath::CharType* input;
22  bool expected;
23};
24
25struct BinaryTestData {
26  const FilePath::CharType* inputs[2];
27  const FilePath::CharType* expected;
28};
29
30struct BinaryBooleanTestData {
31  const FilePath::CharType* inputs[2];
32  bool expected;
33};
34
35struct 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
42class FilePathTest : public PlatformTest {
43 protected:
44  virtual void SetUp() {
45    PlatformTest::SetUp();
46  }
47  virtual void TearDown() {
48    PlatformTest::TearDown();
49  }
50};
51
52TEST_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
141TEST_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
228TEST_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 = WideToUTF8(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
320TEST_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
384TEST_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
452TEST_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
505TEST_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
557TEST_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
637TEST_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
697TEST_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(FILE_PATH_LITERAL(".jpg"), jpg.Extension());
702
703  FilePath base = jpg.BaseName().RemoveExtension();
704  EXPECT_EQ(FILE_PATH_LITERAL("foo"), base.value());
705
706  FilePath path_no_ext = base_dir.Append(base);
707  EXPECT_EQ(path_no_ext.value(), jpg.RemoveExtension().value());
708
709  EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value());
710  EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.Extension());
711}
712
713TEST_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("/foo.tar.gz"),            FPL(".tar.gz") },
734    { FPL("/foo.tar.Z"),             FPL(".tar.Z") },
735    { FPL("/foo.tar.bz2"),           FPL(".tar.bz2") },
736    { FPL("/subversion-1.6.12.zip"), FPL(".zip") },
737    { FPL("/foo.1234.gz"),           FPL(".1234.gz") },
738    { FPL("/foo.12345.gz"),          FPL(".gz") },
739    { FPL("/foo..gz"),               FPL(".gz") },
740    { FPL("/foo.1234.tar.gz"),       FPL(".tar.gz") },
741    { FPL("/foo.tar.tar.gz"),        FPL(".tar.gz") },
742    { FPL("/foo.tar.gz.gz"),         FPL(".gz.gz") },
743    { FPL("."),                      FPL("") },
744    { FPL(".."),                     FPL("") },
745    { FPL("./foo"),                  FPL("") },
746    { FPL("./foo.ext"),              FPL(".ext") },
747    { FPL("/foo.ext1/bar.ext2"),     FPL(".ext2") },
748    { FPL("/foo.bar////"),           FPL(".bar") },
749    { FPL("/foo.bar/.."),            FPL("") },
750    { FPL("/foo.bar/..////"),        FPL("") },
751  };
752  for (unsigned int i = 0; i < arraysize(cases); ++i) {
753    FilePath path(cases[i].input);
754    FilePath::StringType extension = path.Extension();
755    EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
756        ", path: " << path.value();
757  }
758}
759
760TEST_F(FilePathTest, InsertBeforeExtension) {
761  const struct BinaryTestData cases[] = {
762    { { FPL(""),                FPL("") },        FPL("") },
763    { { FPL(""),                FPL("txt") },     FPL("") },
764    { { FPL("."),               FPL("txt") },     FPL("") },
765    { { FPL(".."),              FPL("txt") },     FPL("") },
766    { { FPL("foo.dll"),         FPL("txt") },     FPL("footxt.dll") },
767    { { FPL("."),               FPL("") },        FPL(".") },
768    { { FPL("foo.dll"),         FPL(".txt") },    FPL("foo.txt.dll") },
769    { { FPL("foo"),             FPL("txt") },     FPL("footxt") },
770    { { FPL("foo"),             FPL(".txt") },    FPL("foo.txt") },
771    { { FPL("foo.baz.dll"),     FPL("txt") },     FPL("foo.baztxt.dll") },
772    { { FPL("foo.baz.dll"),     FPL(".txt") },    FPL("foo.baz.txt.dll") },
773    { { FPL("foo.dll"),         FPL("") },        FPL("foo.dll") },
774    { { FPL("foo.dll"),         FPL(".") },       FPL("foo..dll") },
775    { { FPL("foo"),             FPL("") },        FPL("foo") },
776    { { FPL("foo"),             FPL(".") },       FPL("foo.") },
777    { { FPL("foo.baz.dll"),     FPL("") },        FPL("foo.baz.dll") },
778    { { FPL("foo.baz.dll"),     FPL(".") },       FPL("foo.baz..dll") },
779#if defined(FILE_PATH_USES_WIN_SEPARATORS)
780    { { FPL("\\"),              FPL("") },        FPL("\\") },
781    { { FPL("\\"),              FPL("txt") },     FPL("\\txt") },
782    { { FPL("\\."),             FPL("txt") },     FPL("") },
783    { { FPL("\\.."),            FPL("txt") },     FPL("") },
784    { { FPL("\\."),             FPL("") },        FPL("\\.") },
785    { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
786        FPL("C:\\bar\\footxt.dll") },
787    { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
788        FPL("C:\\bar.baz\\foodlltxt") },
789    { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
790        FPL("C:\\bar.baz\\footxt.dll") },
791    { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
792        FPL("C:\\bar.baz\\foo.dlltxt.exe") },
793    { { FPL("C:\\bar.baz\\foo"), FPL("") },
794        FPL("C:\\bar.baz\\foo") },
795    { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
796        FPL("C:\\bar.baz\\foo.exe") },
797    { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
798        FPL("C:\\bar.baz\\foo.dll.exe") },
799    { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
800        FPL("C:\\bar\\baz\\foo (1).exe") },
801    { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") },    FPL("C:\\foo (1).baz") },
802    { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") },  FPL("") },
803#endif
804    { { FPL("/"),               FPL("") },        FPL("/") },
805    { { FPL("/"),               FPL("txt") },     FPL("/txt") },
806    { { FPL("/."),              FPL("txt") },     FPL("") },
807    { { FPL("/.."),             FPL("txt") },     FPL("") },
808    { { FPL("/."),              FPL("") },        FPL("/.") },
809    { { FPL("/bar/foo.dll"),    FPL("txt") },     FPL("/bar/footxt.dll") },
810    { { FPL("/bar.baz/foodll"), FPL("txt") },     FPL("/bar.baz/foodlltxt") },
811    { { FPL("/bar.baz/foo.dll"), FPL("txt") },    FPL("/bar.baz/footxt.dll") },
812    { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
813        FPL("/bar.baz/foo.dlltxt.exe") },
814    { { FPL("/bar.baz/foo"),    FPL("") },        FPL("/bar.baz/foo") },
815    { { FPL("/bar.baz/foo.exe"), FPL("") },       FPL("/bar.baz/foo.exe") },
816    { { FPL("/bar.baz/foo.dll.exe"), FPL("") },   FPL("/bar.baz/foo.dll.exe") },
817    { { FPL("/bar/baz/foo.exe"), FPL(" (1)") },   FPL("/bar/baz/foo (1).exe") },
818    { { FPL("/bar/baz/..////"), FPL(" (1)") },    FPL("") },
819  };
820  for (unsigned int i = 0; i < arraysize(cases); ++i) {
821    FilePath path(cases[i].inputs[0]);
822    FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
823    EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
824        ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
825  }
826}
827
828TEST_F(FilePathTest, RemoveExtension) {
829  const struct UnaryTestData cases[] = {
830    { FPL(""),                    FPL("") },
831    { FPL("."),                   FPL(".") },
832    { FPL(".."),                  FPL("..") },
833    { FPL("foo.dll"),             FPL("foo") },
834    { FPL("./foo.dll"),           FPL("./foo") },
835    { FPL("foo..dll"),            FPL("foo.") },
836    { FPL("foo"),                 FPL("foo") },
837    { FPL("foo."),                FPL("foo") },
838    { FPL("foo.."),               FPL("foo.") },
839    { FPL("foo.baz.dll"),         FPL("foo.baz") },
840    { FPL("foo.tar.gz"),          FPL("foo") },
841#if defined(FILE_PATH_USES_WIN_SEPARATORS)
842    { FPL("C:\\foo.bar\\foo"),    FPL("C:\\foo.bar\\foo") },
843    { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") },
844#endif
845    { FPL("/foo.bar/foo"),        FPL("/foo.bar/foo") },
846    { FPL("/foo.bar/..////"),     FPL("/foo.bar/..////") },
847  };
848  for (unsigned int i = 0; i < arraysize(cases); ++i) {
849    FilePath path(cases[i].input);
850    FilePath removed = path.RemoveExtension();
851    EXPECT_EQ(cases[i].expected, removed.value()) << "i: " << i <<
852        ", path: " << path.value();
853  }
854}
855
856TEST_F(FilePathTest, ReplaceExtension) {
857  const struct BinaryTestData cases[] = {
858    { { FPL(""),              FPL("") },      FPL("") },
859    { { FPL(""),              FPL("txt") },   FPL("") },
860    { { FPL("."),             FPL("txt") },   FPL("") },
861    { { FPL(".."),            FPL("txt") },   FPL("") },
862    { { FPL("."),             FPL("") },      FPL("") },
863    { { FPL("foo.dll"),       FPL("txt") },   FPL("foo.txt") },
864    { { FPL("./foo.dll"),     FPL("txt") },   FPL("./foo.txt") },
865    { { FPL("foo..dll"),      FPL("txt") },   FPL("foo..txt") },
866    { { FPL("foo.dll"),       FPL(".txt") },  FPL("foo.txt") },
867    { { FPL("foo"),           FPL("txt") },   FPL("foo.txt") },
868    { { FPL("foo."),          FPL("txt") },   FPL("foo.txt") },
869    { { FPL("foo.."),         FPL("txt") },   FPL("foo..txt") },
870    { { FPL("foo"),           FPL(".txt") },  FPL("foo.txt") },
871    { { FPL("foo.baz.dll"),   FPL("txt") },   FPL("foo.baz.txt") },
872    { { FPL("foo.baz.dll"),   FPL(".txt") },  FPL("foo.baz.txt") },
873    { { FPL("foo.dll"),       FPL("") },      FPL("foo") },
874    { { FPL("foo.dll"),       FPL(".") },     FPL("foo") },
875    { { FPL("foo"),           FPL("") },      FPL("foo") },
876    { { FPL("foo"),           FPL(".") },     FPL("foo") },
877    { { FPL("foo.baz.dll"),   FPL("") },      FPL("foo.baz") },
878    { { FPL("foo.baz.dll"),   FPL(".") },     FPL("foo.baz") },
879#if defined(FILE_PATH_USES_WIN_SEPARATORS)
880    { { FPL("C:\\foo.bar\\foo"),    FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
881    { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
882#endif
883    { { FPL("/foo.bar/foo"),        FPL("baz") }, FPL("/foo.bar/foo.baz") },
884    { { FPL("/foo.bar/..////"),     FPL("baz") }, FPL("") },
885  };
886  for (unsigned int i = 0; i < arraysize(cases); ++i) {
887    FilePath path(cases[i].inputs[0]);
888    FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
889    EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
890        ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
891  }
892}
893
894TEST_F(FilePathTest, MatchesExtension) {
895  const struct BinaryBooleanTestData cases[] = {
896    { { FPL("foo"),                     FPL("") },                    true},
897    { { FPL("foo"),                     FPL(".") },                   false},
898    { { FPL("foo."),                    FPL("") },                    false},
899    { { FPL("foo."),                    FPL(".") },                   true},
900    { { FPL("foo.txt"),                 FPL(".dll") },                false},
901    { { FPL("foo.txt"),                 FPL(".txt") },                true},
902    { { FPL("foo.txt.dll"),             FPL(".txt") },                false},
903    { { FPL("foo.txt.dll"),             FPL(".dll") },                true},
904    { { FPL("foo.TXT"),                 FPL(".txt") },                true},
905    { { FPL("foo.txt"),                 FPL(".TXT") },                true},
906    { { FPL("foo.tXt"),                 FPL(".txt") },                true},
907    { { FPL("foo.txt"),                 FPL(".tXt") },                true},
908    { { FPL("foo.tXt"),                 FPL(".TXT") },                true},
909    { { FPL("foo.tXt"),                 FPL(".tXt") },                true},
910#if defined(FILE_PATH_USES_DRIVE_LETTERS)
911    { { FPL("c:/foo.txt.dll"),          FPL(".txt") },                false},
912    { { FPL("c:/foo.txt"),              FPL(".txt") },                true},
913#endif  // FILE_PATH_USES_DRIVE_LETTERS
914#if defined(FILE_PATH_USES_WIN_SEPARATORS)
915    { { FPL("c:\\bar\\foo.txt.dll"),    FPL(".txt") },                false},
916    { { FPL("c:\\bar\\foo.txt"),        FPL(".txt") },                true},
917#endif  // FILE_PATH_USES_DRIVE_LETTERS
918    { { FPL("/bar/foo.txt.dll"),        FPL(".txt") },                false},
919    { { FPL("/bar/foo.txt"),            FPL(".txt") },                true},
920#if defined(OS_WIN) || defined(OS_MACOSX)
921    // Umlauts A, O, U: direct comparison, and upper case vs. lower case
922    { { FPL("foo.\u00E4\u00F6\u00FC"),  FPL(".\u00E4\u00F6\u00FC") }, true},
923    { { FPL("foo.\u00C4\u00D6\u00DC"),  FPL(".\u00E4\u00F6\u00FC") }, true},
924    // C with circumflex: direct comparison, and upper case vs. lower case
925    { { FPL("foo.\u0109"),              FPL(".\u0109") },             true},
926    { { FPL("foo.\u0108"),              FPL(".\u0109") },             true},
927#endif
928  };
929
930  for (size_t i = 0; i < arraysize(cases); ++i) {
931    FilePath path(cases[i].inputs[0]);
932    FilePath::StringType ext(cases[i].inputs[1]);
933
934    EXPECT_EQ(cases[i].expected, path.MatchesExtension(ext)) <<
935        "i: " << i << ", path: " << path.value() << ", ext: " << ext;
936  }
937}
938
939TEST_F(FilePathTest, CompareIgnoreCase) {
940  const struct BinaryIntTestData cases[] = {
941    { { FPL("foo"),                          FPL("foo") },                  0},
942    { { FPL("FOO"),                          FPL("foo") },                  0},
943    { { FPL("foo.ext"),                      FPL("foo.ext") },              0},
944    { { FPL("FOO.EXT"),                      FPL("foo.ext") },              0},
945    { { FPL("Foo.Ext"),                      FPL("foo.ext") },              0},
946    { { FPL("foO"),                          FPL("foo") },                  0},
947    { { FPL("foo"),                          FPL("foO") },                  0},
948    { { FPL("fOo"),                          FPL("foo") },                  0},
949    { { FPL("foo"),                          FPL("fOo") },                  0},
950    { { FPL("bar"),                          FPL("foo") },                 -1},
951    { { FPL("foo"),                          FPL("bar") },                  1},
952    { { FPL("BAR"),                          FPL("foo") },                 -1},
953    { { FPL("FOO"),                          FPL("bar") },                  1},
954    { { FPL("bar"),                          FPL("FOO") },                 -1},
955    { { FPL("foo"),                          FPL("BAR") },                  1},
956    { { FPL("BAR"),                          FPL("FOO") },                 -1},
957    { { FPL("FOO"),                          FPL("BAR") },                  1},
958    // German "Eszett" (lower case and the new-fangled upper case)
959    // Note that uc(<lowercase eszett>) => "SS", NOT <uppercase eszett>!
960    // However, neither Windows nor Mac OSX converts these.
961    // (or even have glyphs for <uppercase eszett>)
962    { { FPL("\u00DF"),                       FPL("\u00DF") },               0},
963    { { FPL("\u1E9E"),                       FPL("\u1E9E") },               0},
964    { { FPL("\u00DF"),                       FPL("\u1E9E") },              -1},
965    { { FPL("SS"),                           FPL("\u00DF") },              -1},
966    { { FPL("SS"),                           FPL("\u1E9E") },              -1},
967#if defined(OS_WIN) || defined(OS_MACOSX)
968    // Umlauts A, O, U: direct comparison, and upper case vs. lower case
969    { { FPL("\u00E4\u00F6\u00FC"),           FPL("\u00E4\u00F6\u00FC") },   0},
970    { { FPL("\u00C4\u00D6\u00DC"),           FPL("\u00E4\u00F6\u00FC") },   0},
971    // C with circumflex: direct comparison, and upper case vs. lower case
972    { { FPL("\u0109"),                       FPL("\u0109") },               0},
973    { { FPL("\u0108"),                       FPL("\u0109") },               0},
974    // Cyrillic letter SHA: direct comparison, and upper case vs. lower case
975    { { FPL("\u0428"),                       FPL("\u0428") },               0},
976    { { FPL("\u0428"),                       FPL("\u0448") },               0},
977    // Greek letter DELTA: direct comparison, and upper case vs. lower case
978    { { FPL("\u0394"),                       FPL("\u0394") },               0},
979    { { FPL("\u0394"),                       FPL("\u03B4") },               0},
980    // Japanese full-width A: direct comparison, and upper case vs. lower case
981    // Note that full-width and standard characters are considered different.
982    { { FPL("\uFF21"),                       FPL("\uFF21") },               0},
983    { { FPL("\uFF21"),                       FPL("\uFF41") },               0},
984    { { FPL("A"),                            FPL("\uFF21") },              -1},
985    { { FPL("A"),                            FPL("\uFF41") },              -1},
986    { { FPL("a"),                            FPL("\uFF21") },              -1},
987    { { FPL("a"),                            FPL("\uFF41") },              -1},
988#endif
989#if defined(OS_MACOSX)
990    // Codepoints > 0x1000
991    // Georgian letter DON: direct comparison, and upper case vs. lower case
992    { { FPL("\u10A3"),                       FPL("\u10A3") },               0},
993    { { FPL("\u10A3"),                       FPL("\u10D3") },               0},
994    // Combining characters vs. pre-composed characters, upper and lower case
995    { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E31\u1E77\u1E53n") },  0},
996    { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("kuon") },                 1},
997    { { FPL("kuon"), FPL("k\u0301u\u032Do\u0304\u0301n") },                -1},
998    { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("KUON") },                 1},
999    { { FPL("KUON"), FPL("K\u0301U\u032DO\u0304\u0301N") },                -1},
1000    { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("KUON") },                 1},
1001    { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("\u1E31\u1E77\u1E53n") },  0},
1002    { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E30\u1E76\u1E52n") },  0},
1003    { { FPL("k\u0301u\u032Do\u0304\u0302n"), FPL("\u1E30\u1E76\u1E52n") },  1},
1004#endif
1005  };
1006
1007  for (size_t i = 0; i < arraysize(cases); ++i) {
1008    FilePath::StringType s1(cases[i].inputs[0]);
1009    FilePath::StringType s2(cases[i].inputs[1]);
1010    int result = FilePath::CompareIgnoreCase(s1, s2);
1011    EXPECT_EQ(cases[i].expected, result) <<
1012        "i: " << i << ", s1: " << s1 << ", s2: " << s2;
1013  }
1014}
1015
1016TEST_F(FilePathTest, ReferencesParent) {
1017  const struct UnaryBooleanTestData cases[] = {
1018    { FPL("."),        false },
1019    { FPL(".."),       true },
1020    { FPL("a.."),      false },
1021    { FPL("..a"),      false },
1022    { FPL("../"),      true },
1023    { FPL("/.."),      true },
1024    { FPL("/../"),     true },
1025    { FPL("/a../"),    false },
1026    { FPL("/..a/"),    false },
1027    { FPL("//.."),     true },
1028    { FPL("..//"),     true },
1029    { FPL("//..//"),   true },
1030    { FPL("a//..//c"), true },
1031    { FPL("../b/c"),   true },
1032    { FPL("/../b/c"),  true },
1033    { FPL("a/b/.."),   true },
1034    { FPL("a/b/../"),  true },
1035    { FPL("a/../c"),   true },
1036    { FPL("a/b/c"),    false },
1037  };
1038
1039  for (size_t i = 0; i < arraysize(cases); ++i) {
1040    FilePath input(cases[i].input);
1041    bool observed = input.ReferencesParent();
1042    EXPECT_EQ(cases[i].expected, observed) <<
1043              "i: " << i << ", input: " << input.value();
1044  }
1045}
1046
1047#if defined(FILE_PATH_USES_WIN_SEPARATORS)
1048TEST_F(FilePathTest, NormalizeWindowsPathSeparators) {
1049  const struct UnaryTestData cases[] = {
1050    { FPL("foo/bar"), FPL("foo\\bar") },
1051    { FPL("foo/bar\\betz"), FPL("foo\\bar\\betz") },
1052    { FPL("foo\\bar"), FPL("foo\\bar") },
1053    { FPL("foo\\bar/betz"), FPL("foo\\bar\\betz") },
1054    { FPL("foo"), FPL("foo") },
1055    // Trailing slashes don't automatically get stripped.  That's what
1056    // StripTrailingSeparators() is for.
1057    { FPL("foo\\"), FPL("foo\\") },
1058    { FPL("foo/"), FPL("foo\\") },
1059    { FPL("foo/bar\\"), FPL("foo\\bar\\") },
1060    { FPL("foo\\bar/"), FPL("foo\\bar\\") },
1061    { FPL("foo/bar/"), FPL("foo\\bar\\") },
1062    { FPL("foo\\bar\\"), FPL("foo\\bar\\") },
1063    { FPL("\\foo/bar"), FPL("\\foo\\bar") },
1064    { FPL("/foo\\bar"), FPL("\\foo\\bar") },
1065    { FPL("c:/foo/bar/"), FPL("c:\\foo\\bar\\") },
1066    { FPL("/foo/bar/"), FPL("\\foo\\bar\\") },
1067    { FPL("\\foo\\bar\\"), FPL("\\foo\\bar\\") },
1068    { FPL("c:\\foo/bar"), FPL("c:\\foo\\bar") },
1069    { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1070    { FPL("\\\\foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1071    { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1072    // This method does not normalize the number of path separators.
1073    { FPL("foo\\\\bar"), FPL("foo\\\\bar") },
1074    { FPL("foo//bar"), FPL("foo\\\\bar") },
1075    { FPL("foo/\\bar"), FPL("foo\\\\bar") },
1076    { FPL("foo\\/bar"), FPL("foo\\\\bar") },
1077    { FPL("///foo\\\\bar"), FPL("\\\\\\foo\\\\bar") },
1078    { FPL("foo//bar///"), FPL("foo\\\\bar\\\\\\") },
1079    { FPL("foo/\\bar/\\"), FPL("foo\\\\bar\\\\") },
1080    { FPL("/\\foo\\/bar"), FPL("\\\\foo\\\\bar") },
1081  };
1082  for (size_t i = 0; i < arraysize(cases); ++i) {
1083    FilePath input(cases[i].input);
1084    FilePath observed = input.NormalizeWindowsPathSeparators();
1085    EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
1086              "i: " << i << ", input: " << input.value();
1087  }
1088}
1089#endif
1090