Unify expand and collapse icons.
[chromium-blink-merge.git] / base / command_line_unittest.cc
blobe395c85677ab3f36844e9572b535e8f4d77adc90
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
3 // found in the LICENSE file.
5 #include <string>
6 #include <vector>
8 #include "base/basictypes.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace base {
16 // To test Windows quoting behavior, we use a string that has some backslashes
17 // and quotes.
18 // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\"
19 // Here it is with C-style escapes.
20 static const CommandLine::StringType kTrickyQuoted =
21 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\"");
22 // It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
23 // Here that is with C-style escapes.
24 static const CommandLine::StringType kTricky =
25 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\"");
27 TEST(CommandLineTest, CommandLineConstructor) {
28 const CommandLine::CharType* argv[] = {
29 FILE_PATH_LITERAL("program"),
30 FILE_PATH_LITERAL("--foo="),
31 FILE_PATH_LITERAL("-bAr"),
32 FILE_PATH_LITERAL("-spaetzel=pierogi"),
33 FILE_PATH_LITERAL("-baz"),
34 FILE_PATH_LITERAL("flim"),
35 FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"),
36 FILE_PATH_LITERAL("-spaetzle=Crepe"),
37 FILE_PATH_LITERAL("-=loosevalue"),
38 FILE_PATH_LITERAL("-"),
39 FILE_PATH_LITERAL("FLAN"),
40 FILE_PATH_LITERAL("a"),
41 FILE_PATH_LITERAL("--input-translation=45--output-rotation"),
42 FILE_PATH_LITERAL("--"),
43 FILE_PATH_LITERAL("--"),
44 FILE_PATH_LITERAL("--not-a-switch"),
45 FILE_PATH_LITERAL("\"in the time of submarines...\""),
46 FILE_PATH_LITERAL("unquoted arg-with-space")};
47 CommandLine cl(arraysize(argv), argv);
49 EXPECT_FALSE(cl.GetCommandLineString().empty());
50 EXPECT_FALSE(cl.HasSwitch("cruller"));
51 EXPECT_FALSE(cl.HasSwitch("flim"));
52 EXPECT_FALSE(cl.HasSwitch("program"));
53 EXPECT_FALSE(cl.HasSwitch("dog"));
54 EXPECT_FALSE(cl.HasSwitch("cat"));
55 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
56 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
57 EXPECT_FALSE(cl.HasSwitch("--"));
59 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
60 cl.GetProgram().value());
62 EXPECT_TRUE(cl.HasSwitch("foo"));
63 EXPECT_TRUE(cl.HasSwitch("bAr"));
64 EXPECT_TRUE(cl.HasSwitch("baz"));
65 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
66 #if defined(OS_WIN)
67 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
68 #endif
69 EXPECT_TRUE(cl.HasSwitch("other-switches"));
70 EXPECT_TRUE(cl.HasSwitch("input-translation"));
72 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
73 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo"));
74 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
75 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
76 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
77 "other-switches"));
78 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
80 const CommandLine::StringVector& args = cl.GetArgs();
81 ASSERT_EQ(8U, args.size());
83 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
84 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
85 ++iter;
86 EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter);
87 ++iter;
88 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
89 ++iter;
90 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter);
91 ++iter;
92 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
93 ++iter;
94 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
95 ++iter;
96 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter);
97 ++iter;
98 EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter);
99 ++iter;
100 EXPECT_TRUE(iter == args.end());
103 TEST(CommandLineTest, CommandLineFromString) {
104 #if defined(OS_WIN)
105 CommandLine cl = CommandLine::FromString(
106 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
107 L"--other-switches=\"--dog=canine --cat=feline\" "
108 L"-spaetzle=Crepe -=loosevalue FLAN "
109 L"--input-translation=\"45\"--output-rotation "
110 L"--quotes=" + kTrickyQuoted + L" "
111 L"-- -- --not-a-switch "
112 L"\"in the time of submarines...\"");
114 EXPECT_FALSE(cl.GetCommandLineString().empty());
115 EXPECT_FALSE(cl.HasSwitch("cruller"));
116 EXPECT_FALSE(cl.HasSwitch("flim"));
117 EXPECT_FALSE(cl.HasSwitch("program"));
118 EXPECT_FALSE(cl.HasSwitch("dog"));
119 EXPECT_FALSE(cl.HasSwitch("cat"));
120 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
121 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
122 EXPECT_FALSE(cl.HasSwitch("--"));
124 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
125 cl.GetProgram().value());
127 EXPECT_TRUE(cl.HasSwitch("foo"));
128 EXPECT_TRUE(cl.HasSwitch("bar"));
129 EXPECT_TRUE(cl.HasSwitch("baz"));
130 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
131 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
132 EXPECT_TRUE(cl.HasSwitch("other-switches"));
133 EXPECT_TRUE(cl.HasSwitch("input-translation"));
134 EXPECT_TRUE(cl.HasSwitch("quotes"));
136 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
137 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo"));
138 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
139 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
140 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
141 "other-switches"));
142 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
143 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes"));
145 const CommandLine::StringVector& args = cl.GetArgs();
146 ASSERT_EQ(5U, args.size());
148 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
149 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
150 ++iter;
151 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
152 ++iter;
153 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
154 ++iter;
155 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
156 ++iter;
157 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
158 ++iter;
159 EXPECT_TRUE(iter == args.end());
161 // Check that a generated string produces an equivalent command line.
162 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString());
163 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString());
164 #endif
167 // Tests behavior with an empty input string.
168 TEST(CommandLineTest, EmptyString) {
169 #if defined(OS_WIN)
170 CommandLine cl_from_string = CommandLine::FromString(L"");
171 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
172 EXPECT_TRUE(cl_from_string.GetProgram().empty());
173 EXPECT_EQ(1U, cl_from_string.argv().size());
174 EXPECT_TRUE(cl_from_string.GetArgs().empty());
175 #endif
176 CommandLine cl_from_argv(0, NULL);
177 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty());
178 EXPECT_TRUE(cl_from_argv.GetProgram().empty());
179 EXPECT_EQ(1U, cl_from_argv.argv().size());
180 EXPECT_TRUE(cl_from_argv.GetArgs().empty());
183 TEST(CommandLineTest, GetArgumentsString) {
184 static const FilePath::CharType kPath1[] =
185 FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg");
186 static const FilePath::CharType kPath2[] =
187 FILE_PATH_LITERAL("C:\\no\\spaces.ggg");
189 static const char kFirstArgName[] = "first-arg";
190 static const char kSecondArgName[] = "arg2";
191 static const char kThirdArgName[] = "arg with space";
192 static const char kFourthArgName[] = "nospace";
193 static const char kFifthArgName[] = "%1";
195 CommandLine cl(CommandLine::NO_PROGRAM);
196 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1));
197 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2));
198 cl.AppendArg(kThirdArgName);
199 cl.AppendArg(kFourthArgName);
200 cl.AppendArg(kFifthArgName);
202 #if defined(OS_WIN)
203 CommandLine::StringType expected_first_arg(UTF8ToUTF16(kFirstArgName));
204 CommandLine::StringType expected_second_arg(UTF8ToUTF16(kSecondArgName));
205 CommandLine::StringType expected_third_arg(UTF8ToUTF16(kThirdArgName));
206 CommandLine::StringType expected_fourth_arg(UTF8ToUTF16(kFourthArgName));
207 CommandLine::StringType expected_fifth_arg(UTF8ToUTF16(kFifthArgName));
208 #elif defined(OS_POSIX)
209 CommandLine::StringType expected_first_arg(kFirstArgName);
210 CommandLine::StringType expected_second_arg(kSecondArgName);
211 CommandLine::StringType expected_third_arg(kThirdArgName);
212 CommandLine::StringType expected_fourth_arg(kFourthArgName);
213 CommandLine::StringType expected_fifth_arg(kFifthArgName);
214 #endif
216 #if defined(OS_WIN)
217 #define QUOTE_ON_WIN FILE_PATH_LITERAL("\"")
218 #else
219 #define QUOTE_ON_WIN FILE_PATH_LITERAL("")
220 #endif // OS_WIN
222 CommandLine::StringType expected_str;
223 expected_str.append(FILE_PATH_LITERAL("--"))
224 .append(expected_first_arg)
225 .append(FILE_PATH_LITERAL("="))
226 .append(QUOTE_ON_WIN)
227 .append(kPath1)
228 .append(QUOTE_ON_WIN)
229 .append(FILE_PATH_LITERAL(" "))
230 .append(FILE_PATH_LITERAL("--"))
231 .append(expected_second_arg)
232 .append(FILE_PATH_LITERAL("="))
233 .append(QUOTE_ON_WIN)
234 .append(kPath2)
235 .append(QUOTE_ON_WIN)
236 .append(FILE_PATH_LITERAL(" "))
237 .append(QUOTE_ON_WIN)
238 .append(expected_third_arg)
239 .append(QUOTE_ON_WIN)
240 .append(FILE_PATH_LITERAL(" "))
241 .append(expected_fourth_arg)
242 .append(FILE_PATH_LITERAL(" "));
244 CommandLine::StringType expected_str_no_quote_placeholders(expected_str);
245 expected_str_no_quote_placeholders.append(expected_fifth_arg);
246 EXPECT_EQ(expected_str_no_quote_placeholders, cl.GetArgumentsString());
248 #if defined(OS_WIN)
249 CommandLine::StringType expected_str_quote_placeholders(expected_str);
250 expected_str_quote_placeholders.append(QUOTE_ON_WIN)
251 .append(expected_fifth_arg)
252 .append(QUOTE_ON_WIN);
253 EXPECT_EQ(expected_str_quote_placeholders,
254 cl.GetArgumentsStringWithPlaceholders());
255 #endif
258 // Test methods for appending switches to a command line.
259 TEST(CommandLineTest, AppendSwitches) {
260 std::string switch1 = "switch1";
261 std::string switch2 = "switch2";
262 std::string value2 = "value";
263 std::string switch3 = "switch3";
264 std::string value3 = "a value with spaces";
265 std::string switch4 = "switch4";
266 std::string value4 = "\"a value with quotes\"";
267 std::string switch5 = "quotes";
268 CommandLine::StringType value5 = kTricky;
270 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
272 cl.AppendSwitch(switch1);
273 cl.AppendSwitchASCII(switch2, value2);
274 cl.AppendSwitchASCII(switch3, value3);
275 cl.AppendSwitchASCII(switch4, value4);
276 cl.AppendSwitchNative(switch5, value5);
278 EXPECT_TRUE(cl.HasSwitch(switch1));
279 EXPECT_TRUE(cl.HasSwitch(switch2));
280 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
281 EXPECT_TRUE(cl.HasSwitch(switch3));
282 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
283 EXPECT_TRUE(cl.HasSwitch(switch4));
284 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
285 EXPECT_TRUE(cl.HasSwitch(switch5));
286 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
288 #if defined(OS_WIN)
289 EXPECT_EQ(L"Program "
290 L"--switch1 "
291 L"--switch2=value "
292 L"--switch3=\"a value with spaces\" "
293 L"--switch4=\"\\\"a value with quotes\\\"\" "
294 L"--quotes=\"" + kTrickyQuoted + L"\"",
295 cl.GetCommandLineString());
296 #endif
299 TEST(CommandLineTest, AppendSwitchesDashDash) {
300 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"),
301 FILE_PATH_LITERAL("--"),
302 FILE_PATH_LITERAL("--arg1") };
303 CommandLine cl(arraysize(raw_argv), raw_argv);
305 cl.AppendSwitch("switch1");
306 cl.AppendSwitchASCII("switch2", "foo");
308 cl.AppendArg("--arg2");
310 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"),
311 cl.GetCommandLineString());
312 CommandLine::StringVector cl_argv = cl.argv();
313 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]);
314 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]);
315 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]);
316 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]);
317 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]);
318 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]);
321 // Tests that when AppendArguments is called that the program is set correctly
322 // on the target CommandLine object and the switches from the source
323 // CommandLine are added to the target.
324 TEST(CommandLineTest, AppendArguments) {
325 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program")));
326 cl1.AppendSwitch("switch1");
327 cl1.AppendSwitchASCII("switch2", "foo");
329 CommandLine cl2(CommandLine::NO_PROGRAM);
330 cl2.AppendArguments(cl1, true);
331 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value());
332 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString());
334 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1")));
335 c1.AppendSwitch("switch1");
336 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2")));
337 c2.AppendSwitch("switch2");
339 c1.AppendArguments(c2, true);
340 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value());
341 EXPECT_TRUE(c1.HasSwitch("switch1"));
342 EXPECT_TRUE(c1.HasSwitch("switch2"));
345 #if defined(OS_WIN)
346 // Make sure that the command line string program paths are quoted as necessary.
347 // This only makes sense on Windows and the test is basically here to guard
348 // against regressions.
349 TEST(CommandLineTest, ProgramQuotes) {
350 // Check that quotes are not added for paths without spaces.
351 const FilePath kProgram(L"Program");
352 CommandLine cl_program(kProgram);
353 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
354 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
356 const FilePath kProgramPath(L"Program Path");
358 // Check that quotes are not returned from GetProgram().
359 CommandLine cl_program_path(kProgramPath);
360 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
362 // Check that quotes are added to command line string paths containing spaces.
363 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
364 EXPECT_EQ(L"\"Program Path\"", cmd_string);
366 // Check the optional quoting of placeholders in programs.
367 CommandLine cl_quote_placeholder(FilePath(L"%1"));
368 EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString());
369 EXPECT_EQ(L"\"%1\"",
370 cl_quote_placeholder.GetCommandLineStringWithPlaceholders());
372 #endif
374 // Calling Init multiple times should not modify the previous CommandLine.
375 TEST(CommandLineTest, Init) {
376 CommandLine* initial = CommandLine::ForCurrentProcess();
377 EXPECT_FALSE(CommandLine::Init(0, NULL));
378 CommandLine* current = CommandLine::ForCurrentProcess();
379 EXPECT_EQ(initial, current);
382 } // namespace base