Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1217)

Side by Side Diff: base/file_path_unittest.cc

Issue 14827: Implement FilePath::Contains() (Closed)
Patch Set: Make it work on mac and linux Created 12 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/file_path.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. 1 // 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h"
5 #include "base/file_path.h" 6 #include "base/file_path.h"
6 7 #include "base/file_util.h"
7 #include "base/basictypes.h" 8 #include "base/path_service.h"
9 #include "chrome/common/chrome_paths.h"
8 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
9 12
10 // This macro helps avoid wrapped lines in the test structs. 13 // This macro helps avoid wrapped lines in the test structs.
11 #define FPL(x) FILE_PATH_LITERAL(x) 14 #define FPL(x) FILE_PATH_LITERAL(x)
12 15
13 struct UnaryTestData { 16 struct UnaryTestData {
14 const FilePath::CharType* input; 17 const FilePath::CharType* input;
15 const FilePath::CharType* expected; 18 const FilePath::CharType* expected;
16 }; 19 };
17 20
18 struct UnaryBooleanTestData { 21 struct UnaryBooleanTestData {
19 const FilePath::CharType* input; 22 const FilePath::CharType* input;
20 bool expected; 23 bool expected;
21 }; 24 };
22 25
23 struct BinaryTestData { 26 struct BinaryTestData {
24 const FilePath::CharType* inputs[2]; 27 const FilePath::CharType* inputs[2];
25 const FilePath::CharType* expected; 28 const FilePath::CharType* expected;
26 }; 29 };
27 30
28 TEST(FilePathTest, DirName) { 31 // file_util winds up using autoreleased objects on the Mac, so this needs
32 // to be a PlatformTest
33 class FilePathTest : public PlatformTest {
34 protected:
35 virtual void SetUp() {
36 PlatformTest::SetUp();
37 }
38 virtual void TearDown() {
39 PlatformTest::TearDown();
40 }
41 };
42
43 TEST_F(FilePathTest, DirName) {
29 const struct UnaryTestData cases[] = { 44 const struct UnaryTestData cases[] = {
30 { FPL(""), FPL(".") }, 45 { FPL(""), FPL(".") },
31 { FPL("aa"), FPL(".") }, 46 { FPL("aa"), FPL(".") },
32 { FPL("/aa/bb"), FPL("/aa") }, 47 { FPL("/aa/bb"), FPL("/aa") },
33 { FPL("/aa/bb/"), FPL("/aa") }, 48 { FPL("/aa/bb/"), FPL("/aa") },
34 { FPL("/aa/bb//"), FPL("/aa") }, 49 { FPL("/aa/bb//"), FPL("/aa") },
35 { FPL("/aa/bb/ccc"), FPL("/aa/bb") }, 50 { FPL("/aa/bb/ccc"), FPL("/aa/bb") },
36 { FPL("/aa"), FPL("/") }, 51 { FPL("/aa"), FPL("/") },
37 { FPL("/aa/"), FPL("/") }, 52 { FPL("/aa/"), FPL("/") },
38 { FPL("/"), FPL("/") }, 53 { FPL("/"), FPL("/") },
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 }; 122 };
108 123
109 for (size_t i = 0; i < arraysize(cases); ++i) { 124 for (size_t i = 0; i < arraysize(cases); ++i) {
110 FilePath input(cases[i].input); 125 FilePath input(cases[i].input);
111 FilePath observed = input.DirName(); 126 FilePath observed = input.DirName();
112 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) << 127 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
113 "i: " << i << ", input: " << input.value(); 128 "i: " << i << ", input: " << input.value();
114 } 129 }
115 } 130 }
116 131
117 TEST(FilePathTest, BaseName) { 132 TEST_F(FilePathTest, BaseName) {
118 const struct UnaryTestData cases[] = { 133 const struct UnaryTestData cases[] = {
119 { FPL(""), FPL("") }, 134 { FPL(""), FPL("") },
120 { FPL("aa"), FPL("aa") }, 135 { FPL("aa"), FPL("aa") },
121 { FPL("/aa/bb"), FPL("bb") }, 136 { FPL("/aa/bb"), FPL("bb") },
122 { FPL("/aa/bb/"), FPL("bb") }, 137 { FPL("/aa/bb/"), FPL("bb") },
123 { FPL("/aa/bb//"), FPL("bb") }, 138 { FPL("/aa/bb//"), FPL("bb") },
124 { FPL("/aa/bb/ccc"), FPL("ccc") }, 139 { FPL("/aa/bb/ccc"), FPL("ccc") },
125 { FPL("/aa"), FPL("aa") }, 140 { FPL("/aa"), FPL("aa") },
126 { FPL("/"), FPL("/") }, 141 { FPL("/"), FPL("/") },
127 { FPL("//"), FPL("//") }, 142 { FPL("//"), FPL("//") },
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 }; 209 };
195 210
196 for (size_t i = 0; i < arraysize(cases); ++i) { 211 for (size_t i = 0; i < arraysize(cases); ++i) {
197 FilePath input(cases[i].input); 212 FilePath input(cases[i].input);
198 FilePath observed = input.BaseName(); 213 FilePath observed = input.BaseName();
199 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) << 214 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) <<
200 "i: " << i << ", input: " << input.value(); 215 "i: " << i << ", input: " << input.value();
201 } 216 }
202 } 217 }
203 218
204 TEST(FilePathTest, Append) { 219 TEST_F(FilePathTest, Append) {
205 const struct BinaryTestData cases[] = { 220 const struct BinaryTestData cases[] = {
206 { { FPL(""), FPL("cc") }, FPL("cc") }, 221 { { FPL(""), FPL("cc") }, FPL("cc") },
207 { { FPL("."), FPL("ff") }, FPL("ff") }, 222 { { FPL("."), FPL("ff") }, FPL("ff") },
208 { { FPL("/"), FPL("cc") }, FPL("/cc") }, 223 { { FPL("/"), FPL("cc") }, FPL("/cc") },
209 { { FPL("/aa"), FPL("") }, FPL("/aa") }, 224 { { FPL("/aa"), FPL("") }, FPL("/aa") },
210 { { FPL("/aa/"), FPL("") }, FPL("/aa") }, 225 { { FPL("/aa/"), FPL("") }, FPL("/aa") },
211 { { FPL("//aa"), FPL("") }, FPL("//aa") }, 226 { { FPL("//aa"), FPL("") }, FPL("//aa") },
212 { { FPL("//aa/"), FPL("") }, FPL("//aa") }, 227 { { FPL("//aa/"), FPL("") }, FPL("//aa") },
213 { { FPL("//"), FPL("aa") }, FPL("//aa") }, 228 { { FPL("//"), FPL("aa") }, FPL("//aa") },
214 #if defined(FILE_PATH_USES_DRIVE_LETTERS) 229 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 FilePath::StringType leaf(cases[i].inputs[1]); 290 FilePath::StringType leaf(cases[i].inputs[1]);
276 FilePath observed_str = root.Append(leaf); 291 FilePath observed_str = root.Append(leaf);
277 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) << 292 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_str.value()) <<
278 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf; 293 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
279 FilePath observed_path = root.Append(FilePath(leaf)); 294 FilePath observed_path = root.Append(FilePath(leaf));
280 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) << 295 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed_path.value()) <<
281 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf; 296 "i: " << i << ", root: " << root.value() << ", leaf: " << leaf;
282 } 297 }
283 } 298 }
284 299
285 TEST(FilePathTest, IsAbsolute) { 300 TEST_F(FilePathTest, IsAbsolute) {
286 const struct UnaryBooleanTestData cases[] = { 301 const struct UnaryBooleanTestData cases[] = {
287 { FPL(""), false }, 302 { FPL(""), false },
288 { FPL("a"), false }, 303 { FPL("a"), false },
289 { FPL("c:"), false }, 304 { FPL("c:"), false },
290 { FPL("c:a"), false }, 305 { FPL("c:a"), false },
291 { FPL("a/b"), false }, 306 { FPL("a/b"), false },
292 { FPL("//"), true }, 307 { FPL("//"), true },
293 { FPL("//a"), true }, 308 { FPL("//a"), true },
294 { FPL("c:a/b"), false }, 309 { FPL("c:a/b"), false },
295 { FPL("?:/a"), false }, 310 { FPL("?:/a"), false },
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 #endif // FILE_PATH_USES_WIN_SEPARATORS 357 #endif // FILE_PATH_USES_WIN_SEPARATORS
343 }; 358 };
344 359
345 for (size_t i = 0; i < arraysize(cases); ++i) { 360 for (size_t i = 0; i < arraysize(cases); ++i) {
346 FilePath input(cases[i].input); 361 FilePath input(cases[i].input);
347 bool observed = input.IsAbsolute(); 362 bool observed = input.IsAbsolute();
348 EXPECT_EQ(cases[i].expected, observed) << 363 EXPECT_EQ(cases[i].expected, observed) <<
349 "i: " << i << ", input: " << input.value(); 364 "i: " << i << ", input: " << input.value();
350 } 365 }
351 } 366 }
367
368 TEST_F(FilePathTest, Contains) {
369 FilePath data_dir;
370 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &data_dir));
371 data_dir = data_dir.Append(FILE_PATH_LITERAL("FilePathTest"));
372
373 // Create a fresh, empty copy of this directory.
374 file_util::Delete(data_dir, true);
375 file_util::CreateDirectory(data_dir);
376
377 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
378 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
379 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
380 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
381
382 // Annoyingly, the directories must actually exist in order for realpath(),
383 // which Contains() relies on in posix, to work.
384 file_util::CreateDirectory(foo);
385 std::string data("hello");
386 file_util::WriteFile(bar.ToWStringHack(), data.c_str(), data.length());
387 file_util::WriteFile(baz.ToWStringHack(), data.c_str(), data.length());
388 file_util::WriteFile(foobar.ToWStringHack(), data.c_str(), data.length());
389
390 EXPECT_TRUE(foo.Contains(bar));
391 EXPECT_FALSE(foo.Contains(baz));
392 EXPECT_FALSE(foo.Contains(foobar));
393 EXPECT_FALSE(foo.Contains(foo));
394
395 // Platform-specific concerns
396 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
397 #if defined(OS_WIN)
398 EXPECT_TRUE(foo.Contains(foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
399 EXPECT_TRUE(foo.Contains(
400 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
401 #elif defined(OS_LINUX)
402 EXPECT_FALSE(foo.Contains(foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
403 #else
404 // We can't really do this test on osx since the case-sensitivity of the
405 // filesystem is configurable.
406 #endif
407
408 // Note: whether
409 }
OLDNEW
« no previous file with comments | « base/file_path.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698