blob: 5a9e5cd6c8246fcd7bc6e13e40875370c95979f8 [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"
aa@chromium.org76e5c632008-12-18 17:38:16 +09006#include "base/file_path.h"
7#include "base/file_util.h"
brettw@chromium.org11f89b02010-08-18 08:05:28 +09008#include "base/utf_string_conversions.h"
mark@chromium.orge3e1ce72008-10-04 04:05:46 +09009#include "testing/gtest/include/gtest/gtest.h"
aa@chromium.org76e5c632008-12-18 17:38:16 +090010#include "testing/platform_test.h"
mark@chromium.org6380e262008-10-04 03:21:47 +090011
12// This macro helps avoid wrapped lines in the test structs.
13#define FPL(x) FILE_PATH_LITERAL(x)
14
aedla@chromium.org51127b92013-01-28 22:47:55 +090015// This macro constructs strings which can contain NULs.
16#define FPS(x) FilePath::StringType(FPL(x), arraysize(FPL(x)) - 1)
17
mark@chromium.org6380e262008-10-04 03:21:47 +090018struct UnaryTestData {
19 const FilePath::CharType* input;
20 const FilePath::CharType* expected;
21};
22
23struct UnaryBooleanTestData {
24 const FilePath::CharType* input;
25 bool expected;
26};
27
28struct BinaryTestData {
29 const FilePath::CharType* inputs[2];
30 const FilePath::CharType* expected;
31};
32
rafaelw@chromium.org465facc2009-06-25 06:28:30 +090033struct BinaryBooleanTestData {
34 const FilePath::CharType* inputs[2];
35 bool expected;
36};
37
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +090038struct BinaryIntTestData {
39 const FilePath::CharType* inputs[2];
40 int expected;
41};
42
satorux@chromium.orgefba4172011-11-02 13:55:23 +090043struct UTF8TestData {
44 const FilePath::CharType* native;
45 const char* utf8;
46};
47
aa@chromium.org76e5c632008-12-18 17:38:16 +090048// file_util winds up using autoreleased objects on the Mac, so this needs
49// to be a PlatformTest
50class FilePathTest : public PlatformTest {
51 protected:
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +090052 virtual void SetUp() OVERRIDE {
aa@chromium.org76e5c632008-12-18 17:38:16 +090053 PlatformTest::SetUp();
54 }
rsleevi@chromium.orgde3a6cf2012-04-06 12:53:02 +090055 virtual void TearDown() OVERRIDE {
aa@chromium.org76e5c632008-12-18 17:38:16 +090056 PlatformTest::TearDown();
57 }
58};
59
60TEST_F(FilePathTest, DirName) {
mark@chromium.org6380e262008-10-04 03:21:47 +090061 const struct UnaryTestData cases[] = {
62 { FPL(""), FPL(".") },
63 { FPL("aa"), FPL(".") },
64 { FPL("/aa/bb"), FPL("/aa") },
65 { FPL("/aa/bb/"), FPL("/aa") },
66 { FPL("/aa/bb//"), FPL("/aa") },
67 { FPL("/aa/bb/ccc"), FPL("/aa/bb") },
68 { FPL("/aa"), FPL("/") },
69 { FPL("/aa/"), FPL("/") },
70 { FPL("/"), FPL("/") },
71 { FPL("//"), FPL("//") },
72 { FPL("///"), FPL("/") },
73 { FPL("aa/"), FPL(".") },
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/bb"), FPL("//aa") },
81 { FPL("//aa/"), FPL("//") },
82 { FPL("//aa"), FPL("//") },
83 { FPL("0:"), FPL(".") },
84 { FPL("@:"), FPL(".") },
85 { FPL("[:"), FPL(".") },
86 { FPL("`:"), FPL(".") },
87 { FPL("{:"), FPL(".") },
88 { FPL("\xB3:"), FPL(".") },
89 { FPL("\xC5:"), FPL(".") },
90#if defined(OS_WIN)
91 { FPL("\x0143:"), FPL(".") },
92#endif // OS_WIN
93#if defined(FILE_PATH_USES_DRIVE_LETTERS)
94 { FPL("c:"), FPL("c:") },
95 { FPL("C:"), FPL("C:") },
96 { FPL("A:"), FPL("A:") },
97 { FPL("Z:"), FPL("Z:") },
98 { FPL("a:"), FPL("a:") },
99 { FPL("z:"), FPL("z:") },
100 { FPL("c:aa"), FPL("c:") },
101 { FPL("c:/"), FPL("c:/") },
102 { FPL("c://"), FPL("c://") },
103 { FPL("c:///"), FPL("c:/") },
104 { FPL("c:/aa"), FPL("c:/") },
105 { FPL("c:/aa/"), FPL("c:/") },
106 { FPL("c:/aa/bb"), FPL("c:/aa") },
107 { FPL("c:aa/bb"), FPL("c:aa") },
108#endif // FILE_PATH_USES_DRIVE_LETTERS
109#if defined(FILE_PATH_USES_WIN_SEPARATORS)
110 { FPL("\\aa\\bb"), FPL("\\aa") },
111 { FPL("\\aa\\bb\\"), FPL("\\aa") },
112 { FPL("\\aa\\bb\\\\"), FPL("\\aa") },
113 { FPL("\\aa\\bb\\ccc"), FPL("\\aa\\bb") },
114 { FPL("\\aa"), FPL("\\") },
115 { FPL("\\aa\\"), FPL("\\") },
116 { FPL("\\"), FPL("\\") },
117 { FPL("\\\\"), FPL("\\\\") },
118 { FPL("\\\\\\"), FPL("\\") },
119 { FPL("aa\\"), FPL(".") },
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\\bb"), FPL("\\\\aa") },
127 { FPL("\\\\aa\\"), FPL("\\\\") },
128 { FPL("\\\\aa"), FPL("\\\\") },
129#if defined(FILE_PATH_USES_DRIVE_LETTERS)
130 { FPL("c:\\"), FPL("c:\\") },
131 { FPL("c:\\\\"), FPL("c:\\\\") },
132 { FPL("c:\\\\\\"), FPL("c:\\") },
133 { FPL("c:\\aa"), FPL("c:\\") },
134 { FPL("c:\\aa\\"), FPL("c:\\") },
135 { FPL("c:\\aa\\bb"), FPL("c:\\aa") },
136 { FPL("c:aa\\bb"), FPL("c:aa") },
137#endif // FILE_PATH_USES_DRIVE_LETTERS
138#endif // FILE_PATH_USES_WIN_SEPARATORS
139 };
140
141 for (size_t i = 0; i < arraysize(cases); ++i) {
142 FilePath input(cases[i].input);
143 FilePath observed = input.DirName();
144 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
145 "i: " << i << ", input: " << input.value();
146 }
147}
148
aa@chromium.org76e5c632008-12-18 17:38:16 +0900149TEST_F(FilePathTest, BaseName) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900150 const struct UnaryTestData cases[] = {
151 { FPL(""), FPL("") },
152 { FPL("aa"), FPL("aa") },
153 { FPL("/aa/bb"), FPL("bb") },
154 { FPL("/aa/bb/"), FPL("bb") },
155 { FPL("/aa/bb//"), FPL("bb") },
156 { FPL("/aa/bb/ccc"), FPL("ccc") },
157 { FPL("/aa"), FPL("aa") },
158 { FPL("/"), FPL("/") },
159 { FPL("//"), FPL("//") },
160 { FPL("///"), FPL("/") },
161 { FPL("aa/"), FPL("aa") },
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/bb"), FPL("bb") },
169 { FPL("//aa/"), FPL("aa") },
170 { FPL("//aa"), FPL("aa") },
171 { FPL("0:"), FPL("0:") },
172 { FPL("@:"), FPL("@:") },
173 { FPL("[:"), FPL("[:") },
174 { FPL("`:"), FPL("`:") },
175 { FPL("{:"), FPL("{:") },
176 { FPL("\xB3:"), FPL("\xB3:") },
177 { FPL("\xC5:"), FPL("\xC5:") },
178#if defined(OS_WIN)
179 { FPL("\x0143:"), FPL("\x0143:") },
180#endif // OS_WIN
181#if defined(FILE_PATH_USES_DRIVE_LETTERS)
182 { FPL("c:"), FPL("") },
183 { FPL("C:"), FPL("") },
184 { FPL("A:"), FPL("") },
185 { FPL("Z:"), FPL("") },
186 { FPL("a:"), FPL("") },
187 { FPL("z:"), FPL("") },
188 { FPL("c:aa"), FPL("aa") },
189 { FPL("c:/"), FPL("/") },
190 { FPL("c://"), FPL("//") },
191 { FPL("c:///"), FPL("/") },
192 { FPL("c:/aa"), FPL("aa") },
193 { FPL("c:/aa/"), FPL("aa") },
194 { FPL("c:/aa/bb"), FPL("bb") },
195 { FPL("c:aa/bb"), FPL("bb") },
196#endif // FILE_PATH_USES_DRIVE_LETTERS
197#if defined(FILE_PATH_USES_WIN_SEPARATORS)
198 { FPL("\\aa\\bb"), FPL("bb") },
199 { FPL("\\aa\\bb\\"), FPL("bb") },
200 { FPL("\\aa\\bb\\\\"), FPL("bb") },
201 { FPL("\\aa\\bb\\ccc"), FPL("ccc") },
202 { FPL("\\aa"), FPL("aa") },
203 { FPL("\\"), FPL("\\") },
204 { FPL("\\\\"), FPL("\\\\") },
205 { FPL("\\\\\\"), FPL("\\") },
206 { FPL("aa\\"), FPL("aa") },
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\\bb"), FPL("bb") },
214 { FPL("\\\\aa\\"), FPL("aa") },
215 { FPL("\\\\aa"), FPL("aa") },
216#if defined(FILE_PATH_USES_DRIVE_LETTERS)
217 { FPL("c:\\"), FPL("\\") },
218 { FPL("c:\\\\"), FPL("\\\\") },
219 { FPL("c:\\\\\\"), FPL("\\") },
220 { FPL("c:\\aa"), FPL("aa") },
221 { FPL("c:\\aa\\"), FPL("aa") },
222 { FPL("c:\\aa\\bb"), FPL("bb") },
223 { FPL("c:aa\\bb"), FPL("bb") },
224#endif // FILE_PATH_USES_DRIVE_LETTERS
225#endif // FILE_PATH_USES_WIN_SEPARATORS
226 };
227
228 for (size_t i = 0; i < arraysize(cases); ++i) {
229 FilePath input(cases[i].input);
estade@chromium.orgd872d682009-01-06 08:59:36 +0900230 FilePath observed = input.BaseName();
231 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
mark@chromium.org6380e262008-10-04 03:21:47 +0900232 "i: " << i << ", input: " << input.value();
233 }
234}
235
aa@chromium.org76e5c632008-12-18 17:38:16 +0900236TEST_F(FilePathTest, Append) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900237 const struct BinaryTestData cases[] = {
238 { { FPL(""), FPL("cc") }, FPL("cc") },
239 { { FPL("."), FPL("ff") }, FPL("ff") },
240 { { FPL("/"), FPL("cc") }, FPL("/cc") },
241 { { FPL("/aa"), FPL("") }, FPL("/aa") },
242 { { FPL("/aa/"), FPL("") }, FPL("/aa") },
243 { { FPL("//aa"), FPL("") }, FPL("//aa") },
244 { { FPL("//aa/"), FPL("") }, FPL("//aa") },
245 { { FPL("//"), FPL("aa") }, FPL("//aa") },
246#if defined(FILE_PATH_USES_DRIVE_LETTERS)
247 { { FPL("c:"), FPL("a") }, FPL("c:a") },
248 { { FPL("c:"), FPL("") }, FPL("c:") },
249 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
250 { { FPL("c://"), FPL("a") }, FPL("c://a") },
251 { { FPL("c:///"), FPL("a") }, FPL("c:/a") },
252#endif // FILE_PATH_USES_DRIVE_LETTERS
253#if defined(FILE_PATH_USES_WIN_SEPARATORS)
254 // Append introduces the default separator character, so these test cases
255 // need to be defined with different expected results on platforms that use
256 // different default separator characters.
257 { { FPL("\\"), FPL("cc") }, FPL("\\cc") },
258 { { FPL("\\aa"), FPL("") }, FPL("\\aa") },
259 { { FPL("\\aa\\"), FPL("") }, FPL("\\aa") },
260 { { FPL("\\\\aa"), FPL("") }, FPL("\\\\aa") },
261 { { FPL("\\\\aa\\"), FPL("") }, FPL("\\\\aa") },
262 { { FPL("\\\\"), FPL("aa") }, FPL("\\\\aa") },
263 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb\\cc") },
264 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb\\cc") },
265 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb\\cc") },
266 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb\\cc") },
267 { { FPL("a/b"), FPL("c") }, FPL("a/b\\c") },
268 { { FPL("a/b/"), FPL("c") }, FPL("a/b\\c") },
269 { { FPL("//aa"), FPL("bb") }, FPL("//aa\\bb") },
270 { { FPL("//aa/"), FPL("bb") }, FPL("//aa\\bb") },
271 { { 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("aa\\bb"), FPL("cc") }, FPL("aa\\bb\\cc") },
275 { { FPL("a\\b"), FPL("c") }, FPL("a\\b\\c") },
276 { { FPL("a\\b\\"), FPL("c") }, FPL("a\\b\\c") },
277 { { FPL("\\\\aa"), FPL("bb") }, FPL("\\\\aa\\bb") },
278 { { FPL("\\\\aa\\"), FPL("bb") }, FPL("\\\\aa\\bb") },
279#if defined(FILE_PATH_USES_DRIVE_LETTERS)
280 { { FPL("c:\\"), FPL("a") }, FPL("c:\\a") },
281 { { FPL("c:\\\\"), FPL("a") }, FPL("c:\\\\a") },
282 { { FPL("c:\\\\\\"), FPL("a") }, FPL("c:\\a") },
283 { { FPL("c:\\"), FPL("") }, FPL("c:\\") },
284 { { FPL("c:\\a"), FPL("b") }, FPL("c:\\a\\b") },
285 { { FPL("c:\\a\\"), FPL("b") }, FPL("c:\\a\\b") },
286#endif // FILE_PATH_USES_DRIVE_LETTERS
287#else // FILE_PATH_USES_WIN_SEPARATORS
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("aa/bb"), FPL("cc") }, FPL("aa/bb/cc") },
292 { { FPL("a/b"), FPL("c") }, FPL("a/b/c") },
293 { { FPL("a/b/"), FPL("c") }, FPL("a/b/c") },
294 { { FPL("//aa"), FPL("bb") }, FPL("//aa/bb") },
295 { { FPL("//aa/"), FPL("bb") }, FPL("//aa/bb") },
296#if defined(FILE_PATH_USES_DRIVE_LETTERS)
297 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
298 { { FPL("c:/"), FPL("") }, FPL("c:/") },
299 { { FPL("c:/a"), FPL("b") }, FPL("c:/a/b") },
300 { { FPL("c:/a/"), FPL("b") }, FPL("c:/a/b") },
301#endif // FILE_PATH_USES_DRIVE_LETTERS
302#endif // FILE_PATH_USES_WIN_SEPARATORS
303 };
304
305 for (size_t i = 0; i < arraysize(cases); ++i) {
306 FilePath root(cases[i].inputs[0]);
307 FilePath::StringType leaf(cases[i].inputs[1]);
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900308 FilePath observed_str = root.Append(leaf);
309 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
310 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
311 FilePath observed_path = root.Append(FilePath(leaf));
312 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) <<
mark@chromium.org6380e262008-10-04 03:21:47 +0900313 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900314
315 // TODO(erikkay): It would be nice to have a unicode test append value to
316 // handle the case when AppendASCII is passed UTF8
317#if defined(OS_WIN)
brettw@chromium.org11f89b02010-08-18 08:05:28 +0900318 std::string ascii = WideToUTF8(leaf);
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900319#elif defined(OS_POSIX)
320 std::string ascii = leaf;
321#endif
322 observed_str = root.AppendASCII(ascii);
323 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
324 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
mark@chromium.org6380e262008-10-04 03:21:47 +0900325 }
326}
327
mark@chromium.org778d9cf2009-10-14 02:31:39 +0900328TEST_F(FilePathTest, StripTrailingSeparators) {
329 const struct UnaryTestData cases[] = {
330 { FPL(""), FPL("") },
331 { FPL("/"), FPL("/") },
332 { FPL("//"), FPL("//") },
333 { FPL("///"), FPL("/") },
334 { FPL("////"), FPL("/") },
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 { FPL("/a////"), FPL("/a") },
344#if defined(FILE_PATH_USES_DRIVE_LETTERS)
345 { FPL("c:"), FPL("c:") },
346 { FPL("c:/"), FPL("c:/") },
347 { FPL("c://"), FPL("c://") },
348 { FPL("c:///"), FPL("c:/") },
349 { FPL("c:////"), FPL("c:/") },
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 { FPL("c:/a////"), FPL("c:/a") },
355#endif // FILE_PATH_USES_DRIVE_LETTERS
356#if defined(FILE_PATH_USES_WIN_SEPARATORS)
357 { FPL("\\"), FPL("\\") },
358 { FPL("\\\\"), FPL("\\\\") },
359 { FPL("\\\\\\"), FPL("\\") },
360 { FPL("\\\\\\\\"), FPL("\\") },
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 { FPL("\\a\\\\\\\\"), FPL("\\a") },
370#if defined(FILE_PATH_USES_DRIVE_LETTERS)
371 { FPL("c:\\"), FPL("c:\\") },
372 { FPL("c:\\\\"), FPL("c:\\\\") },
373 { FPL("c:\\\\\\"), FPL("c:\\") },
374 { FPL("c:\\\\\\\\"), FPL("c:\\") },
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 { FPL("c:\\a\\\\\\\\"), FPL("c:\\a") },
380#endif // FILE_PATH_USES_DRIVE_LETTERS
381#endif // FILE_PATH_USES_WIN_SEPARATORS
382 };
383
384 for (size_t i = 0; i < arraysize(cases); ++i) {
385 FilePath input(cases[i].input);
386 FilePath observed = input.StripTrailingSeparators();
387 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
388 "i: " << i << ", input: " << input.value();
389 }
390}
391
aa@chromium.org76e5c632008-12-18 17:38:16 +0900392TEST_F(FilePathTest, IsAbsolute) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900393 const struct UnaryBooleanTestData cases[] = {
394 { FPL(""), false },
395 { FPL("a"), false },
396 { FPL("c:"), false },
397 { FPL("c:a"), false },
398 { FPL("a/b"), false },
399 { FPL("//"), true },
400 { FPL("//a"), true },
401 { FPL("c:a/b"), false },
402 { FPL("?:/a"), false },
403#if defined(FILE_PATH_USES_DRIVE_LETTERS)
404 { FPL("/"), false },
405 { FPL("/a"), false },
406 { FPL("/."), false },
407 { FPL("/.."), false },
408 { FPL("c:/"), true },
409 { FPL("c:/a"), true },
410 { FPL("c:/."), true },
411 { FPL("c:/.."), true },
412 { FPL("C:/a"), true },
413 { FPL("d:/a"), true },
414#else // FILE_PATH_USES_DRIVE_LETTERS
415 { FPL("/"), true },
416 { FPL("/a"), true },
417 { FPL("/."), true },
418 { FPL("/.."), true },
419 { FPL("c:/"), false },
420#endif // FILE_PATH_USES_DRIVE_LETTERS
421#if defined(FILE_PATH_USES_WIN_SEPARATORS)
422 { FPL("a\\b"), false },
423 { FPL("\\\\"), true },
424 { FPL("\\\\a"), true },
425 { FPL("a\\b"), false },
426 { FPL("\\\\"), true },
427 { FPL("//a"), true },
428 { FPL("c:a\\b"), false },
429 { FPL("?:\\a"), false },
430#if defined(FILE_PATH_USES_DRIVE_LETTERS)
431 { FPL("\\"), false },
432 { FPL("\\a"), false },
433 { FPL("\\."), false },
434 { FPL("\\.."), false },
435 { FPL("c:\\"), true },
436 { FPL("c:\\"), true },
437 { FPL("c:\\a"), true },
438 { FPL("c:\\."), true },
439 { FPL("c:\\.."), true },
440 { FPL("C:\\a"), true },
441 { FPL("d:\\a"), true },
442#else // FILE_PATH_USES_DRIVE_LETTERS
443 { FPL("\\"), true },
444 { FPL("\\a"), true },
445 { FPL("\\."), true },
446 { FPL("\\.."), true },
447 { FPL("c:\\"), false },
448#endif // FILE_PATH_USES_DRIVE_LETTERS
449#endif // FILE_PATH_USES_WIN_SEPARATORS
450 };
451
452 for (size_t i = 0; i < arraysize(cases); ++i) {
453 FilePath input(cases[i].input);
454 bool observed = input.IsAbsolute();
455 EXPECT_EQ(cases[i].expected, observed) <<
456 "i: " << i << ", input: " << input.value();
457 }
458}
aa@chromium.org76e5c632008-12-18 17:38:16 +0900459
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900460TEST_F(FilePathTest, PathComponentsTest) {
461 const struct UnaryTestData cases[] = {
462 { FPL("//foo/bar/baz/"), FPL("|//|foo|bar|baz")},
463 { FPL("///"), FPL("|/")},
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/baz"), FPL("|/|foo|bar|baz")},
469 { FPL("/foo/bar.bot/baz.txt"), FPL("|/|foo|bar.bot|baz.txt")},
470 { FPL("//foo//bar/baz"), FPL("|//|foo|bar|baz")},
471 { FPL("/"), FPL("|/")},
472 { FPL("foo"), FPL("|foo")},
473 { FPL(""), FPL("")},
474#if defined(FILE_PATH_USES_DRIVE_LETTERS)
475 { FPL("e:/foo"), FPL("|e:|/|foo")},
476 { FPL("e:/"), FPL("|e:|/")},
477 { FPL("e:"), FPL("|e:")},
478#endif // FILE_PATH_USES_DRIVE_LETTERS
479#if defined(FILE_PATH_USES_WIN_SEPARATORS)
480 { FPL("../foo"), FPL("|..|foo")},
481 { FPL("./foo"), FPL("|foo")},
482 { FPL("../foo/bar/"), FPL("|..|foo|bar") },
483 { FPL("\\\\foo\\bar\\baz\\"), FPL("|\\\\|foo|bar|baz")},
484 { FPL("\\\\\\"), FPL("|\\")},
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\\baz"), FPL("|/|foo|bar|baz")},
492 { FPL("\\foo\\bar.bot\\baz.txt"), FPL("|\\|foo|bar.bot|baz.txt")},
493 { FPL("\\\\foo\\\\bar\\baz"), FPL("|\\\\|foo|bar|baz")},
494 { FPL("\\"), FPL("|\\")},
495#endif // FILE_PATH_USES_WIN_SEPARATORS
496 };
497
498 for (size_t i = 0; i < arraysize(cases); ++i) {
499 FilePath input(cases[i].input);
500 std::vector<FilePath::StringType> comps;
501 input.GetComponents(&comps);
502
503 FilePath::StringType observed;
504 for (size_t j = 0; j < comps.size(); ++j) {
505 observed.append(FILE_PATH_LITERAL("|"), 1);
506 observed.append(comps[j]);
507 }
508 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed) <<
509 "i: " << i << ", input: " << input.value();
510 }
511}
512
513TEST_F(FilePathTest, IsParentTest) {
514 const struct BinaryBooleanTestData cases[] = {
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900515 { { FPL("/"), 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("//foo/bar/baz") }, true},
519 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, false},
520 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, false},
521 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, false},
522 { { FPL("/foo/bar"), FPL("/foo/bar") }, false},
523 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
524 { { FPL("foo/bar"), FPL("foo/bar/baz") }, true},
525 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, false},
526 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, false},
527 { { FPL(""), FPL("foo") }, false},
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900528#if defined(FILE_PATH_USES_DRIVE_LETTERS)
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900529 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, true},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900530 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, true},
531 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, true},
532 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, false},
533 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900534 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, true},
535 { { FPL("c:"), FPL("c:/foo/bar/baz") }, true},
536 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900537 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, false},
538 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900539 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900540 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, false},
541 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900542 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, false},
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900543#endif // FILE_PATH_USES_DRIVE_LETTERS
544#if defined(FILE_PATH_USES_WIN_SEPARATORS)
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900545 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, true},
546 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, true},
547 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, true},
548 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, true},
549 { { FPL(""), FPL("\\foo\\bar\\baz") }, false},
550 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, false},
551 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900552#endif // FILE_PATH_USES_WIN_SEPARATORS
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900553 };
554
555 for (size_t i = 0; i < arraysize(cases); ++i) {
556 FilePath parent(cases[i].inputs[0]);
557 FilePath child(cases[i].inputs[1]);
558
559 EXPECT_EQ(parent.IsParent(child), cases[i].expected) <<
mad@chromium.orgcc4f7b32009-10-21 00:27:21 +0900560 "i: " << i << ", parent: " << parent.value() << ", child: " <<
561 child.value();
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900562 }
563}
564
mark@chromium.org752010c2009-09-24 05:32:41 +0900565TEST_F(FilePathTest, AppendRelativePathTest) {
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900566 const struct BinaryTestData cases[] = {
mark@chromium.org752010c2009-09-24 05:32:41 +0900567#if defined(FILE_PATH_USES_WIN_SEPARATORS)
568 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo\\bar\\baz")},
569#else // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900570 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo/bar/baz")},
mark@chromium.org752010c2009-09-24 05:32:41 +0900571#endif // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900572 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, FPL("baz")},
573 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, FPL("baz")},
574 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, FPL("baz")},
575 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, FPL("")},
576 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, FPL("")},
577 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, FPL("")},
578 { { FPL("/foo/bar"), FPL("/foo/bar") }, FPL("")},
579 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, FPL("")},
580 { { FPL("foo/bar"), FPL("foo/bar/baz") }, FPL("baz")},
581 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, FPL("")},
582 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, FPL("")},
583 { { FPL(""), FPL("foo") }, FPL("")},
584#if defined(FILE_PATH_USES_DRIVE_LETTERS)
585 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, FPL("baz")},
586 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, FPL("baz")},
587 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, FPL("baz")},
588 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, FPL("")},
589 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, FPL("")},
mark@chromium.org752010c2009-09-24 05:32:41 +0900590#if defined(FILE_PATH_USES_WIN_SEPARATORS)
591 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
592 // TODO(akalin): Figure out how to handle the corner case in the
593 // commented-out test case below. Appending to an empty path gives
594 // /foo\bar\baz but appending to a nonempty path "blah" gives
595 // blah\foo\bar\baz.
596 // { { FPL("c:"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
597#endif // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900598 { { 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("d:/foo/bar/baz") }, FPL("")},
601 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, FPL("")},
602 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, FPL("")},
603 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, FPL("")},
604 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, FPL("")},
605#endif // FILE_PATH_USES_DRIVE_LETTERS
606#if defined(FILE_PATH_USES_WIN_SEPARATORS)
607 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
608 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
609 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, FPL("baz")},
610 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, FPL("foo\\bar\\baz")},
611 { { FPL(""), FPL("\\foo\\bar\\baz") }, FPL("")},
612 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, FPL("")},
613 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, FPL("")},
614#endif // FILE_PATH_USES_WIN_SEPARATORS
615 };
616
617 const FilePath base(FPL("blah"));
618
619 for (size_t i = 0; i < arraysize(cases); ++i) {
620 FilePath parent(cases[i].inputs[0]);
621 FilePath child(cases[i].inputs[1]);
622 {
623 FilePath result;
624 bool success = parent.AppendRelativePath(child, &result);
625 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
626 "i: " << i << ", parent: " << parent.value() << ", child: " <<
627 child.value();
628 EXPECT_STREQ(cases[i].expected, result.value().c_str()) <<
629 "i: " << i << ", parent: " << parent.value() << ", child: " <<
630 child.value();
631 }
632 {
633 FilePath result(base);
634 bool success = parent.AppendRelativePath(child, &result);
635 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
636 "i: " << i << ", parent: " << parent.value() << ", child: " <<
637 child.value();
638 EXPECT_EQ(base.Append(cases[i].expected).value(), result.value()) <<
639 "i: " << i << ", parent: " << parent.value() << ", child: " <<
640 child.value();
641 }
642 }
643}
644
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900645TEST_F(FilePathTest, EqualityTest) {
646 const struct BinaryBooleanTestData cases[] = {
647 { { FPL("/foo/bar/baz"), FPL("/foo/bar/baz") }, true},
648 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, false},
649 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
650 { { FPL("//foo/bar/"), FPL("//foo/bar/") }, true},
651 { { FPL("/foo/bar"), FPL("/foo2/bar") }, false},
652 { { FPL("/foo/bar.txt"), FPL("/foo/bar") }, false},
653 { { FPL("foo/bar"), FPL("foo/bar") }, true},
654 { { FPL("foo/bar"), FPL("foo/bar/baz") }, false},
655 { { FPL(""), FPL("foo") }, false},
656#if defined(FILE_PATH_USES_DRIVE_LETTERS)
657 { { FPL("c:/foo/bar"), FPL("c:/foo/bar") }, true},
658 { { FPL("E:/foo/bar"), FPL("e:/foo/bar") }, true},
659 { { FPL("f:/foo/bar"), FPL("F:/foo/bar") }, true},
660 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar") }, false},
661 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar") }, false},
662 { { FPL("c:/"), FPL("c:/") }, true},
663 { { FPL("c:"), FPL("c:") }, true},
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("d:/foo/bar") }, false},
667 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar") }, false},
668#endif // FILE_PATH_USES_DRIVE_LETTERS
669#if defined(FILE_PATH_USES_WIN_SEPARATORS)
670 { { FPL("\\foo\\bar"), FPL("\\foo\\bar") }, true},
671 { { FPL("\\foo/bar"), FPL("\\foo/bar") }, true},
tony@chromium.org65049b12010-06-25 14:33:17 +0900672 { { FPL("\\foo/bar"), FPL("\\foo\\bar") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900673 { { FPL("\\"), FPL("\\") }, true},
674 { { FPL("\\"), FPL("/") }, false},
675 { { FPL(""), FPL("\\") }, false},
676 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar") }, false},
677 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2") }, false},
678#if defined(FILE_PATH_USES_DRIVE_LETTERS)
679 { { FPL("c:\\foo\\bar"), FPL("c:\\foo\\bar") }, true},
680 { { FPL("E:\\foo\\bar"), FPL("e:\\foo\\bar") }, true},
681 { { FPL("f:\\foo\\bar"), FPL("F:\\foo/bar") }, false},
682#endif // FILE_PATH_USES_DRIVE_LETTERS
683#endif // FILE_PATH_USES_WIN_SEPARATORS
684 };
685
686 for (size_t i = 0; i < arraysize(cases); ++i) {
687 FilePath a(cases[i].inputs[0]);
688 FilePath b(cases[i].inputs[1]);
689
690 EXPECT_EQ(a == b, cases[i].expected) <<
691 "equality i: " << i << ", a: " << a.value() << ", b: " <<
692 b.value();
693 }
694
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900695 for (size_t i = 0; i < arraysize(cases); ++i) {
696 FilePath a(cases[i].inputs[0]);
697 FilePath b(cases[i].inputs[1]);
698
699 EXPECT_EQ(a != b, !cases[i].expected) <<
700 "inequality i: " << i << ", a: " << a.value() << ", b: " <<
701 b.value();
702 }
703}
704
erikkay@google.com985d4202009-01-10 03:26:19 +0900705TEST_F(FilePathTest, Extension) {
706 FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
707
708 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
estade@chromium.org99a42e72010-07-28 06:24:39 +0900709 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.Extension());
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());
erikkay@google.com985d4202009-01-10 03:26:19 +0900719}
720
721TEST_F(FilePathTest, Extension2) {
722 const struct UnaryTestData cases[] = {
723#if defined(FILE_PATH_USES_WIN_SEPARATORS)
724 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
725 { FPL("C:\\a\\b\\c."), FPL(".") },
726 { FPL("C:\\a\\b\\c"), FPL("") },
727 { FPL("C:\\a\\b\\"), FPL("") },
728 { FPL("C:\\a\\b.\\"), FPL(".") },
729 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
730 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
731 { FPL("C:\\foo.bar\\.."), FPL("") },
732 { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
733#endif
734 { FPL("/foo/bar/baz.ext"), FPL(".ext") },
735 { FPL("/foo/bar/baz."), FPL(".") },
736 { FPL("/foo/bar/baz.."), FPL(".") },
737 { FPL("/foo/bar/baz"), FPL("") },
738 { FPL("/foo/bar/"), FPL("") },
739 { FPL("/foo/bar./"), FPL(".") },
740 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900741 { FPL("/foo.tar.gz"), FPL(".tar.gz") },
742 { FPL("/foo.tar.Z"), FPL(".tar.Z") },
743 { FPL("/foo.tar.bz2"), FPL(".tar.bz2") },
744 { FPL("/subversion-1.6.12.zip"), FPL(".zip") },
745 { FPL("/foo.1234.gz"), FPL(".1234.gz") },
746 { FPL("/foo.12345.gz"), FPL(".gz") },
747 { FPL("/foo..gz"), FPL(".gz") },
748 { FPL("/foo.1234.tar.gz"), FPL(".tar.gz") },
749 { FPL("/foo.tar.tar.gz"), FPL(".tar.gz") },
750 { FPL("/foo.tar.gz.gz"), FPL(".gz.gz") },
erikkay@google.com985d4202009-01-10 03:26:19 +0900751 { FPL("."), FPL("") },
752 { FPL(".."), FPL("") },
753 { FPL("./foo"), FPL("") },
754 { FPL("./foo.ext"), FPL(".ext") },
755 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
756 { FPL("/foo.bar////"), FPL(".bar") },
757 { FPL("/foo.bar/.."), FPL("") },
758 { FPL("/foo.bar/..////"), FPL("") },
aa@chromium.orgf8395de2012-08-26 21:11:10 +0900759 { FPL("/foo.1234.user.js"), FPL(".user.js") },
760 { FPL("foo.user.js"), FPL(".user.js") },
761 { FPL("/foo.1234.luser.js"), FPL(".js") },
762 { FPL("/user.js"), FPL(".js") },
erikkay@google.com985d4202009-01-10 03:26:19 +0900763 };
764 for (unsigned int i = 0; i < arraysize(cases); ++i) {
765 FilePath path(cases[i].input);
766 FilePath::StringType extension = path.Extension();
767 EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
768 ", path: " << path.value();
769 }
770}
771
772TEST_F(FilePathTest, InsertBeforeExtension) {
773 const struct BinaryTestData cases[] = {
774 { { FPL(""), FPL("") }, FPL("") },
775 { { FPL(""), FPL("txt") }, FPL("") },
776 { { FPL("."), FPL("txt") }, FPL("") },
777 { { FPL(".."), FPL("txt") }, FPL("") },
778 { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
779 { { FPL("."), FPL("") }, FPL(".") },
780 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
781 { { FPL("foo"), FPL("txt") }, FPL("footxt") },
782 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
783 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
784 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
785 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
786 { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
787 { { FPL("foo"), FPL("") }, FPL("foo") },
788 { { FPL("foo"), FPL(".") }, FPL("foo.") },
789 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
790 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
791#if defined(FILE_PATH_USES_WIN_SEPARATORS)
792 { { FPL("\\"), FPL("") }, FPL("\\") },
793 { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
794 { { FPL("\\."), FPL("txt") }, FPL("") },
795 { { FPL("\\.."), FPL("txt") }, FPL("") },
796 { { FPL("\\."), FPL("") }, FPL("\\.") },
797 { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
798 FPL("C:\\bar\\footxt.dll") },
799 { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
800 FPL("C:\\bar.baz\\foodlltxt") },
801 { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
802 FPL("C:\\bar.baz\\footxt.dll") },
803 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
804 FPL("C:\\bar.baz\\foo.dlltxt.exe") },
805 { { FPL("C:\\bar.baz\\foo"), FPL("") },
806 FPL("C:\\bar.baz\\foo") },
807 { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
808 FPL("C:\\bar.baz\\foo.exe") },
809 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
810 FPL("C:\\bar.baz\\foo.dll.exe") },
811 { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
812 FPL("C:\\bar\\baz\\foo (1).exe") },
813 { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
814 { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
815#endif
816 { { FPL("/"), FPL("") }, FPL("/") },
817 { { FPL("/"), FPL("txt") }, FPL("/txt") },
818 { { FPL("/."), FPL("txt") }, FPL("") },
819 { { FPL("/.."), FPL("txt") }, FPL("") },
820 { { FPL("/."), FPL("") }, FPL("/.") },
821 { { FPL("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
822 { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
823 { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
824 { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
825 FPL("/bar.baz/foo.dlltxt.exe") },
826 { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
827 { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
828 { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
829 { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
830 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
831 };
832 for (unsigned int i = 0; i < arraysize(cases); ++i) {
833 FilePath path(cases[i].inputs[0]);
834 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
835 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
836 ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
837 }
838}
839
estade@chromium.org99a42e72010-07-28 06:24:39 +0900840TEST_F(FilePathTest, RemoveExtension) {
841 const struct UnaryTestData cases[] = {
842 { FPL(""), FPL("") },
843 { FPL("."), FPL(".") },
844 { FPL(".."), FPL("..") },
845 { FPL("foo.dll"), FPL("foo") },
846 { FPL("./foo.dll"), FPL("./foo") },
847 { FPL("foo..dll"), FPL("foo.") },
848 { FPL("foo"), FPL("foo") },
849 { FPL("foo."), FPL("foo") },
850 { FPL("foo.."), FPL("foo.") },
851 { FPL("foo.baz.dll"), FPL("foo.baz") },
852 { FPL("foo.tar.gz"), FPL("foo") },
853#if defined(FILE_PATH_USES_WIN_SEPARATORS)
854 { FPL("C:\\foo.bar\\foo"), FPL("C:\\foo.bar\\foo") },
855 { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") },
856#endif
857 { FPL("/foo.bar/foo"), FPL("/foo.bar/foo") },
858 { FPL("/foo.bar/..////"), FPL("/foo.bar/..////") },
859 };
860 for (unsigned int i = 0; i < arraysize(cases); ++i) {
861 FilePath path(cases[i].input);
862 FilePath removed = path.RemoveExtension();
863 EXPECT_EQ(cases[i].expected, removed.value()) << "i: " << i <<
864 ", path: " << path.value();
865 }
866}
867
erikkay@google.com985d4202009-01-10 03:26:19 +0900868TEST_F(FilePathTest, ReplaceExtension) {
869 const struct BinaryTestData cases[] = {
870 { { FPL(""), FPL("") }, FPL("") },
871 { { FPL(""), FPL("txt") }, FPL("") },
872 { { FPL("."), FPL("txt") }, FPL("") },
873 { { FPL(".."), FPL("txt") }, FPL("") },
874 { { FPL("."), FPL("") }, FPL("") },
875 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
estade@chromium.org99a42e72010-07-28 06:24:39 +0900876 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.txt") },
erikkay@google.com985d4202009-01-10 03:26:19 +0900877 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
878 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
879 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
880 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
881 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
882 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
883 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
884 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
885 { { FPL("foo.dll"), FPL("") }, FPL("foo") },
886 { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
887 { { FPL("foo"), FPL("") }, FPL("foo") },
888 { { FPL("foo"), FPL(".") }, FPL("foo") },
889 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
890 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
891#if defined(FILE_PATH_USES_WIN_SEPARATORS)
892 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
893 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
894#endif
895 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
896 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
897 };
898 for (unsigned int i = 0; i < arraysize(cases); ++i) {
899 FilePath path(cases[i].inputs[0]);
900 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
901 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
902 ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
903 }
904}
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900905
rdevlin.cronin@chromium.org9a980f72012-04-25 10:53:44 +0900906TEST_F(FilePathTest, AddExtension) {
907 const struct BinaryTestData cases[] = {
908 { { FPL(""), FPL("") }, FPL("") },
909 { { FPL(""), FPL("txt") }, FPL("") },
910 { { FPL("."), FPL("txt") }, FPL("") },
911 { { FPL(".."), FPL("txt") }, FPL("") },
912 { { FPL("."), FPL("") }, FPL("") },
913 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.dll.txt") },
914 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.dll.txt") },
915 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..dll.txt") },
916 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.dll.txt") },
917 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
918 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
919 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
920 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
921 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.dll.txt") },
922 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.dll.txt") },
923 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
924 { { FPL("foo.dll"), FPL(".") }, FPL("foo.dll") },
925 { { FPL("foo"), FPL("") }, FPL("foo") },
926 { { FPL("foo"), FPL(".") }, FPL("foo") },
927 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
928 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz.dll") },
929#if defined(FILE_PATH_USES_WIN_SEPARATORS)
930 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
931 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
932#endif
933 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
934 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
935 };
936 for (unsigned int i = 0; i < arraysize(cases); ++i) {
937 FilePath path(cases[i].inputs[0]);
938 FilePath added = path.AddExtension(cases[i].inputs[1]);
939 EXPECT_EQ(cases[i].expected, added.value()) << "i: " << i <<
940 ", path: " << path.value() << ", add: " << cases[i].inputs[1];
941 }
942}
943
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900944TEST_F(FilePathTest, MatchesExtension) {
945 const struct BinaryBooleanTestData cases[] = {
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900946 { { FPL("foo"), FPL("") }, true},
947 { { FPL("foo"), FPL(".") }, false},
948 { { FPL("foo."), FPL("") }, false},
949 { { FPL("foo."), FPL(".") }, true},
950 { { FPL("foo.txt"), FPL(".dll") }, false},
951 { { FPL("foo.txt"), FPL(".txt") }, true},
952 { { FPL("foo.txt.dll"), FPL(".txt") }, false},
953 { { FPL("foo.txt.dll"), FPL(".dll") }, true},
954 { { FPL("foo.TXT"), FPL(".txt") }, true},
955 { { FPL("foo.txt"), FPL(".TXT") }, true},
956 { { FPL("foo.tXt"), FPL(".txt") }, true},
957 { { FPL("foo.txt"), FPL(".tXt") }, true},
958 { { FPL("foo.tXt"), FPL(".TXT") }, true},
959 { { FPL("foo.tXt"), FPL(".tXt") }, true},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900960#if defined(FILE_PATH_USES_DRIVE_LETTERS)
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900961 { { FPL("c:/foo.txt.dll"), FPL(".txt") }, false},
962 { { FPL("c:/foo.txt"), FPL(".txt") }, true},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900963#endif // FILE_PATH_USES_DRIVE_LETTERS
964#if defined(FILE_PATH_USES_WIN_SEPARATORS)
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900965 { { FPL("c:\\bar\\foo.txt.dll"), FPL(".txt") }, false},
966 { { FPL("c:\\bar\\foo.txt"), FPL(".txt") }, true},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900967#endif // FILE_PATH_USES_DRIVE_LETTERS
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900968 { { FPL("/bar/foo.txt.dll"), FPL(".txt") }, false},
969 { { FPL("/bar/foo.txt"), FPL(".txt") }, true},
970#if defined(OS_WIN) || defined(OS_MACOSX)
971 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
972 { { FPL("foo.\u00E4\u00F6\u00FC"), FPL(".\u00E4\u00F6\u00FC") }, true},
973 { { FPL("foo.\u00C4\u00D6\u00DC"), FPL(".\u00E4\u00F6\u00FC") }, true},
974 // C with circumflex: direct comparison, and upper case vs. lower case
975 { { FPL("foo.\u0109"), FPL(".\u0109") }, true},
976 { { FPL("foo.\u0108"), FPL(".\u0109") }, true},
977#endif
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900978 };
979
980 for (size_t i = 0; i < arraysize(cases); ++i) {
981 FilePath path(cases[i].inputs[0]);
982 FilePath::StringType ext(cases[i].inputs[1]);
983
984 EXPECT_EQ(cases[i].expected, path.MatchesExtension(ext)) <<
985 "i: " << i << ", path: " << path.value() << ", ext: " << ext;
986 }
987}
cevans@chromium.org48dc54c2009-08-16 06:44:31 +0900988
rolandsteiner@chromium.org7cde0312009-10-28 14:40:09 +0900989TEST_F(FilePathTest, CompareIgnoreCase) {
990 const struct BinaryIntTestData cases[] = {
991 { { FPL("foo"), FPL("foo") }, 0},
992 { { FPL("FOO"), FPL("foo") }, 0},
993 { { FPL("foo.ext"), FPL("foo.ext") }, 0},
994 { { FPL("FOO.EXT"), FPL("foo.ext") }, 0},
995 { { FPL("Foo.Ext"), FPL("foo.ext") }, 0},
996 { { FPL("foO"), FPL("foo") }, 0},
997 { { FPL("foo"), FPL("foO") }, 0},
998 { { FPL("fOo"), FPL("foo") }, 0},
999 { { FPL("foo"), FPL("fOo") }, 0},
1000 { { FPL("bar"), FPL("foo") }, -1},
1001 { { FPL("foo"), FPL("bar") }, 1},
1002 { { FPL("BAR"), FPL("foo") }, -1},
1003 { { FPL("FOO"), FPL("bar") }, 1},
1004 { { FPL("bar"), FPL("FOO") }, -1},
1005 { { FPL("foo"), FPL("BAR") }, 1},
1006 { { FPL("BAR"), FPL("FOO") }, -1},
1007 { { FPL("FOO"), FPL("BAR") }, 1},
1008 // German "Eszett" (lower case and the new-fangled upper case)
1009 // Note that uc(<lowercase eszett>) => "SS", NOT <uppercase eszett>!
1010 // However, neither Windows nor Mac OSX converts these.
1011 // (or even have glyphs for <uppercase eszett>)
1012 { { FPL("\u00DF"), FPL("\u00DF") }, 0},
1013 { { FPL("\u1E9E"), FPL("\u1E9E") }, 0},
1014 { { FPL("\u00DF"), FPL("\u1E9E") }, -1},
1015 { { FPL("SS"), FPL("\u00DF") }, -1},
1016 { { FPL("SS"), FPL("\u1E9E") }, -1},
1017#if defined(OS_WIN) || defined(OS_MACOSX)
1018 // Umlauts A, O, U: direct comparison, and upper case vs. lower case
1019 { { FPL("\u00E4\u00F6\u00FC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1020 { { FPL("\u00C4\u00D6\u00DC"), FPL("\u00E4\u00F6\u00FC") }, 0},
1021 // C with circumflex: direct comparison, and upper case vs. lower case
1022 { { FPL("\u0109"), FPL("\u0109") }, 0},
1023 { { FPL("\u0108"), FPL("\u0109") }, 0},
1024 // Cyrillic letter SHA: direct comparison, and upper case vs. lower case
1025 { { FPL("\u0428"), FPL("\u0428") }, 0},
1026 { { FPL("\u0428"), FPL("\u0448") }, 0},
1027 // Greek letter DELTA: direct comparison, and upper case vs. lower case
1028 { { FPL("\u0394"), FPL("\u0394") }, 0},
1029 { { FPL("\u0394"), FPL("\u03B4") }, 0},
1030 // Japanese full-width A: direct comparison, and upper case vs. lower case
1031 // Note that full-width and standard characters are considered different.
1032 { { FPL("\uFF21"), FPL("\uFF21") }, 0},
1033 { { FPL("\uFF21"), FPL("\uFF41") }, 0},
1034 { { FPL("A"), FPL("\uFF21") }, -1},
1035 { { FPL("A"), FPL("\uFF41") }, -1},
1036 { { FPL("a"), FPL("\uFF21") }, -1},
1037 { { FPL("a"), FPL("\uFF41") }, -1},
1038#endif
1039#if defined(OS_MACOSX)
1040 // Codepoints > 0x1000
1041 // Georgian letter DON: direct comparison, and upper case vs. lower case
1042 { { FPL("\u10A3"), FPL("\u10A3") }, 0},
1043 { { FPL("\u10A3"), FPL("\u10D3") }, 0},
1044 // Combining characters vs. pre-composed characters, upper and lower case
1045 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1046 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("kuon") }, 1},
1047 { { FPL("kuon"), FPL("k\u0301u\u032Do\u0304\u0301n") }, -1},
1048 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("KUON") }, 1},
1049 { { FPL("KUON"), FPL("K\u0301U\u032DO\u0304\u0301N") }, -1},
1050 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("KUON") }, 1},
1051 { { FPL("K\u0301U\u032DO\u0304\u0301N"), FPL("\u1E31\u1E77\u1E53n") }, 0},
1052 { { FPL("k\u0301u\u032Do\u0304\u0301n"), FPL("\u1E30\u1E76\u1E52n") }, 0},
1053 { { FPL("k\u0301u\u032Do\u0304\u0302n"), FPL("\u1E30\u1E76\u1E52n") }, 1},
1054#endif
1055 };
1056
1057 for (size_t i = 0; i < arraysize(cases); ++i) {
1058 FilePath::StringType s1(cases[i].inputs[0]);
1059 FilePath::StringType s2(cases[i].inputs[1]);
1060 int result = FilePath::CompareIgnoreCase(s1, s2);
1061 EXPECT_EQ(cases[i].expected, result) <<
1062 "i: " << i << ", s1: " << s1 << ", s2: " << s2;
1063 }
1064}
1065
cevans@chromium.org48dc54c2009-08-16 06:44:31 +09001066TEST_F(FilePathTest, ReferencesParent) {
1067 const struct UnaryBooleanTestData cases[] = {
1068 { FPL("."), false },
1069 { FPL(".."), true },
1070 { FPL("a.."), false },
1071 { FPL("..a"), false },
1072 { FPL("../"), true },
1073 { FPL("/.."), true },
1074 { FPL("/../"), true },
1075 { FPL("/a../"), false },
1076 { FPL("/..a/"), false },
1077 { FPL("//.."), true },
1078 { FPL("..//"), true },
1079 { FPL("//..//"), true },
1080 { FPL("a//..//c"), true },
1081 { FPL("../b/c"), true },
1082 { FPL("/../b/c"), true },
1083 { FPL("a/b/.."), true },
1084 { FPL("a/b/../"), true },
1085 { FPL("a/../c"), true },
1086 { FPL("a/b/c"), false },
1087 };
1088
1089 for (size_t i = 0; i < arraysize(cases); ++i) {
1090 FilePath input(cases[i].input);
1091 bool observed = input.ReferencesParent();
1092 EXPECT_EQ(cases[i].expected, observed) <<
1093 "i: " << i << ", input: " << input.value();
1094 }
1095}
tony@chromium.org65049b12010-06-25 14:33:17 +09001096
satorux@chromium.orgefba4172011-11-02 13:55:23 +09001097TEST_F(FilePathTest, FromUTF8Unsafe_And_AsUTF8Unsafe) {
1098 const struct UTF8TestData cases[] = {
1099 { FPL("foo.txt"), "foo.txt" },
1100 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them.
1101 { FPL("\u00E0\u00E8\u00F2.txt"), "\xC3\xA0\xC3\xA8\xC3\xB2.txt" },
1102 // Full-width "ABC".
1103 { FPL("\uFF21\uFF22\uFF23.txt"),
1104 "\xEF\xBC\xA1\xEF\xBC\xA2\xEF\xBC\xA3.txt" },
1105 };
1106
1107 for (size_t i = 0; i < arraysize(cases); ++i) {
1108 // Test FromUTF8Unsafe() works.
1109 FilePath from_utf8 = FilePath::FromUTF8Unsafe(cases[i].utf8);
1110 EXPECT_EQ(cases[i].native, from_utf8.value())
1111 << "i: " << i << ", input: " << cases[i].native;
1112 // Test AsUTF8Unsafe() works.
1113 FilePath from_native = FilePath(cases[i].native);
1114 EXPECT_EQ(cases[i].utf8, from_native.AsUTF8Unsafe())
1115 << "i: " << i << ", input: " << cases[i].native;
1116 // Test the two file paths are identical.
1117 EXPECT_EQ(from_utf8.value(), from_native.value());
1118 }
1119}
1120
aedla@chromium.org51127b92013-01-28 22:47:55 +09001121TEST_F(FilePathTest, ConstructWithNUL) {
1122 // Assert FPS() works.
1123 ASSERT_TRUE(FPS("a\0b").length() == 3);
1124
1125 // Test constructor strips '\0'
1126 FilePath path(FPS("a\0b"));
1127 EXPECT_TRUE(path.value().length() == 1);
1128 EXPECT_EQ(path.value(), FPL("a"));
1129}
1130
1131TEST_F(FilePathTest, AppendWithNUL) {
1132 // Assert FPS() works.
1133 ASSERT_TRUE(FPS("b\0b").length() == 3);
1134
1135 // Test Append() strips '\0'
1136 FilePath path(FPL("a"));
1137 path = path.Append(FPS("b\0b"));
1138 EXPECT_TRUE(path.value().length() == 3);
1139#if defined(FILE_PATH_USES_WIN_SEPARATORS)
1140 EXPECT_EQ(path.value(), FPL("a\\b"));
1141#else
1142 EXPECT_EQ(path.value(), FPL("a/b"));
1143#endif
1144}
1145
1146TEST_F(FilePathTest, ReferencesParentWithNUL) {
1147 // Assert FPS() works.
1148 ASSERT_TRUE(FPS("..\0").length() == 3);
1149
1150 // Test ReferencesParent() doesn't break with "..\0"
1151 FilePath path(FPS("..\0"));
1152 EXPECT_TRUE(path.ReferencesParent());
1153}
1154
tony@chromium.org65049b12010-06-25 14:33:17 +09001155#if defined(FILE_PATH_USES_WIN_SEPARATORS)
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001156TEST_F(FilePathTest, NormalizePathSeparators) {
tony@chromium.org65049b12010-06-25 14:33:17 +09001157 const struct UnaryTestData cases[] = {
1158 { FPL("foo/bar"), FPL("foo\\bar") },
1159 { FPL("foo/bar\\betz"), FPL("foo\\bar\\betz") },
1160 { FPL("foo\\bar"), FPL("foo\\bar") },
1161 { FPL("foo\\bar/betz"), FPL("foo\\bar\\betz") },
1162 { FPL("foo"), FPL("foo") },
1163 // Trailing slashes don't automatically get stripped. That's what
1164 // StripTrailingSeparators() is for.
1165 { FPL("foo\\"), FPL("foo\\") },
1166 { FPL("foo/"), FPL("foo\\") },
1167 { FPL("foo/bar\\"), FPL("foo\\bar\\") },
1168 { FPL("foo\\bar/"), FPL("foo\\bar\\") },
1169 { FPL("foo/bar/"), FPL("foo\\bar\\") },
1170 { FPL("foo\\bar\\"), FPL("foo\\bar\\") },
1171 { FPL("\\foo/bar"), FPL("\\foo\\bar") },
1172 { FPL("/foo\\bar"), FPL("\\foo\\bar") },
1173 { FPL("c:/foo/bar/"), FPL("c:\\foo\\bar\\") },
1174 { FPL("/foo/bar/"), FPL("\\foo\\bar\\") },
1175 { FPL("\\foo\\bar\\"), FPL("\\foo\\bar\\") },
1176 { FPL("c:\\foo/bar"), FPL("c:\\foo\\bar") },
1177 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1178 { FPL("\\\\foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1179 { FPL("//foo\\bar\\"), FPL("\\\\foo\\bar\\") },
1180 // This method does not normalize the number of path separators.
1181 { FPL("foo\\\\bar"), FPL("foo\\\\bar") },
1182 { FPL("foo//bar"), FPL("foo\\\\bar") },
1183 { FPL("foo/\\bar"), FPL("foo\\\\bar") },
1184 { FPL("foo\\/bar"), FPL("foo\\\\bar") },
1185 { FPL("///foo\\\\bar"), FPL("\\\\\\foo\\\\bar") },
1186 { FPL("foo//bar///"), FPL("foo\\\\bar\\\\\\") },
1187 { FPL("foo/\\bar/\\"), FPL("foo\\\\bar\\\\") },
1188 { FPL("/\\foo\\/bar"), FPL("\\\\foo\\\\bar") },
1189 };
1190 for (size_t i = 0; i < arraysize(cases); ++i) {
1191 FilePath input(cases[i].input);
kinuko@chromium.orgbc0e4022012-02-10 11:04:40 +09001192 FilePath observed = input.NormalizePathSeparators();
tony@chromium.org65049b12010-06-25 14:33:17 +09001193 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
1194 "i: " << i << ", input: " << input.value();
1195 }
1196}
satorux@chromium.orgefba4172011-11-02 13:55:23 +09001197
tony@chromium.org65049b12010-06-25 14:33:17 +09001198#endif