blob: 60eaa8f002f2e1103ad78dfc6fdaf4f5b7849bb0 [file] [log] [blame]
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
mark@chromium.org6380e262008-10-04 03:21:47 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
mgiuca36c0d632015-05-28 19:11:33 +09005#include <sstream>
6
mark@chromium.org6380e262008-10-04 03:21:47 +09007#include "base/basictypes.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +09008#include "base/files/file_path.h"
avi@chromium.org17f60622013-06-08 03:37:07 +09009#include "base/strings/utf_string_conversions.h"
mark@chromium.orge3e1ce72008-10-04 04:05:46 +090010#include "testing/gtest/include/gtest/gtest.h"
aa@chromium.org76e5c632008-12-18 17:38:16 +090011#include "testing/platform_test.h"
mark@chromium.org6380e262008-10-04 03:21:47 +090012
13// This macro helps avoid wrapped lines in the test structs.
14#define FPL(x) FILE_PATH_LITERAL(x)
15
aedla@chromium.org51127b92013-01-28 22:47:55 +090016// This macro constructs strings which can contain NULs.
17#define FPS(x) FilePath::StringType(FPL(x), arraysize(FPL(x)) - 1)
18
brettw@chromium.org82bcf512013-02-17 14:07:23 +090019namespace base {
20
mark@chromium.org6380e262008-10-04 03:21:47 +090021struct UnaryTestData {
22 const FilePath::CharType* input;
23 const FilePath::CharType* expected;
24};
25
26struct UnaryBooleanTestData {
27 const FilePath::CharType* input;
28 bool expected;
29};
30
31struct BinaryTestData {
32 const FilePath::CharType* inputs[2];
33 const FilePath::CharType* expected;
34};
35
rafaelw@chromium.org465facc2009-06-25 06:28:30 +090036struct BinaryBooleanTestData {
37 const FilePath::CharType* inputs[2];
38 bool expected;
39};
40
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +090041struct BinaryIntTestData {
42 const FilePath::CharType* inputs[2];
43 int expected;
44};
45
satorux@chromium.orgefba4172011-11-02 13:55:23 +090046struct UTF8TestData {
47 const FilePath::CharType* native;
48 const char* utf8;
49};
50
aa@chromium.org76e5c632008-12-18 17:38:16 +090051// file_util winds up using autoreleased objects on the Mac, so this needs
52// to be a PlatformTest
dchengca87abb2014-12-23 11:56:47 +090053typedef PlatformTest FilePathTest;
aa@chromium.org76e5c632008-12-18 17:38:16 +090054
55TEST_F(FilePathTest, DirName) {
mark@chromium.org6380e262008-10-04 03:21:47 +090056 const struct UnaryTestData cases[] = {
57 { FPL(""), FPL(".") },
58 { FPL("aa"), FPL(".") },
59 { FPL("/aa/bb"), FPL("/aa") },
60 { FPL("/aa/bb/"), FPL("/aa") },
61 { FPL("/aa/bb//"), FPL("/aa") },
62 { FPL("/aa/bb/ccc"), FPL("/aa/bb") },
63 { FPL("/aa"), FPL("/") },
64 { FPL("/aa/"), FPL("/") },
65 { FPL("/"), FPL("/") },
66 { FPL("//"), FPL("//") },
67 { FPL("///"), FPL("/") },
68 { FPL("aa/"), FPL(".") },
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//bb/"), FPL("aa") },
74 { FPL("aa//bb"), FPL("aa") },
75 { FPL("//aa/bb"), FPL("//aa") },
76 { FPL("//aa/"), FPL("//") },
77 { FPL("//aa"), FPL("//") },
78 { FPL("0:"), FPL(".") },
79 { FPL("@:"), FPL(".") },
80 { FPL("[:"), FPL(".") },
81 { FPL("`:"), FPL(".") },
82 { FPL("{:"), FPL(".") },
83 { FPL("\xB3:"), FPL(".") },
84 { FPL("\xC5:"), FPL(".") },
85#if defined(OS_WIN)
86 { FPL("\x0143:"), FPL(".") },
87#endif // OS_WIN
88#if defined(FILE_PATH_USES_DRIVE_LETTERS)
89 { FPL("c:"), FPL("c:") },
90 { FPL("C:"), FPL("C:") },
91 { FPL("A:"), FPL("A:") },
92 { FPL("Z:"), FPL("Z:") },
93 { FPL("a:"), FPL("a:") },
94 { FPL("z:"), FPL("z:") },
95 { FPL("c:aa"), FPL("c:") },
96 { FPL("c:/"), FPL("c:/") },
97 { FPL("c://"), FPL("c://") },
98 { FPL("c:///"), FPL("c:/") },
99 { FPL("c:/aa"), FPL("c:/") },
100 { FPL("c:/aa/"), FPL("c:/") },
101 { FPL("c:/aa/bb"), FPL("c:/aa") },
102 { FPL("c:aa/bb"), FPL("c:aa") },
103#endif // FILE_PATH_USES_DRIVE_LETTERS
104#if defined(FILE_PATH_USES_WIN_SEPARATORS)
105 { FPL("\\aa\\bb"), FPL("\\aa") },
106 { FPL("\\aa\\bb\\"), FPL("\\aa") },
107 { FPL("\\aa\\bb\\\\"), FPL("\\aa") },
108 { FPL("\\aa\\bb\\ccc"), FPL("\\aa\\bb") },
109 { FPL("\\aa"), FPL("\\") },
110 { FPL("\\aa\\"), FPL("\\") },
111 { FPL("\\"), FPL("\\") },
112 { FPL("\\\\"), FPL("\\\\") },
113 { FPL("\\\\\\"), FPL("\\") },
114 { FPL("aa\\"), FPL(".") },
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\\\\bb\\"), FPL("aa") },
120 { FPL("aa\\\\bb"), FPL("aa") },
121 { FPL("\\\\aa\\bb"), FPL("\\\\aa") },
122 { FPL("\\\\aa\\"), FPL("\\\\") },
123 { FPL("\\\\aa"), FPL("\\\\") },
124#if defined(FILE_PATH_USES_DRIVE_LETTERS)
125 { FPL("c:\\"), FPL("c:\\") },
126 { FPL("c:\\\\"), FPL("c:\\\\") },
127 { FPL("c:\\\\\\"), FPL("c:\\") },
128 { FPL("c:\\aa"), FPL("c:\\") },
129 { FPL("c:\\aa\\"), FPL("c:\\") },
130 { FPL("c:\\aa\\bb"), FPL("c:\\aa") },
131 { FPL("c:aa\\bb"), FPL("c:aa") },
132#endif // FILE_PATH_USES_DRIVE_LETTERS
133#endif // FILE_PATH_USES_WIN_SEPARATORS
134 };
135
136 for (size_t i = 0; i < arraysize(cases); ++i) {
137 FilePath input(cases[i].input);
138 FilePath observed = input.DirName();
139 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
140 "i: " << i << ", input: " << input.value();
141 }
142}
143
aa@chromium.org76e5c632008-12-18 17:38:16 +0900144TEST_F(FilePathTest, BaseName) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900145 const struct UnaryTestData cases[] = {
146 { FPL(""), FPL("") },
147 { FPL("aa"), FPL("aa") },
148 { FPL("/aa/bb"), FPL("bb") },
149 { FPL("/aa/bb/"), FPL("bb") },
150 { FPL("/aa/bb//"), FPL("bb") },
151 { FPL("/aa/bb/ccc"), FPL("ccc") },
152 { FPL("/aa"), FPL("aa") },
153 { FPL("/"), FPL("/") },
154 { FPL("//"), FPL("//") },
155 { FPL("///"), FPL("/") },
156 { FPL("aa/"), FPL("aa") },
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//bb/"), FPL("bb") },
162 { FPL("aa//bb"), FPL("bb") },
163 { FPL("//aa/bb"), FPL("bb") },
164 { FPL("//aa/"), FPL("aa") },
165 { FPL("//aa"), FPL("aa") },
166 { FPL("0:"), FPL("0:") },
167 { FPL("@:"), FPL("@:") },
168 { FPL("[:"), FPL("[:") },
169 { FPL("`:"), FPL("`:") },
170 { FPL("{:"), FPL("{:") },
171 { FPL("\xB3:"), FPL("\xB3:") },
172 { FPL("\xC5:"), FPL("\xC5:") },
173#if defined(OS_WIN)
174 { FPL("\x0143:"), FPL("\x0143:") },
175#endif // OS_WIN
176#if defined(FILE_PATH_USES_DRIVE_LETTERS)
177 { FPL("c:"), FPL("") },
178 { FPL("C:"), FPL("") },
179 { FPL("A:"), FPL("") },
180 { FPL("Z:"), FPL("") },
181 { FPL("a:"), FPL("") },
182 { FPL("z:"), FPL("") },
183 { FPL("c:aa"), FPL("aa") },
184 { FPL("c:/"), FPL("/") },
185 { FPL("c://"), FPL("//") },
186 { FPL("c:///"), FPL("/") },
187 { FPL("c:/aa"), FPL("aa") },
188 { FPL("c:/aa/"), FPL("aa") },
189 { FPL("c:/aa/bb"), FPL("bb") },
190 { FPL("c:aa/bb"), FPL("bb") },
191#endif // FILE_PATH_USES_DRIVE_LETTERS
192#if defined(FILE_PATH_USES_WIN_SEPARATORS)
193 { FPL("\\aa\\bb"), FPL("bb") },
194 { FPL("\\aa\\bb\\"), FPL("bb") },
195 { FPL("\\aa\\bb\\\\"), FPL("bb") },
196 { FPL("\\aa\\bb\\ccc"), FPL("ccc") },
197 { FPL("\\aa"), FPL("aa") },
198 { FPL("\\"), FPL("\\") },
199 { FPL("\\\\"), FPL("\\\\") },
200 { FPL("\\\\\\"), FPL("\\") },
201 { FPL("aa\\"), FPL("aa") },
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\\\\bb\\"), FPL("bb") },
207 { FPL("aa\\\\bb"), FPL("bb") },
208 { FPL("\\\\aa\\bb"), FPL("bb") },
209 { FPL("\\\\aa\\"), FPL("aa") },
210 { FPL("\\\\aa"), FPL("aa") },
211#if defined(FILE_PATH_USES_DRIVE_LETTERS)
212 { FPL("c:\\"), FPL("\\") },
213 { FPL("c:\\\\"), FPL("\\\\") },
214 { FPL("c:\\\\\\"), FPL("\\") },
215 { FPL("c:\\aa"), FPL("aa") },
216 { FPL("c:\\aa\\"), FPL("aa") },
217 { FPL("c:\\aa\\bb"), FPL("bb") },
218 { FPL("c:aa\\bb"), FPL("bb") },
219#endif // FILE_PATH_USES_DRIVE_LETTERS
220#endif // FILE_PATH_USES_WIN_SEPARATORS
221 };
222
223 for (size_t i = 0; i < arraysize(cases); ++i) {
224 FilePath input(cases[i].input);
estade@chromium.orgd872d682009-01-06 08:59:36 +0900225 FilePath observed = input.BaseName();
226 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
mark@chromium.org6380e262008-10-04 03:21:47 +0900227 "i: " << i << ", input: " << input.value();
228 }
229}
230
aa@chromium.org76e5c632008-12-18 17:38:16 +0900231TEST_F(FilePathTest, Append) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900232 const struct BinaryTestData cases[] = {
233 { { FPL(""), FPL("cc") }, FPL("cc") },
234 { { FPL("."), FPL("ff") }, FPL("ff") },
235 { { FPL("/"), FPL("cc") }, FPL("/cc") },
236 { { FPL("/aa"), FPL("") }, FPL("/aa") },
237 { { FPL("/aa/"), FPL("") }, FPL("/aa") },
238 { { FPL("//aa"), FPL("") }, FPL("//aa") },
239 { { FPL("//aa/"), FPL("") }, FPL("//aa") },
240 { { FPL("//"), FPL("aa") }, FPL("//aa") },
241#if defined(FILE_PATH_USES_DRIVE_LETTERS)
242 { { FPL("c:"), FPL("a") }, FPL("c:a") },
243 { { FPL("c:"), FPL("") }, FPL("c:") },
244 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
245 { { FPL("c://"), FPL("a") }, FPL("c://a") },
246 { { FPL("c:///"), FPL("a") }, FPL("c:/a") },
247#endif // FILE_PATH_USES_DRIVE_LETTERS
248#if defined(FILE_PATH_USES_WIN_SEPARATORS)
249 // Append introduces the default separator character, so these test cases
250 // need to be defined with different expected results on platforms that use
251 // different default separator characters.
252 { { FPL("\\"), FPL("cc") }, FPL("\\cc") },
253 { { FPL("\\aa"), FPL("") }, FPL("\\aa") },
254 { { FPL("\\aa\\"), FPL("") }, FPL("\\aa") },
255 { { FPL("\\\\aa"), FPL("") }, FPL("\\\\aa") },
256 { { FPL("\\\\aa\\"), FPL("") }, FPL("\\\\aa") },
257 { { FPL("\\\\"), FPL("aa") }, FPL("\\\\aa") },
258 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb\\cc") },
259 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb\\cc") },
260 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb\\cc") },
261 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb\\cc") },
262 { { FPL("a/b"), FPL("c") }, FPL("a/b\\c") },
263 { { FPL("a/b/"), FPL("c") }, FPL("a/b\\c") },
264 { { FPL("//aa"), FPL("bb") }, FPL("//aa\\bb") },
265 { { FPL("//aa/"), FPL("bb") }, FPL("//aa\\bb") },
266 { { FPL("\\aa\\bb"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
267 { { FPL("\\aa\\bb\\"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
268 { { FPL("aa\\bb\\"), FPL("cc") }, FPL("aa\\bb\\cc") },
269 { { FPL("aa\\bb"), FPL("cc") }, FPL("aa\\bb\\cc") },
270 { { FPL("a\\b"), FPL("c") }, FPL("a\\b\\c") },
271 { { FPL("a\\b\\"), FPL("c") }, FPL("a\\b\\c") },
272 { { FPL("\\\\aa"), FPL("bb") }, FPL("\\\\aa\\bb") },
273 { { FPL("\\\\aa\\"), FPL("bb") }, FPL("\\\\aa\\bb") },
274#if defined(FILE_PATH_USES_DRIVE_LETTERS)
275 { { FPL("c:\\"), FPL("a") }, FPL("c:\\a") },
276 { { FPL("c:\\\\"), FPL("a") }, FPL("c:\\\\a") },
277 { { FPL("c:\\\\\\"), FPL("a") }, FPL("c:\\a") },
278 { { FPL("c:\\"), FPL("") }, FPL("c:\\") },
279 { { FPL("c:\\a"), FPL("b") }, FPL("c:\\a\\b") },
280 { { FPL("c:\\a\\"), FPL("b") }, FPL("c:\\a\\b") },
281#endif // FILE_PATH_USES_DRIVE_LETTERS
282#else // FILE_PATH_USES_WIN_SEPARATORS
283 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb/cc") },
284 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb/cc") },
285 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb/cc") },
286 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb/cc") },
287 { { FPL("a/b"), FPL("c") }, FPL("a/b/c") },
288 { { FPL("a/b/"), FPL("c") }, FPL("a/b/c") },
289 { { FPL("//aa"), FPL("bb") }, FPL("//aa/bb") },
290 { { FPL("//aa/"), FPL("bb") }, FPL("//aa/bb") },
291#if defined(FILE_PATH_USES_DRIVE_LETTERS)
292 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
293 { { FPL("c:/"), FPL("") }, FPL("c:/") },
294 { { FPL("c:/a"), FPL("b") }, FPL("c:/a/b") },
295 { { FPL("c:/a/"), FPL("b") }, FPL("c:/a/b") },
296#endif // FILE_PATH_USES_DRIVE_LETTERS
297#endif // FILE_PATH_USES_WIN_SEPARATORS
298 };
299
300 for (size_t i = 0; i < arraysize(cases); ++i) {
301 FilePath root(cases[i].inputs[0]);
302 FilePath::StringType leaf(cases[i].inputs[1]);
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900303 FilePath observed_str = root.Append(leaf);
304 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
305 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
306 FilePath observed_path = root.Append(FilePath(leaf));
307 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) <<
mark@chromium.org6380e262008-10-04 03:21:47 +0900308 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900309
310 // TODO(erikkay): It would be nice to have a unicode test append value to
311 // handle the case when AppendASCII is passed UTF8
312#if defined(OS_WIN)
brettw@chromium.org11f89b02010-08-18 08:05:28 +0900313 std::string ascii = WideToUTF8(leaf);
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900314#elif defined(OS_POSIX)
315 std::string ascii = leaf;
316#endif
317 observed_str = root.AppendASCII(ascii);
318 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
319 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
mark@chromium.org6380e262008-10-04 03:21:47 +0900320 }
321}
322
mark@chromium.org778d9cf2009-10-14 02:31:39 +0900323TEST_F(FilePathTest, StripTrailingSeparators) {
324 const struct UnaryTestData cases[] = {
325 { FPL(""), FPL("") },
326 { FPL("/"), FPL("/") },
327 { FPL("//"), FPL("//") },
328 { FPL("///"), FPL("/") },
329 { FPL("////"), FPL("/") },
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 { FPL("/a//"), FPL("/a") },
337 { FPL("/a///"), FPL("/a") },
338 { FPL("/a////"), FPL("/a") },
339#if defined(FILE_PATH_USES_DRIVE_LETTERS)
340 { FPL("c:"), FPL("c:") },
341 { FPL("c:/"), FPL("c:/") },
342 { FPL("c://"), FPL("c://") },
343 { FPL("c:///"), FPL("c:/") },
344 { FPL("c:////"), FPL("c:/") },
345 { FPL("c:/a"), FPL("c:/a") },
346 { FPL("c:/a/"), FPL("c:/a") },
347 { FPL("c:/a//"), FPL("c:/a") },
348 { FPL("c:/a///"), FPL("c:/a") },
349 { FPL("c:/a////"), FPL("c:/a") },
350#endif // FILE_PATH_USES_DRIVE_LETTERS
351#if defined(FILE_PATH_USES_WIN_SEPARATORS)
352 { FPL("\\"), FPL("\\") },
353 { FPL("\\\\"), FPL("\\\\") },
354 { FPL("\\\\\\"), FPL("\\") },
355 { FPL("\\\\\\\\"), FPL("\\") },
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 { FPL("\\a\\\\"), FPL("\\a") },
363 { FPL("\\a\\\\\\"), FPL("\\a") },
364 { FPL("\\a\\\\\\\\"), FPL("\\a") },
365#if defined(FILE_PATH_USES_DRIVE_LETTERS)
366 { FPL("c:\\"), FPL("c:\\") },
367 { FPL("c:\\\\"), FPL("c:\\\\") },
368 { FPL("c:\\\\\\"), FPL("c:\\") },
369 { FPL("c:\\\\\\\\"), FPL("c:\\") },
370 { FPL("c:\\a"), FPL("c:\\a") },
371 { FPL("c:\\a\\"), FPL("c:\\a") },
372 { FPL("c:\\a\\\\"), FPL("c:\\a") },
373 { FPL("c:\\a\\\\\\"), FPL("c:\\a") },
374 { FPL("c:\\a\\\\\\\\"), FPL("c:\\a") },
375#endif // FILE_PATH_USES_DRIVE_LETTERS
376#endif // FILE_PATH_USES_WIN_SEPARATORS
377 };
378
379 for (size_t i = 0; i < arraysize(cases); ++i) {
380 FilePath input(cases[i].input);
381 FilePath observed = input.StripTrailingSeparators();
382 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
383 "i: " << i << ", input: " << input.value();
384 }
385}
386
aa@chromium.org76e5c632008-12-18 17:38:16 +0900387TEST_F(FilePathTest, IsAbsolute) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900388 const struct UnaryBooleanTestData cases[] = {
389 { FPL(""), false },
390 { FPL("a"), false },
391 { FPL("c:"), false },
392 { FPL("c:a"), false },
393 { FPL("a/b"), false },
394 { FPL("//"), true },
395 { FPL("//a"), true },
396 { FPL("c:a/b"), false },
397 { FPL("?:/a"), false },
398#if defined(FILE_PATH_USES_DRIVE_LETTERS)
399 { FPL("/"), false },
400 { FPL("/a"), false },
401 { FPL("/."), false },
402 { FPL("/.."), false },
403 { FPL("c:/"), true },
404 { FPL("c:/a"), true },
405 { FPL("c:/."), true },
406 { FPL("c:/.."), true },
407 { FPL("C:/a"), true },
408 { FPL("d:/a"), true },
409#else // FILE_PATH_USES_DRIVE_LETTERS
410 { FPL("/"), true },
411 { FPL("/a"), true },
412 { FPL("/."), true },
413 { FPL("/.."), true },
414 { FPL("c:/"), false },
415#endif // FILE_PATH_USES_DRIVE_LETTERS
416#if defined(FILE_PATH_USES_WIN_SEPARATORS)
417 { FPL("a\\b"), false },
418 { FPL("\\\\"), true },
419 { FPL("\\\\a"), true },
420 { FPL("a\\b"), false },
421 { FPL("\\\\"), true },
422 { FPL("//a"), true },
423 { FPL("c:a\\b"), false },
424 { FPL("?:\\a"), false },
425#if defined(FILE_PATH_USES_DRIVE_LETTERS)
426 { FPL("\\"), false },
427 { FPL("\\a"), false },
428 { FPL("\\."), false },
429 { FPL("\\.."), false },
430 { FPL("c:\\"), true },
431 { FPL("c:\\"), true },
432 { FPL("c:\\a"), true },
433 { FPL("c:\\."), true },
434 { FPL("c:\\.."), true },
435 { FPL("C:\\a"), true },
436 { FPL("d:\\a"), true },
437#else // FILE_PATH_USES_DRIVE_LETTERS
438 { FPL("\\"), true },
439 { FPL("\\a"), true },
440 { FPL("\\."), true },
441 { FPL("\\.."), true },
442 { FPL("c:\\"), false },
443#endif // FILE_PATH_USES_DRIVE_LETTERS
444#endif // FILE_PATH_USES_WIN_SEPARATORS
445 };
446
447 for (size_t i = 0; i < arraysize(cases); ++i) {
448 FilePath input(cases[i].input);
449 bool observed = input.IsAbsolute();
450 EXPECT_EQ(cases[i].expected, observed) <<
451 "i: " << i << ", input: " << input.value();
452 }
453}
aa@chromium.org76e5c632008-12-18 17:38:16 +0900454
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900455TEST_F(FilePathTest, PathComponentsTest) {
456 const struct UnaryTestData cases[] = {
457 { FPL("//foo/bar/baz/"), FPL("|//|foo|bar|baz")},
458 { FPL("///"), FPL("|/")},
459 { FPL("/foo//bar//baz/"), FPL("|/|foo|bar|baz")},
460 { FPL("/foo/bar/baz/"), FPL("|/|foo|bar|baz")},
461 { FPL("/foo/bar/baz//"), FPL("|/|foo|bar|baz")},
462 { FPL("/foo/bar/baz///"), FPL("|/|foo|bar|baz")},
463 { FPL("/foo/bar/baz"), FPL("|/|foo|bar|baz")},
464 { FPL("/foo/bar.bot/baz.txt"), FPL("|/|foo|bar.bot|baz.txt")},
465 { FPL("//foo//bar/baz"), FPL("|//|foo|bar|baz")},
466 { FPL("/"), FPL("|/")},
467 { FPL("foo"), FPL("|foo")},
468 { FPL(""), FPL("")},
469#if defined(FILE_PATH_USES_DRIVE_LETTERS)
470 { FPL("e:/foo"), FPL("|e:|/|foo")},
471 { FPL("e:/"), FPL("|e:|/")},
472 { FPL("e:"), FPL("|e:")},
473#endif // FILE_PATH_USES_DRIVE_LETTERS
474#if defined(FILE_PATH_USES_WIN_SEPARATORS)
475 { FPL("../foo"), FPL("|..|foo")},
476 { FPL("./foo"), FPL("|foo")},
477 { FPL("../foo/bar/"), FPL("|..|foo|bar") },
478 { FPL("\\\\foo\\bar\\baz\\"), FPL("|\\\\|foo|bar|baz")},
479 { FPL("\\\\\\"), FPL("|\\")},
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\\baz"), FPL("|\\|foo|bar|baz")},
485 { FPL("\\foo\\bar/baz\\\\\\"), FPL("|\\|foo|bar|baz")},
486 { FPL("/foo\\bar\\baz"), FPL("|/|foo|bar|baz")},
487 { FPL("\\foo\\bar.bot\\baz.txt"), FPL("|\\|foo|bar.bot|baz.txt")},
488 { FPL("\\\\foo\\\\bar\\baz"), FPL("|\\\\|foo|bar|baz")},
489 { FPL("\\"), FPL("|\\")},
490#endif // FILE_PATH_USES_WIN_SEPARATORS
491 };
492
493 for (size_t i = 0; i < arraysize(cases); ++i) {
494 FilePath input(cases[i].input);
495 std::vector<FilePath::StringType> comps;
496 input.GetComponents(&comps);
497
498 FilePath::StringType observed;
499 for (size_t j = 0; j < comps.size(); ++j) {
500 observed.append(FILE_PATH_LITERAL("|"), 1);
501 observed.append(comps[j]);
502 }
503 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed) <<
504 "i: " << i << ", input: " << input.value();
505 }
506}
507
508TEST_F(FilePathTest, IsParentTest) {
509 const struct BinaryBooleanTestData cases[] = {
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900510 { { FPL("/"), FPL("/foo/bar/baz") }, true},
511 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, true},
512 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, true},
513 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, true},
514 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, false},
515 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, false},
516 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, false},
517 { { FPL("/foo/bar"), FPL("/foo/bar") }, false},
518 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
519 { { FPL("foo/bar"), FPL("foo/bar/baz") }, true},
520 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, false},
521 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, false},
522 { { FPL(""), FPL("foo") }, false},
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900523#if defined(FILE_PATH_USES_DRIVE_LETTERS)
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900524 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, true},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900525 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, true},
526 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, true},
527 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, false},
528 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900529 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, true},
530 { { FPL("c:"), FPL("c:/foo/bar/baz") }, true},
531 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900532 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, false},
533 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900534 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900535 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, false},
536 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900537 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, false},
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900538#endif // FILE_PATH_USES_DRIVE_LETTERS
539#if defined(FILE_PATH_USES_WIN_SEPARATORS)
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900540 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, true},
541 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, true},
542 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, true},
543 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, true},
544 { { FPL(""), FPL("\\foo\\bar\\baz") }, false},
545 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, false},
546 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900547#endif // FILE_PATH_USES_WIN_SEPARATORS
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900548 };
549
550 for (size_t i = 0; i < arraysize(cases); ++i) {
551 FilePath parent(cases[i].inputs[0]);
552 FilePath child(cases[i].inputs[1]);
553
554 EXPECT_EQ(parent.IsParent(child), cases[i].expected) <<
mad@chromium.orgcc4f7b32009-10-21 00:27:21 +0900555 "i: " << i << ", parent: " << parent.value() << ", child: " <<
556 child.value();
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900557 }
558}
559
mark@chromium.org752010c2009-09-24 05:32:41 +0900560TEST_F(FilePathTest, AppendRelativePathTest) {
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900561 const struct BinaryTestData cases[] = {
mark@chromium.org752010c2009-09-24 05:32:41 +0900562#if defined(FILE_PATH_USES_WIN_SEPARATORS)
563 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo\\bar\\baz")},
564#else // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900565 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo/bar/baz")},
mark@chromium.org752010c2009-09-24 05:32:41 +0900566#endif // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900567 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, FPL("baz")},
568 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, FPL("baz")},
569 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, FPL("baz")},
570 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, FPL("")},
571 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, FPL("")},
572 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, FPL("")},
573 { { FPL("/foo/bar"), FPL("/foo/bar") }, FPL("")},
574 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, FPL("")},
575 { { FPL("foo/bar"), FPL("foo/bar/baz") }, FPL("baz")},
576 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, FPL("")},
577 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, FPL("")},
578 { { FPL(""), FPL("foo") }, FPL("")},
579#if defined(FILE_PATH_USES_DRIVE_LETTERS)
580 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, FPL("baz")},
581 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, FPL("baz")},
582 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, FPL("baz")},
583 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, FPL("")},
584 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, FPL("")},
mark@chromium.org752010c2009-09-24 05:32:41 +0900585#if defined(FILE_PATH_USES_WIN_SEPARATORS)
586 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
587 // TODO(akalin): Figure out how to handle the corner case in the
588 // commented-out test case below. Appending to an empty path gives
589 // /foo\bar\baz but appending to a nonempty path "blah" gives
590 // blah\foo\bar\baz.
591 // { { FPL("c:"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
592#endif // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900593 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
594 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, FPL("")},
595 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
596 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, FPL("")},
597 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, FPL("")},
598 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, FPL("")},
599 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, FPL("")},
600#endif // FILE_PATH_USES_DRIVE_LETTERS
601#if defined(FILE_PATH_USES_WIN_SEPARATORS)
602 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
603 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
604 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, FPL("baz")},
605 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, FPL("foo\\bar\\baz")},
606 { { FPL(""), FPL("\\foo\\bar\\baz") }, FPL("")},
607 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, FPL("")},
608 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, FPL("")},
609#endif // FILE_PATH_USES_WIN_SEPARATORS
610 };
611
612 const FilePath base(FPL("blah"));
613
614 for (size_t i = 0; i < arraysize(cases); ++i) {
615 FilePath parent(cases[i].inputs[0]);
616 FilePath child(cases[i].inputs[1]);
617 {
618 FilePath result;
619 bool success = parent.AppendRelativePath(child, &result);
620 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
621 "i: " << i << ", parent: " << parent.value() << ", child: " <<
622 child.value();
623 EXPECT_STREQ(cases[i].expected, result.value().c_str()) <<
624 "i: " << i << ", parent: " << parent.value() << ", child: " <<
625 child.value();
626 }
627 {
628 FilePath result(base);
629 bool success = parent.AppendRelativePath(child, &result);
630 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
631 "i: " << i << ", parent: " << parent.value() << ", child: " <<
632 child.value();
633 EXPECT_EQ(base.Append(cases[i].expected).value(), result.value()) <<
634 "i: " << i << ", parent: " << parent.value() << ", child: " <<
635 child.value();
636 }
637 }
638}
639
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900640TEST_F(FilePathTest, EqualityTest) {
641 const struct BinaryBooleanTestData cases[] = {
642 { { FPL("/foo/bar/baz"), FPL("/foo/bar/baz") }, true},
643 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, false},
644 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
645 { { FPL("//foo/bar/"), FPL("//foo/bar/") }, true},
646 { { FPL("/foo/bar"), FPL("/foo2/bar") }, false},
647 { { FPL("/foo/bar.txt"), FPL("/foo/bar") }, false},
648 { { FPL("foo/bar"), FPL("foo/bar") }, true},
649 { { FPL("foo/bar"), FPL("foo/bar/baz") }, false},
650 { { FPL(""), FPL("foo") }, false},
651#if defined(FILE_PATH_USES_DRIVE_LETTERS)
652 { { FPL("c:/foo/bar"), FPL("c:/foo/bar") }, true},
653 { { FPL("E:/foo/bar"), FPL("e:/foo/bar") }, true},
654 { { FPL("f:/foo/bar"), FPL("F:/foo/bar") }, true},
655 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar") }, false},
656 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar") }, false},
657 { { FPL("c:/"), FPL("c:/") }, true},
658 { { FPL("c:"), FPL("c:") }, true},
659 { { FPL("c:/foo/bar"), FPL("d:/foo/bar") }, false},
660 { { FPL("c:/foo/bar"), FPL("D:/foo/bar") }, false},
661 { { FPL("C:/foo/bar"), FPL("d:/foo/bar") }, false},
662 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar") }, false},
663#endif // FILE_PATH_USES_DRIVE_LETTERS
664#if defined(FILE_PATH_USES_WIN_SEPARATORS)
665 { { FPL("\\foo\\bar"), FPL("\\foo\\bar") }, true},
666 { { FPL("\\foo/bar"), FPL("\\foo/bar") }, true},
tony@chromium.org65049b12010-06-25 14:33:17 +0900667 { { FPL("\\foo/bar"), FPL("\\foo\\bar") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900668 { { FPL("\\"), FPL("\\") }, true},
669 { { FPL("\\"), FPL("/") }, false},
670 { { FPL(""), FPL("\\") }, false},
671 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar") }, false},
672 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2") }, false},
673#if defined(FILE_PATH_USES_DRIVE_LETTERS)
674 { { FPL("c:\\foo\\bar"), FPL("c:\\foo\\bar") }, true},
675 { { FPL("E:\\foo\\bar"), FPL("e:\\foo\\bar") }, true},
676 { { FPL("f:\\foo\\bar"), FPL("F:\\foo/bar") }, false},
677#endif // FILE_PATH_USES_DRIVE_LETTERS
678#endif // FILE_PATH_USES_WIN_SEPARATORS
679 };
680
681 for (size_t i = 0; i < arraysize(cases); ++i) {
682 FilePath a(cases[i].inputs[0]);
683 FilePath b(cases[i].inputs[1]);
684
685 EXPECT_EQ(a == b, cases[i].expected) <<
686 "equality i: " << i << ", a: " << a.value() << ", b: " <<
687 b.value();
688 }
689
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900690 for (size_t i = 0; i < arraysize(cases); ++i) {
691 FilePath a(cases[i].inputs[0]);
692 FilePath b(cases[i].inputs[1]);
693
694 EXPECT_EQ(a != b, !cases[i].expected) <<
695 "inequality i: " << i << ", a: " << a.value() << ", b: " <<
696 b.value();
697 }
698}
699
erikkay@google.com985d4202009-01-10 03:26:19 +0900700TEST_F(FilePathTest, Extension) {
701 FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
702
703 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
estade@chromium.org99a42e72010-07-28 06:24:39 +0900704 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.Extension());
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900705 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.FinalExtension());
erikkay@google.com985d4202009-01-10 03:26:19 +0900706
707 FilePath base = jpg.BaseName().RemoveExtension();
estade@chromium.org99a42e72010-07-28 06:24:39 +0900708 EXPECT_EQ(FILE_PATH_LITERAL("foo"), base.value());
erikkay@google.com985d4202009-01-10 03:26:19 +0900709
710 FilePath path_no_ext = base_dir.Append(base);
estade@chromium.org99a42e72010-07-28 06:24:39 +0900711 EXPECT_EQ(path_no_ext.value(), jpg.RemoveExtension().value());
erikkay@google.com985d4202009-01-10 03:26:19 +0900712
713 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value());
estade@chromium.org99a42e72010-07-28 06:24:39 +0900714 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.Extension());
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900715 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.FinalExtension());
erikkay@google.com985d4202009-01-10 03:26:19 +0900716}
717
718TEST_F(FilePathTest, Extension2) {
719 const struct UnaryTestData cases[] = {
720#if defined(FILE_PATH_USES_WIN_SEPARATORS)
721 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
722 { FPL("C:\\a\\b\\c."), FPL(".") },
723 { FPL("C:\\a\\b\\c"), FPL("") },
724 { FPL("C:\\a\\b\\"), FPL("") },
725 { FPL("C:\\a\\b.\\"), FPL(".") },
726 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
727 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
728 { FPL("C:\\foo.bar\\.."), FPL("") },
729 { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
730#endif
731 { FPL("/foo/bar/baz.ext"), FPL(".ext") },
732 { FPL("/foo/bar/baz."), FPL(".") },
733 { FPL("/foo/bar/baz.."), FPL(".") },
734 { FPL("/foo/bar/baz"), FPL("") },
735 { FPL("/foo/bar/"), FPL("") },
736 { FPL("/foo/bar./"), FPL(".") },
737 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900738 { FPL("/subversion-1.6.12.zip"), FPL(".zip") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900739 { FPL("/foo.12345.gz"), FPL(".gz") },
740 { FPL("/foo..gz"), FPL(".gz") },
erikkay@google.com985d4202009-01-10 03:26:19 +0900741 { FPL("."), FPL("") },
742 { FPL(".."), FPL("") },
743 { FPL("./foo"), FPL("") },
744 { FPL("./foo.ext"), FPL(".ext") },
745 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
746 { FPL("/foo.bar////"), FPL(".bar") },
747 { FPL("/foo.bar/.."), FPL("") },
748 { FPL("/foo.bar/..////"), FPL("") },
aa@chromium.orgf8395de2012-08-26 21:11:10 +0900749 { FPL("/foo.1234.luser.js"), FPL(".js") },
750 { FPL("/user.js"), FPL(".js") },
erikkay@google.com985d4202009-01-10 03:26:19 +0900751 };
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900752 const struct UnaryTestData double_extension_cases[] = {
753 { FPL("/foo.tar.gz"), FPL(".tar.gz") },
754 { FPL("/foo.tar.Z"), FPL(".tar.Z") },
755 { FPL("/foo.tar.bz2"), FPL(".tar.bz2") },
756 { FPL("/foo.1234.gz"), FPL(".1234.gz") },
757 { FPL("/foo.1234.tar.gz"), FPL(".tar.gz") },
758 { FPL("/foo.tar.tar.gz"), FPL(".tar.gz") },
759 { FPL("/foo.tar.gz.gz"), FPL(".gz.gz") },
760 { FPL("/foo.1234.user.js"), FPL(".user.js") },
761 { FPL("foo.user.js"), FPL(".user.js") },
arun87.kumarf0c7ccf2014-10-09 13:50:44 +0900762 { FPL("/foo.tar.bz"), FPL(".tar.bz") },
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900763 };
erikkay@google.com985d4202009-01-10 03:26:19 +0900764 for (unsigned int i = 0; i < arraysize(cases); ++i) {
765 FilePath path(cases[i].input);
766 FilePath::StringType extension = path.Extension();
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900767 FilePath::StringType final_extension = path.FinalExtension();
pkasting9043c4e2014-10-02 07:18:43 +0900768 EXPECT_STREQ(cases[i].expected, extension.c_str())
769 << "i: " << i << ", path: " << path.value();
770 EXPECT_STREQ(cases[i].expected, final_extension.c_str())
771 << "i: " << i << ", path: " << path.value();
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900772 }
773 for (unsigned int i = 0; i < arraysize(double_extension_cases); ++i) {
pkasting9043c4e2014-10-02 07:18:43 +0900774 FilePath path(double_extension_cases[i].input);
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900775 FilePath::StringType extension = path.Extension();
pkasting9043c4e2014-10-02 07:18:43 +0900776 EXPECT_STREQ(double_extension_cases[i].expected, extension.c_str())
777 << "i: " << i << ", path: " << path.value();
erikkay@google.com985d4202009-01-10 03:26:19 +0900778 }
779}
780
781TEST_F(FilePathTest, InsertBeforeExtension) {
782 const struct BinaryTestData cases[] = {
783 { { FPL(""), FPL("") }, FPL("") },
784 { { FPL(""), FPL("txt") }, FPL("") },
785 { { FPL("."), FPL("txt") }, FPL("") },
786 { { FPL(".."), FPL("txt") }, FPL("") },
787 { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
788 { { FPL("."), FPL("") }, FPL(".") },
789 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
790 { { FPL("foo"), FPL("txt") }, FPL("footxt") },
791 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
792 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
793 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
794 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
795 { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
796 { { FPL("foo"), FPL("") }, FPL("foo") },
797 { { FPL("foo"), FPL(".") }, FPL("foo.") },
798 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
799 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
800#if defined(FILE_PATH_USES_WIN_SEPARATORS)
801 { { FPL("\\"), FPL("") }, FPL("\\") },
802 { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
803 { { FPL("\\."), FPL("txt") }, FPL("") },
804 { { FPL("\\.."), FPL("txt") }, FPL("") },
805 { { FPL("\\."), FPL("") }, FPL("\\.") },
806 { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
807 FPL("C:\\bar\\footxt.dll") },
808 { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
809 FPL("C:\\bar.baz\\foodlltxt") },
810 { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
811 FPL("C:\\bar.baz\\footxt.dll") },
812 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
813 FPL("C:\\bar.baz\\foo.dlltxt.exe") },
814 { { FPL("C:\\bar.baz\\foo"), FPL("") },
815 FPL("C:\\bar.baz\\foo") },
816 { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
817 FPL("C:\\bar.baz\\foo.exe") },
818 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
819 FPL("C:\\bar.baz\\foo.dll.exe") },
820 { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
821 FPL("C:\\bar\\baz\\foo (1).exe") },
822 { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
823 { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
824#endif
825 { { FPL("/"), FPL("") }, FPL("/") },
826 { { FPL("/"), FPL("txt") }, FPL("/txt") },
827 { { FPL("/."), FPL("txt") }, FPL("") },
828 { { FPL("/.."), FPL("txt") }, FPL("") },
829 { { FPL("/."), FPL("") }, FPL("/.") },
830 { { FPL("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
831 { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
832 { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
833 { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
834 FPL("/bar.baz/foo.dlltxt.exe") },
835 { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
836 { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
837 { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
838 { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
839 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
840 };
841 for (unsigned int i = 0; i < arraysize(cases); ++i) {
842 FilePath path(cases[i].inputs[0]);
843 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
844 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
845 ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
846 }
847}
848
estade@chromium.org99a42e72010-07-28 06:24:39 +0900849TEST_F(FilePathTest, RemoveExtension) {
850 const struct UnaryTestData cases[] = {
851 { FPL(""), FPL("") },
852 { FPL("."), FPL(".") },
853 { FPL(".."), FPL("..") },
854 { FPL("foo.dll"), FPL("foo") },
855 { FPL("./foo.dll"), FPL("./foo") },
856 { FPL("foo..dll"), FPL("foo.") },
857 { FPL("foo"), FPL("foo") },
858 { FPL("foo."), FPL("foo") },
859 { FPL("foo.."), FPL("foo.") },
860 { FPL("foo.baz.dll"), FPL("foo.baz") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900861#if defined(FILE_PATH_USES_WIN_SEPARATORS)
862 { FPL("C:\\foo.bar\\foo"), FPL("C:\\foo.bar\\foo") },
863 { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") },
864#endif
865 { FPL("/foo.bar/foo"), FPL("/foo.bar/foo") },
866 { FPL("/foo.bar/..////"), FPL("/foo.bar/..////") },
867 };
868 for (unsigned int i = 0; i < arraysize(cases); ++i) {
869 FilePath path(cases[i].input);
870 FilePath removed = path.RemoveExtension();
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900871 FilePath removed_final = path.RemoveFinalExtension();
estade@chromium.org99a42e72010-07-28 06:24:39 +0900872 EXPECT_EQ(cases[i].expected, removed.value()) << "i: " << i <<
873 ", path: " << path.value();
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900874 EXPECT_EQ(cases[i].expected, removed_final.value()) << "i: " << i <<
875 ", path: " << path.value();
876 }
877 {
878 FilePath path(FPL("foo.tar.gz"));
879 FilePath removed = path.RemoveExtension();
880 FilePath removed_final = path.RemoveFinalExtension();
881 EXPECT_EQ(FPL("foo"), removed.value()) << ", path: " << path.value();
882 EXPECT_EQ(FPL("foo.tar"), removed_final.value()) << ", path: "
883 << path.value();
estade@chromium.org99a42e72010-07-28 06:24:39 +0900884 }
885}
886
erikkay@google.com985d4202009-01-10 03:26:19 +0900887TEST_F(FilePathTest, ReplaceExtension) {
888 const struct BinaryTestData cases[] = {
889 { { FPL(""), FPL("") }, FPL("") },
890 { { FPL(""), FPL("txt") }, FPL("") },
891 { { FPL("."), FPL("txt") }, FPL("") },
892 { { FPL(".."), FPL("txt") }, FPL("") },
893 { { FPL("."), FPL("") }, FPL("") },
894 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900895 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.txt") },
erikkay@google.com985d4202009-01-10 03:26:19 +0900896 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
897 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
898 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
899 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
900 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
901 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
902 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
903 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
904 { { FPL("foo.dll"), FPL("") }, FPL("foo") },
905 { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
906 { { FPL("foo"), FPL("") }, FPL("foo") },
907 { { FPL("foo"), FPL(".") }, FPL("foo") },
908 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
909 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
910#if defined(FILE_PATH_USES_WIN_SEPARATORS)
911 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
912 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
913#endif
914 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
915 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
916 };
917 for (unsigned int i = 0; i < arraysize(cases); ++i) {
918 FilePath path(cases[i].inputs[0]);
919 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
920 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
921 ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
922 }
923}
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900924
rdevlin.cronin@chromium.org9a980f72012-04-25 10:53:44 +0900925TEST_F(FilePathTest, AddExtension) {
926 const struct BinaryTestData cases[] = {
927 { { FPL(""), FPL("") }, FPL("") },
928 { { FPL(""), FPL("txt") }, FPL("") },
929 { { FPL("."), FPL("txt") }, FPL("") },
930 { { FPL(".."), FPL("txt") }, FPL("") },
931 { { FPL("."), FPL("") }, FPL("") },
932 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.dll.txt") },
933 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.dll.txt") },
934 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..dll.txt") },
935 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.dll.txt") },
936 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
937 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
938 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
939 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
940 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.dll.txt") },
941 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.dll.txt") },
942 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
943 { { FPL("foo.dll"), FPL(".") }, FPL("foo.dll") },
944 { { FPL("foo"), FPL("") }, FPL("foo") },
945 { { FPL("foo"), FPL(".") }, FPL("foo") },
946 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
947 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz.dll") },
948#if defined(FILE_PATH_USES_WIN_SEPARATORS)
949 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
950 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
951#endif
952 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
953 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
954 };
955 for (unsigned int i = 0; i < arraysize(cases); ++i) {
956 FilePath path(cases[i].inputs[0]);
957 FilePath added = path.AddExtension(cases[i].inputs[1]);
958 EXPECT_EQ(cases[i].expected, added.value()) << "i: " << i <<
959 ", path: " << path.value() << ", add: " << cases[i].inputs[1];
960 }
961}
962
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900963TEST_F(FilePathTest, MatchesExtension) {
964 const struct BinaryBooleanTestData cases[] = {
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900965 { { FPL("foo"), FPL("") }, true},
966 { { FPL("foo"), FPL(".") }, false},
967 { { FPL("foo."), FPL("") }, false},
968 { { FPL("foo."), FPL(".") }, true},
969 { { FPL("foo.txt"), FPL(".dll") }, false},
970 { { FPL("foo.txt"), FPL(".txt") }, true},
971 { { FPL("foo.txt.dll"), FPL(".txt") }, false},
972 { { FPL("foo.txt.dll"), FPL(".dll") }, true},
973 { { FPL("foo.TXT"), FPL(".txt") }, true},
974 { { FPL("foo.txt"), FPL(".TXT") }, true},
975 { { FPL("foo.tXt"), FPL(".txt") }, true},
976 { { FPL("foo.txt"), FPL(".tXt") }, true},
977 { { FPL("foo.tXt"), FPL(".TXT") }, true},
978 { { FPL("foo.tXt"), FPL(".tXt") }, true},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900979#if defined(FILE_PATH_USES_DRIVE_LETTERS)
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900980 { { FPL("c:/foo.txt.dll"), FPL(".txt") }, false},
981 { { FPL("c:/foo.txt"), FPL(".txt") }, true},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900982#endif // FILE_PATH_USES_DRIVE_LETTERS
983#if defined(FILE_PATH_USES_WIN_SEPARATORS)
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900984 { { FPL("c:\\bar\\foo.txt.dll"), FPL(".txt") }, false},
985 { { FPL("c:\\bar\\foo.txt"), FPL(".txt") }, true},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900986#endif // FILE_PATH_USES_DRIVE_LETTERS
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900987 { { FPL("/bar/foo.txt.dll"), FPL(".txt") }, false},
988 { { FPL("/bar/foo.txt"), FPL(".txt") }, true},
989#if defined(OS_WIN) || defined(OS_MACOSX)
990 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
991 { { FPL("foo.\u00E4\u00F6\u00FC"), FPL(".\u00E4\u00F6\u00FC") }, true},
992 { { FPL("foo.\u00C4\u00D6\u00DC"), FPL(".\u00E4\u00F6\u00FC") }, true},
993 // C with circumflex: direct comparison, and upper case vs. lower case
994 { { FPL("foo.\u0109"), FPL(".\u0109") }, true},
995 { { FPL("foo.\u0108"), FPL(".\u0109") }, true},
996#endif
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900997 };
998
999 for (size_t i = 0; i < arraysize(cases); ++i) {
1000 FilePath path(cases[i].inputs[0]);
1001 FilePath::StringType ext(cases[i].inputs[1]);
1002
1003 EXPECT_EQ(cases[i].expected, path.MatchesExtension(ext)) <<
1004 "i: " << i << ", path: " << path.value() << ", ext: " << ext;
1005 }
1006}
cevans@chromium.org48dc54c2009-08-16 06:44:31 +09001007
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +09001008TEST_F(FilePathTest, CompareIgnoreCase) {
1009 const struct BinaryIntTestData cases[] = {
1010 { { FPL("foo"), FPL("foo") }, 0},
1011 { { FPL("FOO"), FPL("foo") }, 0},
1012 { { FPL("foo.ext"), FPL("foo.ext") }, 0},
1013 { { FPL("FOO.EXT"), FPL("foo.ext") }, 0},
1014 { { FPL("Foo.Ext"), FPL("foo.ext") }, 0},
1015 { { FPL("foO"), FPL("foo") }, 0},
1016 { { FPL("foo"), FPL("foO") }, 0},
1017 { { FPL("fOo"), FPL("foo") }, 0},
1018 { { FPL("foo"), FPL("fOo") }, 0},
1019 { { FPL("bar"), FPL("foo") }, -1},
1020 { { FPL("foo"), FPL("bar") }, 1},
1021 { { FPL("BAR"), FPL("foo") }, -1},
1022 { { FPL("FOO"), FPL("bar") }, 1},
1023 { { FPL("bar"), FPL("FOO") }, -1},
1024 { { FPL("foo"), FPL("BAR") }, 1},
1025 { { FPL("BAR"), FPL("FOO") }, -1},
1026 { { FPL("FOO"), FPL("BAR") }, 1},
1027 // German "Eszett" (lower case and the new-fangled upper case)
1028 // Note that uc(<lowercase eszett>) => "SS", NOT <uppercase eszett>!
1029 // However, neither Windows nor Mac OSX converts these.
1030 // (or even have glyphs for <uppercase eszett>)
1031 { { FPL("\u00DF"), FPL("\u00DF") }, 0},
1032 { { FPL("\u1E9E"), FPL("\u1E9E") }, 0},
1033 { { FPL("\u00DF"), FPL("\u1E9E") }, -1},
1034 { { FPL("SS"), FPL("\u00DF") }, -1},
1035 { { FPL("SS"), FPL("\u1E9E") }, -1},
1036#if defined(OS_WIN) || defined(OS_MACOSX)
1037 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
1038 { { FPL("\u00E4\u00F6\u00FC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1039 { { FPL("\u00C4\u00D6\u00DC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1040 // C with circumflex: direct comparison, and upper case vs. lower case
1041 { { FPL("\u0109"), FPL("\u0109") }, 0},
1042 { { FPL("\u0108"), FPL("\u0109") }, 0},
1043 // Cyrillic letter SHA: direct comparison, and upper case vs. lower case
1044 { { FPL("\u0428"), FPL("\u0428") }, 0},
1045 { { FPL("\u0428"), FPL("\u0448") }, 0},
1046 // Greek letter DELTA: direct comparison, and upper case vs. lower case
1047 { { FPL("\u0394"), FPL("\u0394") }, 0},
1048 { { FPL("\u0394"), FPL("\u03B4") }, 0},
1049 // Japanese full-width A: direct comparison, and upper case vs. lower case
1050 // Note that full-width and standard characters are considered different.
1051 { { FPL("\uFF21"), FPL("\uFF21") }, 0},
1052 { { FPL("\uFF21"), FPL("\uFF41") }, 0},
1053 { { FPL("A"), FPL("\uFF21") }, -1},
1054 { { FPL("A"), FPL("\uFF41") }, -1},
1055 { { FPL("a"), FPL("\uFF21") }, -1},
1056 { { FPL("a"), FPL("\uFF41") }, -1},
1057#endif
1058#if defined(OS_MACOSX)
1059 // Codepoints > 0x1000
1060 // Georgian letter DON: direct comparison, and upper case vs. lower case
1061 { { FPL("\u10A3"), FPL("\u10A3") }, 0},
1062 { { FPL("\u10A3"), FPL("\u10D3") }, 0},
1063 // Combining characters vs. pre-composed characters, upper and lower case
1064 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1065 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("kuon") }, 1},
1066 { { FPL("kuon"), FPL("k\u0301u\u032Do\u0304\u0301n") }, -1},
1067 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("KUON") }, 1},
1068 { { FPL("KUON"), FPL("K\u0301U\u032DO\u0304\u0301N") }, -1},
1069 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("KUON") }, 1},
1070 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1071 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E30\u1E76\u1E52n") }, 0},
1072 { { FPL("k\u0301u\u032Do\u0304\u0302n"), FPL("\u1E30\u1E76\u1E52n") }, 1},
1073#endif
1074 };
1075
1076 for (size_t i = 0; i < arraysize(cases); ++i) {
1077 FilePath::StringType s1(cases[i].inputs[0]);
1078 FilePath::StringType s2(cases[i].inputs[1]);
1079 int result = FilePath::CompareIgnoreCase(s1, s2);
1080 EXPECT_EQ(cases[i].expected, result) <<
1081 "i: " << i << ", s1: " << s1 << ", s2: " << s2;
1082 }
1083}
1084
cevans@chromium.org48dc54c2009-08-16 06:44:31 +09001085TEST_F(FilePathTest, ReferencesParent) {
1086 const struct UnaryBooleanTestData cases[] = {
1087 { FPL("."), false },
1088 { FPL(".."), true },
jschuh@chromium.orgab41a002013-05-17 11:27:46 +09001089 { FPL(".. "), true },
1090 { FPL(" .."), true },
1091 { FPL("..."), true },
cevans@chromium.org48dc54c2009-08-16 06:44:31 +09001092 { FPL("a.."), false },
1093 { FPL("..a"), false },
1094 { FPL("../"), true },
1095 { FPL("/.."), true },
1096 { FPL("/../"), true },
1097 { FPL("/a../"), false },
1098 { FPL("/..a/"), false },
1099 { FPL("//.."), true },
1100 { FPL("..//"), true },
1101 { FPL("//..//"), true },
1102 { FPL("a//..//c"), true },
1103 { FPL("../b/c"), true },
1104 { FPL("/../b/c"), true },
1105 { FPL("a/b/.."), true },
1106 { FPL("a/b/../"), true },
1107 { FPL("a/../c"), true },
1108 { FPL("a/b/c"), false },
1109 };
1110
1111 for (size_t i = 0; i < arraysize(cases); ++i) {
1112 FilePath input(cases[i].input);
1113 bool observed = input.ReferencesParent();
1114 EXPECT_EQ(cases[i].expected, observed) <<
1115 "i: " << i << ", input: " << input.value();
1116 }
1117}
tony@chromium.org65049b12010-06-25 14:33:17 +09001118
satorux@chromium.orgefba4172011-11-02 13:55:23 +09001119TEST_F(FilePathTest, FromUTF8Unsafe_And_AsUTF8Unsafe) {
1120 const struct UTF8TestData cases[] = {
1121 { FPL("foo.txt"), "foo.txt" },
1122 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them.
1123 { FPL("\u00E0\u00E8\u00F2.txt"), "\xC3\xA0\xC3\xA8\xC3\xB2.txt" },
1124 // Full-width "ABC".
1125 { FPL("\uFF21\uFF22\uFF23.txt"),
1126 "\xEF\xBC\xA1\xEF\xBC\xA2\xEF\xBC\xA3.txt" },
1127 };
1128
1129 for (size_t i = 0; i < arraysize(cases); ++i) {
1130 // Test FromUTF8Unsafe() works.
1131 FilePath from_utf8 = FilePath::FromUTF8Unsafe(cases[i].utf8);
1132 EXPECT_EQ(cases[i].native, from_utf8.value())
1133 << "i: " << i << ", input: " << cases[i].native;
1134 // Test AsUTF8Unsafe() works.
1135 FilePath from_native = FilePath(cases[i].native);
1136 EXPECT_EQ(cases[i].utf8, from_native.AsUTF8Unsafe())
1137 << "i: " << i << ", input: " << cases[i].native;
1138 // Test the two file paths are identical.
1139 EXPECT_EQ(from_utf8.value(), from_native.value());
1140 }
1141}
1142
aedla@chromium.org51127b92013-01-28 22:47:55 +09001143TEST_F(FilePathTest, ConstructWithNUL) {
1144 // Assert FPS() works.
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001145 ASSERT_EQ(3U, FPS("a\0b").length());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001146
1147 // Test constructor strips '\0'
1148 FilePath path(FPS("a\0b"));
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001149 EXPECT_EQ(1U, path.value().length());
1150 EXPECT_EQ(FPL("a"), path.value());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001151}
1152
1153TEST_F(FilePathTest, AppendWithNUL) {
1154 // Assert FPS() works.
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001155 ASSERT_EQ(3U, FPS("b\0b").length());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001156
1157 // Test Append() strips '\0'
1158 FilePath path(FPL("a"));
1159 path = path.Append(FPS("b\0b"));
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001160 EXPECT_EQ(3U, path.value().length());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001161#if defined(FILE_PATH_USES_WIN_SEPARATORS)
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001162 EXPECT_EQ(FPL("a\\b"), path.value());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001163#else
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001164 EXPECT_EQ(FPL("a/b"), path.value());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001165#endif
1166}
1167
1168TEST_F(FilePathTest, ReferencesParentWithNUL) {
1169 // Assert FPS() works.
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001170 ASSERT_EQ(3U, FPS("..\0").length());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001171
1172 // Test ReferencesParent() doesn't break with "..\0"
1173 FilePath path(FPS("..\0"));
1174 EXPECT_TRUE(path.ReferencesParent());
1175}
1176
tony@chromium.org65049b12010-06-25 14:33:17 +09001177#if defined(FILE_PATH_USES_WIN_SEPARATORS)
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001178TEST_F(FilePathTest, NormalizePathSeparators) {
tony@chromium.org65049b12010-06-25 14:33:17 +09001179 const struct UnaryTestData cases[] = {
1180 { FPL("foo/bar"), FPL("foo\\bar") },
1181 { FPL("foo/bar\\betz"), FPL("foo\\bar\\betz") },
1182 { FPL("foo\\bar"), FPL("foo\\bar") },
1183 { FPL("foo\\bar/betz"), FPL("foo\\bar\\betz") },
1184 { FPL("foo"), FPL("foo") },
1185 // Trailing slashes don't automatically get stripped. That's what
1186 // StripTrailingSeparators() is for.
1187 { FPL("foo\\"), FPL("foo\\") },
1188 { FPL("foo/"), FPL("foo\\") },
1189 { FPL("foo/bar\\"), FPL("foo\\bar\\") },
1190 { FPL("foo\\bar/"), FPL("foo\\bar\\") },
1191 { FPL("foo/bar/"), FPL("foo\\bar\\") },
1192 { FPL("foo\\bar\\"), FPL("foo\\bar\\") },
1193 { FPL("\\foo/bar"), FPL("\\foo\\bar") },
1194 { FPL("/foo\\bar"), FPL("\\foo\\bar") },
1195 { FPL("c:/foo/bar/"), FPL("c:\\foo\\bar\\") },
1196 { FPL("/foo/bar/"), FPL("\\foo\\bar\\") },
1197 { FPL("\\foo\\bar\\"), FPL("\\foo\\bar\\") },
1198 { FPL("c:\\foo/bar"), FPL("c:\\foo\\bar") },
1199 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1200 { FPL("\\\\foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1201 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1202 // This method does not normalize the number of path separators.
1203 { FPL("foo\\\\bar"), FPL("foo\\\\bar") },
1204 { FPL("foo//bar"), FPL("foo\\\\bar") },
1205 { FPL("foo/\\bar"), FPL("foo\\\\bar") },
1206 { FPL("foo\\/bar"), FPL("foo\\\\bar") },
1207 { FPL("///foo\\\\bar"), FPL("\\\\\\foo\\\\bar") },
1208 { FPL("foo//bar///"), FPL("foo\\\\bar\\\\\\") },
1209 { FPL("foo/\\bar/\\"), FPL("foo\\\\bar\\\\") },
1210 { FPL("/\\foo\\/bar"), FPL("\\\\foo\\\\bar") },
1211 };
1212 for (size_t i = 0; i < arraysize(cases); ++i) {
1213 FilePath input(cases[i].input);
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001214 FilePath observed = input.NormalizePathSeparators();
tony@chromium.org65049b12010-06-25 14:33:17 +09001215 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
1216 "i: " << i << ", input: " << input.value();
1217 }
1218}
1219#endif
brettw@chromium.org82bcf512013-02-17 14:07:23 +09001220
brettw@chromium.org99b198e2013-04-12 14:17:15 +09001221TEST_F(FilePathTest, EndsWithSeparator) {
1222 const UnaryBooleanTestData cases[] = {
1223 { FPL(""), false },
1224 { FPL("/"), true },
1225 { FPL("foo/"), true },
1226 { FPL("bar"), false },
1227 { FPL("/foo/bar"), false },
1228 };
1229 for (size_t i = 0; i < arraysize(cases); ++i) {
1230 FilePath input = FilePath(cases[i].input).NormalizePathSeparators();
1231 EXPECT_EQ(cases[i].expected, input.EndsWithSeparator());
1232 }
1233}
1234
1235TEST_F(FilePathTest, AsEndingWithSeparator) {
1236 const UnaryTestData cases[] = {
1237 { FPL(""), FPL("") },
1238 { FPL("/"), FPL("/") },
1239 { FPL("foo"), FPL("foo/") },
1240 { FPL("foo/"), FPL("foo/") }
1241 };
1242 for (size_t i = 0; i < arraysize(cases); ++i) {
1243 FilePath input = FilePath(cases[i].input).NormalizePathSeparators();
1244 FilePath expected = FilePath(cases[i].expected).NormalizePathSeparators();
1245 EXPECT_EQ(expected.value(), input.AsEndingWithSeparator().value());
1246 }
1247}
1248
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09001249#if defined(OS_ANDROID)
1250TEST_F(FilePathTest, ContentUriTest) {
1251 const struct UnaryBooleanTestData cases[] = {
1252 { FPL("content://foo.bar"), true },
1253 { FPL("content://foo.bar/"), true },
1254 { FPL("content://foo/bar"), true },
1255 { FPL("CoNTenT://foo.bar"), true },
1256 { FPL("content://"), true },
1257 { FPL("content:///foo.bar"), true },
1258 { FPL("content://3foo/bar"), true },
1259 { FPL("content://_foo/bar"), true },
1260 { FPL(".. "), false },
1261 { FPL("foo.bar"), false },
1262 { FPL("content:foo.bar"), false },
1263 { FPL("content:/foo.ba"), false },
1264 { FPL("content:/dir/foo.bar"), false },
1265 { FPL("content: //foo.bar"), false },
1266 { FPL("content%2a%2f%2f"), false },
1267 };
1268
1269 for (size_t i = 0; i < arraysize(cases); ++i) {
1270 FilePath input(cases[i].input);
1271 bool observed = input.IsContentUri();
1272 EXPECT_EQ(cases[i].expected, observed) <<
1273 "i: " << i << ", input: " << input.value();
1274 }
1275}
1276#endif
1277
mgiuca36c0d632015-05-28 19:11:33 +09001278// Test the PrintTo overload for FilePath (used when a test fails to compare two
1279// FilePaths).
1280TEST_F(FilePathTest, PrintTo) {
1281 std::stringstream ss;
1282 FilePath fp(FPL("foo"));
1283 base::PrintTo(fp, &ss);
1284 EXPECT_EQ("foo", ss.str());
1285}
1286
brettw@chromium.org82bcf512013-02-17 14:07:23 +09001287} // namespace base