blob: 8fdd605db79249f31179fababa00c706f9cbb263 [file] [log] [blame]
[email protected]72e2e2422012-02-27 18:38:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
5#include <string>
6#include <vector>
7
[email protected]9d84e1a2010-11-15 00:18:308#include "base/basictypes.h"
[email protected]e6124ad52010-11-15 04:17:529#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
[email protected]a4ea1f12013-06-07 18:37:0711#include "base/strings/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:3812#include "testing/gtest/include/gtest/gtest.h"
13
[email protected]023ad6ab2013-02-17 05:07:2314using base::FilePath;
15
[email protected]98a1c2682010-08-10 18:14:1916// 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.
[email protected]a40ca4302011-05-14 01:10:2420static const CommandLine::StringType kTrickyQuoted =
21 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\"");
[email protected]98a1c2682010-08-10 18:14:1922// It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
23// Here that is with C-style escapes.
[email protected]a40ca4302011-05-14 01:10:2424static const CommandLine::StringType kTricky =
25 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\"");
[email protected]98a1c2682010-08-10 18:14:1926
initial.commitd7cae122008-07-26 21:49:3827TEST(CommandLineTest, CommandLineConstructor) {
[email protected]a40ca4302011-05-14 01:10:2428 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"),
[email protected]21e342f2012-10-19 06:19:5938 FILE_PATH_LITERAL("-"),
[email protected]a40ca4302011-05-14 01:10:2439 FILE_PATH_LITERAL("FLAN"),
[email protected]1fa39f02011-09-13 15:45:3440 FILE_PATH_LITERAL("a"),
[email protected]a40ca4302011-05-14 01:10:2441 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);
48
[email protected]61a4c6f2011-07-20 04:54:5249 EXPECT_FALSE(cl.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:2450 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("--"));
58
59 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
60 cl.GetProgram().value());
61
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"));
71
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"));
79
[email protected]75f1c782011-07-13 23:41:2280 const CommandLine::StringVector& args = cl.GetArgs();
[email protected]21e342f2012-10-19 06:19:5981 ASSERT_EQ(8U, args.size());
[email protected]a40ca4302011-05-14 01:10:2482
83 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
84 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
85 ++iter;
[email protected]21e342f2012-10-19 06:19:5986 EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter);
87 ++iter;
[email protected]a40ca4302011-05-14 01:10:2488 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
89 ++iter;
[email protected]1fa39f02011-09-13 15:45:3490 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter);
91 ++iter;
[email protected]a40ca4302011-05-14 01:10:2492 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());
101}
102
103TEST(CommandLineTest, CommandLineFromString) {
[email protected]bb975362009-01-21 01:00:22104#if defined(OS_WIN)
[email protected]51343d5a2009-10-26 22:39:33105 CommandLine cl = CommandLine::FromString(
[email protected]a40ca4302011-05-14 01:10:24106 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...\"");
113
[email protected]61a4c6f2011-07-20 04:54:52114 EXPECT_FALSE(cl.GetCommandLineString().empty());
[email protected]b7e0a2a2009-10-13 02:07:25115 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("--"));
initial.commitd7cae122008-07-26 21:49:38123
[email protected]78c4c422010-10-08 00:06:31124 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
125 cl.GetProgram().value());
initial.commitd7cae122008-07-26 21:49:38126
[email protected]b7e0a2a2009-10-13 02:07:25127 EXPECT_TRUE(cl.HasSwitch("foo"));
128 EXPECT_TRUE(cl.HasSwitch("bar"));
129 EXPECT_TRUE(cl.HasSwitch("baz"));
130 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
[email protected]b7e0a2a2009-10-13 02:07:25131 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
[email protected]b7e0a2a2009-10-13 02:07:25132 EXPECT_TRUE(cl.HasSwitch("other-switches"));
133 EXPECT_TRUE(cl.HasSwitch("input-translation"));
[email protected]98a1c2682010-08-10 18:14:19134 EXPECT_TRUE(cl.HasSwitch("quotes"));
initial.commitd7cae122008-07-26 21:49:38135
[email protected]c4e52f0d2009-11-06 19:55:16136 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"));
[email protected]a40ca4302011-05-14 01:10:24143 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes"));
initial.commitd7cae122008-07-26 21:49:38144
[email protected]75f1c782011-07-13 23:41:22145 const CommandLine::StringVector& args = cl.GetArgs();
[email protected]2e4c50c2010-07-21 15:57:23146 ASSERT_EQ(5U, args.size());
initial.commitd7cae122008-07-26 21:49:38147
[email protected]2e4c50c2010-07-21 15:57:23148 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
149 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
initial.commitd7cae122008-07-26 21:49:38150 ++iter;
[email protected]a40ca4302011-05-14 01:10:24151 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
initial.commitd7cae122008-07-26 21:49:38152 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23153 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
[email protected]02c87962008-10-06 10:25:35154 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23155 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
[email protected]02c87962008-10-06 10:25:35156 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23157 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
initial.commitd7cae122008-07-26 21:49:38158 ++iter;
[email protected]2e4c50c2010-07-21 15:57:23159 EXPECT_TRUE(iter == args.end());
[email protected]10e42bf2008-10-15 21:59:08160
[email protected]a40ca4302011-05-14 01:10:24161 // Check that a generated string produces an equivalent command line.
[email protected]61a4c6f2011-07-20 04:54:52162 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString());
163 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString());
[email protected]10e42bf2008-10-15 21:59:08164#endif
initial.commitd7cae122008-07-26 21:49:38165}
166
initial.commitd7cae122008-07-26 21:49:38167// Tests behavior with an empty input string.
168TEST(CommandLineTest, EmptyString) {
[email protected]f3adb5c2008-08-07 20:07:32169#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:24170 CommandLine cl_from_string = CommandLine::FromString(L"");
[email protected]61a4c6f2011-07-20 04:54:52171 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:24172 EXPECT_TRUE(cl_from_string.GetProgram().empty());
173 EXPECT_EQ(1U, cl_from_string.argv().size());
[email protected]75f1c782011-07-13 23:41:22174 EXPECT_TRUE(cl_from_string.GetArgs().empty());
[email protected]f3adb5c2008-08-07 20:07:32175#endif
[email protected]a40ca4302011-05-14 01:10:24176 CommandLine cl_from_argv(0, NULL);
[email protected]61a4c6f2011-07-20 04:54:52177 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty());
[email protected]a40ca4302011-05-14 01:10:24178 EXPECT_TRUE(cl_from_argv.GetProgram().empty());
179 EXPECT_EQ(1U, cl_from_argv.argv().size());
[email protected]75f1c782011-07-13 23:41:22180 EXPECT_TRUE(cl_from_argv.GetArgs().empty());
initial.commitd7cae122008-07-26 21:49:38181}
182
[email protected]45f982e2012-10-29 21:31:31183TEST(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");
188
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";
mgiucac974d5102014-10-01 09:24:51193 static const char kFifthArgName[] = "%1";
[email protected]45f982e2012-10-29 21:31:31194
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);
mgiucac974d5102014-10-01 09:24:51200 cl.AppendArg(kFifthArgName);
[email protected]45f982e2012-10-29 21:31:31201
202#if defined(OS_WIN)
[email protected]f729d7a2013-12-26 07:07:56203 CommandLine::StringType expected_first_arg(
204 base::UTF8ToUTF16(kFirstArgName));
205 CommandLine::StringType expected_second_arg(
206 base::UTF8ToUTF16(kSecondArgName));
207 CommandLine::StringType expected_third_arg(
208 base::UTF8ToUTF16(kThirdArgName));
209 CommandLine::StringType expected_fourth_arg(
210 base::UTF8ToUTF16(kFourthArgName));
mgiucac974d5102014-10-01 09:24:51211 CommandLine::StringType expected_fifth_arg(base::UTF8ToUTF16(kFifthArgName));
[email protected]45f982e2012-10-29 21:31:31212#elif defined(OS_POSIX)
213 CommandLine::StringType expected_first_arg(kFirstArgName);
214 CommandLine::StringType expected_second_arg(kSecondArgName);
215 CommandLine::StringType expected_third_arg(kThirdArgName);
216 CommandLine::StringType expected_fourth_arg(kFourthArgName);
mgiucac974d5102014-10-01 09:24:51217 CommandLine::StringType expected_fifth_arg(kFifthArgName);
[email protected]45f982e2012-10-29 21:31:31218#endif
219
220#if defined(OS_WIN)
221#define QUOTE_ON_WIN FILE_PATH_LITERAL("\"")
222#else
223#define QUOTE_ON_WIN FILE_PATH_LITERAL("")
224#endif // OS_WIN
225
226 CommandLine::StringType expected_str;
227 expected_str.append(FILE_PATH_LITERAL("--"))
228 .append(expected_first_arg)
229 .append(FILE_PATH_LITERAL("="))
230 .append(QUOTE_ON_WIN)
231 .append(kPath1)
232 .append(QUOTE_ON_WIN)
233 .append(FILE_PATH_LITERAL(" "))
234 .append(FILE_PATH_LITERAL("--"))
235 .append(expected_second_arg)
236 .append(FILE_PATH_LITERAL("="))
237 .append(QUOTE_ON_WIN)
238 .append(kPath2)
239 .append(QUOTE_ON_WIN)
240 .append(FILE_PATH_LITERAL(" "))
241 .append(QUOTE_ON_WIN)
242 .append(expected_third_arg)
243 .append(QUOTE_ON_WIN)
244 .append(FILE_PATH_LITERAL(" "))
mgiucac974d5102014-10-01 09:24:51245 .append(expected_fourth_arg)
246 .append(FILE_PATH_LITERAL(" "));
247
248 CommandLine::StringType expected_str_no_quote_placeholders(expected_str);
249 expected_str_no_quote_placeholders.append(expected_fifth_arg);
250 EXPECT_EQ(expected_str_no_quote_placeholders, cl.GetArgumentsString());
251
252#if defined(OS_WIN)
253 CommandLine::StringType expected_str_quote_placeholders(expected_str);
254 expected_str_quote_placeholders.append(QUOTE_ON_WIN)
255 .append(expected_fifth_arg)
256 .append(QUOTE_ON_WIN);
257 EXPECT_EQ(expected_str_quote_placeholders,
258 cl.GetArgumentsStringWithPlaceholders());
259#endif
[email protected]45f982e2012-10-29 21:31:31260}
261
[email protected]bb975362009-01-21 01:00:22262// Test methods for appending switches to a command line.
initial.commitd7cae122008-07-26 21:49:38263TEST(CommandLineTest, AppendSwitches) {
[email protected]b7e0a2a2009-10-13 02:07:25264 std::string switch1 = "switch1";
265 std::string switch2 = "switch2";
[email protected]a40ca4302011-05-14 01:10:24266 std::string value2 = "value";
[email protected]b7e0a2a2009-10-13 02:07:25267 std::string switch3 = "switch3";
[email protected]05076ba22010-07-30 05:59:57268 std::string value3 = "a value with spaces";
[email protected]b7e0a2a2009-10-13 02:07:25269 std::string switch4 = "switch4";
[email protected]05076ba22010-07-30 05:59:57270 std::string value4 = "\"a value with quotes\"";
[email protected]98a1c2682010-08-10 18:14:19271 std::string switch5 = "quotes";
[email protected]a40ca4302011-05-14 01:10:24272 CommandLine::StringType value5 = kTricky;
initial.commitd7cae122008-07-26 21:49:38273
[email protected]51343d5a2009-10-26 22:39:33274 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
initial.commitd7cae122008-07-26 21:49:38275
[email protected]bb975362009-01-21 01:00:22276 cl.AppendSwitch(switch1);
[email protected]a40ca4302011-05-14 01:10:24277 cl.AppendSwitchASCII(switch2, value2);
[email protected]05076ba22010-07-30 05:59:57278 cl.AppendSwitchASCII(switch3, value3);
279 cl.AppendSwitchASCII(switch4, value4);
[email protected]a40ca4302011-05-14 01:10:24280 cl.AppendSwitchNative(switch5, value5);
[email protected]bb975362009-01-21 01:00:22281
initial.commitd7cae122008-07-26 21:49:38282 EXPECT_TRUE(cl.HasSwitch(switch1));
283 EXPECT_TRUE(cl.HasSwitch(switch2));
[email protected]a40ca4302011-05-14 01:10:24284 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
initial.commitd7cae122008-07-26 21:49:38285 EXPECT_TRUE(cl.HasSwitch(switch3));
[email protected]05076ba22010-07-30 05:59:57286 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
[email protected]8c9510d2008-10-10 21:38:20287 EXPECT_TRUE(cl.HasSwitch(switch4));
[email protected]05076ba22010-07-30 05:59:57288 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
[email protected]98a1c2682010-08-10 18:14:19289 EXPECT_TRUE(cl.HasSwitch(switch5));
[email protected]a40ca4302011-05-14 01:10:24290 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
[email protected]98a1c2682010-08-10 18:14:19291
292#if defined(OS_WIN)
[email protected]a40ca4302011-05-14 01:10:24293 EXPECT_EQ(L"Program "
[email protected]98a1c2682010-08-10 18:14:19294 L"--switch1 "
295 L"--switch2=value "
296 L"--switch3=\"a value with spaces\" "
297 L"--switch4=\"\\\"a value with quotes\\\"\" "
[email protected]a40ca4302011-05-14 01:10:24298 L"--quotes=\"" + kTrickyQuoted + L"\"",
[email protected]61a4c6f2011-07-20 04:54:52299 cl.GetCommandLineString());
[email protected]98a1c2682010-08-10 18:14:19300#endif
initial.commitd7cae122008-07-26 21:49:38301}
[email protected]e6124ad52010-11-15 04:17:52302
[email protected]a40ca4302011-05-14 01:10:24303TEST(CommandLineTest, AppendSwitchesDashDash) {
304 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"),
305 FILE_PATH_LITERAL("--"),
306 FILE_PATH_LITERAL("--arg1") };
307 CommandLine cl(arraysize(raw_argv), raw_argv);
308
309 cl.AppendSwitch("switch1");
310 cl.AppendSwitchASCII("switch2", "foo");
311
312 cl.AppendArg("--arg2");
313
314 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"),
[email protected]61a4c6f2011-07-20 04:54:52315 cl.GetCommandLineString());
[email protected]a40ca4302011-05-14 01:10:24316 CommandLine::StringVector cl_argv = cl.argv();
317 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]);
318 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]);
319 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]);
320 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]);
321 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]);
322 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]);
323}
324
[email protected]e6124ad52010-11-15 04:17:52325// Tests that when AppendArguments is called that the program is set correctly
326// on the target CommandLine object and the switches from the source
327// CommandLine are added to the target.
328TEST(CommandLineTest, AppendArguments) {
329 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program")));
330 cl1.AppendSwitch("switch1");
331 cl1.AppendSwitchASCII("switch2", "foo");
332
333 CommandLine cl2(CommandLine::NO_PROGRAM);
334 cl2.AppendArguments(cl1, true);
335 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value());
[email protected]61a4c6f2011-07-20 04:54:52336 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString());
[email protected]e6124ad52010-11-15 04:17:52337
338 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1")));
339 c1.AppendSwitch("switch1");
340 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2")));
341 c2.AppendSwitch("switch2");
342
343 c1.AppendArguments(c2, true);
344 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value());
345 EXPECT_TRUE(c1.HasSwitch("switch1"));
346 EXPECT_TRUE(c1.HasSwitch("switch2"));
347}
348
[email protected]450b34ec2010-11-29 21:12:22349#if defined(OS_WIN)
[email protected]61a4c6f2011-07-20 04:54:52350// Make sure that the command line string program paths are quoted as necessary.
[email protected]450b34ec2010-11-29 21:12:22351// This only makes sense on Windows and the test is basically here to guard
352// against regressions.
353TEST(CommandLineTest, ProgramQuotes) {
[email protected]a40ca4302011-05-14 01:10:24354 // Check that quotes are not added for paths without spaces.
[email protected]450b34ec2010-11-29 21:12:22355 const FilePath kProgram(L"Program");
[email protected]a40ca4302011-05-14 01:10:24356 CommandLine cl_program(kProgram);
357 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
[email protected]61a4c6f2011-07-20 04:54:52358 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
[email protected]a40ca4302011-05-14 01:10:24359
360 const FilePath kProgramPath(L"Program Path");
[email protected]450b34ec2010-11-29 21:12:22361
362 // Check that quotes are not returned from GetProgram().
[email protected]a40ca4302011-05-14 01:10:24363 CommandLine cl_program_path(kProgramPath);
364 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
[email protected]450b34ec2010-11-29 21:12:22365
[email protected]a40ca4302011-05-14 01:10:24366 // Check that quotes are added to command line string paths containing spaces.
[email protected]61a4c6f2011-07-20 04:54:52367 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
mgiucab937b2422014-09-24 04:55:31368 EXPECT_EQ(L"\"Program Path\"", cmd_string);
mgiucac974d5102014-10-01 09:24:51369
370 // Check the optional quoting of placeholders in programs.
371 CommandLine cl_quote_placeholder(base::FilePath(L"%1"));
372 EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString());
373 EXPECT_EQ(L"\"%1\"",
374 cl_quote_placeholder.GetCommandLineStringWithPlaceholders());
[email protected]450b34ec2010-11-29 21:12:22375}
376#endif
[email protected]f96fe2c42011-07-13 18:03:34377
378// Calling Init multiple times should not modify the previous CommandLine.
379TEST(CommandLineTest, Init) {
380 CommandLine* initial = CommandLine::ForCurrentProcess();
[email protected]72e2e2422012-02-27 18:38:12381 EXPECT_FALSE(CommandLine::Init(0, NULL));
[email protected]f96fe2c42011-07-13 18:03:34382 CommandLine* current = CommandLine::ForCurrentProcess();
383 EXPECT_EQ(initial, current);
384}