blob: ac45e46e6070018c13578bbcd89273504044e20d [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"
8#include "base/path_service.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
aa@chromium.org76e5c632008-12-18 17:38:16 +090030// file_util winds up using autoreleased objects on the Mac, so this needs
31// to be a PlatformTest
32class FilePathTest : public PlatformTest {
33 protected:
34 virtual void SetUp() {
35 PlatformTest::SetUp();
36 }
37 virtual void TearDown() {
38 PlatformTest::TearDown();
39 }
40};
41
42TEST_F(FilePathTest, DirName) {
mark@chromium.org6380e262008-10-04 03:21:47 +090043 const struct UnaryTestData cases[] = {
44 { FPL(""), FPL(".") },
45 { FPL("aa"), FPL(".") },
46 { FPL("/aa/bb"), FPL("/aa") },
47 { FPL("/aa/bb/"), FPL("/aa") },
48 { FPL("/aa/bb//"), FPL("/aa") },
49 { FPL("/aa/bb/ccc"), FPL("/aa/bb") },
50 { FPL("/aa"), FPL("/") },
51 { FPL("/aa/"), FPL("/") },
52 { FPL("/"), FPL("/") },
53 { FPL("//"), FPL("//") },
54 { FPL("///"), FPL("/") },
55 { FPL("aa/"), FPL(".") },
56 { FPL("aa/bb"), FPL("aa") },
57 { FPL("aa/bb/"), FPL("aa") },
58 { FPL("aa/bb//"), FPL("aa") },
59 { FPL("aa//bb//"), FPL("aa") },
60 { FPL("aa//bb/"), FPL("aa") },
61 { FPL("aa//bb"), FPL("aa") },
62 { FPL("//aa/bb"), FPL("//aa") },
63 { FPL("//aa/"), FPL("//") },
64 { FPL("//aa"), FPL("//") },
65 { FPL("0:"), FPL(".") },
66 { FPL("@:"), FPL(".") },
67 { FPL("[:"), FPL(".") },
68 { FPL("`:"), FPL(".") },
69 { FPL("{:"), FPL(".") },
70 { FPL("\xB3:"), FPL(".") },
71 { FPL("\xC5:"), FPL(".") },
72#if defined(OS_WIN)
73 { FPL("\x0143:"), FPL(".") },
74#endif // OS_WIN
75#if defined(FILE_PATH_USES_DRIVE_LETTERS)
76 { FPL("c:"), FPL("c:") },
77 { FPL("C:"), FPL("C:") },
78 { FPL("A:"), FPL("A:") },
79 { FPL("Z:"), FPL("Z:") },
80 { FPL("a:"), FPL("a:") },
81 { FPL("z:"), FPL("z:") },
82 { FPL("c:aa"), FPL("c:") },
83 { FPL("c:/"), FPL("c:/") },
84 { FPL("c://"), FPL("c://") },
85 { FPL("c:///"), FPL("c:/") },
86 { FPL("c:/aa"), FPL("c:/") },
87 { FPL("c:/aa/"), FPL("c:/") },
88 { FPL("c:/aa/bb"), FPL("c:/aa") },
89 { FPL("c:aa/bb"), FPL("c:aa") },
90#endif // FILE_PATH_USES_DRIVE_LETTERS
91#if defined(FILE_PATH_USES_WIN_SEPARATORS)
92 { FPL("\\aa\\bb"), FPL("\\aa") },
93 { FPL("\\aa\\bb\\"), FPL("\\aa") },
94 { FPL("\\aa\\bb\\\\"), FPL("\\aa") },
95 { FPL("\\aa\\bb\\ccc"), FPL("\\aa\\bb") },
96 { FPL("\\aa"), FPL("\\") },
97 { FPL("\\aa\\"), FPL("\\") },
98 { FPL("\\"), FPL("\\") },
99 { FPL("\\\\"), FPL("\\\\") },
100 { FPL("\\\\\\"), FPL("\\") },
101 { FPL("aa\\"), FPL(".") },
102 { FPL("aa\\bb"), FPL("aa") },
103 { FPL("aa\\bb\\"), FPL("aa") },
104 { FPL("aa\\bb\\\\"), FPL("aa") },
105 { FPL("aa\\\\bb\\\\"), FPL("aa") },
106 { FPL("aa\\\\bb\\"), FPL("aa") },
107 { FPL("aa\\\\bb"), FPL("aa") },
108 { FPL("\\\\aa\\bb"), FPL("\\\\aa") },
109 { FPL("\\\\aa\\"), FPL("\\\\") },
110 { FPL("\\\\aa"), FPL("\\\\") },
111#if defined(FILE_PATH_USES_DRIVE_LETTERS)
112 { FPL("c:\\"), FPL("c:\\") },
113 { FPL("c:\\\\"), FPL("c:\\\\") },
114 { FPL("c:\\\\\\"), FPL("c:\\") },
115 { FPL("c:\\aa"), FPL("c:\\") },
116 { FPL("c:\\aa\\"), FPL("c:\\") },
117 { FPL("c:\\aa\\bb"), FPL("c:\\aa") },
118 { FPL("c:aa\\bb"), FPL("c:aa") },
119#endif // FILE_PATH_USES_DRIVE_LETTERS
120#endif // FILE_PATH_USES_WIN_SEPARATORS
121 };
122
123 for (size_t i = 0; i < arraysize(cases); ++i) {
124 FilePath input(cases[i].input);
125 FilePath observed = input.DirName();
126 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
127 "i: " << i << ", input: " << input.value();
128 }
129}
130
aa@chromium.org76e5c632008-12-18 17:38:16 +0900131TEST_F(FilePathTest, BaseName) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900132 const struct UnaryTestData cases[] = {
133 { FPL(""), FPL("") },
134 { FPL("aa"), FPL("aa") },
135 { FPL("/aa/bb"), FPL("bb") },
136 { FPL("/aa/bb/"), FPL("bb") },
137 { FPL("/aa/bb//"), FPL("bb") },
138 { FPL("/aa/bb/ccc"), FPL("ccc") },
139 { FPL("/aa"), FPL("aa") },
140 { FPL("/"), FPL("/") },
141 { FPL("//"), FPL("//") },
142 { FPL("///"), FPL("/") },
143 { FPL("aa/"), FPL("aa") },
144 { FPL("aa/bb"), FPL("bb") },
145 { FPL("aa/bb/"), FPL("bb") },
146 { FPL("aa/bb//"), FPL("bb") },
147 { FPL("aa//bb//"), FPL("bb") },
148 { FPL("aa//bb/"), FPL("bb") },
149 { FPL("aa//bb"), FPL("bb") },
150 { FPL("//aa/bb"), FPL("bb") },
151 { FPL("//aa/"), FPL("aa") },
152 { FPL("//aa"), FPL("aa") },
153 { FPL("0:"), FPL("0:") },
154 { FPL("@:"), FPL("@:") },
155 { FPL("[:"), FPL("[:") },
156 { FPL("`:"), FPL("`:") },
157 { FPL("{:"), FPL("{:") },
158 { FPL("\xB3:"), FPL("\xB3:") },
159 { FPL("\xC5:"), FPL("\xC5:") },
160#if defined(OS_WIN)
161 { FPL("\x0143:"), FPL("\x0143:") },
162#endif // OS_WIN
163#if defined(FILE_PATH_USES_DRIVE_LETTERS)
164 { FPL("c:"), FPL("") },
165 { FPL("C:"), FPL("") },
166 { FPL("A:"), FPL("") },
167 { FPL("Z:"), FPL("") },
168 { FPL("a:"), FPL("") },
169 { FPL("z:"), FPL("") },
170 { FPL("c:aa"), FPL("aa") },
171 { FPL("c:/"), FPL("/") },
172 { FPL("c://"), FPL("//") },
173 { FPL("c:///"), FPL("/") },
174 { FPL("c:/aa"), FPL("aa") },
175 { FPL("c:/aa/"), FPL("aa") },
176 { FPL("c:/aa/bb"), FPL("bb") },
177 { FPL("c:aa/bb"), FPL("bb") },
178#endif // FILE_PATH_USES_DRIVE_LETTERS
179#if defined(FILE_PATH_USES_WIN_SEPARATORS)
180 { FPL("\\aa\\bb"), FPL("bb") },
181 { FPL("\\aa\\bb\\"), FPL("bb") },
182 { FPL("\\aa\\bb\\\\"), FPL("bb") },
183 { FPL("\\aa\\bb\\ccc"), FPL("ccc") },
184 { FPL("\\aa"), FPL("aa") },
185 { FPL("\\"), FPL("\\") },
186 { FPL("\\\\"), FPL("\\\\") },
187 { FPL("\\\\\\"), FPL("\\") },
188 { FPL("aa\\"), FPL("aa") },
189 { FPL("aa\\bb"), FPL("bb") },
190 { FPL("aa\\bb\\"), FPL("bb") },
191 { FPL("aa\\bb\\\\"), FPL("bb") },
192 { FPL("aa\\\\bb\\\\"), FPL("bb") },
193 { FPL("aa\\\\bb\\"), FPL("bb") },
194 { FPL("aa\\\\bb"), FPL("bb") },
195 { FPL("\\\\aa\\bb"), FPL("bb") },
196 { FPL("\\\\aa\\"), FPL("aa") },
197 { FPL("\\\\aa"), FPL("aa") },
198#if defined(FILE_PATH_USES_DRIVE_LETTERS)
199 { FPL("c:\\"), FPL("\\") },
200 { FPL("c:\\\\"), FPL("\\\\") },
201 { FPL("c:\\\\\\"), FPL("\\") },
202 { FPL("c:\\aa"), FPL("aa") },
203 { FPL("c:\\aa\\"), FPL("aa") },
204 { FPL("c:\\aa\\bb"), FPL("bb") },
205 { FPL("c:aa\\bb"), FPL("bb") },
206#endif // FILE_PATH_USES_DRIVE_LETTERS
207#endif // FILE_PATH_USES_WIN_SEPARATORS
208 };
209
210 for (size_t i = 0; i < arraysize(cases); ++i) {
211 FilePath input(cases[i].input);
estade@chromium.orgd872d682009-01-06 08:59:36 +0900212 FilePath observed = input.BaseName();
213 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
mark@chromium.org6380e262008-10-04 03:21:47 +0900214 "i: " << i << ", input: " << input.value();
215 }
216}
217
aa@chromium.org76e5c632008-12-18 17:38:16 +0900218TEST_F(FilePathTest, Append) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900219 const struct BinaryTestData cases[] = {
220 { { FPL(""), FPL("cc") }, FPL("cc") },
221 { { FPL("."), FPL("ff") }, FPL("ff") },
222 { { FPL("/"), FPL("cc") }, FPL("/cc") },
223 { { FPL("/aa"), FPL("") }, FPL("/aa") },
224 { { FPL("/aa/"), FPL("") }, FPL("/aa") },
225 { { FPL("//aa"), FPL("") }, FPL("//aa") },
226 { { FPL("//aa/"), FPL("") }, FPL("//aa") },
227 { { FPL("//"), FPL("aa") }, FPL("//aa") },
228#if defined(FILE_PATH_USES_DRIVE_LETTERS)
229 { { FPL("c:"), FPL("a") }, FPL("c:a") },
230 { { FPL("c:"), FPL("") }, FPL("c:") },
231 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
232 { { FPL("c://"), FPL("a") }, FPL("c://a") },
233 { { FPL("c:///"), FPL("a") }, FPL("c:/a") },
234#endif // FILE_PATH_USES_DRIVE_LETTERS
235#if defined(FILE_PATH_USES_WIN_SEPARATORS)
236 // Append introduces the default separator character, so these test cases
237 // need to be defined with different expected results on platforms that use
238 // different default separator characters.
239 { { FPL("\\"), FPL("cc") }, FPL("\\cc") },
240 { { FPL("\\aa"), FPL("") }, FPL("\\aa") },
241 { { FPL("\\aa\\"), FPL("") }, FPL("\\aa") },
242 { { FPL("\\\\aa"), FPL("") }, FPL("\\\\aa") },
243 { { FPL("\\\\aa\\"), FPL("") }, FPL("\\\\aa") },
244 { { FPL("\\\\"), FPL("aa") }, FPL("\\\\aa") },
245 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb\\cc") },
246 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb\\cc") },
247 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb\\cc") },
248 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb\\cc") },
249 { { FPL("a/b"), FPL("c") }, FPL("a/b\\c") },
250 { { FPL("a/b/"), FPL("c") }, FPL("a/b\\c") },
251 { { FPL("//aa"), FPL("bb") }, FPL("//aa\\bb") },
252 { { FPL("//aa/"), FPL("bb") }, FPL("//aa\\bb") },
253 { { FPL("\\aa\\bb"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
254 { { FPL("\\aa\\bb\\"), FPL("cc") }, FPL("\\aa\\bb\\cc") },
255 { { FPL("aa\\bb\\"), FPL("cc") }, FPL("aa\\bb\\cc") },
256 { { FPL("aa\\bb"), FPL("cc") }, FPL("aa\\bb\\cc") },
257 { { FPL("a\\b"), FPL("c") }, FPL("a\\b\\c") },
258 { { FPL("a\\b\\"), FPL("c") }, FPL("a\\b\\c") },
259 { { FPL("\\\\aa"), FPL("bb") }, FPL("\\\\aa\\bb") },
260 { { FPL("\\\\aa\\"), FPL("bb") }, FPL("\\\\aa\\bb") },
261#if defined(FILE_PATH_USES_DRIVE_LETTERS)
262 { { FPL("c:\\"), FPL("a") }, FPL("c:\\a") },
263 { { FPL("c:\\\\"), FPL("a") }, FPL("c:\\\\a") },
264 { { FPL("c:\\\\\\"), FPL("a") }, FPL("c:\\a") },
265 { { FPL("c:\\"), FPL("") }, FPL("c:\\") },
266 { { FPL("c:\\a"), FPL("b") }, FPL("c:\\a\\b") },
267 { { FPL("c:\\a\\"), FPL("b") }, FPL("c:\\a\\b") },
268#endif // FILE_PATH_USES_DRIVE_LETTERS
269#else // FILE_PATH_USES_WIN_SEPARATORS
270 { { FPL("/aa/bb"), FPL("cc") }, FPL("/aa/bb/cc") },
271 { { FPL("/aa/bb/"), FPL("cc") }, FPL("/aa/bb/cc") },
272 { { FPL("aa/bb/"), FPL("cc") }, FPL("aa/bb/cc") },
273 { { FPL("aa/bb"), FPL("cc") }, FPL("aa/bb/cc") },
274 { { FPL("a/b"), FPL("c") }, FPL("a/b/c") },
275 { { FPL("a/b/"), FPL("c") }, FPL("a/b/c") },
276 { { FPL("//aa"), FPL("bb") }, FPL("//aa/bb") },
277 { { FPL("//aa/"), FPL("bb") }, FPL("//aa/bb") },
278#if defined(FILE_PATH_USES_DRIVE_LETTERS)
279 { { FPL("c:/"), FPL("a") }, FPL("c:/a") },
280 { { FPL("c:/"), FPL("") }, FPL("c:/") },
281 { { FPL("c:/a"), FPL("b") }, FPL("c:/a/b") },
282 { { FPL("c:/a/"), FPL("b") }, FPL("c:/a/b") },
283#endif // FILE_PATH_USES_DRIVE_LETTERS
284#endif // FILE_PATH_USES_WIN_SEPARATORS
285 };
286
287 for (size_t i = 0; i < arraysize(cases); ++i) {
288 FilePath root(cases[i].inputs[0]);
289 FilePath::StringType leaf(cases[i].inputs[1]);
mark@chromium.orgfac7b262008-12-09 01:49:08 +0900290 FilePath observed_str = root.Append(leaf);
291 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
292 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
293 FilePath observed_path = root.Append(FilePath(leaf));
294 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) <<
mark@chromium.org6380e262008-10-04 03:21:47 +0900295 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
296 }
297}
298
aa@chromium.org76e5c632008-12-18 17:38:16 +0900299TEST_F(FilePathTest, IsAbsolute) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900300 const struct UnaryBooleanTestData cases[] = {
301 { FPL(""), false },
302 { FPL("a"), false },
303 { FPL("c:"), false },
304 { FPL("c:a"), false },
305 { FPL("a/b"), false },
306 { FPL("//"), true },
307 { FPL("//a"), true },
308 { FPL("c:a/b"), false },
309 { FPL("?:/a"), false },
310#if defined(FILE_PATH_USES_DRIVE_LETTERS)
311 { FPL("/"), false },
312 { FPL("/a"), false },
313 { FPL("/."), false },
314 { FPL("/.."), false },
315 { FPL("c:/"), true },
316 { FPL("c:/a"), true },
317 { FPL("c:/."), true },
318 { FPL("c:/.."), true },
319 { FPL("C:/a"), true },
320 { FPL("d:/a"), true },
321#else // FILE_PATH_USES_DRIVE_LETTERS
322 { FPL("/"), true },
323 { FPL("/a"), true },
324 { FPL("/."), true },
325 { FPL("/.."), true },
326 { FPL("c:/"), false },
327#endif // FILE_PATH_USES_DRIVE_LETTERS
328#if defined(FILE_PATH_USES_WIN_SEPARATORS)
329 { FPL("a\\b"), false },
330 { FPL("\\\\"), true },
331 { FPL("\\\\a"), true },
332 { FPL("a\\b"), false },
333 { FPL("\\\\"), true },
334 { FPL("//a"), true },
335 { FPL("c:a\\b"), false },
336 { FPL("?:\\a"), false },
337#if defined(FILE_PATH_USES_DRIVE_LETTERS)
338 { FPL("\\"), false },
339 { FPL("\\a"), false },
340 { FPL("\\."), false },
341 { FPL("\\.."), false },
342 { FPL("c:\\"), true },
343 { FPL("c:\\"), true },
344 { FPL("c:\\a"), true },
345 { FPL("c:\\."), true },
346 { FPL("c:\\.."), true },
347 { FPL("C:\\a"), true },
348 { FPL("d:\\a"), true },
349#else // FILE_PATH_USES_DRIVE_LETTERS
350 { FPL("\\"), true },
351 { FPL("\\a"), true },
352 { FPL("\\."), true },
353 { FPL("\\.."), true },
354 { FPL("c:\\"), false },
355#endif // FILE_PATH_USES_DRIVE_LETTERS
356#endif // FILE_PATH_USES_WIN_SEPARATORS
357 };
358
359 for (size_t i = 0; i < arraysize(cases); ++i) {
360 FilePath input(cases[i].input);
361 bool observed = input.IsAbsolute();
362 EXPECT_EQ(cases[i].expected, observed) <<
363 "i: " << i << ", input: " << input.value();
364 }
365}
aa@chromium.org76e5c632008-12-18 17:38:16 +0900366
367TEST_F(FilePathTest, Contains) {
368 FilePath data_dir;
369 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &data_dir));
370 data_dir = data_dir.Append(FILE_PATH_LITERAL("FilePathTest"));
371
372 // Create a fresh, empty copy of this directory.
373 file_util::Delete(data_dir, true);
374 file_util::CreateDirectory(data_dir);
375
376 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
377 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
378 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
379 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
380
381 // Annoyingly, the directories must actually exist in order for realpath(),
382 // which Contains() relies on in posix, to work.
383 file_util::CreateDirectory(foo);
384 std::string data("hello");
385 file_util::WriteFile(bar.ToWStringHack(), data.c_str(), data.length());
386 file_util::WriteFile(baz.ToWStringHack(), data.c_str(), data.length());
387 file_util::WriteFile(foobar.ToWStringHack(), data.c_str(), data.length());
388
389 EXPECT_TRUE(foo.Contains(bar));
390 EXPECT_FALSE(foo.Contains(baz));
391 EXPECT_FALSE(foo.Contains(foobar));
392 EXPECT_FALSE(foo.Contains(foo));
393
394// Platform-specific concerns
395 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
396#if defined(OS_WIN)
397 EXPECT_TRUE(foo.Contains(foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
398 EXPECT_TRUE(foo.Contains(
399 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
400#elif defined(OS_LINUX)
401 EXPECT_FALSE(foo.Contains(foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
402#else
403 // We can't really do this test on osx since the case-sensitivity of the
404 // filesystem is configurable.
405#endif
406
407 // Note: whether
408}
erikkay@google.com985d4202009-01-10 03:26:19 +0900409
410TEST_F(FilePathTest, Extension) {
411 FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
412
413 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
414 EXPECT_EQ(jpg.Extension(), FILE_PATH_LITERAL(".jpg"));
415
416 FilePath base = jpg.BaseName().RemoveExtension();
417 EXPECT_EQ(base.value(), FILE_PATH_LITERAL("foo"));
418
419 FilePath path_no_ext = base_dir.Append(base);
420 EXPECT_EQ(jpg.RemoveExtension().value(), path_no_ext.value());
421
422 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value());
423 EXPECT_EQ(path_no_ext.Extension(), FILE_PATH_LITERAL(""));
424}
425
426TEST_F(FilePathTest, Extension2) {
427 const struct UnaryTestData cases[] = {
428#if defined(FILE_PATH_USES_WIN_SEPARATORS)
429 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
430 { FPL("C:\\a\\b\\c."), FPL(".") },
431 { FPL("C:\\a\\b\\c"), FPL("") },
432 { FPL("C:\\a\\b\\"), FPL("") },
433 { FPL("C:\\a\\b.\\"), FPL(".") },
434 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
435 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
436 { FPL("C:\\foo.bar\\.."), FPL("") },
437 { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
438#endif
439 { FPL("/foo/bar/baz.ext"), FPL(".ext") },
440 { FPL("/foo/bar/baz."), FPL(".") },
441 { FPL("/foo/bar/baz.."), FPL(".") },
442 { FPL("/foo/bar/baz"), FPL("") },
443 { FPL("/foo/bar/"), FPL("") },
444 { FPL("/foo/bar./"), FPL(".") },
445 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
446 { FPL("."), FPL("") },
447 { FPL(".."), FPL("") },
448 { FPL("./foo"), FPL("") },
449 { FPL("./foo.ext"), FPL(".ext") },
450 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
451 { FPL("/foo.bar////"), FPL(".bar") },
452 { FPL("/foo.bar/.."), FPL("") },
453 { FPL("/foo.bar/..////"), FPL("") },
454 };
455 for (unsigned int i = 0; i < arraysize(cases); ++i) {
456 FilePath path(cases[i].input);
457 FilePath::StringType extension = path.Extension();
458 EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
459 ", path: " << path.value();
460 }
461}
462
463TEST_F(FilePathTest, InsertBeforeExtension) {
464 const struct BinaryTestData cases[] = {
465 { { FPL(""), FPL("") }, FPL("") },
466 { { FPL(""), FPL("txt") }, FPL("") },
467 { { FPL("."), FPL("txt") }, FPL("") },
468 { { FPL(".."), FPL("txt") }, FPL("") },
469 { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
470 { { FPL("."), FPL("") }, FPL(".") },
471 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
472 { { FPL("foo"), FPL("txt") }, FPL("footxt") },
473 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
474 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
475 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
476 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
477 { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
478 { { FPL("foo"), FPL("") }, FPL("foo") },
479 { { FPL("foo"), FPL(".") }, FPL("foo.") },
480 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
481 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
482#if defined(FILE_PATH_USES_WIN_SEPARATORS)
483 { { FPL("\\"), FPL("") }, FPL("\\") },
484 { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
485 { { FPL("\\."), FPL("txt") }, FPL("") },
486 { { FPL("\\.."), FPL("txt") }, FPL("") },
487 { { FPL("\\."), FPL("") }, FPL("\\.") },
488 { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
489 FPL("C:\\bar\\footxt.dll") },
490 { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
491 FPL("C:\\bar.baz\\foodlltxt") },
492 { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
493 FPL("C:\\bar.baz\\footxt.dll") },
494 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
495 FPL("C:\\bar.baz\\foo.dlltxt.exe") },
496 { { FPL("C:\\bar.baz\\foo"), FPL("") },
497 FPL("C:\\bar.baz\\foo") },
498 { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
499 FPL("C:\\bar.baz\\foo.exe") },
500 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
501 FPL("C:\\bar.baz\\foo.dll.exe") },
502 { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
503 FPL("C:\\bar\\baz\\foo (1).exe") },
504 { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
505 { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
506#endif
507 { { FPL("/"), FPL("") }, FPL("/") },
508 { { FPL("/"), FPL("txt") }, FPL("/txt") },
509 { { FPL("/."), FPL("txt") }, FPL("") },
510 { { FPL("/.."), FPL("txt") }, FPL("") },
511 { { FPL("/."), FPL("") }, FPL("/.") },
512 { { FPL("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
513 { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
514 { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
515 { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
516 FPL("/bar.baz/foo.dlltxt.exe") },
517 { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
518 { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
519 { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
520 { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
521 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
522 };
523 for (unsigned int i = 0; i < arraysize(cases); ++i) {
524 FilePath path(cases[i].inputs[0]);
525 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
526 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
527 ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
528 }
529}
530
531TEST_F(FilePathTest, ReplaceExtension) {
532 const struct BinaryTestData cases[] = {
533 { { FPL(""), FPL("") }, FPL("") },
534 { { FPL(""), FPL("txt") }, FPL("") },
535 { { FPL("."), FPL("txt") }, FPL("") },
536 { { FPL(".."), FPL("txt") }, FPL("") },
537 { { FPL("."), FPL("") }, FPL("") },
538 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
539 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
540 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
541 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
542 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
543 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
544 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
545 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
546 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
547 { { FPL("foo.dll"), FPL("") }, FPL("foo") },
548 { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
549 { { FPL("foo"), FPL("") }, FPL("foo") },
550 { { FPL("foo"), FPL(".") }, FPL("foo") },
551 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
552 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
553#if defined(FILE_PATH_USES_WIN_SEPARATORS)
554 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
555 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
556#endif
557 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
558 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
559 };
560 for (unsigned int i = 0; i < arraysize(cases); ++i) {
561 FilePath path(cases[i].inputs[0]);
562 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
563 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
564 ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
565 }
566}
567