OLD | NEW |
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" |
(...skipping 680 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 EXPECT_EQ(a != b, !cases[i].expected) << | 691 EXPECT_EQ(a != b, !cases[i].expected) << |
692 "inequality i: " << i << ", a: " << a.value() << ", b: " << | 692 "inequality i: " << i << ", a: " << a.value() << ", b: " << |
693 b.value(); | 693 b.value(); |
694 } | 694 } |
695 } | 695 } |
696 | 696 |
697 TEST_F(FilePathTest, Extension) { | 697 TEST_F(FilePathTest, Extension) { |
698 FilePath base_dir(FILE_PATH_LITERAL("base_dir")); | 698 FilePath base_dir(FILE_PATH_LITERAL("base_dir")); |
699 | 699 |
700 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg")); | 700 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg")); |
701 EXPECT_EQ(jpg.Extension(), FILE_PATH_LITERAL(".jpg")); | 701 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.Extension()); |
702 | 702 |
703 FilePath base = jpg.BaseName().RemoveExtension(); | 703 FilePath base = jpg.BaseName().RemoveExtension(); |
704 EXPECT_EQ(base.value(), FILE_PATH_LITERAL("foo")); | 704 EXPECT_EQ(FILE_PATH_LITERAL("foo"), base.value()); |
705 | 705 |
706 FilePath path_no_ext = base_dir.Append(base); | 706 FilePath path_no_ext = base_dir.Append(base); |
707 EXPECT_EQ(jpg.RemoveExtension().value(), path_no_ext.value()); | 707 EXPECT_EQ(path_no_ext.value(), jpg.RemoveExtension().value()); |
708 | 708 |
709 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value()); | 709 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value()); |
710 EXPECT_EQ(path_no_ext.Extension(), FILE_PATH_LITERAL("")); | 710 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.Extension()); |
711 } | 711 } |
712 | 712 |
713 TEST_F(FilePathTest, Extension2) { | 713 TEST_F(FilePathTest, Extension2) { |
714 const struct UnaryTestData cases[] = { | 714 const struct UnaryTestData cases[] = { |
715 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 715 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
716 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") }, | 716 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") }, |
717 { FPL("C:\\a\\b\\c."), FPL(".") }, | 717 { FPL("C:\\a\\b\\c."), FPL(".") }, |
718 { FPL("C:\\a\\b\\c"), FPL("") }, | 718 { FPL("C:\\a\\b\\c"), FPL("") }, |
719 { FPL("C:\\a\\b\\"), FPL("") }, | 719 { FPL("C:\\a\\b\\"), FPL("") }, |
720 { FPL("C:\\a\\b.\\"), FPL(".") }, | 720 { FPL("C:\\a\\b.\\"), FPL(".") }, |
721 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") }, | 721 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") }, |
722 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") }, | 722 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") }, |
723 { FPL("C:\\foo.bar\\.."), FPL("") }, | 723 { FPL("C:\\foo.bar\\.."), FPL("") }, |
724 { FPL("C:\\foo.bar\\..\\\\"), FPL("") }, | 724 { FPL("C:\\foo.bar\\..\\\\"), FPL("") }, |
725 #endif | 725 #endif |
726 { FPL("/foo/bar/baz.ext"), FPL(".ext") }, | 726 { FPL("/foo/bar/baz.ext"), FPL(".ext") }, |
727 { FPL("/foo/bar/baz."), FPL(".") }, | 727 { FPL("/foo/bar/baz."), FPL(".") }, |
728 { FPL("/foo/bar/baz.."), FPL(".") }, | 728 { FPL("/foo/bar/baz.."), FPL(".") }, |
729 { FPL("/foo/bar/baz"), FPL("") }, | 729 { FPL("/foo/bar/baz"), FPL("") }, |
730 { FPL("/foo/bar/"), FPL("") }, | 730 { FPL("/foo/bar/"), FPL("") }, |
731 { FPL("/foo/bar./"), FPL(".") }, | 731 { FPL("/foo/bar./"), FPL(".") }, |
732 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") }, | 732 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") }, |
| 733 { FPL("/foo.tar.gz"), FPL(".tar.gz") }, |
| 734 { FPL("/foo.tar.Z"), FPL(".tar.Z") }, |
| 735 { FPL("/foo.tar.bz2"), FPL(".tar.bz2") }, |
| 736 { FPL("/subversion-1.6.12.zip"), FPL(".zip") }, |
| 737 { FPL("/foo.1234.gz"), FPL(".1234.gz") }, |
| 738 { FPL("/foo.12345.gz"), FPL(".gz") }, |
| 739 { FPL("/foo..gz"), FPL(".gz") }, |
| 740 { FPL("/foo.1234.tar.gz"), FPL(".tar.gz") }, |
| 741 { FPL("/foo.tar.tar.gz"), FPL(".tar.gz") }, |
| 742 { FPL("/foo.tar.gz.gz"), FPL(".gz.gz") }, |
733 { FPL("."), FPL("") }, | 743 { FPL("."), FPL("") }, |
734 { FPL(".."), FPL("") }, | 744 { FPL(".."), FPL("") }, |
735 { FPL("./foo"), FPL("") }, | 745 { FPL("./foo"), FPL("") }, |
736 { FPL("./foo.ext"), FPL(".ext") }, | 746 { FPL("./foo.ext"), FPL(".ext") }, |
737 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") }, | 747 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") }, |
738 { FPL("/foo.bar////"), FPL(".bar") }, | 748 { FPL("/foo.bar////"), FPL(".bar") }, |
739 { FPL("/foo.bar/.."), FPL("") }, | 749 { FPL("/foo.bar/.."), FPL("") }, |
740 { FPL("/foo.bar/..////"), FPL("") }, | 750 { FPL("/foo.bar/..////"), FPL("") }, |
741 }; | 751 }; |
742 for (unsigned int i = 0; i < arraysize(cases); ++i) { | 752 for (unsigned int i = 0; i < arraysize(cases); ++i) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") }, | 818 { { FPL("/bar/baz/..////"), FPL(" (1)") }, FPL("") }, |
809 }; | 819 }; |
810 for (unsigned int i = 0; i < arraysize(cases); ++i) { | 820 for (unsigned int i = 0; i < arraysize(cases); ++i) { |
811 FilePath path(cases[i].inputs[0]); | 821 FilePath path(cases[i].inputs[0]); |
812 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]); | 822 FilePath result = path.InsertBeforeExtension(cases[i].inputs[1]); |
813 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i << | 823 EXPECT_EQ(cases[i].expected, result.value()) << "i: " << i << |
814 ", path: " << path.value() << ", insert: " << cases[i].inputs[1]; | 824 ", path: " << path.value() << ", insert: " << cases[i].inputs[1]; |
815 } | 825 } |
816 } | 826 } |
817 | 827 |
| 828 TEST_F(FilePathTest, RemoveExtension) { |
| 829 const struct UnaryTestData cases[] = { |
| 830 { FPL(""), FPL("") }, |
| 831 { FPL("."), FPL(".") }, |
| 832 { FPL(".."), FPL("..") }, |
| 833 { FPL("foo.dll"), FPL("foo") }, |
| 834 { FPL("./foo.dll"), FPL("./foo") }, |
| 835 { FPL("foo..dll"), FPL("foo.") }, |
| 836 { FPL("foo"), FPL("foo") }, |
| 837 { FPL("foo."), FPL("foo") }, |
| 838 { FPL("foo.."), FPL("foo.") }, |
| 839 { FPL("foo.baz.dll"), FPL("foo.baz") }, |
| 840 { FPL("foo.tar.gz"), FPL("foo") }, |
| 841 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| 842 { FPL("C:\\foo.bar\\foo"), FPL("C:\\foo.bar\\foo") }, |
| 843 { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") }, |
| 844 #endif |
| 845 { FPL("/foo.bar/foo"), FPL("/foo.bar/foo") }, |
| 846 { FPL("/foo.bar/..////"), FPL("/foo.bar/..////") }, |
| 847 }; |
| 848 for (unsigned int i = 0; i < arraysize(cases); ++i) { |
| 849 FilePath path(cases[i].input); |
| 850 FilePath removed = path.RemoveExtension(); |
| 851 EXPECT_EQ(cases[i].expected, removed.value()) << "i: " << i << |
| 852 ", path: " << path.value(); |
| 853 } |
| 854 } |
| 855 |
818 TEST_F(FilePathTest, ReplaceExtension) { | 856 TEST_F(FilePathTest, ReplaceExtension) { |
819 const struct BinaryTestData cases[] = { | 857 const struct BinaryTestData cases[] = { |
820 { { FPL(""), FPL("") }, FPL("") }, | 858 { { FPL(""), FPL("") }, FPL("") }, |
821 { { FPL(""), FPL("txt") }, FPL("") }, | 859 { { FPL(""), FPL("txt") }, FPL("") }, |
822 { { FPL("."), FPL("txt") }, FPL("") }, | 860 { { FPL("."), FPL("txt") }, FPL("") }, |
823 { { FPL(".."), FPL("txt") }, FPL("") }, | 861 { { FPL(".."), FPL("txt") }, FPL("") }, |
824 { { FPL("."), FPL("") }, FPL("") }, | 862 { { FPL("."), FPL("") }, FPL("") }, |
825 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") }, | 863 { { FPL("foo.dll"), FPL("txt") }, FPL("foo.txt") }, |
| 864 { { FPL("./foo.dll"), FPL("txt") }, FPL("./foo.txt") }, |
826 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") }, | 865 { { FPL("foo..dll"), FPL("txt") }, FPL("foo..txt") }, |
827 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") }, | 866 { { FPL("foo.dll"), FPL(".txt") }, FPL("foo.txt") }, |
828 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") }, | 867 { { FPL("foo"), FPL("txt") }, FPL("foo.txt") }, |
829 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") }, | 868 { { FPL("foo."), FPL("txt") }, FPL("foo.txt") }, |
830 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") }, | 869 { { FPL("foo.."), FPL("txt") }, FPL("foo..txt") }, |
831 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") }, | 870 { { FPL("foo"), FPL(".txt") }, FPL("foo.txt") }, |
832 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") }, | 871 { { FPL("foo.baz.dll"), FPL("txt") }, FPL("foo.baz.txt") }, |
833 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") }, | 872 { { FPL("foo.baz.dll"), FPL(".txt") }, FPL("foo.baz.txt") }, |
834 { { FPL("foo.dll"), FPL("") }, FPL("foo") }, | 873 { { FPL("foo.dll"), FPL("") }, FPL("foo") }, |
835 { { FPL("foo.dll"), FPL(".") }, FPL("foo") }, | 874 { { FPL("foo.dll"), FPL(".") }, FPL("foo") }, |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1041 { FPL("/\\foo\\/bar"), FPL("\\\\foo\\\\bar") }, | 1080 { FPL("/\\foo\\/bar"), FPL("\\\\foo\\\\bar") }, |
1042 }; | 1081 }; |
1043 for (size_t i = 0; i < arraysize(cases); ++i) { | 1082 for (size_t i = 0; i < arraysize(cases); ++i) { |
1044 FilePath input(cases[i].input); | 1083 FilePath input(cases[i].input); |
1045 FilePath observed = input.NormalizeWindowsPathSeparators(); | 1084 FilePath observed = input.NormalizeWindowsPathSeparators(); |
1046 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) << | 1085 EXPECT_EQ(FilePath::StringType(cases[i].expected), observed.value()) << |
1047 "i: " << i << ", input: " << input.value(); | 1086 "i: " << i << ", input: " << input.value(); |
1048 } | 1087 } |
1049 } | 1088 } |
1050 #endif | 1089 #endif |
OLD | NEW |