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

Side by Side Diff: base/files/file_path_unittest.cc

Issue 4883003: Add FilePath::FinalExtension() to avoid double extensions (.tar.gz) for file selector (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment (also a rebase) Created 7 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 | Annotate | Revision Log
« no previous file with comments | « base/files/file_path.cc ('k') | chrome/browser/download/download_extensions.cc » ('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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/files/file_path.h" 6 #include "base/files/file_path.h"
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "testing/platform_test.h" 9 #include "testing/platform_test.h"
10 10
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 "inequality i: " << i << ", a: " << a.value() << ", b: " << 701 "inequality i: " << i << ", a: " << a.value() << ", b: " <<
702 b.value(); 702 b.value();
703 } 703 }
704 } 704 }
705 705
706 TEST_F(FilePathTest, Extension) { 706 TEST_F(FilePathTest, Extension) {
707 FilePath base_dir(FILE_PATH_LITERAL("base_dir")); 707 FilePath base_dir(FILE_PATH_LITERAL("base_dir"));
708 708
709 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg")); 709 FilePath jpg = base_dir.Append(FILE_PATH_LITERAL("foo.jpg"));
710 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.Extension()); 710 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.Extension());
711 EXPECT_EQ(FILE_PATH_LITERAL(".jpg"), jpg.FinalExtension());
711 712
712 FilePath base = jpg.BaseName().RemoveExtension(); 713 FilePath base = jpg.BaseName().RemoveExtension();
713 EXPECT_EQ(FILE_PATH_LITERAL("foo"), base.value()); 714 EXPECT_EQ(FILE_PATH_LITERAL("foo"), base.value());
714 715
715 FilePath path_no_ext = base_dir.Append(base); 716 FilePath path_no_ext = base_dir.Append(base);
716 EXPECT_EQ(path_no_ext.value(), jpg.RemoveExtension().value()); 717 EXPECT_EQ(path_no_ext.value(), jpg.RemoveExtension().value());
717 718
718 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value()); 719 EXPECT_EQ(path_no_ext.value(), path_no_ext.RemoveExtension().value());
719 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.Extension()); 720 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.Extension());
721 EXPECT_EQ(FILE_PATH_LITERAL(""), path_no_ext.FinalExtension());
720 } 722 }
721 723
722 TEST_F(FilePathTest, Extension2) { 724 TEST_F(FilePathTest, Extension2) {
723 const struct UnaryTestData cases[] = { 725 const struct UnaryTestData cases[] = {
724 #if defined(FILE_PATH_USES_WIN_SEPARATORS) 726 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
725 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") }, 727 { FPL("C:\\a\\b\\c.ext"), FPL(".ext") },
726 { FPL("C:\\a\\b\\c."), FPL(".") }, 728 { FPL("C:\\a\\b\\c."), FPL(".") },
727 { FPL("C:\\a\\b\\c"), FPL("") }, 729 { FPL("C:\\a\\b\\c"), FPL("") },
728 { FPL("C:\\a\\b\\"), FPL("") }, 730 { FPL("C:\\a\\b\\"), FPL("") },
729 { FPL("C:\\a\\b.\\"), FPL(".") }, 731 { FPL("C:\\a\\b.\\"), FPL(".") },
730 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") }, 732 { FPL("C:\\a\\b\\c.ext1.ext2"), FPL(".ext2") },
731 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") }, 733 { FPL("C:\\foo.bar\\\\\\"), FPL(".bar") },
732 { FPL("C:\\foo.bar\\.."), FPL("") }, 734 { FPL("C:\\foo.bar\\.."), FPL("") },
733 { FPL("C:\\foo.bar\\..\\\\"), FPL("") }, 735 { FPL("C:\\foo.bar\\..\\\\"), FPL("") },
734 #endif 736 #endif
735 { FPL("/foo/bar/baz.ext"), FPL(".ext") }, 737 { FPL("/foo/bar/baz.ext"), FPL(".ext") },
736 { FPL("/foo/bar/baz."), FPL(".") }, 738 { FPL("/foo/bar/baz."), FPL(".") },
737 { FPL("/foo/bar/baz.."), FPL(".") }, 739 { FPL("/foo/bar/baz.."), FPL(".") },
738 { FPL("/foo/bar/baz"), FPL("") }, 740 { FPL("/foo/bar/baz"), FPL("") },
739 { FPL("/foo/bar/"), FPL("") }, 741 { FPL("/foo/bar/"), FPL("") },
740 { FPL("/foo/bar./"), FPL(".") }, 742 { FPL("/foo/bar./"), FPL(".") },
741 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") }, 743 { FPL("/foo/bar/baz.ext1.ext2"), FPL(".ext2") },
742 { FPL("/foo.tar.gz"), FPL(".tar.gz") },
743 { FPL("/foo.tar.Z"), FPL(".tar.Z") },
744 { FPL("/foo.tar.bz2"), FPL(".tar.bz2") },
745 { FPL("/subversion-1.6.12.zip"), FPL(".zip") }, 744 { FPL("/subversion-1.6.12.zip"), FPL(".zip") },
746 { FPL("/foo.1234.gz"), FPL(".1234.gz") },
747 { FPL("/foo.12345.gz"), FPL(".gz") }, 745 { FPL("/foo.12345.gz"), FPL(".gz") },
748 { FPL("/foo..gz"), FPL(".gz") }, 746 { FPL("/foo..gz"), FPL(".gz") },
749 { FPL("/foo.1234.tar.gz"), FPL(".tar.gz") },
750 { FPL("/foo.tar.tar.gz"), FPL(".tar.gz") },
751 { FPL("/foo.tar.gz.gz"), FPL(".gz.gz") },
752 { FPL("."), FPL("") }, 747 { FPL("."), FPL("") },
753 { FPL(".."), FPL("") }, 748 { FPL(".."), FPL("") },
754 { FPL("./foo"), FPL("") }, 749 { FPL("./foo"), FPL("") },
755 { FPL("./foo.ext"), FPL(".ext") }, 750 { FPL("./foo.ext"), FPL(".ext") },
756 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") }, 751 { FPL("/foo.ext1/bar.ext2"), FPL(".ext2") },
757 { FPL("/foo.bar////"), FPL(".bar") }, 752 { FPL("/foo.bar////"), FPL(".bar") },
758 { FPL("/foo.bar/.."), FPL("") }, 753 { FPL("/foo.bar/.."), FPL("") },
759 { FPL("/foo.bar/..////"), FPL("") }, 754 { FPL("/foo.bar/..////"), FPL("") },
760 { FPL("/foo.1234.user.js"), FPL(".user.js") },
761 { FPL("foo.user.js"), FPL(".user.js") },
762 { FPL("/foo.1234.luser.js"), FPL(".js") }, 755 { FPL("/foo.1234.luser.js"), FPL(".js") },
763 { FPL("/user.js"), FPL(".js") }, 756 { FPL("/user.js"), FPL(".js") },
764 }; 757 };
758 const struct UnaryTestData double_extension_cases[] = {
759 { FPL("/foo.tar.gz"), FPL(".tar.gz") },
760 { FPL("/foo.tar.Z"), FPL(".tar.Z") },
761 { FPL("/foo.tar.bz2"), FPL(".tar.bz2") },
762 { FPL("/foo.1234.gz"), FPL(".1234.gz") },
763 { FPL("/foo.1234.tar.gz"), FPL(".tar.gz") },
764 { FPL("/foo.tar.tar.gz"), FPL(".tar.gz") },
765 { FPL("/foo.tar.gz.gz"), FPL(".gz.gz") },
766 { FPL("/foo.1234.user.js"), FPL(".user.js") },
767 { FPL("foo.user.js"), FPL(".user.js") },
768 };
765 for (unsigned int i = 0; i < arraysize(cases); ++i) { 769 for (unsigned int i = 0; i < arraysize(cases); ++i) {
766 FilePath path(cases[i].input); 770 FilePath path(cases[i].input);
767 FilePath::StringType extension = path.Extension(); 771 FilePath::StringType extension = path.Extension();
772 FilePath::StringType final_extension = path.FinalExtension();
773 EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
774 ", path: " << path.value();
775 EXPECT_STREQ(cases[i].expected, final_extension.c_str()) << "i: " << i <<
776 ", path: " << path.value();
777 }
778 for (unsigned int i = 0; i < arraysize(double_extension_cases); ++i) {
779 FilePath path(cases[i].input);
780 FilePath::StringType extension = path.Extension();
768 EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i << 781 EXPECT_STREQ(cases[i].expected, extension.c_str()) << "i: " << i <<
769 ", path: " << path.value(); 782 ", path: " << path.value();
770 } 783 }
771 } 784 }
772 785
773 TEST_F(FilePathTest, InsertBeforeExtension) { 786 TEST_F(FilePathTest, InsertBeforeExtension) {
774 const struct BinaryTestData cases[] = { 787 const struct BinaryTestData cases[] = {
775 { { FPL(""), FPL("") }, FPL("") }, 788 { { FPL(""), FPL("") }, FPL("") },
776 { { FPL(""), FPL("txt") }, FPL("") }, 789 { { FPL(""), FPL("txt") }, FPL("") },
777 { { FPL("."), FPL("txt") }, FPL("") }, 790 { { FPL("."), FPL("txt") }, FPL("") },
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 { FPL(""), FPL("") }, 856 { FPL(""), FPL("") },
844 { FPL("."), FPL(".") }, 857 { FPL("."), FPL(".") },
845 { FPL(".."), FPL("..") }, 858 { FPL(".."), FPL("..") },
846 { FPL("foo.dll"), FPL("foo") }, 859 { FPL("foo.dll"), FPL("foo") },
847 { FPL("./foo.dll"), FPL("./foo") }, 860 { FPL("./foo.dll"), FPL("./foo") },
848 { FPL("foo..dll"), FPL("foo.") }, 861 { FPL("foo..dll"), FPL("foo.") },
849 { FPL("foo"), FPL("foo") }, 862 { FPL("foo"), FPL("foo") },
850 { FPL("foo."), FPL("foo") }, 863 { FPL("foo."), FPL("foo") },
851 { FPL("foo.."), FPL("foo.") }, 864 { FPL("foo.."), FPL("foo.") },
852 { FPL("foo.baz.dll"), FPL("foo.baz") }, 865 { FPL("foo.baz.dll"), FPL("foo.baz") },
853 { FPL("foo.tar.gz"), FPL("foo") },
854 #if defined(FILE_PATH_USES_WIN_SEPARATORS) 866 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
855 { FPL("C:\\foo.bar\\foo"), FPL("C:\\foo.bar\\foo") }, 867 { FPL("C:\\foo.bar\\foo"), FPL("C:\\foo.bar\\foo") },
856 { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") }, 868 { FPL("C:\\foo.bar\\..\\\\"), FPL("C:\\foo.bar\\..\\\\") },
857 #endif 869 #endif
858 { FPL("/foo.bar/foo"), FPL("/foo.bar/foo") }, 870 { FPL("/foo.bar/foo"), FPL("/foo.bar/foo") },
859 { FPL("/foo.bar/..////"), FPL("/foo.bar/..////") }, 871 { FPL("/foo.bar/..////"), FPL("/foo.bar/..////") },
860 }; 872 };
861 for (unsigned int i = 0; i < arraysize(cases); ++i) { 873 for (unsigned int i = 0; i < arraysize(cases); ++i) {
862 FilePath path(cases[i].input); 874 FilePath path(cases[i].input);
863 FilePath removed = path.RemoveExtension(); 875 FilePath removed = path.RemoveExtension();
876 FilePath removed_final = path.RemoveFinalExtension();
864 EXPECT_EQ(cases[i].expected, removed.value()) << "i: " << i << 877 EXPECT_EQ(cases[i].expected, removed.value()) << "i: " << i <<
865 ", path: " << path.value(); 878 ", path: " << path.value();
879 EXPECT_EQ(cases[i].expected, removed_final.value()) << "i: " << i <<
880 ", path: " << path.value();
881 }
882 {
883 FilePath path(FPL("foo.tar.gz"));
884 FilePath removed = path.RemoveExtension();
885 FilePath removed_final = path.RemoveFinalExtension();
886 EXPECT_EQ(FPL("foo"), removed.value()) << ", path: " << path.value();
887 EXPECT_EQ(FPL("foo.tar"), removed_final.value()) << ", path: "
888 << path.value();
866 } 889 }
867 } 890 }
868 891
869 TEST_F(FilePathTest, ReplaceExtension) { 892 TEST_F(FilePathTest, ReplaceExtension) {
870 const struct BinaryTestData cases[] = { 893 const struct BinaryTestData cases[] = {
871 { { FPL(""), FPL("") }, FPL("") }, 894 { { FPL(""), FPL("") }, FPL("") },
872 { { FPL(""), FPL("txt") }, FPL("") }, 895 { { FPL(""), FPL("txt") }, FPL("") },
873 { { FPL("."), FPL("txt") }, FPL("") }, 896 { { FPL("."), FPL("txt") }, FPL("") },
874 { { FPL(".."), FPL("txt") }, FPL("") }, 897 { { FPL(".."), FPL("txt") }, FPL("") },
875 { { FPL("."), FPL("") }, FPL("") }, 898 { { FPL("."), FPL("") }, FPL("") },
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after
1251 for (size_t i = 0; i < arraysize(cases); ++i) { 1274 for (size_t i = 0; i < arraysize(cases); ++i) {
1252 FilePath input(cases[i].input); 1275 FilePath input(cases[i].input);
1253 bool observed = input.IsContentUri(); 1276 bool observed = input.IsContentUri();
1254 EXPECT_EQ(cases[i].expected, observed) << 1277 EXPECT_EQ(cases[i].expected, observed) <<
1255 "i: " << i << ", input: " << input.value(); 1278 "i: " << i << ", input: " << input.value();
1256 } 1279 }
1257 } 1280 }
1258 #endif 1281 #endif
1259 1282
1260 } // namespace base 1283 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_path.cc ('k') | chrome/browser/download/download_extensions.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698