blob: c16293861ce7a6b6d94a6f6304486cae274b05a6 [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
mark@chromium.org6380e262008-10-04 03:21:47 +09005#include "base/basictypes.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +09006#include "base/files/file_path.h"
avi@chromium.org17f60622013-06-08 03:37:07 +09007#include "base/strings/utf_string_conversions.h"
mark@chromium.orge3e1ce72008-10-04 04:05:46 +09008#include "testing/gtest/include/gtest/gtest.h"
aa@chromium.org76e5c632008-12-18 17:38:16 +09009#include "testing/platform_test.h"
mark@chromium.org6380e262008-10-04 03:21:47 +090010
11// This macro helps avoid wrapped lines in the test structs.
12#define FPL(x) FILE_PATH_LITERAL(x)
13
aedla@chromium.org51127b92013-01-28 22:47:55 +090014// This macro constructs strings which can contain NULs.
15#define FPS(x) FilePath::StringType(FPL(x), arraysize(FPL(x)) - 1)
16
brettw@chromium.org82bcf512013-02-17 14:07:23 +090017namespace base {
18
mark@chromium.org6380e262008-10-04 03:21:47 +090019struct UnaryTestData {
20 const FilePath::CharType* input;
21 const FilePath::CharType* expected;
22};
23
24struct UnaryBooleanTestData {
25 const FilePath::CharType* input;
26 bool expected;
27};
28
29struct BinaryTestData {
30 const FilePath::CharType* inputs[2];
31 const FilePath::CharType* expected;
32};
33
rafaelw@chromium.org465facc2009-06-25 06:28:30 +090034struct BinaryBooleanTestData {
35 const FilePath::CharType* inputs[2];
36 bool expected;
37};
38
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +090039struct BinaryIntTestData {
40 const FilePath::CharType* inputs[2];
41 int expected;
42};
43
satorux@chromium.orgefba4172011-11-02 13:55:23 +090044struct UTF8TestData {
45 const FilePath::CharType* native;
46 const char* utf8;
47};
48
aa@chromium.org76e5c632008-12-18 17:38:16 +090049// file_util winds up using autoreleased objects on the Mac, so this needs
50// to be a PlatformTest
dchengca87abb2014-12-23 11:56:47 +090051typedef PlatformTest FilePathTest;
aa@chromium.org76e5c632008-12-18 17:38:16 +090052
53TEST_F(FilePathTest, DirName) {
mark@chromium.org6380e262008-10-04 03:21:47 +090054 const struct UnaryTestData cases[] = {
55 { FPL(""), FPL(".") },
56 { FPL("aa"), FPL(".") },
57 { FPL("/aa/bb"), FPL("/aa") },
58 { FPL("/aa/bb/"), FPL("/aa") },
59 { FPL("/aa/bb//"), FPL("/aa") },
60 { FPL("/aa/bb/ccc"), FPL("/aa/bb") },
61 { FPL("/aa"), FPL("/") },
62 { FPL("/aa/"), FPL("/") },
63 { FPL("/"), FPL("/") },
64 { FPL("//"), FPL("//") },
65 { FPL("///"), FPL("/") },
66 { FPL("aa/"), FPL(".") },
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/bb"), FPL("//aa") },
74 { FPL("//aa/"), FPL("//") },
75 { FPL("//aa"), FPL("//") },
76 { FPL("0:"), FPL(".") },
77 { FPL("@:"), FPL(".") },
78 { FPL("[:"), FPL(".") },
79 { FPL("`:"), FPL(".") },
80 { FPL("{:"), FPL(".") },
81 { FPL("\xB3:"), FPL(".") },
82 { FPL("\xC5:"), FPL(".") },
83#if defined(OS_WIN)
84 { FPL("\x0143:"), FPL(".") },
85#endif // OS_WIN
86#if defined(FILE_PATH_USES_DRIVE_LETTERS)
87 { FPL("c:"), FPL("c:") },
88 { FPL("C:"), FPL("C:") },
89 { FPL("A:"), FPL("A:") },
90 { FPL("Z:"), FPL("Z:") },
91 { FPL("a:"), FPL("a:") },
92 { FPL("z:"), FPL("z:") },
93 { FPL("c:aa"), FPL("c:") },
94 { FPL("c:/"), FPL("c:/") },
95 { FPL("c://"), FPL("c://") },
96 { FPL("c:///"), FPL("c:/") },
97 { FPL("c:/aa"), FPL("c:/") },
98 { FPL("c:/aa/"), FPL("c:/") },
99 { FPL("c:/aa/bb"), FPL("c:/aa") },
100 { FPL("c:aa/bb"), FPL("c:aa") },
101#endif // FILE_PATH_USES_DRIVE_LETTERS
102#if defined(FILE_PATH_USES_WIN_SEPARATORS)
103 { FPL("\\aa\\bb"), FPL("\\aa") },
104 { FPL("\\aa\\bb\\"), FPL("\\aa") },
105 { FPL("\\aa\\bb\\\\"), FPL("\\aa") },
106 { FPL("\\aa\\bb\\ccc"), FPL("\\aa\\bb") },
107 { FPL("\\aa"), FPL("\\") },
108 { FPL("\\aa\\"), FPL("\\") },
109 { FPL("\\"), FPL("\\") },
110 { FPL("\\\\"), FPL("\\\\") },
111 { FPL("\\\\\\"), FPL("\\") },
112 { FPL("aa\\"), FPL(".") },
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\\bb"), FPL("\\\\aa") },
120 { FPL("\\\\aa\\"), FPL("\\\\") },
121 { FPL("\\\\aa"), FPL("\\\\") },
122#if defined(FILE_PATH_USES_DRIVE_LETTERS)
123 { FPL("c:\\"), FPL("c:\\") },
124 { FPL("c:\\\\"), FPL("c:\\\\") },
125 { FPL("c:\\\\\\"), FPL("c:\\") },
126 { FPL("c:\\aa"), FPL("c:\\") },
127 { FPL("c:\\aa\\"), FPL("c:\\") },
128 { FPL("c:\\aa\\bb"), FPL("c:\\aa") },
129 { FPL("c:aa\\bb"), FPL("c:aa") },
130#endif // FILE_PATH_USES_DRIVE_LETTERS
131#endif // FILE_PATH_USES_WIN_SEPARATORS
132 };
133
134 for (size_t i = 0; i < arraysize(cases); ++i) {
135 FilePath input(cases[i].input);
136 FilePath observed = input.DirName();
137 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
138 "i: " << i << ", input: " << input.value();
139 }
140}
141
aa@chromium.org76e5c632008-12-18 17:38:16 +0900142TEST_F(FilePathTest, BaseName) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900143 const struct UnaryTestData cases[] = {
144 { FPL(""), FPL("") },
145 { FPL("aa"), FPL("aa") },
146 { FPL("/aa/bb"), FPL("bb") },
147 { FPL("/aa/bb/"), FPL("bb") },
148 { FPL("/aa/bb//"), FPL("bb") },
149 { FPL("/aa/bb/ccc"), FPL("ccc") },
150 { FPL("/aa"), FPL("aa") },
151 { FPL("/"), FPL("/") },
152 { FPL("//"), FPL("//") },
153 { FPL("///"), FPL("/") },
154 { FPL("aa/"), FPL("aa") },
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/bb"), FPL("bb") },
162 { FPL("//aa/"), FPL("aa") },
163 { FPL("//aa"), FPL("aa") },
164 { FPL("0:"), FPL("0:") },
165 { FPL("@:"), FPL("@:") },
166 { FPL("[:"), FPL("[:") },
167 { FPL("`:"), FPL("`:") },
168 { FPL("{:"), FPL("{:") },
169 { FPL("\xB3:"), FPL("\xB3:") },
170 { FPL("\xC5:"), FPL("\xC5:") },
171#if defined(OS_WIN)
172 { FPL("\x0143:"), FPL("\x0143:") },
173#endif // OS_WIN
174#if defined(FILE_PATH_USES_DRIVE_LETTERS)
175 { FPL("c:"), FPL("") },
176 { FPL("C:"), FPL("") },
177 { FPL("A:"), FPL("") },
178 { FPL("Z:"), FPL("") },
179 { FPL("a:"), FPL("") },
180 { FPL("z:"), FPL("") },
181 { FPL("c:aa"), FPL("aa") },
182 { FPL("c:/"), FPL("/") },
183 { FPL("c://"), FPL("//") },
184 { FPL("c:///"), FPL("/") },
185 { FPL("c:/aa"), FPL("aa") },
186 { FPL("c:/aa/"), FPL("aa") },
187 { FPL("c:/aa/bb"), FPL("bb") },
188 { FPL("c:aa/bb"), FPL("bb") },
189#endif // FILE_PATH_USES_DRIVE_LETTERS
190#if defined(FILE_PATH_USES_WIN_SEPARATORS)
191 { FPL("\\aa\\bb"), FPL("bb") },
192 { FPL("\\aa\\bb\\"), FPL("bb") },
193 { FPL("\\aa\\bb\\\\"), FPL("bb") },
194 { FPL("\\aa\\bb\\ccc"), FPL("ccc") },
195 { FPL("\\aa"), FPL("aa") },
196 { FPL("\\"), FPL("\\") },
197 { FPL("\\\\"), FPL("\\\\") },
198 { FPL("\\\\\\"), FPL("\\") },
199 { FPL("aa\\"), FPL("aa") },
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\\bb"), FPL("bb") },
207 { FPL("\\\\aa\\"), FPL("aa") },
208 { FPL("\\\\aa"), FPL("aa") },
209#if defined(FILE_PATH_USES_DRIVE_LETTERS)
210 { FPL("c:\\"), FPL("\\") },
211 { FPL("c:\\\\"), FPL("\\\\") },
212 { FPL("c:\\\\\\"), FPL("\\") },
213 { FPL("c:\\aa"), FPL("aa") },
214 { FPL("c:\\aa\\"), FPL("aa") },
215 { FPL("c:\\aa\\bb"), FPL("bb") },
216 { FPL("c:aa\\bb"), FPL("bb") },
217#endif // FILE_PATH_USES_DRIVE_LETTERS
218#endif // FILE_PATH_USES_WIN_SEPARATORS
219 };
220
221 for (size_t i = 0; i < arraysize(cases); ++i) {
222 FilePath input(cases[i].input);
estade@chromium.orgd872d682009-01-06 08:59:36 +0900223 FilePath observed = input.BaseName();
224 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
mark@chromium.org6380e262008-10-04 03:21:47 +0900225 "i: " << i << ", input: " << input.value();
226 }
227}
228
aa@chromium.org76e5c632008-12-18 17:38:16 +0900229TEST_F(FilePathTest, Append) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900230 const struct BinaryTestData cases[] = {
231 { { FPL(""), FPL("cc") }, FPL("cc") },
232 { { FPL("."), FPL("ff") }, FPL("ff") },
233 { { FPL("/"), FPL("cc") }, FPL("/cc") },
234 { { FPL("/aa"), FPL("") }, FPL("/aa") },
235 { { FPL("/aa/"), FPL("") }, FPL("/aa") },
236 { { FPL("//aa"), FPL("") }, FPL("//aa") },
237 { { FPL("//aa/"), FPL("") }, FPL("//aa") },
238 { { FPL("//"), FPL("aa") }, FPL("//aa") },
239#if defined(FILE_PATH_USES_DRIVE_LETTERS)
240 { { FPL("c:"), FPL("a") }, FPL("c:a") },
241 { { FPL("c:"), FPL("") }, FPL("c:") },
242 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
243 { { FPL("c://"), FPL("a") }, FPL("c://a") },
244 { { FPL("c:///"), FPL("a") }, FPL("c:/a") },
245#endif // FILE_PATH_USES_DRIVE_LETTERS
246#if defined(FILE_PATH_USES_WIN_SEPARATORS)
247 // Append introduces the default separator character, so these test cases
248 // need to be defined with different expected results on platforms that use
249 // different default separator characters.
250 { { FPL("\\"), FPL("cc") }, FPL("\\cc") },
251 { { FPL("\\aa"), FPL("") }, FPL("\\aa") },
252 { { FPL("\\aa\\"), FPL("") }, FPL("\\aa") },
253 { { FPL("\\\\aa"), FPL("") }, FPL("\\\\aa") },
254 { { FPL("\\\\aa\\"), FPL("") }, FPL("\\\\aa") },
255 { { FPL("\\\\"), FPL("aa") }, FPL("\\\\aa") },
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("aa/bb"), FPL("cc") }, FPL("aa/bb\\cc") },
260 { { FPL("a/b"), FPL("c") }, FPL("a/b\\c") },
261 { { FPL("a/b/"), FPL("c") }, FPL("a/b\\c") },
262 { { FPL("//aa"), FPL("bb") }, FPL("//aa\\bb") },
263 { { FPL("//aa/"), FPL("bb") }, FPL("//aa\\bb") },
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("aa\\bb"), FPL("cc") }, FPL("aa\\bb\\cc") },
268 { { FPL("a\\b"), FPL("c") }, FPL("a\\b\\c") },
269 { { FPL("a\\b\\"), FPL("c") }, FPL("a\\b\\c") },
270 { { FPL("\\\\aa"), FPL("bb") }, FPL("\\\\aa\\bb") },
271 { { FPL("\\\\aa\\"), FPL("bb") }, FPL("\\\\aa\\bb") },
272#if defined(FILE_PATH_USES_DRIVE_LETTERS)
273 { { FPL("c:\\"), FPL("a") }, FPL("c:\\a") },
274 { { FPL("c:\\\\"), FPL("a") }, FPL("c:\\\\a") },
275 { { FPL("c:\\\\\\"), FPL("a") }, FPL("c:\\a") },
276 { { FPL("c:\\"), FPL("") }, FPL("c:\\") },
277 { { FPL("c:\\a"), FPL("b") }, FPL("c:\\a\\b") },
278 { { FPL("c:\\a\\"), FPL("b") }, FPL("c:\\a\\b") },
279#endif // FILE_PATH_USES_DRIVE_LETTERS
280#else // FILE_PATH_USES_WIN_SEPARATORS
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("aa/bb"), FPL("cc") }, FPL("aa/bb/cc") },
285 { { FPL("a/b"), FPL("c") }, FPL("a/b/c") },
286 { { FPL("a/b/"), FPL("c") }, FPL("a/b/c") },
287 { { FPL("//aa"), FPL("bb") }, FPL("//aa/bb") },
288 { { FPL("//aa/"), FPL("bb") }, FPL("//aa/bb") },
289#if defined(FILE_PATH_USES_DRIVE_LETTERS)
290 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
291 { { FPL("c:/"), FPL("") }, FPL("c:/") },
292 { { FPL("c:/a"), FPL("b") }, FPL("c:/a/b") },
293 { { FPL("c:/a/"), FPL("b") }, FPL("c:/a/b") },
294#endif // FILE_PATH_USES_DRIVE_LETTERS
295#endif // FILE_PATH_USES_WIN_SEPARATORS
296 };
297
298 for (size_t i = 0; i < arraysize(cases); ++i) {
299 FilePath root(cases[i].inputs[0]);
300 FilePath::StringType leaf(cases[i].inputs[1]);
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900301 FilePath observed_str = root.Append(leaf);
302 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
303 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
304 FilePath observed_path = root.Append(FilePath(leaf));
305 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) <<
mark@chromium.org6380e262008-10-04 03:21:47 +0900306 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900307
308 // TODO(erikkay): It would be nice to have a unicode test append value to
309 // handle the case when AppendASCII is passed UTF8
310#if defined(OS_WIN)
brettw@chromium.org11f89b02010-08-18 08:05:28 +0900311 std::string ascii = WideToUTF8(leaf);
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900312#elif defined(OS_POSIX)
313 std::string ascii = leaf;
314#endif
315 observed_str = root.AppendASCII(ascii);
316 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
317 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
mark@chromium.org6380e262008-10-04 03:21:47 +0900318 }
319}
320
mark@chromium.org778d9cf2009-10-14 02:31:39 +0900321TEST_F(FilePathTest, StripTrailingSeparators) {
322 const struct UnaryTestData cases[] = {
323 { FPL(""), FPL("") },
324 { FPL("/"), FPL("/") },
325 { FPL("//"), FPL("//") },
326 { FPL("///"), FPL("/") },
327 { FPL("////"), FPL("/") },
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 { FPL("/a////"), FPL("/a") },
337#if defined(FILE_PATH_USES_DRIVE_LETTERS)
338 { FPL("c:"), FPL("c:") },
339 { FPL("c:/"), FPL("c:/") },
340 { FPL("c://"), FPL("c://") },
341 { FPL("c:///"), FPL("c:/") },
342 { FPL("c:////"), FPL("c:/") },
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 { FPL("c:/a////"), FPL("c:/a") },
348#endif // FILE_PATH_USES_DRIVE_LETTERS
349#if defined(FILE_PATH_USES_WIN_SEPARATORS)
350 { FPL("\\"), FPL("\\") },
351 { FPL("\\\\"), FPL("\\\\") },
352 { FPL("\\\\\\"), FPL("\\") },
353 { FPL("\\\\\\\\"), FPL("\\") },
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 { FPL("\\a\\\\\\\\"), FPL("\\a") },
363#if defined(FILE_PATH_USES_DRIVE_LETTERS)
364 { FPL("c:\\"), FPL("c:\\") },
365 { FPL("c:\\\\"), FPL("c:\\\\") },
366 { FPL("c:\\\\\\"), FPL("c:\\") },
367 { FPL("c:\\\\\\\\"), FPL("c:\\") },
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 { FPL("c:\\a\\\\\\\\"), FPL("c:\\a") },
373#endif // FILE_PATH_USES_DRIVE_LETTERS
374#endif // FILE_PATH_USES_WIN_SEPARATORS
375 };
376
377 for (size_t i = 0; i < arraysize(cases); ++i) {
378 FilePath input(cases[i].input);
379 FilePath observed = input.StripTrailingSeparators();
380 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
381 "i: " << i << ", input: " << input.value();
382 }
383}
384
aa@chromium.org76e5c632008-12-18 17:38:16 +0900385TEST_F(FilePathTest, IsAbsolute) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900386 const struct UnaryBooleanTestData cases[] = {
387 { FPL(""), false },
388 { FPL("a"), false },
389 { FPL("c:"), false },
390 { FPL("c:a"), false },
391 { FPL("a/b"), false },
392 { FPL("//"), true },
393 { FPL("//a"), true },
394 { FPL("c:a/b"), false },
395 { FPL("?:/a"), false },
396#if defined(FILE_PATH_USES_DRIVE_LETTERS)
397 { FPL("/"), false },
398 { FPL("/a"), false },
399 { FPL("/."), false },
400 { FPL("/.."), false },
401 { FPL("c:/"), true },
402 { FPL("c:/a"), true },
403 { FPL("c:/."), true },
404 { FPL("c:/.."), true },
405 { FPL("C:/a"), true },
406 { FPL("d:/a"), true },
407#else // FILE_PATH_USES_DRIVE_LETTERS
408 { FPL("/"), true },
409 { FPL("/a"), true },
410 { FPL("/."), true },
411 { FPL("/.."), true },
412 { FPL("c:/"), false },
413#endif // FILE_PATH_USES_DRIVE_LETTERS
414#if defined(FILE_PATH_USES_WIN_SEPARATORS)
415 { FPL("a\\b"), false },
416 { FPL("\\\\"), true },
417 { FPL("\\\\a"), true },
418 { FPL("a\\b"), false },
419 { FPL("\\\\"), true },
420 { FPL("//a"), true },
421 { FPL("c:a\\b"), false },
422 { FPL("?:\\a"), false },
423#if defined(FILE_PATH_USES_DRIVE_LETTERS)
424 { FPL("\\"), false },
425 { FPL("\\a"), false },
426 { FPL("\\."), false },
427 { FPL("\\.."), false },
428 { FPL("c:\\"), true },
429 { FPL("c:\\"), true },
430 { FPL("c:\\a"), true },
431 { FPL("c:\\."), true },
432 { FPL("c:\\.."), true },
433 { FPL("C:\\a"), true },
434 { FPL("d:\\a"), true },
435#else // FILE_PATH_USES_DRIVE_LETTERS
436 { FPL("\\"), true },
437 { FPL("\\a"), true },
438 { FPL("\\."), true },
439 { FPL("\\.."), true },
440 { FPL("c:\\"), false },
441#endif // FILE_PATH_USES_DRIVE_LETTERS
442#endif // FILE_PATH_USES_WIN_SEPARATORS
443 };
444
445 for (size_t i = 0; i < arraysize(cases); ++i) {
446 FilePath input(cases[i].input);
447 bool observed = input.IsAbsolute();
448 EXPECT_EQ(cases[i].expected, observed) <<
449 "i: " << i << ", input: " << input.value();
450 }
451}
aa@chromium.org76e5c632008-12-18 17:38:16 +0900452
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900453TEST_F(FilePathTest, PathComponentsTest) {
454 const struct UnaryTestData cases[] = {
455 { FPL("//foo/bar/baz/"), FPL("|//|foo|bar|baz")},
456 { FPL("///"), FPL("|/")},
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/baz"), FPL("|/|foo|bar|baz")},
462 { FPL("/foo/bar.bot/baz.txt"), FPL("|/|foo|bar.bot|baz.txt")},
463 { FPL("//foo//bar/baz"), FPL("|//|foo|bar|baz")},
464 { FPL("/"), FPL("|/")},
465 { FPL("foo"), FPL("|foo")},
466 { FPL(""), FPL("")},
467#if defined(FILE_PATH_USES_DRIVE_LETTERS)
468 { FPL("e:/foo"), FPL("|e:|/|foo")},
469 { FPL("e:/"), FPL("|e:|/")},
470 { FPL("e:"), FPL("|e:")},
471#endif // FILE_PATH_USES_DRIVE_LETTERS
472#if defined(FILE_PATH_USES_WIN_SEPARATORS)
473 { FPL("../foo"), FPL("|..|foo")},
474 { FPL("./foo"), FPL("|foo")},
475 { FPL("../foo/bar/"), FPL("|..|foo|bar") },
476 { FPL("\\\\foo\\bar\\baz\\"), FPL("|\\\\|foo|bar|baz")},
477 { FPL("\\\\\\"), FPL("|\\")},
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\\baz"), FPL("|/|foo|bar|baz")},
485 { FPL("\\foo\\bar.bot\\baz.txt"), FPL("|\\|foo|bar.bot|baz.txt")},
486 { FPL("\\\\foo\\\\bar\\baz"), FPL("|\\\\|foo|bar|baz")},
487 { FPL("\\"), FPL("|\\")},
488#endif // FILE_PATH_USES_WIN_SEPARATORS
489 };
490
491 for (size_t i = 0; i < arraysize(cases); ++i) {
492 FilePath input(cases[i].input);
493 std::vector<FilePath::StringType> comps;
494 input.GetComponents(&comps);
495
496 FilePath::StringType observed;
497 for (size_t j = 0; j < comps.size(); ++j) {
498 observed.append(FILE_PATH_LITERAL("|"), 1);
499 observed.append(comps[j]);
500 }
501 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed) <<
502 "i: " << i << ", input: " << input.value();
503 }
504}
505
506TEST_F(FilePathTest, IsParentTest) {
507 const struct BinaryBooleanTestData cases[] = {
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900508 { { FPL("/"), 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("//foo/bar/baz") }, true},
512 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, false},
513 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, false},
514 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, false},
515 { { FPL("/foo/bar"), FPL("/foo/bar") }, false},
516 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
517 { { FPL("foo/bar"), FPL("foo/bar/baz") }, true},
518 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, false},
519 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, false},
520 { { FPL(""), FPL("foo") }, false},
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900521#if defined(FILE_PATH_USES_DRIVE_LETTERS)
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900522 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, true},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900523 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, true},
524 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, true},
525 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, false},
526 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900527 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, true},
528 { { FPL("c:"), FPL("c:/foo/bar/baz") }, true},
529 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900530 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, false},
531 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900532 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900533 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, false},
534 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900535 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, false},
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900536#endif // FILE_PATH_USES_DRIVE_LETTERS
537#if defined(FILE_PATH_USES_WIN_SEPARATORS)
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900538 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, true},
539 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, true},
540 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, true},
541 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, true},
542 { { FPL(""), FPL("\\foo\\bar\\baz") }, false},
543 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, false},
544 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900545#endif // FILE_PATH_USES_WIN_SEPARATORS
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900546 };
547
548 for (size_t i = 0; i < arraysize(cases); ++i) {
549 FilePath parent(cases[i].inputs[0]);
550 FilePath child(cases[i].inputs[1]);
551
552 EXPECT_EQ(parent.IsParent(child), cases[i].expected) <<
mad@chromium.orgcc4f7b32009-10-21 00:27:21 +0900553 "i: " << i << ", parent: " << parent.value() << ", child: " <<
554 child.value();
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900555 }
556}
557
mark@chromium.org752010c2009-09-24 05:32:41 +0900558TEST_F(FilePathTest, AppendRelativePathTest) {
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900559 const struct BinaryTestData cases[] = {
mark@chromium.org752010c2009-09-24 05:32:41 +0900560#if defined(FILE_PATH_USES_WIN_SEPARATORS)
561 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo\\bar\\baz")},
562#else // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900563 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo/bar/baz")},
mark@chromium.org752010c2009-09-24 05:32:41 +0900564#endif // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900565 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, FPL("baz")},
566 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, FPL("baz")},
567 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, FPL("baz")},
568 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, FPL("")},
569 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, FPL("")},
570 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, FPL("")},
571 { { FPL("/foo/bar"), FPL("/foo/bar") }, FPL("")},
572 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, FPL("")},
573 { { FPL("foo/bar"), FPL("foo/bar/baz") }, FPL("baz")},
574 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, FPL("")},
575 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, FPL("")},
576 { { FPL(""), FPL("foo") }, FPL("")},
577#if defined(FILE_PATH_USES_DRIVE_LETTERS)
578 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, FPL("baz")},
579 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, FPL("baz")},
580 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, FPL("baz")},
581 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, FPL("")},
582 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, FPL("")},
mark@chromium.org752010c2009-09-24 05:32:41 +0900583#if defined(FILE_PATH_USES_WIN_SEPARATORS)
584 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
585 // TODO(akalin): Figure out how to handle the corner case in the
586 // commented-out test case below. Appending to an empty path gives
587 // /foo\bar\baz but appending to a nonempty path "blah" gives
588 // blah\foo\bar\baz.
589 // { { FPL("c:"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
590#endif // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900591 { { 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("d:/foo/bar/baz") }, FPL("")},
594 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, FPL("")},
595 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, FPL("")},
596 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, FPL("")},
597 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, FPL("")},
598#endif // FILE_PATH_USES_DRIVE_LETTERS
599#if defined(FILE_PATH_USES_WIN_SEPARATORS)
600 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
601 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
602 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, FPL("baz")},
603 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, FPL("foo\\bar\\baz")},
604 { { FPL(""), FPL("\\foo\\bar\\baz") }, FPL("")},
605 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, FPL("")},
606 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, FPL("")},
607#endif // FILE_PATH_USES_WIN_SEPARATORS
608 };
609
610 const FilePath base(FPL("blah"));
611
612 for (size_t i = 0; i < arraysize(cases); ++i) {
613 FilePath parent(cases[i].inputs[0]);
614 FilePath child(cases[i].inputs[1]);
615 {
616 FilePath result;
617 bool success = parent.AppendRelativePath(child, &result);
618 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
619 "i: " << i << ", parent: " << parent.value() << ", child: " <<
620 child.value();
621 EXPECT_STREQ(cases[i].expected, result.value().c_str()) <<
622 "i: " << i << ", parent: " << parent.value() << ", child: " <<
623 child.value();
624 }
625 {
626 FilePath result(base);
627 bool success = parent.AppendRelativePath(child, &result);
628 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
629 "i: " << i << ", parent: " << parent.value() << ", child: " <<
630 child.value();
631 EXPECT_EQ(base.Append(cases[i].expected).value(), result.value()) <<
632 "i: " << i << ", parent: " << parent.value() << ", child: " <<
633 child.value();
634 }
635 }
636}
637
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900638TEST_F(FilePathTest, EqualityTest) {
639 const struct BinaryBooleanTestData cases[] = {
640 { { FPL("/foo/bar/baz"), FPL("/foo/bar/baz") }, true},
641 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, false},
642 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
643 { { FPL("//foo/bar/"), FPL("//foo/bar/") }, true},
644 { { FPL("/foo/bar"), FPL("/foo2/bar") }, false},
645 { { FPL("/foo/bar.txt"), FPL("/foo/bar") }, false},
646 { { FPL("foo/bar"), FPL("foo/bar") }, true},
647 { { FPL("foo/bar"), FPL("foo/bar/baz") }, false},
648 { { FPL(""), FPL("foo") }, false},
649#if defined(FILE_PATH_USES_DRIVE_LETTERS)
650 { { FPL("c:/foo/bar"), FPL("c:/foo/bar") }, true},
651 { { FPL("E:/foo/bar"), FPL("e:/foo/bar") }, true},
652 { { FPL("f:/foo/bar"), FPL("F:/foo/bar") }, true},
653 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar") }, false},
654 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar") }, false},
655 { { FPL("c:/"), FPL("c:/") }, true},
656 { { FPL("c:"), FPL("c:") }, true},
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("d:/foo/bar") }, false},
660 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar") }, false},
661#endif // FILE_PATH_USES_DRIVE_LETTERS
662#if defined(FILE_PATH_USES_WIN_SEPARATORS)
663 { { FPL("\\foo\\bar"), FPL("\\foo\\bar") }, true},
664 { { FPL("\\foo/bar"), FPL("\\foo/bar") }, true},
tony@chromium.org65049b12010-06-25 14:33:17 +0900665 { { FPL("\\foo/bar"), FPL("\\foo\\bar") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900666 { { FPL("\\"), FPL("\\") }, true},
667 { { FPL("\\"), FPL("/") }, false},
668 { { FPL(""), FPL("\\") }, false},
669 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar") }, false},
670 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2") }, false},
671#if defined(FILE_PATH_USES_DRIVE_LETTERS)
672 { { FPL("c:\\foo\\bar"), FPL("c:\\foo\\bar") }, true},
673 { { FPL("E:\\foo\\bar"), FPL("e:\\foo\\bar") }, true},
674 { { FPL("f:\\foo\\bar"), FPL("F:\\foo/bar") }, false},
675#endif // FILE_PATH_USES_DRIVE_LETTERS
676#endif // FILE_PATH_USES_WIN_SEPARATORS
677 };
678
679 for (size_t i = 0; i < arraysize(cases); ++i) {
680 FilePath a(cases[i].inputs[0]);
681 FilePath b(cases[i].inputs[1]);
682
683 EXPECT_EQ(a == b, cases[i].expected) <<
684 "equality i: " << i << ", a: " << a.value() << ", b: " <<
685 b.value();
686 }
687
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900688 for (size_t i = 0; i < arraysize(cases); ++i) {
689 FilePath a(cases[i].inputs[0]);
690 FilePath b(cases[i].inputs[1]);
691
692 EXPECT_EQ(a != b, !cases[i].expected) <<
693 "inequality i: " << i << ", a: " << a.value() << ", b: " <<
694 b.value();
695 }
696}
697
erikkay@google.com985d4202009-01-10 03:26:19 +0900698TEST_F(FilePathTest, Extension) {
699 FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
700
701 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
estade@chromium.org99a42e72010-07-28 06:24:39 +0900702 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.Extension());
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900703 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.FinalExtension());
erikkay@google.com985d4202009-01-10 03:26:19 +0900704
705 FilePath base = jpg.BaseName().RemoveExtension();
estade@chromium.org99a42e72010-07-28 06:24:39 +0900706 EXPECT_EQ(FILE_PATH_LITERAL("foo"), base.value());
erikkay@google.com985d4202009-01-10 03:26:19 +0900707
708 FilePath path_no_ext = base_dir.Append(base);
estade@chromium.org99a42e72010-07-28 06:24:39 +0900709 EXPECT_EQ(path_no_ext.value(), jpg.RemoveExtension().value());
erikkay@google.com985d4202009-01-10 03:26:19 +0900710
711 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value());
estade@chromium.org99a42e72010-07-28 06:24:39 +0900712 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.Extension());
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900713 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.FinalExtension());
erikkay@google.com985d4202009-01-10 03:26:19 +0900714}
715
716TEST_F(FilePathTest, Extension2) {
717 const struct UnaryTestData cases[] = {
718#if defined(FILE_PATH_USES_WIN_SEPARATORS)
719 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
720 { FPL("C:\\a\\b\\c."), FPL(".") },
721 { FPL("C:\\a\\b\\c"), FPL("") },
722 { FPL("C:\\a\\b\\"), FPL("") },
723 { FPL("C:\\a\\b.\\"), FPL(".") },
724 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
725 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
726 { FPL("C:\\foo.bar\\.."), FPL("") },
727 { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
728#endif
729 { FPL("/foo/bar/baz.ext"), FPL(".ext") },
730 { FPL("/foo/bar/baz."), FPL(".") },
731 { FPL("/foo/bar/baz.."), FPL(".") },
732 { FPL("/foo/bar/baz"), FPL("") },
733 { FPL("/foo/bar/"), FPL("") },
734 { FPL("/foo/bar./"), FPL(".") },
735 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900736 { FPL("/subversion-1.6.12.zip"), FPL(".zip") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900737 { FPL("/foo.12345.gz"), FPL(".gz") },
738 { FPL("/foo..gz"), FPL(".gz") },
erikkay@google.com985d4202009-01-10 03:26:19 +0900739 { FPL("."), FPL("") },
740 { FPL(".."), FPL("") },
741 { FPL("./foo"), FPL("") },
742 { FPL("./foo.ext"), FPL(".ext") },
743 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
744 { FPL("/foo.bar////"), FPL(".bar") },
745 { FPL("/foo.bar/.."), FPL("") },
746 { FPL("/foo.bar/..////"), FPL("") },
aa@chromium.orgf8395de2012-08-26 21:11:10 +0900747 { FPL("/foo.1234.luser.js"), FPL(".js") },
748 { FPL("/user.js"), FPL(".js") },
erikkay@google.com985d4202009-01-10 03:26:19 +0900749 };
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900750 const struct UnaryTestData double_extension_cases[] = {
751 { FPL("/foo.tar.gz"), FPL(".tar.gz") },
752 { FPL("/foo.tar.Z"), FPL(".tar.Z") },
753 { FPL("/foo.tar.bz2"), FPL(".tar.bz2") },
754 { FPL("/foo.1234.gz"), FPL(".1234.gz") },
755 { FPL("/foo.1234.tar.gz"), FPL(".tar.gz") },
756 { FPL("/foo.tar.tar.gz"), FPL(".tar.gz") },
757 { FPL("/foo.tar.gz.gz"), FPL(".gz.gz") },
758 { FPL("/foo.1234.user.js"), FPL(".user.js") },
759 { FPL("foo.user.js"), FPL(".user.js") },
arun87.kumarf0c7ccf2014-10-09 13:50:44 +0900760 { FPL("/foo.tar.bz"), FPL(".tar.bz") },
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900761 };
erikkay@google.com985d4202009-01-10 03:26:19 +0900762 for (unsigned int i = 0; i < arraysize(cases); ++i) {
763 FilePath path(cases[i].input);
764 FilePath::StringType extension = path.Extension();
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900765 FilePath::StringType final_extension = path.FinalExtension();
pkasting9043c4e2014-10-02 07:18:43 +0900766 EXPECT_STREQ(cases[i].expected, extension.c_str())
767 << "i: " << i << ", path: " << path.value();
768 EXPECT_STREQ(cases[i].expected, final_extension.c_str())
769 << "i: " << i << ", path: " << path.value();
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900770 }
771 for (unsigned int i = 0; i < arraysize(double_extension_cases); ++i) {
pkasting9043c4e2014-10-02 07:18:43 +0900772 FilePath path(double_extension_cases[i].input);
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900773 FilePath::StringType extension = path.Extension();
pkasting9043c4e2014-10-02 07:18:43 +0900774 EXPECT_STREQ(double_extension_cases[i].expected, extension.c_str())
775 << "i: " << i << ", path: " << path.value();
erikkay@google.com985d4202009-01-10 03:26:19 +0900776 }
777}
778
779TEST_F(FilePathTest, InsertBeforeExtension) {
780 const struct BinaryTestData cases[] = {
781 { { FPL(""), FPL("") }, FPL("") },
782 { { FPL(""), FPL("txt") }, FPL("") },
783 { { FPL("."), FPL("txt") }, FPL("") },
784 { { FPL(".."), FPL("txt") }, FPL("") },
785 { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
786 { { FPL("."), FPL("") }, FPL(".") },
787 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
788 { { FPL("foo"), FPL("txt") }, FPL("footxt") },
789 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
790 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
791 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
792 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
793 { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
794 { { FPL("foo"), FPL("") }, FPL("foo") },
795 { { FPL("foo"), FPL(".") }, FPL("foo.") },
796 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
797 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
798#if defined(FILE_PATH_USES_WIN_SEPARATORS)
799 { { FPL("\\"), FPL("") }, FPL("\\") },
800 { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
801 { { FPL("\\."), FPL("txt") }, FPL("") },
802 { { FPL("\\.."), FPL("txt") }, FPL("") },
803 { { FPL("\\."), FPL("") }, FPL("\\.") },
804 { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
805 FPL("C:\\bar\\footxt.dll") },
806 { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
807 FPL("C:\\bar.baz\\foodlltxt") },
808 { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
809 FPL("C:\\bar.baz\\footxt.dll") },
810 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
811 FPL("C:\\bar.baz\\foo.dlltxt.exe") },
812 { { FPL("C:\\bar.baz\\foo"), FPL("") },
813 FPL("C:\\bar.baz\\foo") },
814 { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
815 FPL("C:\\bar.baz\\foo.exe") },
816 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
817 FPL("C:\\bar.baz\\foo.dll.exe") },
818 { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
819 FPL("C:\\bar\\baz\\foo (1).exe") },
820 { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
821 { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
822#endif
823 { { FPL("/"), FPL("") }, FPL("/") },
824 { { FPL("/"), FPL("txt") }, FPL("/txt") },
825 { { FPL("/."), FPL("txt") }, FPL("") },
826 { { FPL("/.."), FPL("txt") }, FPL("") },
827 { { FPL("/."), FPL("") }, FPL("/.") },
828 { { FPL("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
829 { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
830 { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
831 { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
832 FPL("/bar.baz/foo.dlltxt.exe") },
833 { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
834 { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
835 { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
836 { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
837 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
838 };
839 for (unsigned int i = 0; i < arraysize(cases); ++i) {
840 FilePath path(cases[i].inputs[0]);
841 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
842 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
843 ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
844 }
845}
846
estade@chromium.org99a42e72010-07-28 06:24:39 +0900847TEST_F(FilePathTest, RemoveExtension) {
848 const struct UnaryTestData cases[] = {
849 { FPL(""), FPL("") },
850 { FPL("."), FPL(".") },
851 { FPL(".."), FPL("..") },
852 { FPL("foo.dll"), FPL("foo") },
853 { FPL("./foo.dll"), FPL("./foo") },
854 { FPL("foo..dll"), FPL("foo.") },
855 { FPL("foo"), FPL("foo") },
856 { FPL("foo."), FPL("foo") },
857 { FPL("foo.."), FPL("foo.") },
858 { FPL("foo.baz.dll"), FPL("foo.baz") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900859#if defined(FILE_PATH_USES_WIN_SEPARATORS)
860 { FPL("C:\\foo.bar\\foo"), FPL("C:\\foo.bar\\foo") },
861 { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") },
862#endif
863 { FPL("/foo.bar/foo"), FPL("/foo.bar/foo") },
864 { FPL("/foo.bar/..////"), FPL("/foo.bar/..////") },
865 };
866 for (unsigned int i = 0; i < arraysize(cases); ++i) {
867 FilePath path(cases[i].input);
868 FilePath removed = path.RemoveExtension();
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900869 FilePath removed_final = path.RemoveFinalExtension();
estade@chromium.org99a42e72010-07-28 06:24:39 +0900870 EXPECT_EQ(cases[i].expected, removed.value()) << "i: " << i <<
871 ", path: " << path.value();
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900872 EXPECT_EQ(cases[i].expected, removed_final.value()) << "i: " << i <<
873 ", path: " << path.value();
874 }
875 {
876 FilePath path(FPL("foo.tar.gz"));
877 FilePath removed = path.RemoveExtension();
878 FilePath removed_final = path.RemoveFinalExtension();
879 EXPECT_EQ(FPL("foo"), removed.value()) << ", path: " << path.value();
880 EXPECT_EQ(FPL("foo.tar"), removed_final.value()) << ", path: "
881 << path.value();
estade@chromium.org99a42e72010-07-28 06:24:39 +0900882 }
883}
884
erikkay@google.com985d4202009-01-10 03:26:19 +0900885TEST_F(FilePathTest, ReplaceExtension) {
886 const struct BinaryTestData cases[] = {
887 { { FPL(""), FPL("") }, FPL("") },
888 { { FPL(""), FPL("txt") }, FPL("") },
889 { { FPL("."), FPL("txt") }, FPL("") },
890 { { FPL(".."), FPL("txt") }, FPL("") },
891 { { FPL("."), FPL("") }, FPL("") },
892 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900893 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.txt") },
erikkay@google.com985d4202009-01-10 03:26:19 +0900894 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
895 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
896 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
897 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
898 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
899 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
900 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
901 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
902 { { FPL("foo.dll"), FPL("") }, FPL("foo") },
903 { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
904 { { FPL("foo"), FPL("") }, FPL("foo") },
905 { { FPL("foo"), FPL(".") }, FPL("foo") },
906 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
907 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
908#if defined(FILE_PATH_USES_WIN_SEPARATORS)
909 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
910 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
911#endif
912 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
913 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
914 };
915 for (unsigned int i = 0; i < arraysize(cases); ++i) {
916 FilePath path(cases[i].inputs[0]);
917 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
918 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
919 ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
920 }
921}
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900922
rdevlin.cronin@chromium.org9a980f72012-04-25 10:53:44 +0900923TEST_F(FilePathTest, AddExtension) {
924 const struct BinaryTestData cases[] = {
925 { { FPL(""), FPL("") }, FPL("") },
926 { { FPL(""), FPL("txt") }, FPL("") },
927 { { FPL("."), FPL("txt") }, FPL("") },
928 { { FPL(".."), FPL("txt") }, FPL("") },
929 { { FPL("."), FPL("") }, FPL("") },
930 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.dll.txt") },
931 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.dll.txt") },
932 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..dll.txt") },
933 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.dll.txt") },
934 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
935 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
936 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
937 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
938 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.dll.txt") },
939 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.dll.txt") },
940 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
941 { { FPL("foo.dll"), FPL(".") }, FPL("foo.dll") },
942 { { FPL("foo"), FPL("") }, FPL("foo") },
943 { { FPL("foo"), FPL(".") }, FPL("foo") },
944 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
945 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz.dll") },
946#if defined(FILE_PATH_USES_WIN_SEPARATORS)
947 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
948 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
949#endif
950 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
951 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
952 };
953 for (unsigned int i = 0; i < arraysize(cases); ++i) {
954 FilePath path(cases[i].inputs[0]);
955 FilePath added = path.AddExtension(cases[i].inputs[1]);
956 EXPECT_EQ(cases[i].expected, added.value()) << "i: " << i <<
957 ", path: " << path.value() << ", add: " << cases[i].inputs[1];
958 }
959}
960
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900961TEST_F(FilePathTest, MatchesExtension) {
962 const struct BinaryBooleanTestData cases[] = {
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900963 { { FPL("foo"), FPL("") }, true},
964 { { FPL("foo"), FPL(".") }, false},
965 { { FPL("foo."), FPL("") }, false},
966 { { FPL("foo."), FPL(".") }, true},
967 { { FPL("foo.txt"), FPL(".dll") }, false},
968 { { FPL("foo.txt"), FPL(".txt") }, true},
969 { { FPL("foo.txt.dll"), FPL(".txt") }, false},
970 { { FPL("foo.txt.dll"), FPL(".dll") }, true},
971 { { FPL("foo.TXT"), FPL(".txt") }, true},
972 { { FPL("foo.txt"), FPL(".TXT") }, 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},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900977#if defined(FILE_PATH_USES_DRIVE_LETTERS)
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900978 { { FPL("c:/foo.txt.dll"), FPL(".txt") }, false},
979 { { FPL("c:/foo.txt"), FPL(".txt") }, true},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900980#endif // FILE_PATH_USES_DRIVE_LETTERS
981#if defined(FILE_PATH_USES_WIN_SEPARATORS)
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900982 { { FPL("c:\\bar\\foo.txt.dll"), FPL(".txt") }, false},
983 { { FPL("c:\\bar\\foo.txt"), FPL(".txt") }, true},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900984#endif // FILE_PATH_USES_DRIVE_LETTERS
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900985 { { FPL("/bar/foo.txt.dll"), FPL(".txt") }, false},
986 { { FPL("/bar/foo.txt"), FPL(".txt") }, true},
987#if defined(OS_WIN) || defined(OS_MACOSX)
988 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
989 { { FPL("foo.\u00E4\u00F6\u00FC"), FPL(".\u00E4\u00F6\u00FC") }, true},
990 { { FPL("foo.\u00C4\u00D6\u00DC"), FPL(".\u00E4\u00F6\u00FC") }, true},
991 // C with circumflex: direct comparison, and upper case vs. lower case
992 { { FPL("foo.\u0109"), FPL(".\u0109") }, true},
993 { { FPL("foo.\u0108"), FPL(".\u0109") }, true},
994#endif
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900995 };
996
997 for (size_t i = 0; i < arraysize(cases); ++i) {
998 FilePath path(cases[i].inputs[0]);
999 FilePath::StringType ext(cases[i].inputs[1]);
1000
1001 EXPECT_EQ(cases[i].expected, path.MatchesExtension(ext)) <<
1002 "i: " << i << ", path: " << path.value() << ", ext: " << ext;
1003 }
1004}
cevans@chromium.org48dc54c2009-08-16 06:44:31 +09001005
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +09001006TEST_F(FilePathTest, CompareIgnoreCase) {
1007 const struct BinaryIntTestData cases[] = {
1008 { { FPL("foo"), FPL("foo") }, 0},
1009 { { FPL("FOO"), FPL("foo") }, 0},
1010 { { FPL("foo.ext"), FPL("foo.ext") }, 0},
1011 { { FPL("FOO.EXT"), FPL("foo.ext") }, 0},
1012 { { FPL("Foo.Ext"), FPL("foo.ext") }, 0},
1013 { { FPL("foO"), FPL("foo") }, 0},
1014 { { FPL("foo"), FPL("foO") }, 0},
1015 { { FPL("fOo"), FPL("foo") }, 0},
1016 { { FPL("foo"), FPL("fOo") }, 0},
1017 { { FPL("bar"), FPL("foo") }, -1},
1018 { { FPL("foo"), FPL("bar") }, 1},
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 // German "Eszett" (lower case and the new-fangled upper case)
1026 // Note that uc(<lowercase eszett>) => "SS", NOT <uppercase eszett>!
1027 // However, neither Windows nor Mac OSX converts these.
1028 // (or even have glyphs for <uppercase eszett>)
1029 { { FPL("\u00DF"), FPL("\u00DF") }, 0},
1030 { { FPL("\u1E9E"), FPL("\u1E9E") }, 0},
1031 { { FPL("\u00DF"), FPL("\u1E9E") }, -1},
1032 { { FPL("SS"), FPL("\u00DF") }, -1},
1033 { { FPL("SS"), FPL("\u1E9E") }, -1},
1034#if defined(OS_WIN) || defined(OS_MACOSX)
1035 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
1036 { { FPL("\u00E4\u00F6\u00FC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1037 { { FPL("\u00C4\u00D6\u00DC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1038 // C with circumflex: direct comparison, and upper case vs. lower case
1039 { { FPL("\u0109"), FPL("\u0109") }, 0},
1040 { { FPL("\u0108"), FPL("\u0109") }, 0},
1041 // Cyrillic letter SHA: direct comparison, and upper case vs. lower case
1042 { { FPL("\u0428"), FPL("\u0428") }, 0},
1043 { { FPL("\u0428"), FPL("\u0448") }, 0},
1044 // Greek letter DELTA: direct comparison, and upper case vs. lower case
1045 { { FPL("\u0394"), FPL("\u0394") }, 0},
1046 { { FPL("\u0394"), FPL("\u03B4") }, 0},
1047 // Japanese full-width A: direct comparison, and upper case vs. lower case
1048 // Note that full-width and standard characters are considered different.
1049 { { FPL("\uFF21"), FPL("\uFF21") }, 0},
1050 { { FPL("\uFF21"), FPL("\uFF41") }, 0},
1051 { { FPL("A"), FPL("\uFF21") }, -1},
1052 { { FPL("A"), FPL("\uFF41") }, -1},
1053 { { FPL("a"), FPL("\uFF21") }, -1},
1054 { { FPL("a"), FPL("\uFF41") }, -1},
1055#endif
1056#if defined(OS_MACOSX)
1057 // Codepoints > 0x1000
1058 // Georgian letter DON: direct comparison, and upper case vs. lower case
1059 { { FPL("\u10A3"), FPL("\u10A3") }, 0},
1060 { { FPL("\u10A3"), FPL("\u10D3") }, 0},
1061 // Combining characters vs. pre-composed characters, upper and lower case
1062 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1063 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("kuon") }, 1},
1064 { { FPL("kuon"), FPL("k\u0301u\u032Do\u0304\u0301n") }, -1},
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("K\u0301U\u032DO\u0304\u0301N"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1069 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E30\u1E76\u1E52n") }, 0},
1070 { { FPL("k\u0301u\u032Do\u0304\u0302n"), FPL("\u1E30\u1E76\u1E52n") }, 1},
1071#endif
1072 };
1073
1074 for (size_t i = 0; i < arraysize(cases); ++i) {
1075 FilePath::StringType s1(cases[i].inputs[0]);
1076 FilePath::StringType s2(cases[i].inputs[1]);
1077 int result = FilePath::CompareIgnoreCase(s1, s2);
1078 EXPECT_EQ(cases[i].expected, result) <<
1079 "i: " << i << ", s1: " << s1 << ", s2: " << s2;
1080 }
1081}
1082
cevans@chromium.org48dc54c2009-08-16 06:44:31 +09001083TEST_F(FilePathTest, ReferencesParent) {
1084 const struct UnaryBooleanTestData cases[] = {
1085 { FPL("."), false },
1086 { FPL(".."), true },
jschuh@chromium.orgab41a002013-05-17 11:27:46 +09001087 { FPL(".. "), true },
1088 { FPL(" .."), true },
1089 { FPL("..."), true },
cevans@chromium.org48dc54c2009-08-16 06:44:31 +09001090 { FPL("a.."), false },
1091 { FPL("..a"), false },
1092 { FPL("../"), true },
1093 { FPL("/.."), true },
1094 { FPL("/../"), true },
1095 { FPL("/a../"), false },
1096 { FPL("/..a/"), false },
1097 { FPL("//.."), true },
1098 { FPL("..//"), true },
1099 { FPL("//..//"), true },
1100 { FPL("a//..//c"), true },
1101 { FPL("../b/c"), true },
1102 { FPL("/../b/c"), true },
1103 { FPL("a/b/.."), true },
1104 { FPL("a/b/../"), true },
1105 { FPL("a/../c"), true },
1106 { FPL("a/b/c"), false },
1107 };
1108
1109 for (size_t i = 0; i < arraysize(cases); ++i) {
1110 FilePath input(cases[i].input);
1111 bool observed = input.ReferencesParent();
1112 EXPECT_EQ(cases[i].expected, observed) <<
1113 "i: " << i << ", input: " << input.value();
1114 }
1115}
tony@chromium.org65049b12010-06-25 14:33:17 +09001116
satorux@chromium.orgefba4172011-11-02 13:55:23 +09001117TEST_F(FilePathTest, FromUTF8Unsafe_And_AsUTF8Unsafe) {
1118 const struct UTF8TestData cases[] = {
1119 { FPL("foo.txt"), "foo.txt" },
1120 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them.
1121 { FPL("\u00E0\u00E8\u00F2.txt"), "\xC3\xA0\xC3\xA8\xC3\xB2.txt" },
1122 // Full-width "ABC".
1123 { FPL("\uFF21\uFF22\uFF23.txt"),
1124 "\xEF\xBC\xA1\xEF\xBC\xA2\xEF\xBC\xA3.txt" },
1125 };
1126
1127 for (size_t i = 0; i < arraysize(cases); ++i) {
1128 // Test FromUTF8Unsafe() works.
1129 FilePath from_utf8 = FilePath::FromUTF8Unsafe(cases[i].utf8);
1130 EXPECT_EQ(cases[i].native, from_utf8.value())
1131 << "i: " << i << ", input: " << cases[i].native;
1132 // Test AsUTF8Unsafe() works.
1133 FilePath from_native = FilePath(cases[i].native);
1134 EXPECT_EQ(cases[i].utf8, from_native.AsUTF8Unsafe())
1135 << "i: " << i << ", input: " << cases[i].native;
1136 // Test the two file paths are identical.
1137 EXPECT_EQ(from_utf8.value(), from_native.value());
1138 }
1139}
1140
aedla@chromium.org51127b92013-01-28 22:47:55 +09001141TEST_F(FilePathTest, ConstructWithNUL) {
1142 // Assert FPS() works.
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001143 ASSERT_EQ(3U, FPS("a\0b").length());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001144
1145 // Test constructor strips '\0'
1146 FilePath path(FPS("a\0b"));
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001147 EXPECT_EQ(1U, path.value().length());
1148 EXPECT_EQ(FPL("a"), path.value());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001149}
1150
1151TEST_F(FilePathTest, AppendWithNUL) {
1152 // Assert FPS() works.
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001153 ASSERT_EQ(3U, FPS("b\0b").length());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001154
1155 // Test Append() strips '\0'
1156 FilePath path(FPL("a"));
1157 path = path.Append(FPS("b\0b"));
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001158 EXPECT_EQ(3U, path.value().length());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001159#if defined(FILE_PATH_USES_WIN_SEPARATORS)
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001160 EXPECT_EQ(FPL("a\\b"), path.value());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001161#else
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#endif
1164}
1165
1166TEST_F(FilePathTest, ReferencesParentWithNUL) {
1167 // Assert FPS() works.
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001168 ASSERT_EQ(3U, FPS("..\0").length());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001169
1170 // Test ReferencesParent() doesn't break with "..\0"
1171 FilePath path(FPS("..\0"));
1172 EXPECT_TRUE(path.ReferencesParent());
1173}
1174
tony@chromium.org65049b12010-06-25 14:33:17 +09001175#if defined(FILE_PATH_USES_WIN_SEPARATORS)
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001176TEST_F(FilePathTest, NormalizePathSeparators) {
tony@chromium.org65049b12010-06-25 14:33:17 +09001177 const struct UnaryTestData cases[] = {
1178 { FPL("foo/bar"), FPL("foo\\bar") },
1179 { FPL("foo/bar\\betz"), FPL("foo\\bar\\betz") },
1180 { FPL("foo\\bar"), FPL("foo\\bar") },
1181 { FPL("foo\\bar/betz"), FPL("foo\\bar\\betz") },
1182 { FPL("foo"), FPL("foo") },
1183 // Trailing slashes don't automatically get stripped. That's what
1184 // StripTrailingSeparators() is for.
1185 { FPL("foo\\"), FPL("foo\\") },
1186 { FPL("foo/"), FPL("foo\\") },
1187 { FPL("foo/bar\\"), FPL("foo\\bar\\") },
1188 { FPL("foo\\bar/"), FPL("foo\\bar\\") },
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("c:/foo/bar/"), FPL("c:\\foo\\bar\\") },
1194 { FPL("/foo/bar/"), FPL("\\foo\\bar\\") },
1195 { FPL("\\foo\\bar\\"), FPL("\\foo\\bar\\") },
1196 { FPL("c:\\foo/bar"), FPL("c:\\foo\\bar") },
1197 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1198 { FPL("\\\\foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1199 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1200 // This method does not normalize the number of path separators.
1201 { FPL("foo\\\\bar"), FPL("foo\\\\bar") },
1202 { FPL("foo//bar"), FPL("foo\\\\bar") },
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 };
1210 for (size_t i = 0; i < arraysize(cases); ++i) {
1211 FilePath input(cases[i].input);
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001212 FilePath observed = input.NormalizePathSeparators();
tony@chromium.org65049b12010-06-25 14:33:17 +09001213 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
1214 "i: " << i << ", input: " << input.value();
1215 }
1216}
1217#endif
brettw@chromium.org82bcf512013-02-17 14:07:23 +09001218
brettw@chromium.org99b198e2013-04-12 14:17:15 +09001219TEST_F(FilePathTest, EndsWithSeparator) {
1220 const UnaryBooleanTestData cases[] = {
1221 { FPL(""), false },
1222 { FPL("/"), true },
1223 { FPL("foo/"), true },
1224 { FPL("bar"), false },
1225 { FPL("/foo/bar"), false },
1226 };
1227 for (size_t i = 0; i < arraysize(cases); ++i) {
1228 FilePath input = FilePath(cases[i].input).NormalizePathSeparators();
1229 EXPECT_EQ(cases[i].expected, input.EndsWithSeparator());
1230 }
1231}
1232
1233TEST_F(FilePathTest, AsEndingWithSeparator) {
1234 const UnaryTestData cases[] = {
1235 { FPL(""), FPL("") },
1236 { FPL("/"), FPL("/") },
1237 { FPL("foo"), FPL("foo/") },
1238 { FPL("foo/"), FPL("foo/") }
1239 };
1240 for (size_t i = 0; i < arraysize(cases); ++i) {
1241 FilePath input = FilePath(cases[i].input).NormalizePathSeparators();
1242 FilePath expected = FilePath(cases[i].expected).NormalizePathSeparators();
1243 EXPECT_EQ(expected.value(), input.AsEndingWithSeparator().value());
1244 }
1245}
1246
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09001247#if defined(OS_ANDROID)
1248TEST_F(FilePathTest, ContentUriTest) {
1249 const struct UnaryBooleanTestData cases[] = {
1250 { FPL("content://foo.bar"), true },
1251 { FPL("content://foo.bar/"), true },
1252 { FPL("content://foo/bar"), true },
1253 { FPL("CoNTenT://foo.bar"), true },
1254 { FPL("content://"), true },
1255 { FPL("content:///foo.bar"), true },
1256 { FPL("content://3foo/bar"), true },
1257 { FPL("content://_foo/bar"), true },
1258 { FPL(".. "), false },
1259 { FPL("foo.bar"), false },
1260 { FPL("content:foo.bar"), false },
1261 { FPL("content:/foo.ba"), false },
1262 { FPL("content:/dir/foo.bar"), false },
1263 { FPL("content: //foo.bar"), false },
1264 { FPL("content%2a%2f%2f"), false },
1265 };
1266
1267 for (size_t i = 0; i < arraysize(cases); ++i) {
1268 FilePath input(cases[i].input);
1269 bool observed = input.IsContentUri();
1270 EXPECT_EQ(cases[i].expected, observed) <<
1271 "i: " << i << ", input: " << input.value();
1272 }
1273}
1274#endif
1275
brettw@chromium.org82bcf512013-02-17 14:07:23 +09001276} // namespace base