OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 FILE_PATH_LITERAL("-=loosevalue"), | 35 FILE_PATH_LITERAL("-=loosevalue"), |
36 FILE_PATH_LITERAL("FLAN"), | 36 FILE_PATH_LITERAL("FLAN"), |
37 FILE_PATH_LITERAL("--input-translation=45--output-rotation"), | 37 FILE_PATH_LITERAL("--input-translation=45--output-rotation"), |
38 FILE_PATH_LITERAL("--"), | 38 FILE_PATH_LITERAL("--"), |
39 FILE_PATH_LITERAL("--"), | 39 FILE_PATH_LITERAL("--"), |
40 FILE_PATH_LITERAL("--not-a-switch"), | 40 FILE_PATH_LITERAL("--not-a-switch"), |
41 FILE_PATH_LITERAL("\"in the time of submarines...\""), | 41 FILE_PATH_LITERAL("\"in the time of submarines...\""), |
42 FILE_PATH_LITERAL("unquoted arg-with-space")}; | 42 FILE_PATH_LITERAL("unquoted arg-with-space")}; |
43 CommandLine cl(arraysize(argv), argv); | 43 CommandLine cl(arraysize(argv), argv); |
44 | 44 |
45 EXPECT_FALSE(cl.command_line_string().empty()); | 45 EXPECT_FALSE(cl.GetCommandLineString().empty()); |
46 EXPECT_FALSE(cl.HasSwitch("cruller")); | 46 EXPECT_FALSE(cl.HasSwitch("cruller")); |
47 EXPECT_FALSE(cl.HasSwitch("flim")); | 47 EXPECT_FALSE(cl.HasSwitch("flim")); |
48 EXPECT_FALSE(cl.HasSwitch("program")); | 48 EXPECT_FALSE(cl.HasSwitch("program")); |
49 EXPECT_FALSE(cl.HasSwitch("dog")); | 49 EXPECT_FALSE(cl.HasSwitch("dog")); |
50 EXPECT_FALSE(cl.HasSwitch("cat")); | 50 EXPECT_FALSE(cl.HasSwitch("cat")); |
51 EXPECT_FALSE(cl.HasSwitch("output-rotation")); | 51 EXPECT_FALSE(cl.HasSwitch("output-rotation")); |
52 EXPECT_FALSE(cl.HasSwitch("not-a-switch")); | 52 EXPECT_FALSE(cl.HasSwitch("not-a-switch")); |
53 EXPECT_FALSE(cl.HasSwitch("--")); | 53 EXPECT_FALSE(cl.HasSwitch("--")); |
54 | 54 |
55 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), | 55 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 #if defined(OS_WIN) | 96 #if defined(OS_WIN) |
97 CommandLine cl = CommandLine::FromString( | 97 CommandLine cl = CommandLine::FromString( |
98 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " | 98 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
99 L"--other-switches=\"--dog=canine --cat=feline\" " | 99 L"--other-switches=\"--dog=canine --cat=feline\" " |
100 L"-spaetzle=Crepe -=loosevalue FLAN " | 100 L"-spaetzle=Crepe -=loosevalue FLAN " |
101 L"--input-translation=\"45\"--output-rotation " | 101 L"--input-translation=\"45\"--output-rotation " |
102 L"--quotes=" + kTrickyQuoted + L" " | 102 L"--quotes=" + kTrickyQuoted + L" " |
103 L"-- -- --not-a-switch " | 103 L"-- -- --not-a-switch " |
104 L"\"in the time of submarines...\""); | 104 L"\"in the time of submarines...\""); |
105 | 105 |
106 EXPECT_FALSE(cl.command_line_string().empty()); | 106 EXPECT_FALSE(cl.GetCommandLineString().empty()); |
107 EXPECT_FALSE(cl.HasSwitch("cruller")); | 107 EXPECT_FALSE(cl.HasSwitch("cruller")); |
108 EXPECT_FALSE(cl.HasSwitch("flim")); | 108 EXPECT_FALSE(cl.HasSwitch("flim")); |
109 EXPECT_FALSE(cl.HasSwitch("program")); | 109 EXPECT_FALSE(cl.HasSwitch("program")); |
110 EXPECT_FALSE(cl.HasSwitch("dog")); | 110 EXPECT_FALSE(cl.HasSwitch("dog")); |
111 EXPECT_FALSE(cl.HasSwitch("cat")); | 111 EXPECT_FALSE(cl.HasSwitch("cat")); |
112 EXPECT_FALSE(cl.HasSwitch("output-rotation")); | 112 EXPECT_FALSE(cl.HasSwitch("output-rotation")); |
113 EXPECT_FALSE(cl.HasSwitch("not-a-switch")); | 113 EXPECT_FALSE(cl.HasSwitch("not-a-switch")); |
114 EXPECT_FALSE(cl.HasSwitch("--")); | 114 EXPECT_FALSE(cl.HasSwitch("--")); |
115 | 115 |
116 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), | 116 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), |
(...skipping 27 matching lines...) Expand all Loading... |
144 ++iter; | 144 ++iter; |
145 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); | 145 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); |
146 ++iter; | 146 ++iter; |
147 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); | 147 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); |
148 ++iter; | 148 ++iter; |
149 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter); | 149 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter); |
150 ++iter; | 150 ++iter; |
151 EXPECT_TRUE(iter == args.end()); | 151 EXPECT_TRUE(iter == args.end()); |
152 | 152 |
153 // Check that a generated string produces an equivalent command line. | 153 // Check that a generated string produces an equivalent command line. |
154 CommandLine cl_duplicate = CommandLine::FromString(cl.command_line_string()); | 154 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString()); |
155 EXPECT_EQ(cl.command_line_string(), cl_duplicate.command_line_string()); | 155 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString()); |
156 #endif | 156 #endif |
157 } | 157 } |
158 | 158 |
159 // Tests behavior with an empty input string. | 159 // Tests behavior with an empty input string. |
160 TEST(CommandLineTest, EmptyString) { | 160 TEST(CommandLineTest, EmptyString) { |
161 #if defined(OS_WIN) | 161 #if defined(OS_WIN) |
162 CommandLine cl_from_string = CommandLine::FromString(L""); | 162 CommandLine cl_from_string = CommandLine::FromString(L""); |
163 EXPECT_TRUE(cl_from_string.command_line_string().empty()); | 163 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty()); |
164 EXPECT_TRUE(cl_from_string.GetProgram().empty()); | 164 EXPECT_TRUE(cl_from_string.GetProgram().empty()); |
165 EXPECT_EQ(1U, cl_from_string.argv().size()); | 165 EXPECT_EQ(1U, cl_from_string.argv().size()); |
166 EXPECT_TRUE(cl_from_string.GetArgs().empty()); | 166 EXPECT_TRUE(cl_from_string.GetArgs().empty()); |
167 #endif | 167 #endif |
168 CommandLine cl_from_argv(0, NULL); | 168 CommandLine cl_from_argv(0, NULL); |
169 EXPECT_TRUE(cl_from_argv.command_line_string().empty()); | 169 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty()); |
170 EXPECT_TRUE(cl_from_argv.GetProgram().empty()); | 170 EXPECT_TRUE(cl_from_argv.GetProgram().empty()); |
171 EXPECT_EQ(1U, cl_from_argv.argv().size()); | 171 EXPECT_EQ(1U, cl_from_argv.argv().size()); |
172 EXPECT_TRUE(cl_from_argv.GetArgs().empty()); | 172 EXPECT_TRUE(cl_from_argv.GetArgs().empty()); |
173 } | 173 } |
174 | 174 |
175 // Test methods for appending switches to a command line. | 175 // Test methods for appending switches to a command line. |
176 TEST(CommandLineTest, AppendSwitches) { | 176 TEST(CommandLineTest, AppendSwitches) { |
177 std::string switch1 = "switch1"; | 177 std::string switch1 = "switch1"; |
178 std::string switch2 = "switch2"; | 178 std::string switch2 = "switch2"; |
179 std::string value2 = "value"; | 179 std::string value2 = "value"; |
(...skipping 22 matching lines...) Expand all Loading... |
202 EXPECT_TRUE(cl.HasSwitch(switch5)); | 202 EXPECT_TRUE(cl.HasSwitch(switch5)); |
203 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5)); | 203 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5)); |
204 | 204 |
205 #if defined(OS_WIN) | 205 #if defined(OS_WIN) |
206 EXPECT_EQ(L"Program " | 206 EXPECT_EQ(L"Program " |
207 L"--switch1 " | 207 L"--switch1 " |
208 L"--switch2=value " | 208 L"--switch2=value " |
209 L"--switch3=\"a value with spaces\" " | 209 L"--switch3=\"a value with spaces\" " |
210 L"--switch4=\"\\\"a value with quotes\\\"\" " | 210 L"--switch4=\"\\\"a value with quotes\\\"\" " |
211 L"--quotes=\"" + kTrickyQuoted + L"\"", | 211 L"--quotes=\"" + kTrickyQuoted + L"\"", |
212 cl.command_line_string()); | 212 cl.GetCommandLineString()); |
213 #endif | 213 #endif |
214 } | 214 } |
215 | 215 |
216 TEST(CommandLineTest, AppendSwitchesDashDash) { | 216 TEST(CommandLineTest, AppendSwitchesDashDash) { |
217 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"), | 217 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"), |
218 FILE_PATH_LITERAL("--"), | 218 FILE_PATH_LITERAL("--"), |
219 FILE_PATH_LITERAL("--arg1") }; | 219 FILE_PATH_LITERAL("--arg1") }; |
220 CommandLine cl(arraysize(raw_argv), raw_argv); | 220 CommandLine cl(arraysize(raw_argv), raw_argv); |
221 | 221 |
222 cl.AppendSwitch("switch1"); | 222 cl.AppendSwitch("switch1"); |
223 cl.AppendSwitchASCII("switch2", "foo"); | 223 cl.AppendSwitchASCII("switch2", "foo"); |
224 | 224 |
225 cl.AppendArg("--arg2"); | 225 cl.AppendArg("--arg2"); |
226 | 226 |
227 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"), | 227 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"), |
228 cl.command_line_string()); | 228 cl.GetCommandLineString()); |
229 CommandLine::StringVector cl_argv = cl.argv(); | 229 CommandLine::StringVector cl_argv = cl.argv(); |
230 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]); | 230 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]); |
231 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]); | 231 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]); |
232 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]); | 232 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]); |
233 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]); | 233 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]); |
234 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]); | 234 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]); |
235 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]); | 235 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]); |
236 } | 236 } |
237 | 237 |
238 // Tests that when AppendArguments is called that the program is set correctly | 238 // Tests that when AppendArguments is called that the program is set correctly |
239 // on the target CommandLine object and the switches from the source | 239 // on the target CommandLine object and the switches from the source |
240 // CommandLine are added to the target. | 240 // CommandLine are added to the target. |
241 TEST(CommandLineTest, AppendArguments) { | 241 TEST(CommandLineTest, AppendArguments) { |
242 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program"))); | 242 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program"))); |
243 cl1.AppendSwitch("switch1"); | 243 cl1.AppendSwitch("switch1"); |
244 cl1.AppendSwitchASCII("switch2", "foo"); | 244 cl1.AppendSwitchASCII("switch2", "foo"); |
245 | 245 |
246 CommandLine cl2(CommandLine::NO_PROGRAM); | 246 CommandLine cl2(CommandLine::NO_PROGRAM); |
247 cl2.AppendArguments(cl1, true); | 247 cl2.AppendArguments(cl1, true); |
248 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value()); | 248 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value()); |
249 EXPECT_EQ(cl1.command_line_string(), cl2.command_line_string()); | 249 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString()); |
250 | 250 |
251 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1"))); | 251 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1"))); |
252 c1.AppendSwitch("switch1"); | 252 c1.AppendSwitch("switch1"); |
253 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2"))); | 253 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2"))); |
254 c2.AppendSwitch("switch2"); | 254 c2.AppendSwitch("switch2"); |
255 | 255 |
256 c1.AppendArguments(c2, true); | 256 c1.AppendArguments(c2, true); |
257 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value()); | 257 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value()); |
258 EXPECT_TRUE(c1.HasSwitch("switch1")); | 258 EXPECT_TRUE(c1.HasSwitch("switch1")); |
259 EXPECT_TRUE(c1.HasSwitch("switch2")); | 259 EXPECT_TRUE(c1.HasSwitch("switch2")); |
260 } | 260 } |
261 | 261 |
262 #if defined(OS_WIN) | 262 #if defined(OS_WIN) |
263 // Make sure that program paths of command_line_string are quoted as necessary. | 263 // Make sure that the command line string program paths are quoted as necessary. |
264 // This only makes sense on Windows and the test is basically here to guard | 264 // This only makes sense on Windows and the test is basically here to guard |
265 // against regressions. | 265 // against regressions. |
266 TEST(CommandLineTest, ProgramQuotes) { | 266 TEST(CommandLineTest, ProgramQuotes) { |
267 // Check that quotes are not added for paths without spaces. | 267 // Check that quotes are not added for paths without spaces. |
268 const FilePath kProgram(L"Program"); | 268 const FilePath kProgram(L"Program"); |
269 CommandLine cl_program(kProgram); | 269 CommandLine cl_program(kProgram); |
270 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value()); | 270 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value()); |
271 EXPECT_EQ(kProgram.value(), cl_program.command_line_string()); | 271 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString()); |
272 | 272 |
273 const FilePath kProgramPath(L"Program Path"); | 273 const FilePath kProgramPath(L"Program Path"); |
274 | 274 |
275 // Check that quotes are not returned from GetProgram(). | 275 // Check that quotes are not returned from GetProgram(). |
276 CommandLine cl_program_path(kProgramPath); | 276 CommandLine cl_program_path(kProgramPath); |
277 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value()); | 277 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value()); |
278 | 278 |
279 // Check that quotes are added to command line string paths containing spaces. | 279 // Check that quotes are added to command line string paths containing spaces. |
280 CommandLine::StringType cmd_string(cl_program_path.command_line_string()); | 280 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString()); |
281 CommandLine::StringType program_string(cl_program_path.GetProgram().value()); | 281 CommandLine::StringType program_string(cl_program_path.GetProgram().value()); |
282 EXPECT_EQ('"', cmd_string[0]); | 282 EXPECT_EQ('"', cmd_string[0]); |
283 EXPECT_EQ(program_string, cmd_string.substr(1, program_string.length())); | 283 EXPECT_EQ(program_string, cmd_string.substr(1, program_string.length())); |
284 EXPECT_EQ('"', cmd_string[program_string.length() + 1]); | 284 EXPECT_EQ('"', cmd_string[program_string.length() + 1]); |
285 } | 285 } |
286 #endif | 286 #endif |
287 | 287 |
288 // Calling Init multiple times should not modify the previous CommandLine. | 288 // Calling Init multiple times should not modify the previous CommandLine. |
289 TEST(CommandLineTest, Init) { | 289 TEST(CommandLineTest, Init) { |
290 CommandLine* initial = CommandLine::ForCurrentProcess(); | 290 CommandLine* initial = CommandLine::ForCurrentProcess(); |
291 CommandLine::Init(0, NULL); | 291 CommandLine::Init(0, NULL); |
292 CommandLine* current = CommandLine::ForCurrentProcess(); | 292 CommandLine* current = CommandLine::ForCurrentProcess(); |
293 EXPECT_EQ(initial, current); | 293 EXPECT_EQ(initial, current); |
294 } | 294 } |
OLD | NEW |