blob: 3439c6caefec04d6d2173028454f1957a1ca1bc8 [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
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;
erikkay@google.comc51bf5f2009-01-22 07:15:57 +0900296
297 // TODO(erikkay): It would be nice to have a unicode test append value to
298 // handle the case when AppendASCII is passed UTF8
299#if defined(OS_WIN)
300 std::string ascii = WideToASCII(leaf);
301#elif defined(OS_POSIX)
302 std::string ascii = leaf;
303#endif
304 observed_str = root.AppendASCII(ascii);
305 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
306 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
mark@chromium.org6380e262008-10-04 03:21:47 +0900307 }
308}
309
aa@chromium.org76e5c632008-12-18 17:38:16 +0900310TEST_F(FilePathTest, IsAbsolute) {
mark@chromium.org6380e262008-10-04 03:21:47 +0900311 const struct UnaryBooleanTestData cases[] = {
312 { FPL(""), false },
313 { FPL("a"), false },
314 { FPL("c:"), false },
315 { FPL("c:a"), false },
316 { FPL("a/b"), false },
317 { FPL("//"), true },
318 { FPL("//a"), true },
319 { FPL("c:a/b"), false },
320 { FPL("?:/a"), false },
321#if defined(FILE_PATH_USES_DRIVE_LETTERS)
322 { FPL("/"), false },
323 { FPL("/a"), false },
324 { FPL("/."), false },
325 { FPL("/.."), false },
326 { FPL("c:/"), true },
327 { FPL("c:/a"), true },
328 { FPL("c:/."), true },
329 { FPL("c:/.."), true },
330 { FPL("C:/a"), true },
331 { FPL("d:/a"), true },
332#else // FILE_PATH_USES_DRIVE_LETTERS
333 { FPL("/"), true },
334 { FPL("/a"), true },
335 { FPL("/."), true },
336 { FPL("/.."), true },
337 { FPL("c:/"), false },
338#endif // FILE_PATH_USES_DRIVE_LETTERS
339#if defined(FILE_PATH_USES_WIN_SEPARATORS)
340 { FPL("a\\b"), false },
341 { FPL("\\\\"), true },
342 { FPL("\\\\a"), true },
343 { FPL("a\\b"), false },
344 { FPL("\\\\"), true },
345 { FPL("//a"), true },
346 { FPL("c:a\\b"), false },
347 { FPL("?:\\a"), false },
348#if defined(FILE_PATH_USES_DRIVE_LETTERS)
349 { FPL("\\"), false },
350 { FPL("\\a"), false },
351 { FPL("\\."), false },
352 { FPL("\\.."), false },
353 { FPL("c:\\"), true },
354 { FPL("c:\\"), true },
355 { FPL("c:\\a"), true },
356 { FPL("c:\\."), true },
357 { FPL("c:\\.."), true },
358 { FPL("C:\\a"), true },
359 { FPL("d:\\a"), true },
360#else // FILE_PATH_USES_DRIVE_LETTERS
361 { FPL("\\"), true },
362 { FPL("\\a"), true },
363 { FPL("\\."), true },
364 { FPL("\\.."), true },
365 { FPL("c:\\"), false },
366#endif // FILE_PATH_USES_DRIVE_LETTERS
367#endif // FILE_PATH_USES_WIN_SEPARATORS
368 };
369
370 for (size_t i = 0; i < arraysize(cases); ++i) {
371 FilePath input(cases[i].input);
372 bool observed = input.IsAbsolute();
373 EXPECT_EQ(cases[i].expected, observed) <<
374 "i: " << i << ", input: " << input.value();
375 }
376}
aa@chromium.org76e5c632008-12-18 17:38:16 +0900377
erikkay@google.com985d4202009-01-10 03:26:19 +0900378TEST_F(FilePathTest, Extension) {
379 FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
380
381 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
382 EXPECT_EQ(jpg.Extension(), FILE_PATH_LITERAL(".jpg"));
383
384 FilePath base = jpg.BaseName().RemoveExtension();
385 EXPECT_EQ(base.value(), FILE_PATH_LITERAL("foo"));
386
387 FilePath path_no_ext = base_dir.Append(base);
388 EXPECT_EQ(jpg.RemoveExtension().value(), path_no_ext.value());
389
390 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value());
391 EXPECT_EQ(path_no_ext.Extension(), FILE_PATH_LITERAL(""));
392}
393
394TEST_F(FilePathTest, Extension2) {
395 const struct UnaryTestData cases[] = {
396#if defined(FILE_PATH_USES_WIN_SEPARATORS)
397 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
398 { FPL("C:\\a\\b\\c."), FPL(".") },
399 { FPL("C:\\a\\b\\c"), FPL("") },
400 { FPL("C:\\a\\b\\"), FPL("") },
401 { FPL("C:\\a\\b.\\"), FPL(".") },
402 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
403 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
404 { FPL("C:\\foo.bar\\.."), FPL("") },
405 { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
406#endif
407 { FPL("/foo/bar/baz.ext"), FPL(".ext") },
408 { FPL("/foo/bar/baz."), FPL(".") },
409 { FPL("/foo/bar/baz.."), FPL(".") },
410 { FPL("/foo/bar/baz"), FPL("") },
411 { FPL("/foo/bar/"), FPL("") },
412 { FPL("/foo/bar./"), FPL(".") },
413 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
414 { FPL("."), FPL("") },
415 { FPL(".."), FPL("") },
416 { FPL("./foo"), FPL("") },
417 { FPL("./foo.ext"), FPL(".ext") },
418 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
419 { FPL("/foo.bar////"), FPL(".bar") },
420 { FPL("/foo.bar/.."), FPL("") },
421 { FPL("/foo.bar/..////"), FPL("") },
422 };
423 for (unsigned int i = 0; i < arraysize(cases); ++i) {
424 FilePath path(cases[i].input);
425 FilePath::StringType extension = path.Extension();
426 EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
427 ", path: " << path.value();
428 }
429}
430
431TEST_F(FilePathTest, InsertBeforeExtension) {
432 const struct BinaryTestData cases[] = {
433 { { FPL(""), FPL("") }, FPL("") },
434 { { FPL(""), FPL("txt") }, FPL("") },
435 { { FPL("."), FPL("txt") }, FPL("") },
436 { { FPL(".."), FPL("txt") }, FPL("") },
437 { { FPL("foo.dll"), FPL("txt") }, FPL("footxt.dll") },
438 { { FPL("."), FPL("") }, FPL(".") },
439 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt.dll") },
440 { { FPL("foo"), FPL("txt") }, FPL("footxt") },
441 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
442 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baztxt.dll") },
443 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt.dll") },
444 { { FPL("foo.dll"), FPL("") }, FPL("foo.dll") },
445 { { FPL("foo.dll"), FPL(".") }, FPL("foo..dll") },
446 { { FPL("foo"), FPL("") }, FPL("foo") },
447 { { FPL("foo"), FPL(".") }, FPL("foo.") },
448 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz.dll") },
449 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz..dll") },
450#if defined(FILE_PATH_USES_WIN_SEPARATORS)
451 { { FPL("\\"), FPL("") }, FPL("\\") },
452 { { FPL("\\"), FPL("txt") }, FPL("\\txt") },
453 { { FPL("\\."), FPL("txt") }, FPL("") },
454 { { FPL("\\.."), FPL("txt") }, FPL("") },
455 { { FPL("\\."), FPL("") }, FPL("\\.") },
456 { { FPL("C:\\bar\\foo.dll"), FPL("txt") },
457 FPL("C:\\bar\\footxt.dll") },
458 { { FPL("C:\\bar.baz\\foodll"), FPL("txt") },
459 FPL("C:\\bar.baz\\foodlltxt") },
460 { { FPL("C:\\bar.baz\\foo.dll"), FPL("txt") },
461 FPL("C:\\bar.baz\\footxt.dll") },
462 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt") },
463 FPL("C:\\bar.baz\\foo.dlltxt.exe") },
464 { { FPL("C:\\bar.baz\\foo"), FPL("") },
465 FPL("C:\\bar.baz\\foo") },
466 { { FPL("C:\\bar.baz\\foo.exe"), FPL("") },
467 FPL("C:\\bar.baz\\foo.exe") },
468 { { FPL("C:\\bar.baz\\foo.dll.exe"), FPL("") },
469 FPL("C:\\bar.baz\\foo.dll.exe") },
470 { { FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)") },
471 FPL("C:\\bar\\baz\\foo (1).exe") },
472 { { FPL("C:\\foo.baz\\\\"), FPL(" (1)") }, FPL("C:\\foo (1).baz") },
473 { { FPL("C:\\foo.baz\\..\\"), FPL(" (1)") }, FPL("") },
474#endif
475 { { FPL("/"), FPL("") }, FPL("/") },
476 { { FPL("/"), FPL("txt") }, FPL("/txt") },
477 { { FPL("/."), FPL("txt") }, FPL("") },
478 { { FPL("/.."), FPL("txt") }, FPL("") },
479 { { FPL("/."), FPL("") }, FPL("/.") },
480 { { FPL("/bar/foo.dll"), FPL("txt") }, FPL("/bar/footxt.dll") },
481 { { FPL("/bar.baz/foodll"), FPL("txt") }, FPL("/bar.baz/foodlltxt") },
482 { { FPL("/bar.baz/foo.dll"), FPL("txt") }, FPL("/bar.baz/footxt.dll") },
483 { { FPL("/bar.baz/foo.dll.exe"), FPL("txt") },
484 FPL("/bar.baz/foo.dlltxt.exe") },
485 { { FPL("/bar.baz/foo"), FPL("") }, FPL("/bar.baz/foo") },
486 { { FPL("/bar.baz/foo.exe"), FPL("") }, FPL("/bar.baz/foo.exe") },
487 { { FPL("/bar.baz/foo.dll.exe"), FPL("") }, FPL("/bar.baz/foo.dll.exe") },
488 { { FPL("/bar/baz/foo.exe"), FPL(" (1)") }, FPL("/bar/baz/foo (1).exe") },
489 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") },
490 };
491 for (unsigned int i = 0; i < arraysize(cases); ++i) {
492 FilePath path(cases[i].inputs[0]);
493 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]);
494 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i <<
495 ", path: " << path.value() << ", insert: " << cases[i].inputs[1];
496 }
497}
498
499TEST_F(FilePathTest, ReplaceExtension) {
500 const struct BinaryTestData cases[] = {
501 { { FPL(""), FPL("") }, FPL("") },
502 { { FPL(""), FPL("txt") }, FPL("") },
503 { { FPL("."), FPL("txt") }, FPL("") },
504 { { FPL(".."), FPL("txt") }, FPL("") },
505 { { FPL("."), FPL("") }, FPL("") },
506 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") },
507 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") },
508 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") },
509 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") },
510 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") },
511 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") },
512 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") },
513 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") },
514 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") },
515 { { FPL("foo.dll"), FPL("") }, FPL("foo") },
516 { { FPL("foo.dll"), FPL(".") }, FPL("foo") },
517 { { FPL("foo"), FPL("") }, FPL("foo") },
518 { { FPL("foo"), FPL(".") }, FPL("foo") },
519 { { FPL("foo.baz.dll"), FPL("") }, FPL("foo.baz") },
520 { { FPL("foo.baz.dll"), FPL(".") }, FPL("foo.baz") },
521#if defined(FILE_PATH_USES_WIN_SEPARATORS)
522 { { FPL("C:\\foo.bar\\foo"), FPL("baz") }, FPL("C:\\foo.bar\\foo.baz") },
523 { { FPL("C:\\foo.bar\\..\\\\"), FPL("baz") }, FPL("") },
524#endif
525 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
526 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
527 };
528 for (unsigned int i = 0; i < arraysize(cases); ++i) {
529 FilePath path(cases[i].inputs[0]);
530 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
531 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
532 ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
533 }
534}