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

Side by Side Diff: base/file_util_unittest.cc

Issue 62008: Reduce usage of {To,From}WStringHack in file_util_unittest.cc (Closed)
Patch Set: Created 11 years, 8 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 | « no previous file | 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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #include <shellapi.h> 9 #include <shellapi.h>
10 #include <shlobj.h> 10 #include <shlobj.h>
11 #endif 11 #endif
12 12
13 #include <fstream> 13 #include <fstream>
14 #include <iostream> 14 #include <iostream>
15 #include <set> 15 #include <set>
16 16
17 #include "base/base_paths.h" 17 #include "base/base_paths.h"
18 #include "base/file_path.h" 18 #include "base/file_path.h"
19 #include "base/file_util.h" 19 #include "base/file_util.h"
20 #include "base/logging.h" 20 #include "base/logging.h"
21 #include "base/path_service.h" 21 #include "base/path_service.h"
22 #include "base/string_util.h" 22 #include "base/string_util.h"
23 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "testing/platform_test.h" 24 #include "testing/platform_test.h"
25 25
26 // This macro helps avoid wrapped lines in the test structs.
27 #define FPL(x) FILE_PATH_LITERAL(x)
28
26 namespace { 29 namespace {
27 30
28 // file_util winds up using autoreleased objects on the Mac, so this needs 31 // file_util winds up using autoreleased objects on the Mac, so this needs
29 // to be a PlatformTest 32 // to be a PlatformTest
30 class FileUtilTest : public PlatformTest { 33 class FileUtilTest : public PlatformTest {
31 protected: 34 protected:
32 virtual void SetUp() { 35 virtual void SetUp() {
33 PlatformTest::SetUp(); 36 PlatformTest::SetUp();
34 // Name a subdirectory of the temp directory. 37 // Name a subdirectory of the temp directory.
35 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); 38 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 file_util::AppendToPath(&result, value.ending); 144 file_util::AppendToPath(&result, value.ending);
142 EXPECT_EQ(value.result, result); 145 EXPECT_EQ(value.result, result);
143 } 146 }
144 147
145 #ifdef NDEBUG 148 #ifdef NDEBUG
146 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode 149 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode
147 #endif 150 #endif
148 } 151 }
149 152
150 static const struct InsertBeforeExtensionCase { 153 static const struct InsertBeforeExtensionCase {
151 std::wstring path; 154 const FilePath::CharType* path;
152 FilePath::StringType suffix; 155 const FilePath::CharType* suffix;
153 std::wstring result; 156 const FilePath::CharType* result;
154 } kInsertBeforeExtension[] = { 157 } kInsertBeforeExtension[] = {
155 {L"", FILE_PATH_LITERAL(""), L""}, 158 {FPL(""), FPL(""), FPL("")},
156 {L"", FILE_PATH_LITERAL("txt"), L"txt"}, 159 {FPL(""), FPL("txt"), FPL("txt")},
157 {L".", FILE_PATH_LITERAL("txt"), L"txt."}, 160 {FPL("."), FPL("txt"), FPL("txt.")},
158 {L".", FILE_PATH_LITERAL(""), L"."}, 161 {FPL("."), FPL(""), FPL(".")},
159 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"footxt.dll"}, 162 {FPL("foo.dll"), FPL("txt"), FPL("footxt.dll")},
160 {L"foo.dll", FILE_PATH_LITERAL(".txt"), L"foo.txt.dll"}, 163 {FPL("foo.dll"), FPL(".txt"), FPL("foo.txt.dll")},
161 {L"foo", FILE_PATH_LITERAL("txt"), L"footxt"}, 164 {FPL("foo"), FPL("txt"), FPL("footxt")},
162 {L"foo", FILE_PATH_LITERAL(".txt"), L"foo.txt"}, 165 {FPL("foo"), FPL(".txt"), FPL("foo.txt")},
163 {L"foo.baz.dll", FILE_PATH_LITERAL("txt"), L"foo.baztxt.dll"}, 166 {FPL("foo.baz.dll"), FPL("txt"), FPL("foo.baztxt.dll")},
164 {L"foo.baz.dll", FILE_PATH_LITERAL(".txt"), L"foo.baz.txt.dll"}, 167 {FPL("foo.baz.dll"), FPL(".txt"), FPL("foo.baz.txt.dll")},
165 {L"foo.dll", FILE_PATH_LITERAL(""), L"foo.dll"}, 168 {FPL("foo.dll"), FPL(""), FPL("foo.dll")},
166 {L"foo.dll", FILE_PATH_LITERAL("."), L"foo..dll"}, 169 {FPL("foo.dll"), FPL("."), FPL("foo..dll")},
167 {L"foo", FILE_PATH_LITERAL(""), L"foo"}, 170 {FPL("foo"), FPL(""), FPL("foo")},
168 {L"foo", FILE_PATH_LITERAL("."), L"foo."}, 171 {FPL("foo"), FPL("."), FPL("foo.")},
169 {L"foo.baz.dll", FILE_PATH_LITERAL(""), L"foo.baz.dll"}, 172 {FPL("foo.baz.dll"), FPL(""), FPL("foo.baz.dll")},
170 {L"foo.baz.dll", FILE_PATH_LITERAL("."), L"foo.baz..dll"}, 173 {FPL("foo.baz.dll"), FPL("."), FPL("foo.baz..dll")},
171 #if defined(OS_WIN) 174 #if defined(OS_WIN)
172 {L"\\", L"", L"\\"}, 175 {FPL("\\"), FPL(""), FPL("\\")},
173 {L"\\", L"txt", L"\\txt"}, 176 {FPL("\\"), FPL("txt"), FPL("\\txt")},
174 {L"\\.", L"txt", L"\\txt."}, 177 {FPL("\\."), FPL("txt"), FPL("\\txt.")},
175 {L"\\.", L"", L"\\."}, 178 {FPL("\\."), FPL(""), FPL("\\.")},
176 {L"C:\\bar\\foo.dll", L"txt", L"C:\\bar\\footxt.dll"}, 179 {FPL("C:\\bar\\foo.dll"), FPL("txt"), FPL("C:\\bar\\footxt.dll")},
177 {L"C:\\bar.baz\\foodll", L"txt", L"C:\\bar.baz\\foodlltxt"}, 180 {FPL("C:\\bar.baz\\foodll"), FPL("txt"), FPL("C:\\bar.baz\\foodlltxt")},
178 {L"C:\\bar.baz\\foo.dll", L"txt", L"C:\\bar.baz\\footxt.dll"}, 181 {FPL("C:\\bar.baz\\foo.dll"), FPL("txt"), FPL("C:\\bar.baz\\footxt.dll")},
179 {L"C:\\bar.baz\\foo.dll.exe", L"txt", L"C:\\bar.baz\\foo.dlltxt.exe"}, 182 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL("txt"),
180 {L"C:\\bar.baz\\foo", L"", L"C:\\bar.baz\\foo"}, 183 FPL("C:\\bar.baz\\foo.dlltxt.exe")},
181 {L"C:\\bar.baz\\foo.exe", L"", L"C:\\bar.baz\\foo.exe"}, 184 {FPL("C:\\bar.baz\\foo"), FPL(""), FPL("C:\\bar.baz\\foo")},
182 {L"C:\\bar.baz\\foo.dll.exe", L"", L"C:\\bar.baz\\foo.dll.exe"}, 185 {FPL("C:\\bar.baz\\foo.exe"), FPL(""), FPL("C:\\bar.baz\\foo.exe")},
183 {L"C:\\bar\\baz\\foo.exe", L" (1)", L"C:\\bar\\baz\\foo (1).exe"}, 186 {FPL("C:\\bar.baz\\foo.dll.exe"), FPL(""), FPL("C:\\bar.baz\\foo.dll.exe")},
187 {FPL("C:\\bar\\baz\\foo.exe"), FPL(" (1)"), FPL("C:\\bar\\baz\\foo (1).exe")},
184 #elif defined(OS_POSIX) 188 #elif defined(OS_POSIX)
185 {L"/", "", L"/"}, 189 {FPL("/"), FPL(""), FPL("/")},
186 {L"/", "txt", L"/txt"}, 190 {FPL("/"), FPL("txt"), FPL("/txt")},
187 {L"/.", "txt", L"/txt."}, 191 {FPL("/."), FPL("txt"), FPL("/txt.")},
188 {L"/.", "", L"/."}, 192 {FPL("/."), FPL(""), FPL("/.")},
189 {L"/bar/foo.dll", "txt", L"/bar/footxt.dll"}, 193 {FPL("/bar/foo.dll"), FPL("txt"), FPL("/bar/footxt.dll")},
190 {L"/bar.baz/foodll", "txt", L"/bar.baz/foodlltxt"}, 194 {FPL("/bar.baz/foodll"), FPL("txt"), FPL("/bar.baz/foodlltxt")},
191 {L"/bar.baz/foo.dll", "txt", L"/bar.baz/footxt.dll"}, 195 {FPL("/bar.baz/foo.dll"), FPL("txt"), FPL("/bar.baz/footxt.dll")},
192 {L"/bar.baz/foo.dll.exe", "txt", L"/bar.baz/foo.dlltxt.exe"}, 196 {FPL("/bar.baz/foo.dll.exe"), FPL("txt"), FPL("/bar.baz/foo.dlltxt.exe")},
193 {L"/bar.baz/foo", "", L"/bar.baz/foo"}, 197 {FPL("/bar.baz/foo"), FPL(""), FPL("/bar.baz/foo")},
194 {L"/bar.baz/foo.exe", "", L"/bar.baz/foo.exe"}, 198 {FPL("/bar.baz/foo.exe"), FPL(""), FPL("/bar.baz/foo.exe")},
195 {L"/bar.baz/foo.dll.exe", "", L"/bar.baz/foo.dll.exe"}, 199 {FPL("/bar.baz/foo.dll.exe"), FPL(""), FPL("/bar.baz/foo.dll.exe")},
196 {L"/bar/baz/foo.exe", " (1)", L"/bar/baz/foo (1).exe"}, 200 {FPL("/bar/baz/foo.exe"), FPL(" (1)"), FPL("/bar/baz/foo (1).exe")},
197 #endif 201 #endif
198 }; 202 };
199 203
200 TEST_F(FileUtilTest, InsertBeforeExtensionTest) { 204 TEST_F(FileUtilTest, InsertBeforeExtensionTest) {
201 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) { 205 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) {
202 FilePath path = FilePath::FromWStringHack(kInsertBeforeExtension[i].path); 206 FilePath path(kInsertBeforeExtension[i].path);
203 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix); 207 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix);
204 EXPECT_EQ(kInsertBeforeExtension[i].result, path.ToWStringHack()); 208 EXPECT_EQ(kInsertBeforeExtension[i].result, path.value());
205 } 209 }
206 } 210 }
207 211
208 static const struct filename_case { 212 static const struct filename_case {
209 const wchar_t* path; 213 const wchar_t* path;
210 const wchar_t* filename; 214 const wchar_t* filename;
211 } filename_cases[] = { 215 } filename_cases[] = {
212 #if defined(OS_WIN) 216 #if defined(OS_WIN)
213 {L"c:\\colon\\backslash", L"backslash"}, 217 {L"c:\\colon\\backslash", L"backslash"},
214 {L"c:\\colon\\backslash\\", L""}, 218 {L"c:\\colon\\backslash\\", L""},
(...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 888
885 // Make sure ReplaceExtension doesn't replace an extension that occurs as one of 889 // Make sure ReplaceExtension doesn't replace an extension that occurs as one of
886 // the directory names of the path. 890 // the directory names of the path.
887 TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) { 891 TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) {
888 FilePath path; 892 FilePath path;
889 path = path.Append(FILE_PATH_LITERAL("foo.bar")); 893 path = path.Append(FILE_PATH_LITERAL("foo.bar"));
890 path = path.Append(FILE_PATH_LITERAL("foo")); 894 path = path.Append(FILE_PATH_LITERAL("foo"));
891 // '/foo.bar/foo' with extension '.baz' 895 // '/foo.bar/foo' with extension '.baz'
892 FilePath result_path = path; 896 FilePath result_path = path;
893 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz")); 897 file_util::ReplaceExtension(&result_path, FILE_PATH_LITERAL(".baz"));
894 EXPECT_EQ(path.ToWStringHack() + L".baz", result_path.ToWStringHack()); 898 EXPECT_EQ(path.value() + FILE_PATH_LITERAL(".baz"),
899 result_path.value());
895 } 900 }
896 901
897 TEST_F(FileUtilTest, FileEnumeratorTest) { 902 TEST_F(FileUtilTest, FileEnumeratorTest) {
898 // Test an empty directory. 903 // Test an empty directory.
899 file_util::FileEnumerator f0(test_dir_, true, 904 file_util::FileEnumerator f0(test_dir_, true,
900 file_util::FileEnumerator::FILES_AND_DIRECTORIES); 905 file_util::FileEnumerator::FILES_AND_DIRECTORIES);
901 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); 906 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
902 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL("")); 907 EXPECT_EQ(f0.Next().value(), FILE_PATH_LITERAL(""));
903 908
904 // create the directories 909 // create the directories
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
1013 std::wstring component = std::wstring(path, start, end - start); 1018 std::wstring component = std::wstring(path, start, end - start);
1014 components->push_back(component); 1019 components->push_back(component);
1015 start = end + 1; 1020 start = end + 1;
1016 end = path.find('/', start); 1021 end = path.find('/', start);
1017 } 1022 }
1018 std::wstring component = std::wstring(path, start); 1023 std::wstring component = std::wstring(path, start);
1019 components->push_back(component); 1024 components->push_back(component);
1020 } 1025 }
1021 1026
1022 static const struct PathComponentsCase { 1027 static const struct PathComponentsCase {
1023 std::wstring path; 1028 const FilePath::CharType* path;
1024 FilePath::StringType result; 1029 const FilePath::CharType* result;
1025 } kPathComponents[] = { 1030 } kPathComponents[] = {
1026 {L"/foo/bar/baz/", FILE_PATH_LITERAL("/|foo|bar|baz|")}, 1031 {FILE_PATH_LITERAL("/foo/bar/baz/"), FILE_PATH_LITERAL("/|foo|bar|baz|")},
1027 {L"/foo/bar/baz", FILE_PATH_LITERAL("/|foo|bar|baz")}, 1032 {FILE_PATH_LITERAL("/foo/bar/baz"), FILE_PATH_LITERAL("/|foo|bar|baz")},
1028 {L"e:/foo", FILE_PATH_LITERAL("e:|foo")}, 1033 {FILE_PATH_LITERAL("e:/foo"), FILE_PATH_LITERAL("e:|foo")},
1029 }; 1034 };
1030 1035
1031 TEST_F(FileUtilTest, PathComponentsTest) { 1036 TEST_F(FileUtilTest, PathComponentsTest) {
1032 for (size_t i = 0; i < arraysize(kPathComponents); ++i) { 1037 for (size_t i = 0; i < arraysize(kPathComponents); ++i) {
1033 FilePath path = FilePath::FromWStringHack(kPathComponents[i].path); 1038 FilePath path(kPathComponents[i].path);
1034 std::vector<FilePath::StringType> comps; 1039 std::vector<FilePath::StringType> comps;
1035 file_util::PathComponents(path, &comps); 1040 file_util::PathComponents(path, &comps);
1036 1041
1037 FilePath::StringType result; 1042 FilePath::StringType result;
1038 for (size_t j = 0; j < comps.size(); ++j) { 1043 for (size_t j = 0; j < comps.size(); ++j) {
1039 result.append(comps[j]); 1044 result.append(comps[j]);
1040 if (j < comps.size() - 1) 1045 if (j < comps.size() - 1)
1041 result.append(FILE_PATH_LITERAL("|"), 1); 1046 result.append(FILE_PATH_LITERAL("|"), 1);
1042 } 1047 }
1043 EXPECT_EQ(kPathComponents[i].result, result); 1048 EXPECT_EQ(kPathComponents[i].result, result);
(...skipping 11 matching lines...) Expand all
1055 1060
1056 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo"))); 1061 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo")));
1057 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt"))); 1062 FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt")));
1058 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt"))); 1063 FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt")));
1059 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); 1064 FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
1060 1065
1061 // Annoyingly, the directories must actually exist in order for realpath(), 1066 // Annoyingly, the directories must actually exist in order for realpath(),
1062 // which Contains() relies on in posix, to work. 1067 // which Contains() relies on in posix, to work.
1063 ASSERT_TRUE(file_util::CreateDirectory(foo)); 1068 ASSERT_TRUE(file_util::CreateDirectory(foo));
1064 std::string data("hello"); 1069 std::string data("hello");
1065 ASSERT_TRUE(file_util::WriteFile(bar.ToWStringHack(), data.c_str(), 1070 ASSERT_TRUE(file_util::WriteFile(bar, data.c_str(), data.length()));
1066 data.length())); 1071 ASSERT_TRUE(file_util::WriteFile(baz, data.c_str(), data.length()));
1067 ASSERT_TRUE(file_util::WriteFile(baz.ToWStringHack(), data.c_str(), 1072 ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
1068 data.length()));
1069 ASSERT_TRUE(file_util::WriteFile(foobar.ToWStringHack(), data.c_str(),
1070 data.length()));
1071 1073
1072 EXPECT_TRUE(file_util::ContainsPath(foo, bar)); 1074 EXPECT_TRUE(file_util::ContainsPath(foo, bar));
1073 EXPECT_FALSE(file_util::ContainsPath(foo, baz)); 1075 EXPECT_FALSE(file_util::ContainsPath(foo, baz));
1074 EXPECT_FALSE(file_util::ContainsPath(foo, foobar)); 1076 EXPECT_FALSE(file_util::ContainsPath(foo, foobar));
1075 EXPECT_FALSE(file_util::ContainsPath(foo, foo)); 1077 EXPECT_FALSE(file_util::ContainsPath(foo, foo));
1076 1078
1077 // Platform-specific concerns 1079 // Platform-specific concerns
1078 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO"))); 1080 FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO")));
1079 #if defined(OS_WIN) 1081 #if defined(OS_WIN)
1080 EXPECT_TRUE(file_util::ContainsPath(foo, 1082 EXPECT_TRUE(file_util::ContainsPath(foo,
1081 foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); 1083 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
1082 EXPECT_TRUE(file_util::ContainsPath(foo, 1084 EXPECT_TRUE(file_util::ContainsPath(foo,
1083 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt")))); 1085 FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt"))));
1084 #elif defined(OS_LINUX) 1086 #elif defined(OS_LINUX)
1085 EXPECT_FALSE(file_util::ContainsPath(foo, 1087 EXPECT_FALSE(file_util::ContainsPath(foo,
1086 foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); 1088 foo_caps.Append(FILE_PATH_LITERAL("bar.txt"))));
1087 #else 1089 #else
1088 // We can't really do this test on osx since the case-sensitivity of the 1090 // We can't really do this test on osx since the case-sensitivity of the
1089 // filesystem is configurable. 1091 // filesystem is configurable.
1090 #endif 1092 #endif
1091 } 1093 }
1092 1094
1093 } // namespace 1095 } // namespace
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698