blob: 9d451705b15c9d5fbe5078c6b8a19d3975bb033b [file] [log] [blame]
mark@chromium.org6380e262008-10-04 03:21:47 +09001// Copyright (c) 2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
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"
erikkay@google.comc51bf5f2009-01-22 07:15:57 +09008#include "base/string_util.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
15struct UnaryTestData {
16 const FilePath::CharType* input;
17 const FilePath::CharType* expected;
18};
19
20struct UnaryBooleanTestData {
21 const FilePath::CharType* input;
22 bool expected;
23};
24
25struct BinaryTestData {
26 const FilePath::CharType* inputs[2];
27 const FilePath::CharType* expected;
28};
29
rafaelw@chromium.org465facc2009-06-25 06:28:30 +090030struct BinaryBooleanTestData {
31 const FilePath::CharType* inputs[2];
32 bool expected;
33};
34
aa@chromium.org76e5c632008-12-18 17:38:16 +090035// file_util winds up using autoreleased objects on the Mac, so this needs
36// to be a PlatformTest
37class FilePathTest : public PlatformTest {
38 protected:
39 virtual void SetUp() {
40 PlatformTest::SetUp();
41 }
42 virtual void TearDown() {
43 PlatformTest::TearDown();
44 }
45};
46
47TEST_F(FilePathTest, DirName) {
mark@chromium.org6380e262008-10-04 03:21:47 +090048 const struct UnaryTestData cases[] = {
49 { FPL(""), FPL(".") },
50 { FPL("aa"), FPL(".") },
51 { FPL("/aa/bb"), FPL("/aa") },
52 { FPL("/aa/bb/"), FPL("/aa") },
53 { FPL("/aa/bb//"), FPL("/aa") },
54 { FPL("/aa/bb/ccc"), FPL("/aa/bb") },
55 { FPL("/aa"), FPL("/") },
56 { FPL("/aa/"), FPL("/") },
57 { FPL("/"), FPL("/") },
58 { FPL("//"), FPL("//") },
59 { FPL("///"), FPL("/") },
60 { FPL("aa/"), FPL(".") },
61 { FPL("aa/bb"), FPL("aa") },
62 { FPL("aa/bb/"), FPL("aa") },
63 { FPL("aa/bb//"), FPL("aa") },
64 { FPL("aa//bb//"), FPL("aa") },
65 { FPL("aa//bb/"), FPL("aa") },
66 { FPL("aa//bb"), FPL("aa") },
67 { FPL("//aa/bb"), FPL("//aa") },
68 { FPL("//aa/"), FPL("//") },
69 { FPL("//aa"), FPL("//") },
70 { FPL("0:"), FPL(".") },
71 { FPL("@:"), FPL(".") },
72 { FPL("[:"), FPL(".") },
73 { FPL("`:"), FPL(".") },
74 { FPL("{:"), FPL(".") },
75 { FPL("\xB3:"), FPL(".") },
76 { FPL("\xC5:"), FPL(".") },
77#if defined(OS_WIN)
78 { FPL("\x0143:"), FPL(".") },
79#endif // OS_WIN
80#if defined(FILE_PATH_USES_DRIVE_LETTERS)
81 { FPL("c:"), FPL("c:") },
82 { FPL("C:"), FPL("C:") },
83 { FPL("A:"), FPL("A:") },
84 { FPL("Z:"), FPL("Z:") },
85 { FPL("a:"), FPL("a:") },
86 { FPL("z:"), FPL("z:") },
87 { FPL("c:aa"), FPL("c:") },
88 { FPL("c:/"), FPL("c:/") },
89 { FPL("c://"), FPL("c://") },
90 { FPL("c:///"), FPL("c:/") },
91 { FPL("c:/aa"), FPL("c:/") },
92 { FPL("c:/aa/"), FPL("c:/") },
93 { FPL("c:/aa/bb"), FPL("c:/aa") },
94 { FPL("c:aa/bb"), FPL("c:aa") },
95#endif // FILE_PATH_USES_DRIVE_LETTERS
96#if defined(FILE_PATH_USES_WIN_SEPARATORS)
97 { FPL("\\aa\\bb"), FPL("\\aa") },
98 { FPL("\\aa\\bb\\"), FPL("\\aa") },
99 { FPL("\\aa\\bb\\\\"), FPL("\\aa") },
100 { FPL("\\aa\\bb\\ccc"), FPL("\\aa\\bb") },
101 { FPL("\\aa"), FPL("\\") },
102 { FPL("\\aa\\"), FPL("\\") },
103 { FPL("\\"), FPL("\\") },
104 { FPL("\\\\"), FPL("\\\\") },
105 { FPL("\\\\\\"), FPL("\\") },
106 { FPL("aa\\"), FPL(".") },
107 { FPL("aa\\bb"), FPL("aa") },
108 { FPL("aa\\bb\\"), FPL("aa") },
109 { FPL("aa\\bb\\\\"), FPL("aa") },
110 { FPL("aa\\\\bb\\\\"), FPL("aa") },
111 { FPL("aa\\\\bb\\"), FPL("aa") },
112 { FPL("aa\\\\bb"), FPL("aa") },
113 { FPL("\\\\aa\\bb"), FPL("\\\\aa") },
114 { FPL("\\\\aa\\"), FPL("\\\\") },
115 { FPL("\\\\aa"), FPL("\\\\") },
116#if defined(FILE_PATH_USES_DRIVE_LETTERS)
117 { FPL("c:\\"), FPL("c:\\") },
118 { FPL("c:\\\\"), FPL("c:\\\\") },
119 { FPL("c:\\\\\\"), FPL("c:\\") },
120 { FPL("c:\\aa"), FPL("c:\\") },
121 { FPL("c:\\aa\\"), FPL("c:\\") },
122 { FPL("c:\\aa\\bb"), FPL("c:\\aa") },
123 { FPL("c:aa\\bb"), FPL("c:aa") },
124#endif // FILE_PATH_USES_DRIVE_LETTERS
125#endif // FILE_PATH_USES_WIN_SEPARATORS
126 };
127
128 for (size_t i = 0; i < arraysize(cases); ++i) {
129 FilePath input(cases[i].input);
130 FilePath observed = input.DirName();
131 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
132 "i: " << i << ", input: " << input.value();
133 }
134}
135
aa@chromium.org76e5c632008-12-18 17:38:16 +0900136TEST_F(FilePathTest, BaseName) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900137 const struct UnaryTestData cases[] = {
138 { FPL(""), FPL("") },
139 { FPL("aa"), FPL("aa") },
140 { FPL("/aa/bb"), FPL("bb") },
141 { FPL("/aa/bb/"), FPL("bb") },
142 { FPL("/aa/bb//"), FPL("bb") },
143 { FPL("/aa/bb/ccc"), FPL("ccc") },
144 { FPL("/aa"), FPL("aa") },
145 { FPL("/"), FPL("/") },
146 { FPL("//"), FPL("//") },
147 { FPL("///"), FPL("/") },
148 { FPL("aa/"), FPL("aa") },
149 { FPL("aa/bb"), FPL("bb") },
150 { FPL("aa/bb/"), FPL("bb") },
151 { FPL("aa/bb//"), FPL("bb") },
152 { FPL("aa//bb//"), FPL("bb") },
153 { FPL("aa//bb/"), FPL("bb") },
154 { FPL("aa//bb"), FPL("bb") },
155 { FPL("//aa/bb"), FPL("bb") },
156 { FPL("//aa/"), FPL("aa") },
157 { FPL("//aa"), FPL("aa") },
158 { FPL("0:"), FPL("0:") },
159 { FPL("@:"), FPL("@:") },
160 { FPL("[:"), FPL("[:") },
161 { FPL("`:"), FPL("`:") },
162 { FPL("{:"), FPL("{:") },
163 { FPL("\xB3:"), FPL("\xB3:") },
164 { FPL("\xC5:"), FPL("\xC5:") },
165#if defined(OS_WIN)
166 { FPL("\x0143:"), FPL("\x0143:") },
167#endif // OS_WIN
168#if defined(FILE_PATH_USES_DRIVE_LETTERS)
169 { FPL("c:"), FPL("") },
170 { FPL("C:"), FPL("") },
171 { FPL("A:"), FPL("") },
172 { FPL("Z:"), FPL("") },
173 { FPL("a:"), FPL("") },
174 { FPL("z:"), FPL("") },
175 { FPL("c:aa"), FPL("aa") },
176 { FPL("c:/"), FPL("/") },
177 { FPL("c://"), FPL("//") },
178 { FPL("c:///"), FPL("/") },
179 { FPL("c:/aa"), FPL("aa") },
180 { FPL("c:/aa/"), FPL("aa") },
181 { FPL("c:/aa/bb"), FPL("bb") },
182 { FPL("c:aa/bb"), FPL("bb") },
183#endif // FILE_PATH_USES_DRIVE_LETTERS
184#if defined(FILE_PATH_USES_WIN_SEPARATORS)
185 { FPL("\\aa\\bb"), FPL("bb") },
186 { FPL("\\aa\\bb\\"), FPL("bb") },
187 { FPL("\\aa\\bb\\\\"), FPL("bb") },
188 { FPL("\\aa\\bb\\ccc"), FPL("ccc") },
189 { FPL("\\aa"), FPL("aa") },
190 { FPL("\\"), FPL("\\") },
191 { FPL("\\\\"), FPL("\\\\") },
192 { FPL("\\\\\\"), FPL("\\") },
193 { FPL("aa\\"), FPL("aa") },
194 { FPL("aa\\bb"), FPL("bb") },
195 { FPL("aa\\bb\\"), FPL("bb") },
196 { FPL("aa\\bb\\\\"), FPL("bb") },
197 { FPL("aa\\\\bb\\\\"), FPL("bb") },
198 { FPL("aa\\\\bb\\"), FPL("bb") },
199 { FPL("aa\\\\bb"), FPL("bb") },
200 { FPL("\\\\aa\\bb"), FPL("bb") },
201 { FPL("\\\\aa\\"), FPL("aa") },
202 { FPL("\\\\aa"), FPL("aa") },
203#if defined(FILE_PATH_USES_DRIVE_LETTERS)
204 { FPL("c:\\"), FPL("\\") },
205 { FPL("c:\\\\"), FPL("\\\\") },
206 { FPL("c:\\\\\\"), FPL("\\") },
207 { FPL("c:\\aa"), FPL("aa") },
208 { FPL("c:\\aa\\"), FPL("aa") },
209 { FPL("c:\\aa\\bb"), FPL("bb") },
210 { FPL("c:aa\\bb"), FPL("bb") },
211#endif // FILE_PATH_USES_DRIVE_LETTERS
212#endif // FILE_PATH_USES_WIN_SEPARATORS
213 };
214
215 for (size_t i = 0; i < arraysize(cases); ++i) {
216 FilePath input(cases[i].input);
estade@chromium.orgd872d682009-01-06 08:59:36 +0900217 FilePath observed = input.BaseName();
218 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
mark@chromium.org6380e262008-10-04 03:21:47 +0900219 "i: " << i << ", input: " << input.value();
220 }
221}
222
aa@chromium.org76e5c632008-12-18 17:38:16 +0900223TEST_F(FilePathTest, Append) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900224 const struct BinaryTestData cases[] = {
225 { { FPL(""), FPL("cc") }, FPL("cc") },
226 { { FPL("."), FPL("ff") }, FPL("ff") },
227 { { FPL("/"), FPL("cc") }, FPL("/cc") },
228 { { FPL("/aa"), FPL("") }, FPL("/aa") },
229 { { FPL("/aa/"), FPL("") }, FPL("/aa") },
230 { { FPL("//aa"), FPL("") }, FPL("//aa") },
231 { { FPL("//aa/"), FPL("") }, FPL("//aa") },
232 { { FPL("//"), FPL("aa") }, FPL("//aa") },
233#if defined(FILE_PATH_USES_DRIVE_LETTERS)
234 { { FPL("c:"), FPL("a") }, FPL("c:a") },
235 { { FPL("c:"), FPL("") }, FPL("c:") },
236 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
237 { { FPL("c://"), FPL("a") }, FPL("c://a") },
238 { { FPL("c:///"), FPL("a") }, FPL("c:/a") },
239#endif // FILE_PATH_USES_DRIVE_LETTERS
240#if defined(FILE_PATH_USES_WIN_SEPARATORS)
241 // Append introduces the default separator character, so these test cases
242 // need to be defined with different expected results on platforms that use
243 // different default separator characters.
244 { { FPL("\\"), FPL("cc") }, FPL("\\cc") },
245 { { FPL("\\aa"), FPL("") }, FPL("\\aa") },
246 { { FPL("\\aa\\"), FPL("") }, FPL("\\aa") },
247 { { FPL("\\\\aa"), FPL("") }, FPL("\\\\aa") },
248 { { FPL("\\\\aa\\"), FPL("") }, FPL("\\\\aa") },
249 { { FPL("\\\\"), FPL("aa") }, FPL("\\\\aa") },
250 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb\\cc") },
251 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb\\cc") },
252 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb\\cc") },
253 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb\\cc") },
254 { { FPL("a/b"), FPL("c") }, FPL("a/b\\c") },
255 { { FPL("a/b/"), FPL("c") }, FPL("a/b\\c") },
256 { { FPL("//aa"), FPL("bb") }, FPL("//aa\\bb") },
257 { { FPL("//aa/"), FPL("bb") }, FPL("//aa\\bb") },
258 { { FPL("\\aa\\bb"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
259 { { FPL("\\aa\\bb\\"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
260 { { FPL("aa\\bb\\"), FPL("cc") }, FPL("aa\\bb\\cc") },
261 { { FPL("aa\\bb"), FPL("cc") }, FPL("aa\\bb\\cc") },
262 { { FPL("a\\b"), FPL("c") }, FPL("a\\b\\c") },
263 { { FPL("a\\b\\"), FPL("c") }, FPL("a\\b\\c") },
264 { { FPL("\\\\aa"), FPL("bb") }, FPL("\\\\aa\\bb") },
265 { { FPL("\\\\aa\\"), FPL("bb") }, FPL("\\\\aa\\bb") },
266#if defined(FILE_PATH_USES_DRIVE_LETTERS)
267 { { FPL("c:\\"), FPL("a") }, FPL("c:\\a") },
268 { { FPL("c:\\\\"), FPL("a") }, FPL("c:\\\\a") },
269 { { FPL("c:\\\\\\"), FPL("a") }, FPL("c:\\a") },
270 { { FPL("c:\\"), FPL("") }, FPL("c:\\") },
271 { { FPL("c:\\a"), FPL("b") }, FPL("c:\\a\\b") },
272 { { FPL("c:\\a\\"), FPL("b") }, FPL("c:\\a\\b") },
273#endif // FILE_PATH_USES_DRIVE_LETTERS
274#else // FILE_PATH_USES_WIN_SEPARATORS
275 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb/cc") },
276 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb/cc") },
277 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb/cc") },
278 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb/cc") },
279 { { FPL("a/b"), FPL("c") }, FPL("a/b/c") },
280 { { FPL("a/b/"), FPL("c") }, FPL("a/b/c") },
281 { { FPL("//aa"), FPL("bb") }, FPL("//aa/bb") },
282 { { FPL("//aa/"), FPL("bb") }, FPL("//aa/bb") },
283#if defined(FILE_PATH_USES_DRIVE_LETTERS)
284 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
285 { { FPL("c:/"), FPL("") }, FPL("c:/") },
286 { { FPL("c:/a"), FPL("b") }, FPL("c:/a/b") },
287 { { FPL("c:/a/"), FPL("b") }, FPL("c:/a/b") },
288#endif // FILE_PATH_USES_DRIVE_LETTERS
289#endif // FILE_PATH_USES_WIN_SEPARATORS
290 };
291
292 for (size_t i = 0; i < arraysize(cases); ++i) {
293 FilePath root(cases[i].inputs[0]);
294 FilePath::StringType leaf(cases[i].inputs[1]);
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900295 FilePath observed_str = root.Append(leaf);
296 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
297 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
298 FilePath observed_path = root.Append(FilePath(leaf));
299 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) <<
mark@chromium.org6380e262008-10-04 03:21:47 +0900300 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900301
302 // TODO(erikkay): It would be nice to have a unicode test append value to
303 // handle the case when AppendASCII is passed UTF8
304#if defined(OS_WIN)
305 std::string ascii = WideToASCII(leaf);
306#elif defined(OS_POSIX)
307 std::string ascii = leaf;
308#endif
309 observed_str = root.AppendASCII(ascii);
310 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
311 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
mark@chromium.org6380e262008-10-04 03:21:47 +0900312 }
313}
314
mark@chromium.org778d9cf2009-10-14 02:31:39 +0900315TEST_F(FilePathTest, StripTrailingSeparators) {
316 const struct UnaryTestData cases[] = {
317 { FPL(""), FPL("") },
318 { FPL("/"), FPL("/") },
319 { FPL("//"), FPL("//") },
320 { FPL("///"), FPL("/") },
321 { FPL("////"), FPL("/") },
322 { FPL("a/"), FPL("a") },
323 { FPL("a//"), FPL("a") },
324 { FPL("a///"), FPL("a") },
325 { FPL("a////"), FPL("a") },
326 { FPL("/a"), FPL("/a") },
327 { FPL("/a/"), FPL("/a") },
328 { FPL("/a//"), FPL("/a") },
329 { FPL("/a///"), FPL("/a") },
330 { FPL("/a////"), FPL("/a") },
331#if defined(FILE_PATH_USES_DRIVE_LETTERS)
332 { FPL("c:"), FPL("c:") },
333 { FPL("c:/"), FPL("c:/") },
334 { FPL("c://"), FPL("c://") },
335 { FPL("c:///"), FPL("c:/") },
336 { FPL("c:////"), FPL("c:/") },
337 { FPL("c:/a"), FPL("c:/a") },
338 { FPL("c:/a/"), FPL("c:/a") },
339 { FPL("c:/a//"), FPL("c:/a") },
340 { FPL("c:/a///"), FPL("c:/a") },
341 { FPL("c:/a////"), FPL("c:/a") },
342#endif // FILE_PATH_USES_DRIVE_LETTERS
343#if defined(FILE_PATH_USES_WIN_SEPARATORS)
344 { FPL("\\"), FPL("\\") },
345 { FPL("\\\\"), FPL("\\\\") },
346 { FPL("\\\\\\"), FPL("\\") },
347 { FPL("\\\\\\\\"), FPL("\\") },
348 { FPL("a\\"), FPL("a") },
349 { FPL("a\\\\"), FPL("a") },
350 { FPL("a\\\\\\"), FPL("a") },
351 { FPL("a\\\\\\\\"), FPL("a") },
352 { FPL("\\a"), FPL("\\a") },
353 { FPL("\\a\\"), FPL("\\a") },
354 { FPL("\\a\\\\"), FPL("\\a") },
355 { FPL("\\a\\\\\\"), FPL("\\a") },
356 { FPL("\\a\\\\\\\\"), FPL("\\a") },
357#if defined(FILE_PATH_USES_DRIVE_LETTERS)
358 { FPL("c:\\"), FPL("c:\\") },
359 { FPL("c:\\\\"), FPL("c:\\\\") },
360 { FPL("c:\\\\\\"), FPL("c:\\") },
361 { FPL("c:\\\\\\\\"), FPL("c:\\") },
362 { FPL("c:\\a"), FPL("c:\\a") },
363 { FPL("c:\\a\\"), FPL("c:\\a") },
364 { FPL("c:\\a\\\\"), FPL("c:\\a") },
365 { FPL("c:\\a\\\\\\"), FPL("c:\\a") },
366 { FPL("c:\\a\\\\\\\\"), FPL("c:\\a") },
367#endif // FILE_PATH_USES_DRIVE_LETTERS
368#endif // FILE_PATH_USES_WIN_SEPARATORS
369 };
370
371 for (size_t i = 0; i < arraysize(cases); ++i) {
372 FilePath input(cases[i].input);
373 FilePath observed = input.StripTrailingSeparators();
374 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
375 "i: " << i << ", input: " << input.value();
376 }
377}
378
aa@chromium.org76e5c632008-12-18 17:38:16 +0900379TEST_F(FilePathTest, IsAbsolute) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900380 const struct UnaryBooleanTestData cases[] = {
381 { FPL(""), false },
382 { FPL("a"), false },
383 { FPL("c:"), false },
384 { FPL("c:a"), false },
385 { FPL("a/b"), false },
386 { FPL("//"), true },
387 { FPL("//a"), true },
388 { FPL("c:a/b"), false },
389 { FPL("?:/a"), false },
390#if defined(FILE_PATH_USES_DRIVE_LETTERS)
391 { FPL("/"), false },
392 { FPL("/a"), false },
393 { FPL("/."), false },
394 { FPL("/.."), false },
395 { FPL("c:/"), true },
396 { FPL("c:/a"), true },
397 { FPL("c:/."), true },
398 { FPL("c:/.."), true },
399 { FPL("C:/a"), true },
400 { FPL("d:/a"), true },
401#else // FILE_PATH_USES_DRIVE_LETTERS
402 { FPL("/"), true },
403 { FPL("/a"), true },
404 { FPL("/."), true },
405 { FPL("/.."), true },
406 { FPL("c:/"), false },
407#endif // FILE_PATH_USES_DRIVE_LETTERS
408#if defined(FILE_PATH_USES_WIN_SEPARATORS)
409 { FPL("a\\b"), false },
410 { FPL("\\\\"), true },
411 { FPL("\\\\a"), true },
412 { FPL("a\\b"), false },
413 { FPL("\\\\"), true },
414 { FPL("//a"), true },
415 { FPL("c:a\\b"), false },
416 { FPL("?:\\a"), false },
417#if defined(FILE_PATH_USES_DRIVE_LETTERS)
418 { FPL("\\"), false },
419 { FPL("\\a"), false },
420 { FPL("\\."), false },
421 { FPL("\\.."), false },
422 { FPL("c:\\"), true },
423 { FPL("c:\\"), true },
424 { FPL("c:\\a"), true },
425 { FPL("c:\\."), true },
426 { FPL("c:\\.."), true },
427 { FPL("C:\\a"), true },
428 { FPL("d:\\a"), true },
429#else // FILE_PATH_USES_DRIVE_LETTERS
430 { FPL("\\"), true },
431 { FPL("\\a"), true },
432 { FPL("\\."), true },
433 { FPL("\\.."), true },
434 { FPL("c:\\"), false },
435#endif // FILE_PATH_USES_DRIVE_LETTERS
436#endif // FILE_PATH_USES_WIN_SEPARATORS
437 };
438
439 for (size_t i = 0; i < arraysize(cases); ++i) {
440 FilePath input(cases[i].input);
441 bool observed = input.IsAbsolute();
442 EXPECT_EQ(cases[i].expected, observed) <<
443 "i: " << i << ", input: " << input.value();
444 }
445}
aa@chromium.org76e5c632008-12-18 17:38:16 +0900446
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900447TEST_F(FilePathTest, PathComponentsTest) {
448 const struct UnaryTestData cases[] = {
449 { FPL("//foo/bar/baz/"), FPL("|//|foo|bar|baz")},
450 { FPL("///"), FPL("|/")},
451 { FPL("/foo//bar//baz/"), FPL("|/|foo|bar|baz")},
452 { FPL("/foo/bar/baz/"), FPL("|/|foo|bar|baz")},
453 { FPL("/foo/bar/baz//"), FPL("|/|foo|bar|baz")},
454 { FPL("/foo/bar/baz///"), FPL("|/|foo|bar|baz")},
455 { FPL("/foo/bar/baz"), FPL("|/|foo|bar|baz")},
456 { FPL("/foo/bar.bot/baz.txt"), FPL("|/|foo|bar.bot|baz.txt")},
457 { FPL("//foo//bar/baz"), FPL("|//|foo|bar|baz")},
458 { FPL("/"), FPL("|/")},
459 { FPL("foo"), FPL("|foo")},
460 { FPL(""), FPL("")},
461#if defined(FILE_PATH_USES_DRIVE_LETTERS)
462 { FPL("e:/foo"), FPL("|e:|/|foo")},
463 { FPL("e:/"), FPL("|e:|/")},
464 { FPL("e:"), FPL("|e:")},
465#endif // FILE_PATH_USES_DRIVE_LETTERS
466#if defined(FILE_PATH_USES_WIN_SEPARATORS)
467 { FPL("../foo"), FPL("|..|foo")},
468 { FPL("./foo"), FPL("|foo")},
469 { FPL("../foo/bar/"), FPL("|..|foo|bar") },
470 { FPL("\\\\foo\\bar\\baz\\"), FPL("|\\\\|foo|bar|baz")},
471 { FPL("\\\\\\"), FPL("|\\")},
472 { FPL("\\foo\\\\bar\\\\baz\\"), FPL("|\\|foo|bar|baz")},
473 { FPL("\\foo\\bar\\baz\\"), FPL("|\\|foo|bar|baz")},
474 { FPL("\\foo\\bar\\baz\\\\"), FPL("|\\|foo|bar|baz")},
475 { FPL("\\foo\\bar\\baz\\\\\\"), FPL("|\\|foo|bar|baz")},
476 { FPL("\\foo\\bar\\baz"), FPL("|\\|foo|bar|baz")},
477 { FPL("\\foo\\bar/baz\\\\\\"), FPL("|\\|foo|bar|baz")},
478 { FPL("/foo\\bar\\baz"), FPL("|/|foo|bar|baz")},
479 { FPL("\\foo\\bar.bot\\baz.txt"), FPL("|\\|foo|bar.bot|baz.txt")},
480 { FPL("\\\\foo\\\\bar\\baz"), FPL("|\\\\|foo|bar|baz")},
481 { FPL("\\"), FPL("|\\")},
482#endif // FILE_PATH_USES_WIN_SEPARATORS
483 };
484
485 for (size_t i = 0; i < arraysize(cases); ++i) {
486 FilePath input(cases[i].input);
487 std::vector<FilePath::StringType> comps;
488 input.GetComponents(&comps);
489
490 FilePath::StringType observed;
491 for (size_t j = 0; j < comps.size(); ++j) {
492 observed.append(FILE_PATH_LITERAL("|"), 1);
493 observed.append(comps[j]);
494 }
495 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed) <<
496 "i: " << i << ", input: " << input.value();
497 }
498}
499
mad@chromium.orgcc4f7b32009-10-21 00:27:21 +0900500TEST_F(FilePathTest, AppendAndResolveRelativeTest) {
501 const struct BinaryTestData cases[] = {
502#if defined(FILE_PATH_USES_DRIVE_LETTERS)
503 { { FPL("c:/"), FPL("foo") }, FPL("c:/foo") },
504 { { FPL("f:/foo/bar"), FPL("..") }, FPL("f:/foo") },
505 { { FPL("f:/foo.bar"), FPL("..") }, FPL("f:/") },
506 { { FPL("F:/foo/.."), FPL("./bar/.") }, FPL("F:/bar") },
507 { { FPL("E:/Foo/bar"), FPL("../..") }, FPL("E:/") },
508 { { FPL("E:/Foo/bar/."), FPL("../..") }, FPL("E:/") },
509 { { FPL("e:/foo/.."), FPL("bar/..") }, FPL("e:/") },
510 { { FPL("c:/foo/./bar/.."), FPL("../baz") }, FPL("c:/baz") },
511 { { FPL("E:/./foo/bar/.."), FPL("../baz/..") }, FPL("E:/") },
512 { { FPL("x:/foo/../bar/.."), FPL("baz/../boo") }, FPL("x:/boo") },
513 { { FPL("E:/foo.bar/.."), FPL("../baz/..") }, FPL("") },
514 { { FPL("Z:/foo"), FPL("../..") }, FPL("") },
515 { { FPL("y:/"), FPL("..") }, FPL("") },
516 { { FPL("B:/.."), FPL("bar/.") }, FPL("") },
517 { { FPL("a:/foo/.."), FPL("..") }, FPL("") },
518 { { FPL("r:/.."), FPL("..") }, FPL("") },
519 { { FPL("F:/foo/.."), FPL("../..") }, FPL("") },
520 { { FPL("O:/foo/bar/.."), FPL("../..") }, FPL("") },
521#endif // FILE_PATH_USES_DRIVE_LETTERS
522#if defined(FILE_PATH_USES_WIN_SEPARATORS)
523 { { FPL("\\\\"), FPL("foo") }, FPL("\\\\foo") },
524 { { FPL("\\\\foo"), FPL("bar") }, FPL("\\\\foo\\bar") },
525 { { FPL("\\\\foo\\bar"), FPL("..") }, FPL("\\\\foo") },
526 { { FPL("\\\\foo.bar"), FPL("..") }, FPL("\\\\") },
527 { { FPL("\\\\Foo\\bar"), FPL("..\\..") }, FPL("\\\\") },
528 { { FPL("\\\\Foo\\bar\\."), FPL("..\\..") }, FPL("\\\\") },
529 { { FPL("\\\\foo\\bar"), FPL("foo\\..\\baz") }, FPL("\\\\foo\\bar\\baz") },
530 { { FPL("\\\\foo\\.\\bar"), FPL("..\\baz\\.") }, FPL("\\\\foo\\baz") },
531 { { FPL("\\\\.\\foo\\.."), FPL("bar") }, FPL("\\\\bar") },
532 { { FPL("\\\\foo\\.."), FPL(".\\bar\\..") }, FPL("\\\\") },
533 { { FPL("\\\\foo\\bar\\.."), FPL("..\\baz") }, FPL("\\\\baz") },
534 { { FPL("\\\\foo\\bar\\.."), FPL("..\\baz\\..") }, FPL("\\\\") },
535 { { FPL("\\\\foo\\..\\bar\\.."), FPL("baz\\..\\boo") }, FPL("\\\\boo"), },
536 { { FPL("\\\\foo.bar\\.."), FPL("..\\baz\\..") }, FPL("") },
537 { { FPL("\\\\foo"), FPL("..\\..") }, FPL("") },
538 { { FPL("\\\\"), FPL("..") }, FPL("") },
539 { { FPL("\\\\.."), FPL("bar\\.") }, FPL("") },
540 { { FPL("\\\\foo\\.."), FPL("..") }, FPL("") },
541 { { FPL("\\\\.."), FPL("..") }, FPL("") },
542 { { FPL("\\\\foo\\.."), FPL("..\\..") }, FPL("") },
543 { { FPL("\\\\foo\\bar\\.."), FPL("..\\..") }, FPL("") },
544#if defined(FILE_PATH_USES_DRIVE_LETTERS)
545 { { FPL("E:/foo"), FPL("bar") }, FPL("E:/foo\\bar") },
546 { { FPL("C:/foo/bar"), FPL("foo/../baz") }, FPL("C:/foo\\bar\\baz") },
547 { { FPL("e:/foo/bar"), FPL("../baz") }, FPL("e:/foo\\baz") },
548#endif
549#else // FILE_PATH_USES_WIN_SEPARAORS
550 { { FPL("/"), FPL("foo") }, FPL("/foo") },
551 { { FPL("/foo"), FPL("bar") }, FPL("/foo/bar") },
552 { { FPL("/foo/bar/"), FPL("..") }, FPL("/foo") },
553 { { FPL("/foo.bar"), FPL("..") }, FPL("/") },
554 { { FPL("//foo"), FPL("..") }, FPL("//") },
555 { { FPL("/foo/./bar"), FPL("../..") }, FPL("/") },
556 { { FPL("/foo/bar/."), FPL("foo/../baz") }, FPL("/foo/bar/baz") },
557 { { FPL("/./foo/bar"), FPL("../baz/.") }, FPL("/foo/baz") },
558 { { FPL("/foo/.."), FPL("./bar") }, FPL("/bar") },
559 { { FPL("/foo/.."), FPL("bar/..") }, FPL("/") },
560 { { FPL("//foo/bar/.."), FPL("../baz") }, FPL("//baz") },
561 { { FPL("/foo/bar/.."), FPL("../baz/..") }, FPL("/") },
562 { { FPL("/foo/../bar/.."), FPL("baz/../boo") }, FPL("/boo") },
563 { { FPL("//foo.bar/.."), FPL("../baz") }, FPL("") },
564 { { FPL("/foo"), FPL("../..") }, FPL("") },
565 { { FPL("//"), FPL("..") }, FPL("") },
566 { { FPL("/.."), FPL("./bar") }, FPL("") },
567 { { FPL("/foo/.."), FPL("..") }, FPL("") },
568 { { FPL("/.."), FPL("..") }, FPL("") },
569 { { FPL("/foo/.."), FPL("../..") }, FPL("") },
570 { { FPL("/foo/bar/.."), FPL("../..") }, FPL("") },
571#if defined(FILE_PATH_USES_DRIVE_LETTERS)
572 { { FPL("E:/foo"), FPL("bar") }, FPL("E:/foo/bar") },
573 { { FPL("C:/foo/bar"), FPL("foo/../baz") }, FPL("C:/foo/bar/baz") },
574 { { FPL("e:/foo/bar"), FPL("../baz") }, FPL("e:/foo/baz") },
575#endif
576#endif // FILE_PATH_USES_WIN_SEPARAORS
577 };
578
579 for (size_t i = 0; i < arraysize(cases); ++i) {
580 FilePath parent(cases[i].inputs[0]);
581 FilePath child(cases[i].inputs[1]);
582
583 FilePath result;
584 EXPECT_EQ(cases[i].expected[0] != '\0',
585 parent.AppendAndResolveRelative(child, &result)) <<
586 "i: " << i << ", parent: " << parent.value() << ", child: " <<
587 child.value();
588 EXPECT_STREQ(cases[i].expected, result.value().c_str());
589 }
590}
591
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900592TEST_F(FilePathTest, IsParentTest) {
593 const struct BinaryBooleanTestData cases[] = {
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900594 { { FPL("/"), FPL("/foo/bar/baz") }, true},
595 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, true},
596 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, true},
597 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, true},
598 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, false},
599 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, false},
600 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, false},
601 { { FPL("/foo/bar"), FPL("/foo/bar") }, false},
602 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
603 { { FPL("foo/bar"), FPL("foo/bar/baz") }, true},
604 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, false},
605 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, false},
606 { { FPL(""), FPL("foo") }, false},
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900607#if defined(FILE_PATH_USES_DRIVE_LETTERS)
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900608 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, true},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900609 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, true},
610 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, true},
611 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, false},
612 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900613 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, true},
614 { { FPL("c:"), FPL("c:/foo/bar/baz") }, true},
615 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900616 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, false},
617 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900618 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900619 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, false},
620 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, false},
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900621 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, false},
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900622#endif // FILE_PATH_USES_DRIVE_LETTERS
623#if defined(FILE_PATH_USES_WIN_SEPARATORS)
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900624 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, true},
625 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, true},
626 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, true},
627 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, true},
628 { { FPL(""), FPL("\\foo\\bar\\baz") }, false},
629 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, false},
630 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, false},
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900631#endif // FILE_PATH_USES_WIN_SEPARATORS
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900632 };
633
634 for (size_t i = 0; i < arraysize(cases); ++i) {
635 FilePath parent(cases[i].inputs[0]);
636 FilePath child(cases[i].inputs[1]);
637
638 EXPECT_EQ(parent.IsParent(child), cases[i].expected) <<
mad@chromium.orgcc4f7b32009-10-21 00:27:21 +0900639 "i: " << i << ", parent: " << parent.value() << ", child: " <<
640 child.value();
rafaelw@chromium.org465facc2009-06-25 06:28:30 +0900641 }
642}
643
mark@chromium.org752010c2009-09-24 05:32:41 +0900644TEST_F(FilePathTest, AppendRelativePathTest) {
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900645 const struct BinaryTestData cases[] = {
mark@chromium.org752010c2009-09-24 05:32:41 +0900646#if defined(FILE_PATH_USES_WIN_SEPARATORS)
647 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo\\bar\\baz")},
648#else // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900649 { { FPL("/"), FPL("/foo/bar/baz") }, FPL("foo/bar/baz")},
mark@chromium.org752010c2009-09-24 05:32:41 +0900650#endif // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900651 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, FPL("baz")},
652 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, FPL("baz")},
653 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, FPL("baz")},
654 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, FPL("")},
655 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, FPL("")},
656 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, FPL("")},
657 { { FPL("/foo/bar"), FPL("/foo/bar") }, FPL("")},
658 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, FPL("")},
659 { { FPL("foo/bar"), FPL("foo/bar/baz") }, FPL("baz")},
660 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, FPL("")},
661 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, FPL("")},
662 { { FPL(""), FPL("foo") }, FPL("")},
663#if defined(FILE_PATH_USES_DRIVE_LETTERS)
664 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, FPL("baz")},
665 { { FPL("E:/foo/bar"), FPL("e:/foo/bar/baz") }, FPL("baz")},
666 { { FPL("f:/foo/bar"), FPL("F:/foo/bar/baz") }, FPL("baz")},
667 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar/baz") }, FPL("")},
668 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar/baz") }, FPL("")},
mark@chromium.org752010c2009-09-24 05:32:41 +0900669#if defined(FILE_PATH_USES_WIN_SEPARATORS)
670 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
671 // TODO(akalin): Figure out how to handle the corner case in the
672 // commented-out test case below. Appending to an empty path gives
673 // /foo\bar\baz but appending to a nonempty path "blah" gives
674 // blah\foo\bar\baz.
675 // { { FPL("c:"), FPL("c:/foo/bar/baz") }, FPL("foo\\bar\\baz")},
676#endif // FILE_PATH_USES_WIN_SEPARATORS
mark@chromium.orgaf9f62a2009-09-17 06:03:44 +0900677 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
678 { { FPL("c:/foo/bar"), FPL("D:/foo/bar/baz") }, FPL("")},
679 { { FPL("C:/foo/bar"), FPL("d:/foo/bar/baz") }, FPL("")},
680 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, FPL("")},
681 { { FPL("e:/foo/bar"), FPL("E:/foo2/bar/baz") }, FPL("")},
682 { { FPL("F:/foo/bar"), FPL("f:/foo2/bar/baz") }, FPL("")},
683 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, FPL("")},
684#endif // FILE_PATH_USES_DRIVE_LETTERS
685#if defined(FILE_PATH_USES_WIN_SEPARATORS)
686 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
687 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, FPL("baz")},
688 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, FPL("baz")},
689 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, FPL("foo\\bar\\baz")},
690 { { FPL(""), FPL("\\foo\\bar\\baz") }, FPL("")},
691 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, FPL("")},
692 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, FPL("")},
693#endif // FILE_PATH_USES_WIN_SEPARATORS
694 };
695
696 const FilePath base(FPL("blah"));
697
698 for (size_t i = 0; i < arraysize(cases); ++i) {
699 FilePath parent(cases[i].inputs[0]);
700 FilePath child(cases[i].inputs[1]);
701 {
702 FilePath result;
703 bool success = parent.AppendRelativePath(child, &result);
704 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
705 "i: " << i << ", parent: " << parent.value() << ", child: " <<
706 child.value();
707 EXPECT_STREQ(cases[i].expected, result.value().c_str()) <<
708 "i: " << i << ", parent: " << parent.value() << ", child: " <<
709 child.value();
710 }
711 {
712 FilePath result(base);
713 bool success = parent.AppendRelativePath(child, &result);
714 EXPECT_EQ(cases[i].expected[0] != '\0', success) <<
715 "i: " << i << ", parent: " << parent.value() << ", child: " <<
716 child.value();
717 EXPECT_EQ(base.Append(cases[i].expected).value(), result.value()) <<
718 "i: " << i << ", parent: " << parent.value() << ", child: " <<
719 child.value();
720 }
721 }
722}
723
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900724TEST_F(FilePathTest, EqualityTest) {
725 const struct BinaryBooleanTestData cases[] = {
726 { { FPL("/foo/bar/baz"), FPL("/foo/bar/baz") }, true},
727 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, false},
728 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
729 { { FPL("//foo/bar/"), FPL("//foo/bar/") }, true},
730 { { FPL("/foo/bar"), FPL("/foo2/bar") }, false},
731 { { FPL("/foo/bar.txt"), FPL("/foo/bar") }, false},
732 { { FPL("foo/bar"), FPL("foo/bar") }, true},
733 { { FPL("foo/bar"), FPL("foo/bar/baz") }, false},
734 { { FPL(""), FPL("foo") }, false},
735#if defined(FILE_PATH_USES_DRIVE_LETTERS)
736 { { FPL("c:/foo/bar"), FPL("c:/foo/bar") }, true},
737 { { FPL("E:/foo/bar"), FPL("e:/foo/bar") }, true},
738 { { FPL("f:/foo/bar"), FPL("F:/foo/bar") }, true},
739 { { FPL("E:/Foo/bar"), FPL("e:/foo/bar") }, false},
740 { { FPL("f:/foo/bar"), FPL("F:/foo/Bar") }, false},
741 { { FPL("c:/"), FPL("c:/") }, true},
742 { { FPL("c:"), FPL("c:") }, true},
743 { { FPL("c:/foo/bar"), FPL("d:/foo/bar") }, false},
744 { { FPL("c:/foo/bar"), FPL("D:/foo/bar") }, false},
745 { { FPL("C:/foo/bar"), FPL("d:/foo/bar") }, false},
746 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar") }, false},
747#endif // FILE_PATH_USES_DRIVE_LETTERS
748#if defined(FILE_PATH_USES_WIN_SEPARATORS)
749 { { FPL("\\foo\\bar"), FPL("\\foo\\bar") }, true},
750 { { FPL("\\foo/bar"), FPL("\\foo/bar") }, true},
751 { { FPL("\\foo/bar"), FPL("\\foo\bar") }, false},
752 { { FPL("\\"), FPL("\\") }, true},
753 { { FPL("\\"), FPL("/") }, false},
754 { { FPL(""), FPL("\\") }, false},
755 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar") }, false},
756 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2") }, false},
757#if defined(FILE_PATH_USES_DRIVE_LETTERS)
758 { { FPL("c:\\foo\\bar"), FPL("c:\\foo\\bar") }, true},
759 { { FPL("E:\\foo\\bar"), FPL("e:\\foo\\bar") }, true},
760 { { FPL("f:\\foo\\bar"), FPL("F:\\foo/bar") }, false},
761#endif // FILE_PATH_USES_DRIVE_LETTERS
762#endif // FILE_PATH_USES_WIN_SEPARATORS
763 };
764
765 for (size_t i = 0; i < arraysize(cases); ++i) {
766 FilePath a(cases[i].inputs[0]);
767 FilePath b(cases[i].inputs[1]);
768
769 EXPECT_EQ(a == b, cases[i].expected) <<
770 "equality i: " << i << ", a: " << a.value() << ", b: " <<
771 b.value();
772 }
773
rafaelw@chromium.org724cf542009-07-01 10:03:34 +0900774 for (size_t i = 0; i < arraysize(cases); ++i) {
775 FilePath a(cases[i].inputs[0]);
776 FilePath b(cases[i].inputs[1]);
777
778 EXPECT_EQ(a != b, !cases[i].expected) <<
779 "inequality i: " << i << ", a: " << a.value() << ", b: " <<
780 b.value();
781 }
782}
783
erikkay@google.com985d4202009-01-10 03:26:19 +0900784TEST_F(FilePathTest, Extension) {
785 FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
786
787 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
788 EXPECT_EQ(jpg.Extension(), FILE_PATH_LITERAL(".jpg"));
789
790 FilePath base = jpg.BaseName().RemoveExtension();
791 EXPECT_EQ(base.value(), FILE_PATH_LITERAL("foo"));
792
793 FilePath path_no_ext = base_dir.Append(base);
794 EXPECT_EQ(jpg.RemoveExtension().value(), path_no_ext.value());
795
796 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value());
797 EXPECT_EQ(path_no_ext.Extension(), FILE_PATH_LITERAL(""));
798}
799
800TEST_F(FilePathTest, Extension2) {
801 const struct UnaryTestData cases[] = {
802#if defined(FILE_PATH_USES_WIN_SEPARATORS)
803 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
804 { FPL("C:\\a\\b\\c."), FPL(".") },
805 { FPL("C:\\a\\b\\c"), FPL("") },
806 { FPL("C:\\a\\b\\"), FPL("") },
807 { FPL("C:\\a\\b.\\"), FPL(".") },
808 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
809 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
810 { FPL("C:\\foo.bar\\.."), FPL("") },
811 { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
812#endif
813 { FPL("/foo/bar/baz.ext"), FPL(".ext") },
814 { FPL("/foo/bar/baz."), FPL(".") },
815 { FPL("/foo/bar/baz.."), FPL(".") },
816 { FPL("/foo/bar/baz"), FPL("") },
817 { FPL("/foo/bar/"), FPL("") },
818 { FPL("/foo/bar./"), FPL(".") },
819 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
820 { FPL("."), FPL("") },
821 { FPL(".."), FPL("") },
822 { FPL("./foo"), FPL("") },
823 { FPL("./foo.ext"), FPL(".ext") },
824 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
825 { FPL("/foo.bar////"), FPL(".bar") },
826 { FPL("/foo.bar/.."), FPL("") },
827 { FPL("/foo.bar/..////"), FPL("") },
828 };
829 for (unsigned int i = 0; i < arraysize(cases); ++i) {
830 FilePath path(cases[i].input);
831 FilePath::StringType extension = path.Extension();
832 EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
833 ", path: " << path.value();
834 }
835}
836
837TEST_F(FilePathTest, InsertBeforeExtension) {
838 const struct BinaryTestData cases[] = {
839 { { FPL(""), FPL("") }, FPL("") },
840 { { FPL(""), FPL("txt") }, FPL("") },
841 { { FPL("."), FPL("txt") }, FPL("") },
842 { { FPL(".."), FPL("txt") }, FPL("") },
843 { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
844 { { FPL("."), FPL("") }, FPL(".") },
845 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
846 { { FPL("foo"), FPL("txt") }, FPL("footxt") },
847 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
848 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
849 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
850 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
851 { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
852 { { FPL("foo"), FPL("") }, FPL("foo") },
853 { { FPL("foo"), FPL(".") }, FPL("foo.") },
854 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
855 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
856#if defined(FILE_PATH_USES_WIN_SEPARATORS)
857 { { FPL("\\"), FPL("") }, FPL("\\") },
858 { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
859 { { FPL("\\."), FPL("txt") }, FPL("") },
860 { { FPL("\\.."), FPL("txt") }, FPL("") },
861 { { FPL("\\."), FPL("") }, FPL("\\.") },
862 { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
863 FPL("C:\\bar\\footxt.dll") },
864 { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
865 FPL("C:\\bar.baz\\foodlltxt") },
866 { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
867 FPL("C:\\bar.baz\\footxt.dll") },
868 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
869 FPL("C:\\bar.baz\\foo.dlltxt.exe") },
870 { { FPL("C:\\bar.baz\\foo"), FPL("") },
871 FPL("C:\\bar.baz\\foo") },
872 { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
873 FPL("C:\\bar.baz\\foo.exe") },
874 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
875 FPL("C:\\bar.baz\\foo.dll.exe") },
876 { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
877 FPL("C:\\bar\\baz\\foo (1).exe") },
878 { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
879 { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
880#endif
881 { { FPL("/"), FPL("") }, FPL("/") },
882 { { FPL("/"), FPL("txt") }, FPL("/txt") },
883 { { FPL("/."), FPL("txt") }, FPL("") },
884 { { FPL("/.."), FPL("txt") }, FPL("") },
885 { { FPL("/."), FPL("") }, FPL("/.") },
886 { { FPL("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
887 { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
888 { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
889 { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
890 FPL("/bar.baz/foo.dlltxt.exe") },
891 { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
892 { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
893 { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
894 { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
895 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
896 };
897 for (unsigned int i = 0; i < arraysize(cases); ++i) {
898 FilePath path(cases[i].inputs[0]);
899 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
900 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
901 ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
902 }
903}
904
905TEST_F(FilePathTest, ReplaceExtension) {
906 const struct BinaryTestData cases[] = {
907 { { FPL(""), FPL("") }, FPL("") },
908 { { FPL(""), FPL("txt") }, FPL("") },
909 { { FPL("."), FPL("txt") }, FPL("") },
910 { { FPL(".."), FPL("txt") }, FPL("") },
911 { { FPL("."), FPL("") }, FPL("") },
912 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
913 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
914 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
915 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
916 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
917 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
918 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
919 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
920 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
921 { { FPL("foo.dll"), FPL("") }, FPL("foo") },
922 { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
923 { { FPL("foo"), FPL("") }, FPL("foo") },
924 { { FPL("foo"), FPL(".") }, FPL("foo") },
925 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
926 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
927#if defined(FILE_PATH_USES_WIN_SEPARATORS)
928 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
929 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
930#endif
931 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
932 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
933 };
934 for (unsigned int i = 0; i < arraysize(cases); ++i) {
935 FilePath path(cases[i].inputs[0]);
936 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
937 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
938 ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
939 }
940}
avi@chromium.org8c3a9b32009-06-30 05:31:29 +0900941
942TEST_F(FilePathTest, MatchesExtension) {
943 const struct BinaryBooleanTestData cases[] = {
944 { { FPL("foo"), FPL("") }, true},
945 { { FPL("foo"), FPL(".") }, false},
946 { { FPL("foo."), FPL("") }, false},
947 { { FPL("foo."), FPL(".") }, true},
948 { { FPL("foo.txt"), FPL(".dll") }, false},
949 { { FPL("foo.txt"), FPL(".txt") }, true},
950 { { FPL("foo.txt.dll"), FPL(".txt") }, false},
951 { { FPL("foo.txt.dll"), FPL(".dll") }, true},
952#if defined(FILE_PATH_USES_DRIVE_LETTERS)
953 { { FPL("c:/foo.txt.dll"), FPL(".txt") }, false},
954 { { FPL("c:/foo.txt"), FPL(".txt") }, true},
955#endif // FILE_PATH_USES_DRIVE_LETTERS
956#if defined(FILE_PATH_USES_WIN_SEPARATORS)
957 { { FPL("c:\\bar\\foo.txt.dll"), FPL(".txt") }, false},
958 { { FPL("c:\\bar\\foo.txt"), FPL(".txt") }, true},
959#endif // FILE_PATH_USES_DRIVE_LETTERS
960 { { FPL("/bar/foo.txt.dll"), FPL(".txt") }, false},
961 { { FPL("/bar/foo.txt"), FPL(".txt") }, true},
962 };
963
964 for (size_t i = 0; i < arraysize(cases); ++i) {
965 FilePath path(cases[i].inputs[0]);
966 FilePath::StringType ext(cases[i].inputs[1]);
967
968 EXPECT_EQ(cases[i].expected, path.MatchesExtension(ext)) <<
969 "i: " << i << ", path: " << path.value() << ", ext: " << ext;
970 }
971}
cevans@chromium.org48dc54c2009-08-16 06:44:31 +0900972
973TEST_F(FilePathTest, ReferencesParent) {
974 const struct UnaryBooleanTestData cases[] = {
975 { FPL("."), false },
976 { FPL(".."), true },
977 { FPL("a.."), false },
978 { FPL("..a"), false },
979 { FPL("../"), true },
980 { FPL("/.."), true },
981 { FPL("/../"), true },
982 { FPL("/a../"), false },
983 { FPL("/..a/"), false },
984 { FPL("//.."), true },
985 { FPL("..//"), true },
986 { FPL("//..//"), true },
987 { FPL("a//..//c"), true },
988 { FPL("../b/c"), true },
989 { FPL("/../b/c"), true },
990 { FPL("a/b/.."), true },
991 { FPL("a/b/../"), true },
992 { FPL("a/../c"), true },
993 { FPL("a/b/c"), false },
994 };
995
996 for (size_t i = 0; i < arraysize(cases); ++i) {
997 FilePath input(cases[i].input);
998 bool observed = input.ReferencesParent();
999 EXPECT_EQ(cases[i].expected, observed) <<
1000 "i: " << i << ", input: " << input.value();
1001 }
1002}