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

Side by Side Diff: base/file_path_unittest.cc

Issue 145026: Move PathComponents from file_util to FilePath, add FilePath::IsParent() (Closed)
Patch Set: noop Created 11 years, 6 months 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') | base/file_util.h » ('j') | 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/basictypes.h"
6 #include "base/file_path.h" 6 #include "base/file_path.h"
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h" 10 #include "testing/platform_test.h"
11 11
12 // This macro helps avoid wrapped lines in the test structs. 12 // This macro helps avoid wrapped lines in the test structs.
13 #define FPL(x) FILE_PATH_LITERAL(x) 13 #define FPL(x) FILE_PATH_LITERAL(x)
14 14
15 struct UnaryTestData { 15 struct UnaryTestData {
16 const FilePath::CharType* input; 16 const FilePath::CharType* input;
17 const FilePath::CharType* expected; 17 const FilePath::CharType* expected;
18 }; 18 };
19 19
20 struct UnaryBooleanTestData { 20 struct UnaryBooleanTestData {
21 const FilePath::CharType* input; 21 const FilePath::CharType* input;
22 bool expected; 22 bool expected;
23 }; 23 };
24 24
25 struct BinaryTestData { 25 struct BinaryTestData {
26 const FilePath::CharType* inputs[2]; 26 const FilePath::CharType* inputs[2];
27 const FilePath::CharType* expected; 27 const FilePath::CharType* expected;
28 }; 28 };
29 29
30 struct BinaryBooleanTestData {
31 const FilePath::CharType* inputs[2];
32 bool expected;
33 };
34
30 // file_util winds up using autoreleased objects on the Mac, so this needs 35 // file_util winds up using autoreleased objects on the Mac, so this needs
31 // to be a PlatformTest 36 // to be a PlatformTest
32 class FilePathTest : public PlatformTest { 37 class FilePathTest : public PlatformTest {
33 protected: 38 protected:
34 virtual void SetUp() { 39 virtual void SetUp() {
35 PlatformTest::SetUp(); 40 PlatformTest::SetUp();
36 } 41 }
37 virtual void TearDown() { 42 virtual void TearDown() {
38 PlatformTest::TearDown(); 43 PlatformTest::TearDown();
39 } 44 }
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 }; 373 };
369 374
370 for (size_t i = 0; i < arraysize(cases); ++i) { 375 for (size_t i = 0; i < arraysize(cases); ++i) {
371 FilePath input(cases[i].input); 376 FilePath input(cases[i].input);
372 bool observed = input.IsAbsolute(); 377 bool observed = input.IsAbsolute();
373 EXPECT_EQ(cases[i].expected, observed) << 378 EXPECT_EQ(cases[i].expected, observed) <<
374 "i: " << i << ", input: " << input.value(); 379 "i: " << i << ", input: " << input.value();
375 } 380 }
376 } 381 }
377 382
383 TEST_F(FilePathTest, PathComponentsTest) {
384 const struct UnaryTestData cases[] = {
385 { FPL("//foo/bar/baz/"), FPL("|//|foo|bar|baz")},
386 { FPL("///"), FPL("|/")},
387 { FPL("/foo//bar//baz/"), FPL("|/|foo|bar|baz")},
388 { FPL("/foo/bar/baz/"), FPL("|/|foo|bar|baz")},
389 { FPL("/foo/bar/baz//"), FPL("|/|foo|bar|baz")},
390 { FPL("/foo/bar/baz///"), FPL("|/|foo|bar|baz")},
391 { FPL("/foo/bar/baz"), FPL("|/|foo|bar|baz")},
392 { FPL("/foo/bar.bot/baz.txt"), FPL("|/|foo|bar.bot|baz.txt")},
393 { FPL("//foo//bar/baz"), FPL("|//|foo|bar|baz")},
394 { FPL("/"), FPL("|/")},
395 { FPL("foo"), FPL("|foo")},
396 { FPL(""), FPL("")},
397 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
398 { FPL("e:/foo"), FPL("|e:|/|foo")},
399 { FPL("e:/"), FPL("|e:|/")},
400 { FPL("e:"), FPL("|e:")},
401 #endif // FILE_PATH_USES_DRIVE_LETTERS
402 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
403 { FPL("../foo"), FPL("|..|foo")},
404 { FPL("./foo"), FPL("|foo")},
405 { FPL("../foo/bar/"), FPL("|..|foo|bar") },
406 { FPL("\\\\foo\\bar\\baz\\"), FPL("|\\\\|foo|bar|baz")},
407 { FPL("\\\\\\"), FPL("|\\")},
408 { FPL("\\foo\\\\bar\\\\baz\\"), FPL("|\\|foo|bar|baz")},
409 { FPL("\\foo\\bar\\baz\\"), FPL("|\\|foo|bar|baz")},
410 { FPL("\\foo\\bar\\baz\\\\"), FPL("|\\|foo|bar|baz")},
411 { FPL("\\foo\\bar\\baz\\\\\\"), FPL("|\\|foo|bar|baz")},
412 { FPL("\\foo\\bar\\baz"), FPL("|\\|foo|bar|baz")},
413 { FPL("\\foo\\bar/baz\\\\\\"), FPL("|\\|foo|bar|baz")},
414 { FPL("/foo\\bar\\baz"), FPL("|/|foo|bar|baz")},
415 { FPL("\\foo\\bar.bot\\baz.txt"), FPL("|\\|foo|bar.bot|baz.txt")},
416 { FPL("\\\\foo\\\\bar\\baz"), FPL("|\\\\|foo|bar|baz")},
417 { FPL("\\"), FPL("|\\")},
418 #endif // FILE_PATH_USES_WIN_SEPARATORS
419 };
420
421 for (size_t i = 0; i < arraysize(cases); ++i) {
422 FilePath input(cases[i].input);
423 std::vector<FilePath::StringType> comps;
424 input.GetComponents(&comps);
425
426 FilePath::StringType observed;
427 for (size_t j = 0; j < comps.size(); ++j) {
428 observed.append(FILE_PATH_LITERAL("|"), 1);
429 observed.append(comps[j]);
430 }
431 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed) <<
432 "i: " << i << ", input: " << input.value();
433 }
434 }
435
436 TEST_F(FilePathTest, IsParentTest) {
437 const struct BinaryBooleanTestData cases[] = {
438 { { FPL("/"), FPL("/foo/bar/baz") }, true},
439 { { FPL("/foo/bar"), FPL("/foo/bar/baz") }, true},
440 { { FPL("/foo/bar/"), FPL("/foo/bar/baz") }, true},
441 { { FPL("//foo/bar/"), FPL("//foo/bar/baz") }, true},
442 { { FPL("/foo/bar"), FPL("/foo2/bar/baz") }, false},
443 { { FPL("/foo/bar.txt"), FPL("/foo/bar/baz") }, false},
444 { { FPL("/foo/bar"), FPL("/foo/bar2/baz") }, false},
445 { { FPL("/foo/bar"), FPL("/foo/bar") }, false},
446 { { FPL("/foo/bar/baz"), FPL("/foo/bar") }, false},
447 { { FPL("foo/bar"), FPL("foo/bar/baz") }, true},
448 { { FPL("foo/bar"), FPL("foo2/bar/baz") }, false},
449 { { FPL("foo/bar"), FPL("foo/bar2/baz") }, false},
450 { { FPL(""), FPL("foo") }, false},
451 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
452 { { FPL("c:/foo/bar"), FPL("c:/foo/bar/baz") }, true},
453 { { FPL("c:/"), FPL("c:/foo/bar/baz") }, true},
454 { { FPL("c:"), FPL("c:/foo/bar/baz") }, true},
455 { { FPL("c:/foo/bar"), FPL("d:/foo/bar/baz") }, false},
456 { { FPL("c:/foo/bar"), FPL("c:/foo2/bar/baz") }, false},
457 { { FPL("c:/foo/bar"), FPL("c:/foo/bar2/baz") }, false},
458 #endif // FILE_PATH_USES_DRIVE_LETTERS
459 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
460 { { FPL("\\foo\\bar"), FPL("\\foo\\bar\\baz") }, true},
461 { { FPL("\\foo/bar"), FPL("\\foo\\bar\\baz") }, true},
462 { { FPL("\\foo/bar"), FPL("\\foo/bar/baz") }, true},
463 { { FPL("\\"), FPL("\\foo\\bar\\baz") }, true},
464 { { FPL(""), FPL("\\foo\\bar\\baz") }, false},
465 { { FPL("\\foo\\bar"), FPL("\\foo2\\bar\\baz") }, false},
466 { { FPL("\\foo\\bar"), FPL("\\foo\\bar2\\baz") }, false},
467 #endif // FILE_PATH_USES_DRIVE_LETTERS
468 };
469
470 for (size_t i = 0; i < arraysize(cases); ++i) {
471 FilePath parent(cases[i].inputs[0]);
472 FilePath child(cases[i].inputs[1]);
473
474 EXPECT_EQ(parent.IsParent(child), cases[i].expected) <<
475 "i: " << i << ", parent: " << parent.value() << ", child: " <<
476 child.value();
477 }
478 }
479
378 TEST_F(FilePathTest, Extension) { 480 TEST_F(FilePathTest, Extension) {
379 FilePath base_dir(FILE_PATH_LITERAL("base_dir")); 481 FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
380 482
381 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg")); 483 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
382 EXPECT_EQ(jpg.Extension(), FILE_PATH_LITERAL(".jpg")); 484 EXPECT_EQ(jpg.Extension(), FILE_PATH_LITERAL(".jpg"));
383 485
384 FilePath base = jpg.BaseName().RemoveExtension(); 486 FilePath base = jpg.BaseName().RemoveExtension();
385 EXPECT_EQ(base.value(), FILE_PATH_LITERAL("foo")); 487 EXPECT_EQ(base.value(), FILE_PATH_LITERAL("foo"));
386 488
387 FilePath path_no_ext = base_dir.Append(base); 489 FilePath path_no_ext = base_dir.Append(base);
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") }, 627 { { FPL("/foo.bar/foo"), FPL("baz") }, FPL("/foo.bar/foo.baz") },
526 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") }, 628 { { FPL("/foo.bar/..////"), FPL("baz") }, FPL("") },
527 }; 629 };
528 for (unsigned int i = 0; i < arraysize(cases); ++i) { 630 for (unsigned int i = 0; i < arraysize(cases); ++i) {
529 FilePath path(cases[i].inputs[0]); 631 FilePath path(cases[i].inputs[0]);
530 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]); 632 FilePath replaced = path.ReplaceExtension(cases[i].inputs[1]);
531 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i << 633 EXPECT_EQ(cases[i].expected, replaced.value()) << "i: " << i <<
532 ", path: " << path.value() << ", replace: " << cases[i].inputs[1]; 634 ", path: " << path.value() << ", replace: " << cases[i].inputs[1];
533 } 635 }
534 } 636 }
OLDNEW
« no previous file with comments | « base/file_path.cc ('k') | base/file_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698