blob: bc0e8432e0e1466581a37208d8b8d29e343b458b [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
arihc7614aae2015-08-13 08:45:48 +090013#if defined(OS_POSIX)
14#include "base/test/scoped_locale.h"
15#endif
16
mark@chromium.org6380e262008-10-04 03:21:47 +090017// This macro helps avoid wrapped lines in the test structs.
18#define FPL(x) FILE_PATH_LITERAL(x)
19
aedla@chromium.org51127b92013-01-28 22:47:55 +090020// This macro constructs strings which can contain NULs.
21#define FPS(x) FilePath::StringType(FPL(x), arraysize(FPL(x)) - 1)
22
brettw@chromium.org82bcf512013-02-17 14:07:23 +090023namespace base {
24
mark@chromium.org6380e262008-10-04 03:21:47 +090025struct UnaryTestData {
26 const FilePath::CharType* input;
27 const FilePath::CharType* expected;
28};
29
30struct UnaryBooleanTestData {
31 const FilePath::CharType* input;
32 bool expected;
33};
34
35struct BinaryTestData {
36 const FilePath::CharType* inputs[2];
37 const FilePath::CharType* expected;
38};
39
rafaelw@chromium.org465facc2009-06-25 06:28:30 +090040struct BinaryBooleanTestData {
41 const FilePath::CharType* inputs[2];
42 bool expected;
43};
44
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +090045struct BinaryIntTestData {
46 const FilePath::CharType* inputs[2];
47 int expected;
48};
49
satorux@chromium.orgefba4172011-11-02 13:55:23 +090050struct UTF8TestData {
51 const FilePath::CharType* native;
52 const char* utf8;
53};
54
aa@chromium.org76e5c632008-12-18 17:38:16 +090055// file_util winds up using autoreleased objects on the Mac, so this needs
56// to be a PlatformTest
dchengca87abb2014-12-23 11:56:47 +090057typedef PlatformTest FilePathTest;
aa@chromium.org76e5c632008-12-18 17:38:16 +090058
59TEST_F(FilePathTest, DirName) {
mark@chromium.org6380e262008-10-04 03:21:47 +090060 const struct UnaryTestData cases[] = {
61 { FPL(""), FPL(".") },
62 { FPL("aa"), FPL(".") },
63 { FPL("/aa/bb"), FPL("/aa") },
64 { FPL("/aa/bb/"), FPL("/aa") },
65 { FPL("/aa/bb//"), FPL("/aa") },
66 { FPL("/aa/bb/ccc"), FPL("/aa/bb") },
67 { FPL("/aa"), FPL("/") },
68 { FPL("/aa/"), FPL("/") },
69 { FPL("/"), FPL("/") },
70 { FPL("//"), FPL("//") },
71 { FPL("///"), FPL("/") },
72 { FPL("aa/"), FPL(".") },
73 { FPL("aa/bb"), FPL("aa") },
74 { FPL("aa/bb/"), FPL("aa") },
75 { FPL("aa/bb//"), FPL("aa") },
76 { FPL("aa//bb//"), FPL("aa") },
77 { FPL("aa//bb/"), FPL("aa") },
78 { FPL("aa//bb"), FPL("aa") },
79 { FPL("//aa/bb"), FPL("//aa") },
80 { FPL("//aa/"), FPL("//") },
81 { FPL("//aa"), FPL("//") },
82 { FPL("0:"), FPL(".") },
83 { FPL("@:"), FPL(".") },
84 { FPL("[:"), FPL(".") },
85 { FPL("`:"), FPL(".") },
86 { FPL("{:"), FPL(".") },
87 { FPL("\xB3:"), FPL(".") },
88 { FPL("\xC5:"), FPL(".") },
89#if defined(OS_WIN)
90 { FPL("\x0143:"), FPL(".") },
91#endif // OS_WIN
92#if defined(FILE_PATH_USES_DRIVE_LETTERS)
93 { FPL("c:"), FPL("c:") },
94 { FPL("C:"), FPL("C:") },
95 { FPL("A:"), FPL("A:") },
96 { FPL("Z:"), FPL("Z:") },
97 { FPL("a:"), FPL("a:") },
98 { FPL("z:"), FPL("z:") },
99 { FPL("c:aa"), FPL("c:") },
100 { FPL("c:/"), FPL("c:/") },
101 { FPL("c://"), FPL("c://") },
102 { FPL("c:///"), FPL("c:/") },
103 { FPL("c:/aa"), FPL("c:/") },
104 { FPL("c:/aa/"), FPL("c:/") },
105 { FPL("c:/aa/bb"), FPL("c:/aa") },
106 { FPL("c:aa/bb"), FPL("c:aa") },
107#endif // FILE_PATH_USES_DRIVE_LETTERS
108#if defined(FILE_PATH_USES_WIN_SEPARATORS)
109 { FPL("\\aa\\bb"), FPL("\\aa") },
110 { FPL("\\aa\\bb\\"), FPL("\\aa") },
111 { FPL("\\aa\\bb\\\\"), FPL("\\aa") },
112 { FPL("\\aa\\bb\\ccc"), FPL("\\aa\\bb") },
113 { FPL("\\aa"), FPL("\\") },
114 { FPL("\\aa\\"), FPL("\\") },
115 { FPL("\\"), FPL("\\") },
116 { FPL("\\\\"), FPL("\\\\") },
117 { FPL("\\\\\\"), FPL("\\") },
118 { FPL("aa\\"), FPL(".") },
119 { FPL("aa\\bb"), FPL("aa") },
120 { FPL("aa\\bb\\"), FPL("aa") },
121 { FPL("aa\\bb\\\\"), FPL("aa") },
122 { FPL("aa\\\\bb\\\\"), FPL("aa") },
123 { FPL("aa\\\\bb\\"), FPL("aa") },
124 { FPL("aa\\\\bb"), FPL("aa") },
125 { FPL("\\\\aa\\bb"), FPL("\\\\aa") },
126 { FPL("\\\\aa\\"), FPL("\\\\") },
127 { FPL("\\\\aa"), FPL("\\\\") },
128#if defined(FILE_PATH_USES_DRIVE_LETTERS)
129 { FPL("c:\\"), FPL("c:\\") },
130 { FPL("c:\\\\"), FPL("c:\\\\") },
131 { FPL("c:\\\\\\"), FPL("c:\\") },
132 { FPL("c:\\aa"), FPL("c:\\") },
133 { FPL("c:\\aa\\"), FPL("c:\\") },
134 { FPL("c:\\aa\\bb"), FPL("c:\\aa") },
135 { FPL("c:aa\\bb"), FPL("c:aa") },
136#endif // FILE_PATH_USES_DRIVE_LETTERS
137#endif // FILE_PATH_USES_WIN_SEPARATORS
138 };
139
140 for (size_t i = 0; i < arraysize(cases); ++i) {
141 FilePath input(cases[i].input);
142 FilePath observed = input.DirName();
143 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
144 "i: " << i << ", input: " << input.value();
145 }
146}
147
aa@chromium.org76e5c632008-12-18 17:38:16 +0900148TEST_F(FilePathTest, BaseName) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900149 const struct UnaryTestData cases[] = {
150 { FPL(""), FPL("") },
151 { FPL("aa"), FPL("aa") },
152 { FPL("/aa/bb"), FPL("bb") },
153 { FPL("/aa/bb/"), FPL("bb") },
154 { FPL("/aa/bb//"), FPL("bb") },
155 { FPL("/aa/bb/ccc"), FPL("ccc") },
156 { FPL("/aa"), FPL("aa") },
157 { FPL("/"), FPL("/") },
158 { FPL("//"), FPL("//") },
159 { FPL("///"), FPL("/") },
160 { FPL("aa/"), FPL("aa") },
161 { FPL("aa/bb"), FPL("bb") },
162 { FPL("aa/bb/"), FPL("bb") },
163 { FPL("aa/bb//"), FPL("bb") },
164 { FPL("aa//bb//"), FPL("bb") },
165 { FPL("aa//bb/"), FPL("bb") },
166 { FPL("aa//bb"), FPL("bb") },
167 { FPL("//aa/bb"), FPL("bb") },
168 { FPL("//aa/"), FPL("aa") },
169 { FPL("//aa"), FPL("aa") },
170 { FPL("0:"), FPL("0:") },
171 { FPL("@:"), FPL("@:") },
172 { FPL("[:"), FPL("[:") },
173 { FPL("`:"), FPL("`:") },
174 { FPL("{:"), FPL("{:") },
175 { FPL("\xB3:"), FPL("\xB3:") },
176 { FPL("\xC5:"), FPL("\xC5:") },
177#if defined(OS_WIN)
178 { FPL("\x0143:"), FPL("\x0143:") },
179#endif // OS_WIN
180#if defined(FILE_PATH_USES_DRIVE_LETTERS)
181 { FPL("c:"), FPL("") },
182 { FPL("C:"), FPL("") },
183 { FPL("A:"), FPL("") },
184 { FPL("Z:"), FPL("") },
185 { FPL("a:"), FPL("") },
186 { FPL("z:"), FPL("") },
187 { FPL("c:aa"), FPL("aa") },
188 { FPL("c:/"), FPL("/") },
189 { FPL("c://"), FPL("//") },
190 { FPL("c:///"), FPL("/") },
191 { FPL("c:/aa"), FPL("aa") },
192 { FPL("c:/aa/"), FPL("aa") },
193 { FPL("c:/aa/bb"), FPL("bb") },
194 { FPL("c:aa/bb"), FPL("bb") },
195#endif // FILE_PATH_USES_DRIVE_LETTERS
196#if defined(FILE_PATH_USES_WIN_SEPARATORS)
197 { FPL("\\aa\\bb"), FPL("bb") },
198 { FPL("\\aa\\bb\\"), FPL("bb") },
199 { FPL("\\aa\\bb\\\\"), FPL("bb") },
200 { FPL("\\aa\\bb\\ccc"), FPL("ccc") },
201 { FPL("\\aa"), FPL("aa") },
202 { FPL("\\"), FPL("\\") },
203 { FPL("\\\\"), FPL("\\\\") },
204 { FPL("\\\\\\"), FPL("\\") },
205 { FPL("aa\\"), FPL("aa") },
206 { FPL("aa\\bb"), FPL("bb") },
207 { FPL("aa\\bb\\"), FPL("bb") },
208 { FPL("aa\\bb\\\\"), FPL("bb") },
209 { FPL("aa\\\\bb\\\\"), FPL("bb") },
210 { FPL("aa\\\\bb\\"), FPL("bb") },
211 { FPL("aa\\\\bb"), FPL("bb") },
212 { FPL("\\\\aa\\bb"), FPL("bb") },
213 { FPL("\\\\aa\\"), FPL("aa") },
214 { FPL("\\\\aa"), FPL("aa") },
215#if defined(FILE_PATH_USES_DRIVE_LETTERS)
216 { FPL("c:\\"), FPL("\\") },
217 { FPL("c:\\\\"), FPL("\\\\") },
218 { FPL("c:\\\\\\"), FPL("\\") },
219 { FPL("c:\\aa"), FPL("aa") },
220 { FPL("c:\\aa\\"), FPL("aa") },
221 { FPL("c:\\aa\\bb"), FPL("bb") },
222 { FPL("c:aa\\bb"), FPL("bb") },
223#endif // FILE_PATH_USES_DRIVE_LETTERS
224#endif // FILE_PATH_USES_WIN_SEPARATORS
225 };
226
227 for (size_t i = 0; i < arraysize(cases); ++i) {
228 FilePath input(cases[i].input);
estade@chromium.orgd872d682009-01-06 08:59:36 +0900229 FilePath observed = input.BaseName();
230 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
mark@chromium.org6380e262008-10-04 03:21:47 +0900231 "i: " << i << ", input: " << input.value();
232 }
233}
234
aa@chromium.org76e5c632008-12-18 17:38:16 +0900235TEST_F(FilePathTest, Append) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900236 const struct BinaryTestData cases[] = {
237 { { FPL(""), FPL("cc") }, FPL("cc") },
238 { { FPL("."), FPL("ff") }, FPL("ff") },
239 { { FPL("/"), FPL("cc") }, FPL("/cc") },
240 { { FPL("/aa"), FPL("") }, FPL("/aa") },
241 { { FPL("/aa/"), FPL("") }, FPL("/aa") },
242 { { FPL("//aa"), FPL("") }, FPL("//aa") },
243 { { FPL("//aa/"), FPL("") }, FPL("//aa") },
244 { { FPL("//"), FPL("aa") }, FPL("//aa") },
245#if defined(FILE_PATH_USES_DRIVE_LETTERS)
246 { { FPL("c:"), FPL("a") }, FPL("c:a") },
247 { { FPL("c:"), FPL("") }, FPL("c:") },
248 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
249 { { FPL("c://"), FPL("a") }, FPL("c://a") },
250 { { FPL("c:///"), FPL("a") }, FPL("c:/a") },
251#endif // FILE_PATH_USES_DRIVE_LETTERS
252#if defined(FILE_PATH_USES_WIN_SEPARATORS)
253 // Append introduces the default separator character, so these test cases
254 // need to be defined with different expected results on platforms that use
255 // different default separator characters.
256 { { FPL("\\"), FPL("cc") }, FPL("\\cc") },
257 { { FPL("\\aa"), FPL("") }, FPL("\\aa") },
258 { { FPL("\\aa\\"), FPL("") }, FPL("\\aa") },
259 { { FPL("\\\\aa"), FPL("") }, FPL("\\\\aa") },
260 { { FPL("\\\\aa\\"), FPL("") }, FPL("\\\\aa") },
261 { { FPL("\\\\"), FPL("aa") }, FPL("\\\\aa") },
262 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb\\cc") },
263 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb\\cc") },
264 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb\\cc") },
265 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb\\cc") },
266 { { FPL("a/b"), FPL("c") }, FPL("a/b\\c") },
267 { { FPL("a/b/"), FPL("c") }, FPL("a/b\\c") },
268 { { FPL("//aa"), FPL("bb") }, FPL("//aa\\bb") },
269 { { FPL("//aa/"), FPL("bb") }, FPL("//aa\\bb") },
270 { { FPL("\\aa\\bb"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
271 { { FPL("\\aa\\bb\\"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
272 { { FPL("aa\\bb\\"), FPL("cc") }, FPL("aa\\bb\\cc") },
273 { { FPL("aa\\bb"), FPL("cc") }, FPL("aa\\bb\\cc") },
274 { { FPL("a\\b"), FPL("c") }, FPL("a\\b\\c") },
275 { { FPL("a\\b\\"), FPL("c") }, FPL("a\\b\\c") },
276 { { FPL("\\\\aa"), FPL("bb") }, FPL("\\\\aa\\bb") },
277 { { FPL("\\\\aa\\"), FPL("bb") }, FPL("\\\\aa\\bb") },
278#if defined(FILE_PATH_USES_DRIVE_LETTERS)
279 { { FPL("c:\\"), FPL("a") }, FPL("c:\\a") },
280 { { FPL("c:\\\\"), FPL("a") }, FPL("c:\\\\a") },
281 { { FPL("c:\\\\\\"), FPL("a") }, FPL("c:\\a") },
282 { { FPL("c:\\"), FPL("") }, FPL("c:\\") },
283 { { FPL("c:\\a"), FPL("b") }, FPL("c:\\a\\b") },
284 { { FPL("c:\\a\\"), FPL("b") }, FPL("c:\\a\\b") },
285#endif // FILE_PATH_USES_DRIVE_LETTERS
286#else // FILE_PATH_USES_WIN_SEPARATORS
287 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb/cc") },
288 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb/cc") },
289 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb/cc") },
290 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb/cc") },
291 { { FPL("a/b"), FPL("c") }, FPL("a/b/c") },
292 { { FPL("a/b/"), FPL("c") }, FPL("a/b/c") },
293 { { FPL("//aa"), FPL("bb") }, FPL("//aa/bb") },
294 { { FPL("//aa/"), FPL("bb") }, FPL("//aa/bb") },
295#if defined(FILE_PATH_USES_DRIVE_LETTERS)
296 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
297 { { FPL("c:/"), FPL("") }, FPL("c:/") },
298 { { FPL("c:/a"), FPL("b") }, FPL("c:/a/b") },
299 { { FPL("c:/a/"), FPL("b") }, FPL("c:/a/b") },
300#endif // FILE_PATH_USES_DRIVE_LETTERS
301#endif // FILE_PATH_USES_WIN_SEPARATORS
302 };
303
304 for (size_t i = 0; i < arraysize(cases); ++i) {
305 FilePath root(cases[i].inputs[0]);
306 FilePath::StringType leaf(cases[i].inputs[1]);
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900307 FilePath observed_str = root.Append(leaf);
308 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
309 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
310 FilePath observed_path = root.Append(FilePath(leaf));
311 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) <<
mark@chromium.org6380e262008-10-04 03:21:47 +0900312 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900313
314 // TODO(erikkay): It would be nice to have a unicode test append value to
315 // handle the case when AppendASCII is passed UTF8
316#if defined(OS_WIN)
brettw@chromium.org11f89b02010-08-18 08:05:28 +0900317 std::string ascii = WideToUTF8(leaf);
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900318#elif defined(OS_POSIX)
319 std::string ascii = leaf;
320#endif
321 observed_str = root.AppendASCII(ascii);
322 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
323 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
mark@chromium.org6380e262008-10-04 03:21:47 +0900324 }
325}
326
mark@chromium.org778d9cf2009-10-14 02:31:39 +0900327TEST_F(FilePathTest, StripTrailingSeparators) {
328 const struct UnaryTestData cases[] = {
329 { FPL(""), FPL("") },
330 { FPL("/"), FPL("/") },
331 { FPL("//"), FPL("//") },
332 { FPL("///"), FPL("/") },
333 { FPL("////"), FPL("/") },
334 { FPL("a/"), FPL("a") },
335 { FPL("a//"), FPL("a") },
336 { FPL("a///"), FPL("a") },
337 { FPL("a////"), FPL("a") },
338 { FPL("/a"), FPL("/a") },
339 { FPL("/a/"), FPL("/a") },
340 { FPL("/a//"), FPL("/a") },
341 { FPL("/a///"), FPL("/a") },
342 { FPL("/a////"), FPL("/a") },
343#if defined(FILE_PATH_USES_DRIVE_LETTERS)
344 { FPL("c:"), FPL("c:") },
345 { FPL("c:/"), FPL("c:/") },
346 { FPL("c://"), FPL("c://") },
347 { FPL("c:///"), FPL("c:/") },
348 { FPL("c:////"), FPL("c:/") },
349 { FPL("c:/a"), FPL("c:/a") },
350 { FPL("c:/a/"), FPL("c:/a") },
351 { FPL("c:/a//"), FPL("c:/a") },
352 { FPL("c:/a///"), FPL("c:/a") },
353 { FPL("c:/a////"), FPL("c:/a") },
354#endif // FILE_PATH_USES_DRIVE_LETTERS
355#if defined(FILE_PATH_USES_WIN_SEPARATORS)
356 { FPL("\\"), FPL("\\") },
357 { FPL("\\\\"), FPL("\\\\") },
358 { FPL("\\\\\\"), FPL("\\") },
359 { FPL("\\\\\\\\"), FPL("\\") },
360 { FPL("a\\"), FPL("a") },
361 { FPL("a\\\\"), FPL("a") },
362 { FPL("a\\\\\\"), FPL("a") },
363 { FPL("a\\\\\\\\"), FPL("a") },
364 { FPL("\\a"), FPL("\\a") },
365 { FPL("\\a\\"), FPL("\\a") },
366 { FPL("\\a\\\\"), FPL("\\a") },
367 { FPL("\\a\\\\\\"), FPL("\\a") },
368 { FPL("\\a\\\\\\\\"), FPL("\\a") },
369#if defined(FILE_PATH_USES_DRIVE_LETTERS)
370 { FPL("c:\\"), FPL("c:\\") },
371 { FPL("c:\\\\"), FPL("c:\\\\") },
372 { FPL("c:\\\\\\"), FPL("c:\\") },
373 { FPL("c:\\\\\\\\"), FPL("c:\\") },
374 { FPL("c:\\a"), FPL("c:\\a") },
375 { FPL("c:\\a\\"), FPL("c:\\a") },
376 { FPL("c:\\a\\\\"), FPL("c:\\a") },
377 { FPL("c:\\a\\\\\\"), FPL("c:\\a") },
378 { FPL("c:\\a\\\\\\\\"), FPL("c:\\a") },
379#endif // FILE_PATH_USES_DRIVE_LETTERS
380#endif // FILE_PATH_USES_WIN_SEPARATORS
381 };
382
383 for (size_t i = 0; i < arraysize(cases); ++i) {
384 FilePath input(cases[i].input);
385 FilePath observed = input.StripTrailingSeparators();
386 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
387 "i: " << i << ", input: " << input.value();
388 }
389}
390
aa@chromium.org76e5c632008-12-18 17:38:16 +0900391TEST_F(FilePathTest, IsAbsolute) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900392 const struct UnaryBooleanTestData cases[] = {
393 { FPL(""), false },
394 { FPL("a"), false },
395 { FPL("c:"), false },
396 { FPL("c:a"), false },
397 { FPL("a/b"), false },
398 { FPL("//"), true },
399 { FPL("//a"), true },
400 { FPL("c:a/b"), false },
401 { FPL("?:/a"), false },
402#if defined(FILE_PATH_USES_DRIVE_LETTERS)
403 { FPL("/"), false },
404 { FPL("/a"), false },
405 { FPL("/."), false },
406 { FPL("/.."), false },
407 { FPL("c:/"), true },
408 { FPL("c:/a"), true },
409 { FPL("c:/."), true },
410 { FPL("c:/.."), true },
411 { FPL("C:/a"), true },
412 { FPL("d:/a"), true },
413#else // FILE_PATH_USES_DRIVE_LETTERS
414 { FPL("/"), true },
415 { FPL("/a"), true },
416 { FPL("/."), true },
417 { FPL("/.."), true },
418 { FPL("c:/"), false },
419#endif // FILE_PATH_USES_DRIVE_LETTERS
420#if defined(FILE_PATH_USES_WIN_SEPARATORS)
421 { FPL("a\\b"), false },
422 { FPL("\\\\"), true },
423 { FPL("\\\\a"), true },
424 { FPL("a\\b"), false },
425 { FPL("\\\\"), true },
426 { FPL("//a"), true },
427 { FPL("c:a\\b"), false },
428 { FPL("?:\\a"), false },
429#if defined(FILE_PATH_USES_DRIVE_LETTERS)
430 { FPL("\\"), false },
431 { FPL("\\a"), false },
432 { FPL("\\."), false },
433 { FPL("\\.."), false },
434 { FPL("c:\\"), true },
435 { FPL("c:\\"), true },
436 { FPL("c:\\a"), true },
437 { FPL("c:\\."), true },
438 { FPL("c:\\.."), true },
439 { FPL("C:\\a"), true },
440 { FPL("d:\\a"), true },
441#else // FILE_PATH_USES_DRIVE_LETTERS
442 { FPL("\\"), true },
443 { FPL("\\a"), true },
444 { FPL("\\."), true },
445 { FPL("\\.."), true },
446 { FPL("c:\\"), false },
447#endif // FILE_PATH_USES_DRIVE_LETTERS
448#endif // FILE_PATH_USES_WIN_SEPARATORS
449 };
450
451 for (size_t i = 0; i < arraysize(cases); ++i) {
452 FilePath input(cases[i].input);
453 bool observed = input.IsAbsolute();
454 EXPECT_EQ(cases[i].expected, observed) <<
455 "i: " << i << ", input: " << input.value();
456 }
457}
aa@chromium.org76e5c632008-12-18 17:38:16 +0900458
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900459TEST_F(FilePathTest, PathComponentsTest) {
460 const struct UnaryTestData cases[] = {
461 { FPL("//foo/bar/baz/"), FPL("|//|foo|bar|baz")},
462 { FPL("///"), FPL("|/")},
463 { FPL("/foo//bar//baz/"), FPL("|/|foo|bar|baz")},
464 { FPL("/foo/bar/baz/"), FPL("|/|foo|bar|baz")},
465 { FPL("/foo/bar/baz//"), FPL("|/|foo|bar|baz")},
466 { FPL("/foo/bar/baz///"), FPL("|/|foo|bar|baz")},
467 { FPL("/foo/bar/baz"), FPL("|/|foo|bar|baz")},
468 { FPL("/foo/bar.bot/baz.txt"), FPL("|/|foo|bar.bot|baz.txt")},
469 { FPL("//foo//bar/baz"), FPL("|//|foo|bar|baz")},
470 { FPL("/"), FPL("|/")},
471 { FPL("foo"), FPL("|foo")},
472 { FPL(""), FPL("")},
473#if defined(FILE_PATH_USES_DRIVE_LETTERS)
474 { FPL("e:/foo"), FPL("|e:|/|foo")},
475 { FPL("e:/"), FPL("|e:|/")},
476 { FPL("e:"), FPL("|e:")},
477#endif // FILE_PATH_USES_DRIVE_LETTERS
478#if defined(FILE_PATH_USES_WIN_SEPARATORS)
479 { FPL("../foo"), FPL("|..|foo")},
480 { FPL("./foo"), FPL("|foo")},
481 { FPL("../foo/bar/"), FPL("|..|foo|bar") },
482 { FPL("\\\\foo\\bar\\baz\\"), FPL("|\\\\|foo|bar|baz")},
483 { FPL("\\\\\\"), FPL("|\\")},
484 { FPL("\\foo\\\\bar\\\\baz\\"), FPL("|\\|foo|bar|baz")},
485 { FPL("\\foo\\bar\\baz\\"), FPL("|\\|foo|bar|baz")},
486 { FPL("\\foo\\bar\\baz\\\\"), FPL("|\\|foo|bar|baz")},
487 { FPL("\\foo\\bar\\baz\\\\\\"), FPL("|\\|foo|bar|baz")},
488 { FPL("\\foo\\bar\\baz"), FPL("|\\|foo|bar|baz")},
489 { FPL("\\foo\\bar/baz\\\\\\"), FPL("|\\|foo|bar|baz")},
490 { FPL("/foo\\bar\\baz"), FPL("|/|foo|bar|baz")},
491 { FPL("\\foo\\bar.bot\\baz.txt"), FPL("|\\|foo|bar.bot|baz.txt")},
492 { FPL("\\\\foo\\\\bar\\baz"), FPL("|\\\\|foo|bar|baz")},
493 { FPL("\\"), FPL("|\\")},
494#endif // FILE_PATH_USES_WIN_SEPARATORS
495 };
496
497 for (size_t i = 0; i < arraysize(cases); ++i) {
498 FilePath input(cases[i].input);
499 std::vector<FilePath::StringType> comps;
500 input.GetComponents(&comps);
501
502 FilePath::StringType observed;
503 for (size_t j = 0; j < comps.size(); ++j) {
504 observed.append(FILE_PATH_LITERAL("|"), 1);
505 observed.append(comps[j]);
506 }
507 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed) <<
508 "i: " << i << ", input: " << input.value();
509 }
510}
511
512TEST_F(FilePathTest, IsParentTest) {
513 const struct BinaryBooleanTestData cases[] = {
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900514 { { FPL("/"), FPL("/foo/bar/baz") }, true},
515 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, true},
516 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, true},
517 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, true},
518 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, false},
519 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, false},
520 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, false},
521 { { FPL("/foo/bar"), FPL("/foo/bar") }, false},
522 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
523 { { FPL("foo/bar"), FPL("foo/bar/baz") }, true},
524 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, false},
525 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, false},
526 { { FPL(""), FPL("foo") }, false},
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900527#if defined(FILE_PATH_USES_DRIVE_LETTERS)
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900528 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, true},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900529 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, true},
530 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, true},
531 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, false},
532 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900533 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, true},
534 { { FPL("c:"), FPL("c:/foo/bar/baz") }, true},
535 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900536 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, false},
537 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900538 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900539 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, false},
540 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900541 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, false},
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900542#endif // FILE_PATH_USES_DRIVE_LETTERS
543#if defined(FILE_PATH_USES_WIN_SEPARATORS)
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900544 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, true},
545 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, true},
546 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, true},
547 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, true},
548 { { FPL(""), FPL("\\foo\\bar\\baz") }, false},
549 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, false},
550 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900551#endif // FILE_PATH_USES_WIN_SEPARATORS
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900552 };
553
554 for (size_t i = 0; i < arraysize(cases); ++i) {
555 FilePath parent(cases[i].inputs[0]);
556 FilePath child(cases[i].inputs[1]);
557
558 EXPECT_EQ(parent.IsParent(child), cases[i].expected) <<
mad@chromium.orgcc4f7b32009-10-21 00:27:21 +0900559 "i: " << i << ", parent: " << parent.value() << ", child: " <<
560 child.value();
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900561 }
562}
563
mark@chromium.org752010c2009-09-24 05:32:41 +0900564TEST_F(FilePathTest, AppendRelativePathTest) {
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900565 const struct BinaryTestData cases[] = {
mark@chromium.org752010c2009-09-24 05:32:41 +0900566#if defined(FILE_PATH_USES_WIN_SEPARATORS)
567 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo\\bar\\baz")},
568#else // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900569 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo/bar/baz")},
mark@chromium.org752010c2009-09-24 05:32:41 +0900570#endif // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900571 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, FPL("baz")},
572 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, FPL("baz")},
573 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, FPL("baz")},
574 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, FPL("")},
575 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, FPL("")},
576 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, FPL("")},
577 { { FPL("/foo/bar"), FPL("/foo/bar") }, FPL("")},
578 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, FPL("")},
579 { { FPL("foo/bar"), FPL("foo/bar/baz") }, FPL("baz")},
580 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, FPL("")},
581 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, FPL("")},
582 { { FPL(""), FPL("foo") }, FPL("")},
583#if defined(FILE_PATH_USES_DRIVE_LETTERS)
584 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, FPL("baz")},
585 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, FPL("baz")},
586 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, FPL("baz")},
587 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, FPL("")},
588 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, FPL("")},
mark@chromium.org752010c2009-09-24 05:32:41 +0900589#if defined(FILE_PATH_USES_WIN_SEPARATORS)
590 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
591 // TODO(akalin): Figure out how to handle the corner case in the
592 // commented-out test case below. Appending to an empty path gives
593 // /foo\bar\baz but appending to a nonempty path "blah" gives
594 // blah\foo\bar\baz.
595 // { { FPL("c:"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
596#endif // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900597 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
598 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, FPL("")},
599 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
600 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, FPL("")},
601 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, FPL("")},
602 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, FPL("")},
603 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, FPL("")},
604#endif // FILE_PATH_USES_DRIVE_LETTERS
605#if defined(FILE_PATH_USES_WIN_SEPARATORS)
606 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
607 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
608 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, FPL("baz")},
609 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, FPL("foo\\bar\\baz")},
610 { { FPL(""), FPL("\\foo\\bar\\baz") }, FPL("")},
611 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, FPL("")},
612 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, FPL("")},
613#endif // FILE_PATH_USES_WIN_SEPARATORS
614 };
615
616 const FilePath base(FPL("blah"));
617
618 for (size_t i = 0; i < arraysize(cases); ++i) {
619 FilePath parent(cases[i].inputs[0]);
620 FilePath child(cases[i].inputs[1]);
621 {
622 FilePath result;
623 bool success = parent.AppendRelativePath(child, &result);
624 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
625 "i: " << i << ", parent: " << parent.value() << ", child: " <<
626 child.value();
627 EXPECT_STREQ(cases[i].expected, result.value().c_str()) <<
628 "i: " << i << ", parent: " << parent.value() << ", child: " <<
629 child.value();
630 }
631 {
632 FilePath result(base);
633 bool success = parent.AppendRelativePath(child, &result);
634 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
635 "i: " << i << ", parent: " << parent.value() << ", child: " <<
636 child.value();
637 EXPECT_EQ(base.Append(cases[i].expected).value(), result.value()) <<
638 "i: " << i << ", parent: " << parent.value() << ", child: " <<
639 child.value();
640 }
641 }
642}
643
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900644TEST_F(FilePathTest, EqualityTest) {
645 const struct BinaryBooleanTestData cases[] = {
646 { { FPL("/foo/bar/baz"), FPL("/foo/bar/baz") }, true},
647 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, false},
648 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
649 { { FPL("//foo/bar/"), FPL("//foo/bar/") }, true},
650 { { FPL("/foo/bar"), FPL("/foo2/bar") }, false},
651 { { FPL("/foo/bar.txt"), FPL("/foo/bar") }, false},
652 { { FPL("foo/bar"), FPL("foo/bar") }, true},
653 { { FPL("foo/bar"), FPL("foo/bar/baz") }, false},
654 { { FPL(""), FPL("foo") }, false},
655#if defined(FILE_PATH_USES_DRIVE_LETTERS)
656 { { FPL("c:/foo/bar"), FPL("c:/foo/bar") }, true},
657 { { FPL("E:/foo/bar"), FPL("e:/foo/bar") }, true},
658 { { FPL("f:/foo/bar"), FPL("F:/foo/bar") }, true},
659 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar") }, false},
660 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar") }, false},
661 { { FPL("c:/"), FPL("c:/") }, true},
662 { { FPL("c:"), FPL("c:") }, true},
663 { { FPL("c:/foo/bar"), FPL("d:/foo/bar") }, false},
664 { { FPL("c:/foo/bar"), FPL("D:/foo/bar") }, false},
665 { { FPL("C:/foo/bar"), FPL("d:/foo/bar") }, false},
666 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar") }, false},
667#endif // FILE_PATH_USES_DRIVE_LETTERS
668#if defined(FILE_PATH_USES_WIN_SEPARATORS)
669 { { FPL("\\foo\\bar"), FPL("\\foo\\bar") }, true},
670 { { FPL("\\foo/bar"), FPL("\\foo/bar") }, true},
tony@chromium.org65049b12010-06-25 14:33:17 +0900671 { { FPL("\\foo/bar"), FPL("\\foo\\bar") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900672 { { FPL("\\"), FPL("\\") }, true},
673 { { FPL("\\"), FPL("/") }, false},
674 { { FPL(""), FPL("\\") }, false},
675 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar") }, false},
676 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2") }, false},
677#if defined(FILE_PATH_USES_DRIVE_LETTERS)
678 { { FPL("c:\\foo\\bar"), FPL("c:\\foo\\bar") }, true},
679 { { FPL("E:\\foo\\bar"), FPL("e:\\foo\\bar") }, true},
680 { { FPL("f:\\foo\\bar"), FPL("F:\\foo/bar") }, false},
681#endif // FILE_PATH_USES_DRIVE_LETTERS
682#endif // FILE_PATH_USES_WIN_SEPARATORS
683 };
684
685 for (size_t i = 0; i < arraysize(cases); ++i) {
686 FilePath a(cases[i].inputs[0]);
687 FilePath b(cases[i].inputs[1]);
688
689 EXPECT_EQ(a == b, cases[i].expected) <<
690 "equality i: " << i << ", a: " << a.value() << ", b: " <<
691 b.value();
692 }
693
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900694 for (size_t i = 0; i < arraysize(cases); ++i) {
695 FilePath a(cases[i].inputs[0]);
696 FilePath b(cases[i].inputs[1]);
697
698 EXPECT_EQ(a != b, !cases[i].expected) <<
699 "inequality i: " << i << ", a: " << a.value() << ", b: " <<
700 b.value();
701 }
702}
703
erikkay@google.com985d4202009-01-10 03:26:19 +0900704TEST_F(FilePathTest, Extension) {
705 FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
706
707 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
estade@chromium.org99a42e72010-07-28 06:24:39 +0900708 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.Extension());
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900709 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.FinalExtension());
erikkay@google.com985d4202009-01-10 03:26:19 +0900710
711 FilePath base = jpg.BaseName().RemoveExtension();
estade@chromium.org99a42e72010-07-28 06:24:39 +0900712 EXPECT_EQ(FILE_PATH_LITERAL("foo"), base.value());
erikkay@google.com985d4202009-01-10 03:26:19 +0900713
714 FilePath path_no_ext = base_dir.Append(base);
estade@chromium.org99a42e72010-07-28 06:24:39 +0900715 EXPECT_EQ(path_no_ext.value(), jpg.RemoveExtension().value());
erikkay@google.com985d4202009-01-10 03:26:19 +0900716
717 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value());
estade@chromium.org99a42e72010-07-28 06:24:39 +0900718 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.Extension());
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900719 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.FinalExtension());
erikkay@google.com985d4202009-01-10 03:26:19 +0900720}
721
722TEST_F(FilePathTest, Extension2) {
723 const struct UnaryTestData cases[] = {
724#if defined(FILE_PATH_USES_WIN_SEPARATORS)
725 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
726 { FPL("C:\\a\\b\\c."), FPL(".") },
727 { FPL("C:\\a\\b\\c"), FPL("") },
728 { FPL("C:\\a\\b\\"), FPL("") },
729 { FPL("C:\\a\\b.\\"), FPL(".") },
730 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
731 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
732 { FPL("C:\\foo.bar\\.."), FPL("") },
733 { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
734#endif
735 { FPL("/foo/bar/baz.ext"), FPL(".ext") },
736 { FPL("/foo/bar/baz."), FPL(".") },
737 { FPL("/foo/bar/baz.."), FPL(".") },
738 { FPL("/foo/bar/baz"), FPL("") },
739 { FPL("/foo/bar/"), FPL("") },
740 { FPL("/foo/bar./"), FPL(".") },
741 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900742 { FPL("/subversion-1.6.12.zip"), FPL(".zip") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900743 { FPL("/foo.12345.gz"), FPL(".gz") },
744 { FPL("/foo..gz"), FPL(".gz") },
erikkay@google.com985d4202009-01-10 03:26:19 +0900745 { FPL("."), FPL("") },
746 { FPL(".."), FPL("") },
747 { FPL("./foo"), FPL("") },
748 { FPL("./foo.ext"), FPL(".ext") },
749 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
750 { FPL("/foo.bar////"), FPL(".bar") },
751 { FPL("/foo.bar/.."), FPL("") },
752 { FPL("/foo.bar/..////"), FPL("") },
aa@chromium.orgf8395de2012-08-26 21:11:10 +0900753 { FPL("/foo.1234.luser.js"), FPL(".js") },
754 { FPL("/user.js"), FPL(".js") },
erikkay@google.com985d4202009-01-10 03:26:19 +0900755 };
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900756 const struct UnaryTestData double_extension_cases[] = {
757 { FPL("/foo.tar.gz"), FPL(".tar.gz") },
758 { FPL("/foo.tar.Z"), FPL(".tar.Z") },
759 { FPL("/foo.tar.bz2"), FPL(".tar.bz2") },
760 { FPL("/foo.1234.gz"), FPL(".1234.gz") },
761 { FPL("/foo.1234.tar.gz"), FPL(".tar.gz") },
762 { FPL("/foo.tar.tar.gz"), FPL(".tar.gz") },
763 { FPL("/foo.tar.gz.gz"), FPL(".gz.gz") },
764 { FPL("/foo.1234.user.js"), FPL(".user.js") },
765 { FPL("foo.user.js"), FPL(".user.js") },
arun87.kumarf0c7ccf2014-10-09 13:50:44 +0900766 { FPL("/foo.tar.bz"), FPL(".tar.bz") },
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900767 };
erikkay@google.com985d4202009-01-10 03:26:19 +0900768 for (unsigned int i = 0; i < arraysize(cases); ++i) {
769 FilePath path(cases[i].input);
770 FilePath::StringType extension = path.Extension();
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900771 FilePath::StringType final_extension = path.FinalExtension();
pkasting9043c4e2014-10-02 07:18:43 +0900772 EXPECT_STREQ(cases[i].expected, extension.c_str())
773 << "i: " << i << ", path: " << path.value();
774 EXPECT_STREQ(cases[i].expected, final_extension.c_str())
775 << "i: " << i << ", path: " << path.value();
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900776 }
777 for (unsigned int i = 0; i < arraysize(double_extension_cases); ++i) {
pkasting9043c4e2014-10-02 07:18:43 +0900778 FilePath path(double_extension_cases[i].input);
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900779 FilePath::StringType extension = path.Extension();
pkasting9043c4e2014-10-02 07:18:43 +0900780 EXPECT_STREQ(double_extension_cases[i].expected, extension.c_str())
781 << "i: " << i << ", path: " << path.value();
erikkay@google.com985d4202009-01-10 03:26:19 +0900782 }
783}
784
785TEST_F(FilePathTest, InsertBeforeExtension) {
786 const struct BinaryTestData cases[] = {
787 { { FPL(""), FPL("") }, FPL("") },
788 { { FPL(""), FPL("txt") }, FPL("") },
789 { { FPL("."), FPL("txt") }, FPL("") },
790 { { FPL(".."), FPL("txt") }, FPL("") },
791 { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
792 { { FPL("."), FPL("") }, FPL(".") },
793 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
794 { { FPL("foo"), FPL("txt") }, FPL("footxt") },
795 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
796 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
797 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
798 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
799 { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
800 { { FPL("foo"), FPL("") }, FPL("foo") },
801 { { FPL("foo"), FPL(".") }, FPL("foo.") },
802 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
803 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
804#if defined(FILE_PATH_USES_WIN_SEPARATORS)
805 { { FPL("\\"), FPL("") }, FPL("\\") },
806 { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
807 { { FPL("\\."), FPL("txt") }, FPL("") },
808 { { FPL("\\.."), FPL("txt") }, FPL("") },
809 { { FPL("\\."), FPL("") }, FPL("\\.") },
810 { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
811 FPL("C:\\bar\\footxt.dll") },
812 { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
813 FPL("C:\\bar.baz\\foodlltxt") },
814 { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
815 FPL("C:\\bar.baz\\footxt.dll") },
816 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
817 FPL("C:\\bar.baz\\foo.dlltxt.exe") },
818 { { FPL("C:\\bar.baz\\foo"), FPL("") },
819 FPL("C:\\bar.baz\\foo") },
820 { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
821 FPL("C:\\bar.baz\\foo.exe") },
822 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
823 FPL("C:\\bar.baz\\foo.dll.exe") },
824 { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
825 FPL("C:\\bar\\baz\\foo (1).exe") },
826 { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
827 { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
828#endif
829 { { FPL("/"), FPL("") }, FPL("/") },
830 { { FPL("/"), FPL("txt") }, FPL("/txt") },
831 { { FPL("/."), FPL("txt") }, FPL("") },
832 { { FPL("/.."), FPL("txt") }, FPL("") },
833 { { FPL("/."), FPL("") }, FPL("/.") },
834 { { FPL("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
835 { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
836 { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
837 { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
838 FPL("/bar.baz/foo.dlltxt.exe") },
839 { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
840 { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
841 { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
842 { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
843 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
844 };
845 for (unsigned int i = 0; i < arraysize(cases); ++i) {
846 FilePath path(cases[i].inputs[0]);
847 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
848 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
849 ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
850 }
851}
852
estade@chromium.org99a42e72010-07-28 06:24:39 +0900853TEST_F(FilePathTest, RemoveExtension) {
854 const struct UnaryTestData cases[] = {
855 { FPL(""), FPL("") },
856 { FPL("."), FPL(".") },
857 { FPL(".."), FPL("..") },
858 { FPL("foo.dll"), FPL("foo") },
859 { FPL("./foo.dll"), FPL("./foo") },
860 { FPL("foo..dll"), FPL("foo.") },
861 { FPL("foo"), FPL("foo") },
862 { FPL("foo."), FPL("foo") },
863 { FPL("foo.."), FPL("foo.") },
864 { FPL("foo.baz.dll"), FPL("foo.baz") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900865#if defined(FILE_PATH_USES_WIN_SEPARATORS)
866 { FPL("C:\\foo.bar\\foo"), FPL("C:\\foo.bar\\foo") },
867 { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") },
868#endif
869 { FPL("/foo.bar/foo"), FPL("/foo.bar/foo") },
870 { FPL("/foo.bar/..////"), FPL("/foo.bar/..////") },
871 };
872 for (unsigned int i = 0; i < arraysize(cases); ++i) {
873 FilePath path(cases[i].input);
874 FilePath removed = path.RemoveExtension();
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900875 FilePath removed_final = path.RemoveFinalExtension();
estade@chromium.org99a42e72010-07-28 06:24:39 +0900876 EXPECT_EQ(cases[i].expected, removed.value()) << "i: " << i <<
877 ", path: " << path.value();
davidben@chromium.org64a720a2013-12-10 02:06:52 +0900878 EXPECT_EQ(cases[i].expected, removed_final.value()) << "i: " << i <<
879 ", path: " << path.value();
880 }
881 {
882 FilePath path(FPL("foo.tar.gz"));
883 FilePath removed = path.RemoveExtension();
884 FilePath removed_final = path.RemoveFinalExtension();
885 EXPECT_EQ(FPL("foo"), removed.value()) << ", path: " << path.value();
886 EXPECT_EQ(FPL("foo.tar"), removed_final.value()) << ", path: "
887 << path.value();
estade@chromium.org99a42e72010-07-28 06:24:39 +0900888 }
889}
890
erikkay@google.com985d4202009-01-10 03:26:19 +0900891TEST_F(FilePathTest, ReplaceExtension) {
892 const struct BinaryTestData cases[] = {
893 { { FPL(""), FPL("") }, FPL("") },
894 { { FPL(""), FPL("txt") }, FPL("") },
895 { { FPL("."), FPL("txt") }, FPL("") },
896 { { FPL(".."), FPL("txt") }, FPL("") },
897 { { FPL("."), FPL("") }, FPL("") },
898 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900899 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.txt") },
erikkay@google.com985d4202009-01-10 03:26:19 +0900900 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
901 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
902 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
903 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
904 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
905 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
906 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
907 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
908 { { FPL("foo.dll"), FPL("") }, FPL("foo") },
909 { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
910 { { FPL("foo"), FPL("") }, FPL("foo") },
911 { { FPL("foo"), FPL(".") }, FPL("foo") },
912 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
913 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
914#if defined(FILE_PATH_USES_WIN_SEPARATORS)
915 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
916 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
917#endif
918 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
919 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
920 };
921 for (unsigned int i = 0; i < arraysize(cases); ++i) {
922 FilePath path(cases[i].inputs[0]);
923 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
924 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
925 ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
926 }
927}
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900928
rdevlin.cronin@chromium.org9a980f72012-04-25 10:53:44 +0900929TEST_F(FilePathTest, AddExtension) {
930 const struct BinaryTestData cases[] = {
931 { { FPL(""), FPL("") }, FPL("") },
932 { { FPL(""), FPL("txt") }, FPL("") },
933 { { FPL("."), FPL("txt") }, FPL("") },
934 { { FPL(".."), FPL("txt") }, FPL("") },
935 { { FPL("."), FPL("") }, FPL("") },
936 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.dll.txt") },
937 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.dll.txt") },
938 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..dll.txt") },
939 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.dll.txt") },
940 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
941 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
942 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
943 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
944 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.dll.txt") },
945 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.dll.txt") },
946 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
947 { { FPL("foo.dll"), FPL(".") }, FPL("foo.dll") },
948 { { FPL("foo"), FPL("") }, FPL("foo") },
949 { { FPL("foo"), FPL(".") }, FPL("foo") },
950 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
951 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz.dll") },
952#if defined(FILE_PATH_USES_WIN_SEPARATORS)
953 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
954 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
955#endif
956 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
957 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
958 };
959 for (unsigned int i = 0; i < arraysize(cases); ++i) {
960 FilePath path(cases[i].inputs[0]);
961 FilePath added = path.AddExtension(cases[i].inputs[1]);
962 EXPECT_EQ(cases[i].expected, added.value()) << "i: " << i <<
963 ", path: " << path.value() << ", add: " << cases[i].inputs[1];
964 }
965}
966
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900967TEST_F(FilePathTest, MatchesExtension) {
968 const struct BinaryBooleanTestData cases[] = {
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900969 { { FPL("foo"), FPL("") }, true},
970 { { FPL("foo"), FPL(".") }, false},
971 { { FPL("foo."), FPL("") }, false},
972 { { FPL("foo."), FPL(".") }, true},
973 { { FPL("foo.txt"), FPL(".dll") }, false},
974 { { FPL("foo.txt"), FPL(".txt") }, true},
975 { { FPL("foo.txt.dll"), FPL(".txt") }, false},
976 { { FPL("foo.txt.dll"), FPL(".dll") }, true},
977 { { FPL("foo.TXT"), FPL(".txt") }, true},
978 { { FPL("foo.txt"), FPL(".TXT") }, true},
979 { { FPL("foo.tXt"), FPL(".txt") }, true},
980 { { FPL("foo.txt"), FPL(".tXt") }, true},
981 { { FPL("foo.tXt"), FPL(".TXT") }, true},
982 { { FPL("foo.tXt"), FPL(".tXt") }, true},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900983#if defined(FILE_PATH_USES_DRIVE_LETTERS)
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900984 { { FPL("c:/foo.txt.dll"), FPL(".txt") }, false},
985 { { FPL("c:/foo.txt"), FPL(".txt") }, true},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900986#endif // FILE_PATH_USES_DRIVE_LETTERS
987#if defined(FILE_PATH_USES_WIN_SEPARATORS)
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900988 { { FPL("c:\\bar\\foo.txt.dll"), FPL(".txt") }, false},
989 { { FPL("c:\\bar\\foo.txt"), FPL(".txt") }, true},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900990#endif // FILE_PATH_USES_DRIVE_LETTERS
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900991 { { FPL("/bar/foo.txt.dll"), FPL(".txt") }, false},
992 { { FPL("/bar/foo.txt"), FPL(".txt") }, true},
993#if defined(OS_WIN) || defined(OS_MACOSX)
994 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
995 { { FPL("foo.\u00E4\u00F6\u00FC"), FPL(".\u00E4\u00F6\u00FC") }, true},
996 { { FPL("foo.\u00C4\u00D6\u00DC"), FPL(".\u00E4\u00F6\u00FC") }, true},
997 // C with circumflex: direct comparison, and upper case vs. lower case
998 { { FPL("foo.\u0109"), FPL(".\u0109") }, true},
999 { { FPL("foo.\u0108"), FPL(".\u0109") }, true},
1000#endif
avi@chromium.org8c3a9b32009-06-30 05:31:29 +09001001 };
1002
1003 for (size_t i = 0; i < arraysize(cases); ++i) {
1004 FilePath path(cases[i].inputs[0]);
1005 FilePath::StringType ext(cases[i].inputs[1]);
1006
1007 EXPECT_EQ(cases[i].expected, path.MatchesExtension(ext)) <<
1008 "i: " << i << ", path: " << path.value() << ", ext: " << ext;
1009 }
1010}
cevans@chromium.org48dc54c2009-08-16 06:44:31 +09001011
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +09001012TEST_F(FilePathTest, CompareIgnoreCase) {
1013 const struct BinaryIntTestData cases[] = {
1014 { { FPL("foo"), FPL("foo") }, 0},
1015 { { FPL("FOO"), FPL("foo") }, 0},
1016 { { FPL("foo.ext"), FPL("foo.ext") }, 0},
1017 { { FPL("FOO.EXT"), FPL("foo.ext") }, 0},
1018 { { FPL("Foo.Ext"), FPL("foo.ext") }, 0},
1019 { { FPL("foO"), FPL("foo") }, 0},
1020 { { FPL("foo"), FPL("foO") }, 0},
1021 { { FPL("fOo"), FPL("foo") }, 0},
1022 { { FPL("foo"), FPL("fOo") }, 0},
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 { { FPL("bar"), FPL("FOO") }, -1},
1028 { { FPL("foo"), FPL("BAR") }, 1},
1029 { { FPL("BAR"), FPL("FOO") }, -1},
1030 { { FPL("FOO"), FPL("BAR") }, 1},
1031 // German "Eszett" (lower case and the new-fangled upper case)
1032 // Note that uc(<lowercase eszett>) => "SS", NOT <uppercase eszett>!
1033 // However, neither Windows nor Mac OSX converts these.
1034 // (or even have glyphs for <uppercase eszett>)
1035 { { FPL("\u00DF"), FPL("\u00DF") }, 0},
1036 { { FPL("\u1E9E"), FPL("\u1E9E") }, 0},
1037 { { FPL("\u00DF"), FPL("\u1E9E") }, -1},
1038 { { FPL("SS"), FPL("\u00DF") }, -1},
1039 { { FPL("SS"), FPL("\u1E9E") }, -1},
1040#if defined(OS_WIN) || defined(OS_MACOSX)
1041 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
1042 { { FPL("\u00E4\u00F6\u00FC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1043 { { FPL("\u00C4\u00D6\u00DC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1044 // C with circumflex: direct comparison, and upper case vs. lower case
1045 { { FPL("\u0109"), FPL("\u0109") }, 0},
1046 { { FPL("\u0108"), FPL("\u0109") }, 0},
1047 // Cyrillic letter SHA: direct comparison, and upper case vs. lower case
1048 { { FPL("\u0428"), FPL("\u0428") }, 0},
1049 { { FPL("\u0428"), FPL("\u0448") }, 0},
1050 // Greek letter DELTA: direct comparison, and upper case vs. lower case
1051 { { FPL("\u0394"), FPL("\u0394") }, 0},
1052 { { FPL("\u0394"), FPL("\u03B4") }, 0},
1053 // Japanese full-width A: direct comparison, and upper case vs. lower case
1054 // Note that full-width and standard characters are considered different.
1055 { { FPL("\uFF21"), FPL("\uFF21") }, 0},
1056 { { FPL("\uFF21"), FPL("\uFF41") }, 0},
1057 { { FPL("A"), FPL("\uFF21") }, -1},
1058 { { FPL("A"), FPL("\uFF41") }, -1},
1059 { { FPL("a"), FPL("\uFF21") }, -1},
1060 { { FPL("a"), FPL("\uFF41") }, -1},
1061#endif
1062#if defined(OS_MACOSX)
1063 // Codepoints > 0x1000
1064 // Georgian letter DON: direct comparison, and upper case vs. lower case
1065 { { FPL("\u10A3"), FPL("\u10A3") }, 0},
1066 { { FPL("\u10A3"), FPL("\u10D3") }, 0},
1067 // Combining characters vs. pre-composed characters, upper and lower case
1068 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1069 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("kuon") }, 1},
1070 { { FPL("kuon"), FPL("k\u0301u\u032Do\u0304\u0301n") }, -1},
1071 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("KUON") }, 1},
1072 { { FPL("KUON"), FPL("K\u0301U\u032DO\u0304\u0301N") }, -1},
1073 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("KUON") }, 1},
1074 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1075 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E30\u1E76\u1E52n") }, 0},
1076 { { FPL("k\u0301u\u032Do\u0304\u0302n"), FPL("\u1E30\u1E76\u1E52n") }, 1},
1077#endif
1078 };
1079
1080 for (size_t i = 0; i < arraysize(cases); ++i) {
1081 FilePath::StringType s1(cases[i].inputs[0]);
1082 FilePath::StringType s2(cases[i].inputs[1]);
1083 int result = FilePath::CompareIgnoreCase(s1, s2);
1084 EXPECT_EQ(cases[i].expected, result) <<
1085 "i: " << i << ", s1: " << s1 << ", s2: " << s2;
1086 }
1087}
1088
cevans@chromium.org48dc54c2009-08-16 06:44:31 +09001089TEST_F(FilePathTest, ReferencesParent) {
1090 const struct UnaryBooleanTestData cases[] = {
1091 { FPL("."), false },
1092 { FPL(".."), true },
jschuh@chromium.orgab41a002013-05-17 11:27:46 +09001093 { FPL(".. "), true },
1094 { FPL(" .."), true },
1095 { FPL("..."), true },
cevans@chromium.org48dc54c2009-08-16 06:44:31 +09001096 { FPL("a.."), false },
1097 { FPL("..a"), false },
1098 { FPL("../"), true },
1099 { FPL("/.."), true },
1100 { FPL("/../"), true },
1101 { FPL("/a../"), false },
1102 { FPL("/..a/"), false },
1103 { FPL("//.."), true },
1104 { FPL("..//"), true },
1105 { FPL("//..//"), true },
1106 { FPL("a//..//c"), true },
1107 { FPL("../b/c"), true },
1108 { FPL("/../b/c"), true },
1109 { FPL("a/b/.."), true },
1110 { FPL("a/b/../"), true },
1111 { FPL("a/../c"), true },
1112 { FPL("a/b/c"), false },
1113 };
1114
1115 for (size_t i = 0; i < arraysize(cases); ++i) {
1116 FilePath input(cases[i].input);
1117 bool observed = input.ReferencesParent();
1118 EXPECT_EQ(cases[i].expected, observed) <<
1119 "i: " << i << ", input: " << input.value();
1120 }
1121}
tony@chromium.org65049b12010-06-25 14:33:17 +09001122
satorux@chromium.orgefba4172011-11-02 13:55:23 +09001123TEST_F(FilePathTest, FromUTF8Unsafe_And_AsUTF8Unsafe) {
1124 const struct UTF8TestData cases[] = {
1125 { FPL("foo.txt"), "foo.txt" },
1126 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them.
1127 { FPL("\u00E0\u00E8\u00F2.txt"), "\xC3\xA0\xC3\xA8\xC3\xB2.txt" },
1128 // Full-width "ABC".
1129 { FPL("\uFF21\uFF22\uFF23.txt"),
1130 "\xEF\xBC\xA1\xEF\xBC\xA2\xEF\xBC\xA3.txt" },
1131 };
1132
arihc7614aae2015-08-13 08:45:48 +09001133#if !defined(SYSTEM_NATIVE_UTF8) && defined(OS_LINUX)
1134 ScopedLocale locale("en_US.UTF-8");
1135#endif
1136
satorux@chromium.orgefba4172011-11-02 13:55:23 +09001137 for (size_t i = 0; i < arraysize(cases); ++i) {
1138 // Test FromUTF8Unsafe() works.
1139 FilePath from_utf8 = FilePath::FromUTF8Unsafe(cases[i].utf8);
1140 EXPECT_EQ(cases[i].native, from_utf8.value())
1141 << "i: " << i << ", input: " << cases[i].native;
1142 // Test AsUTF8Unsafe() works.
1143 FilePath from_native = FilePath(cases[i].native);
1144 EXPECT_EQ(cases[i].utf8, from_native.AsUTF8Unsafe())
1145 << "i: " << i << ", input: " << cases[i].native;
1146 // Test the two file paths are identical.
1147 EXPECT_EQ(from_utf8.value(), from_native.value());
1148 }
1149}
1150
aedla@chromium.org51127b92013-01-28 22:47:55 +09001151TEST_F(FilePathTest, ConstructWithNUL) {
1152 // Assert FPS() works.
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001153 ASSERT_EQ(3U, FPS("a\0b").length());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001154
1155 // Test constructor strips '\0'
1156 FilePath path(FPS("a\0b"));
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001157 EXPECT_EQ(1U, path.value().length());
1158 EXPECT_EQ(FPL("a"), path.value());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001159}
1160
1161TEST_F(FilePathTest, AppendWithNUL) {
1162 // Assert FPS() works.
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001163 ASSERT_EQ(3U, FPS("b\0b").length());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001164
1165 // Test Append() strips '\0'
1166 FilePath path(FPL("a"));
1167 path = path.Append(FPS("b\0b"));
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001168 EXPECT_EQ(3U, path.value().length());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001169#if defined(FILE_PATH_USES_WIN_SEPARATORS)
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001170 EXPECT_EQ(FPL("a\\b"), path.value());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001171#else
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001172 EXPECT_EQ(FPL("a/b"), path.value());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001173#endif
1174}
1175
1176TEST_F(FilePathTest, ReferencesParentWithNUL) {
1177 // Assert FPS() works.
thestig@chromium.org1b6e1b72013-05-14 19:12:56 +09001178 ASSERT_EQ(3U, FPS("..\0").length());
aedla@chromium.org51127b92013-01-28 22:47:55 +09001179
1180 // Test ReferencesParent() doesn't break with "..\0"
1181 FilePath path(FPS("..\0"));
1182 EXPECT_TRUE(path.ReferencesParent());
1183}
1184
tony@chromium.org65049b12010-06-25 14:33:17 +09001185#if defined(FILE_PATH_USES_WIN_SEPARATORS)
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001186TEST_F(FilePathTest, NormalizePathSeparators) {
tony@chromium.org65049b12010-06-25 14:33:17 +09001187 const struct UnaryTestData cases[] = {
1188 { FPL("foo/bar"), FPL("foo\\bar") },
1189 { FPL("foo/bar\\betz"), FPL("foo\\bar\\betz") },
1190 { FPL("foo\\bar"), FPL("foo\\bar") },
1191 { FPL("foo\\bar/betz"), FPL("foo\\bar\\betz") },
1192 { FPL("foo"), FPL("foo") },
1193 // Trailing slashes don't automatically get stripped. That's what
1194 // StripTrailingSeparators() is for.
1195 { FPL("foo\\"), FPL("foo\\") },
1196 { FPL("foo/"), FPL("foo\\") },
1197 { FPL("foo/bar\\"), FPL("foo\\bar\\") },
1198 { FPL("foo\\bar/"), FPL("foo\\bar\\") },
1199 { FPL("foo/bar/"), FPL("foo\\bar\\") },
1200 { FPL("foo\\bar\\"), FPL("foo\\bar\\") },
1201 { FPL("\\foo/bar"), FPL("\\foo\\bar") },
1202 { FPL("/foo\\bar"), FPL("\\foo\\bar") },
1203 { FPL("c:/foo/bar/"), FPL("c:\\foo\\bar\\") },
1204 { FPL("/foo/bar/"), FPL("\\foo\\bar\\") },
1205 { FPL("\\foo\\bar\\"), FPL("\\foo\\bar\\") },
1206 { FPL("c:\\foo/bar"), FPL("c:\\foo\\bar") },
1207 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1208 { FPL("\\\\foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1209 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1210 // This method does not normalize the number of path separators.
1211 { FPL("foo\\\\bar"), FPL("foo\\\\bar") },
1212 { FPL("foo//bar"), FPL("foo\\\\bar") },
1213 { FPL("foo/\\bar"), FPL("foo\\\\bar") },
1214 { FPL("foo\\/bar"), FPL("foo\\\\bar") },
1215 { FPL("///foo\\\\bar"), FPL("\\\\\\foo\\\\bar") },
1216 { FPL("foo//bar///"), FPL("foo\\\\bar\\\\\\") },
1217 { FPL("foo/\\bar/\\"), FPL("foo\\\\bar\\\\") },
1218 { FPL("/\\foo\\/bar"), FPL("\\\\foo\\\\bar") },
1219 };
1220 for (size_t i = 0; i < arraysize(cases); ++i) {
1221 FilePath input(cases[i].input);
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001222 FilePath observed = input.NormalizePathSeparators();
tony@chromium.org65049b12010-06-25 14:33:17 +09001223 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
1224 "i: " << i << ", input: " << input.value();
1225 }
1226}
1227#endif
brettw@chromium.org82bcf512013-02-17 14:07:23 +09001228
brettw@chromium.org99b198e2013-04-12 14:17:15 +09001229TEST_F(FilePathTest, EndsWithSeparator) {
1230 const UnaryBooleanTestData cases[] = {
1231 { FPL(""), false },
1232 { FPL("/"), true },
1233 { FPL("foo/"), true },
1234 { FPL("bar"), false },
1235 { FPL("/foo/bar"), false },
1236 };
1237 for (size_t i = 0; i < arraysize(cases); ++i) {
1238 FilePath input = FilePath(cases[i].input).NormalizePathSeparators();
1239 EXPECT_EQ(cases[i].expected, input.EndsWithSeparator());
1240 }
1241}
1242
1243TEST_F(FilePathTest, AsEndingWithSeparator) {
1244 const UnaryTestData cases[] = {
1245 { FPL(""), FPL("") },
1246 { FPL("/"), FPL("/") },
1247 { FPL("foo"), FPL("foo/") },
1248 { FPL("foo/"), FPL("foo/") }
1249 };
1250 for (size_t i = 0; i < arraysize(cases); ++i) {
1251 FilePath input = FilePath(cases[i].input).NormalizePathSeparators();
1252 FilePath expected = FilePath(cases[i].expected).NormalizePathSeparators();
1253 EXPECT_EQ(expected.value(), input.AsEndingWithSeparator().value());
1254 }
1255}
1256
qinmin@chromium.org8abcc0c2013-11-20 16:04:55 +09001257#if defined(OS_ANDROID)
1258TEST_F(FilePathTest, ContentUriTest) {
1259 const struct UnaryBooleanTestData cases[] = {
1260 { FPL("content://foo.bar"), true },
1261 { FPL("content://foo.bar/"), true },
1262 { FPL("content://foo/bar"), true },
1263 { FPL("CoNTenT://foo.bar"), true },
1264 { FPL("content://"), true },
1265 { FPL("content:///foo.bar"), true },
1266 { FPL("content://3foo/bar"), true },
1267 { FPL("content://_foo/bar"), true },
1268 { FPL(".. "), false },
1269 { FPL("foo.bar"), false },
1270 { FPL("content:foo.bar"), false },
1271 { FPL("content:/foo.ba"), false },
1272 { FPL("content:/dir/foo.bar"), false },
1273 { FPL("content: //foo.bar"), false },
1274 { FPL("content%2a%2f%2f"), false },
1275 };
1276
1277 for (size_t i = 0; i < arraysize(cases); ++i) {
1278 FilePath input(cases[i].input);
1279 bool observed = input.IsContentUri();
1280 EXPECT_EQ(cases[i].expected, observed) <<
1281 "i: " << i << ", input: " << input.value();
1282 }
1283}
1284#endif
1285
mgiuca36c0d632015-05-28 19:11:33 +09001286// Test the PrintTo overload for FilePath (used when a test fails to compare two
1287// FilePaths).
1288TEST_F(FilePathTest, PrintTo) {
1289 std::stringstream ss;
1290 FilePath fp(FPL("foo"));
1291 base::PrintTo(fp, &ss);
1292 EXPECT_EQ("foo", ss.str());
1293}
1294
brettw@chromium.org82bcf512013-02-17 14:07:23 +09001295} // namespace base