blob: 06c5006505a2c8e592724fb6fdc169b28a627899 [file] [log] [blame]
erikwright@chromium.org506c72b2012-02-28 03:38:12 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#include <string>
6#include <vector>
7
tommi@chromium.orgf6e64922010-11-15 09:18:30 +09008#include "base/basictypes.h"
tommi@chromium.org877e84e2010-11-15 13:17:52 +09009#include "base/command_line.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090010#include "base/files/file_path.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090011#include "base/strings/utf_string_conversions.h"
initial.commit3f4a7322008-07-27 06:49:38 +090012#include "testing/gtest/include/gtest/gtest.h"
13
brettw@chromium.org82bcf512013-02-17 14:07:23 +090014using base::FilePath;
15
evan@chromium.orgcdf80852010-08-11 03:14:19 +090016// 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.
msw@chromium.org53fbac12011-05-14 10:10:24 +090020static const CommandLine::StringType kTrickyQuoted =
21 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\"");
evan@chromium.orgcdf80852010-08-11 03:14:19 +090022// It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
23// Here that is with C-style escapes.
msw@chromium.org53fbac12011-05-14 10:10:24 +090024static const CommandLine::StringType kTricky =
25 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\"");
evan@chromium.orgcdf80852010-08-11 03:14:19 +090026
initial.commit3f4a7322008-07-27 06:49:38 +090027TEST(CommandLineTest, CommandLineConstructor) {
msw@chromium.org53fbac12011-05-14 10:10:24 +090028 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"),
jochen@chromium.org1e9efb12012-10-19 15:19:59 +090038 FILE_PATH_LITERAL("-"),
msw@chromium.org53fbac12011-05-14 10:10:24 +090039 FILE_PATH_LITERAL("FLAN"),
joth@chromium.orgb23d1842011-09-14 00:45:34 +090040 FILE_PATH_LITERAL("a"),
msw@chromium.org53fbac12011-05-14 10:10:24 +090041 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
msw@chromium.orgc57622c2011-07-20 13:54:52 +090049 EXPECT_FALSE(cl.GetCommandLineString().empty());
msw@chromium.org53fbac12011-05-14 10:10:24 +090050 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
msw@chromium.orga25adf42011-07-14 08:41:22 +090080 const CommandLine::StringVector& args = cl.GetArgs();
jochen@chromium.org1e9efb12012-10-19 15:19:59 +090081 ASSERT_EQ(8U, args.size());
msw@chromium.org53fbac12011-05-14 10:10:24 +090082
83 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
84 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
85 ++iter;
jochen@chromium.org1e9efb12012-10-19 15:19:59 +090086 EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter);
87 ++iter;
msw@chromium.org53fbac12011-05-14 10:10:24 +090088 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
89 ++iter;
joth@chromium.orgb23d1842011-09-14 00:45:34 +090090 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter);
91 ++iter;
msw@chromium.org53fbac12011-05-14 10:10:24 +090092 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) {
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900104#if defined(OS_WIN)
evan@chromium.orgc4fee2c2009-10-27 07:39:33 +0900105 CommandLine cl = CommandLine::FromString(
msw@chromium.org53fbac12011-05-14 10:10:24 +0900106 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
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900114 EXPECT_FALSE(cl.GetCommandLineString().empty());
evan@chromium.orge3fca692009-10-13 11:07:25 +0900115 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.commit3f4a7322008-07-27 06:49:38 +0900123
evan@chromium.orgafe7c022010-10-08 09:06:31 +0900124 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
125 cl.GetProgram().value());
initial.commit3f4a7322008-07-27 06:49:38 +0900126
evan@chromium.orge3fca692009-10-13 11:07:25 +0900127 EXPECT_TRUE(cl.HasSwitch("foo"));
128 EXPECT_TRUE(cl.HasSwitch("bar"));
129 EXPECT_TRUE(cl.HasSwitch("baz"));
130 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
evan@chromium.orge3fca692009-10-13 11:07:25 +0900131 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
evan@chromium.orge3fca692009-10-13 11:07:25 +0900132 EXPECT_TRUE(cl.HasSwitch("other-switches"));
133 EXPECT_TRUE(cl.HasSwitch("input-translation"));
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900134 EXPECT_TRUE(cl.HasSwitch("quotes"));
initial.commit3f4a7322008-07-27 06:49:38 +0900135
tony@chromium.org7bcc4c12009-11-07 04:55:16 +0900136 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"));
msw@chromium.org53fbac12011-05-14 10:10:24 +0900143 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes"));
initial.commit3f4a7322008-07-27 06:49:38 +0900144
msw@chromium.orga25adf42011-07-14 08:41:22 +0900145 const CommandLine::StringVector& args = cl.GetArgs();
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900146 ASSERT_EQ(5U, args.size());
initial.commit3f4a7322008-07-27 06:49:38 +0900147
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900148 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
149 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
initial.commit3f4a7322008-07-27 06:49:38 +0900150 ++iter;
msw@chromium.org53fbac12011-05-14 10:10:24 +0900151 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter);
initial.commit3f4a7322008-07-27 06:49:38 +0900152 ++iter;
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900153 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
deanm@chromium.orga638cc92008-10-06 19:25:35 +0900154 ++iter;
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900155 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
deanm@chromium.orga638cc92008-10-06 19:25:35 +0900156 ++iter;
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900157 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
initial.commit3f4a7322008-07-27 06:49:38 +0900158 ++iter;
evan@chromium.orgbb42a352010-07-22 00:57:23 +0900159 EXPECT_TRUE(iter == args.end());
estade@chromium.org92c59f22008-10-16 06:59:08 +0900160
msw@chromium.org53fbac12011-05-14 10:10:24 +0900161 // Check that a generated string produces an equivalent command line.
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900162 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString());
163 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString());
estade@chromium.org92c59f22008-10-16 06:59:08 +0900164#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900165}
166
initial.commit3f4a7322008-07-27 06:49:38 +0900167// Tests behavior with an empty input string.
168TEST(CommandLineTest, EmptyString) {
evanm@google.com91cdff82008-08-08 05:07:32 +0900169#if defined(OS_WIN)
msw@chromium.org53fbac12011-05-14 10:10:24 +0900170 CommandLine cl_from_string = CommandLine::FromString(L"");
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900171 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900172 EXPECT_TRUE(cl_from_string.GetProgram().empty());
173 EXPECT_EQ(1U, cl_from_string.argv().size());
msw@chromium.orga25adf42011-07-14 08:41:22 +0900174 EXPECT_TRUE(cl_from_string.GetArgs().empty());
evanm@google.com91cdff82008-08-08 05:07:32 +0900175#endif
msw@chromium.org53fbac12011-05-14 10:10:24 +0900176 CommandLine cl_from_argv(0, NULL);
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900177 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900178 EXPECT_TRUE(cl_from_argv.GetProgram().empty());
179 EXPECT_EQ(1U, cl_from_argv.argv().size());
msw@chromium.orga25adf42011-07-14 08:41:22 +0900180 EXPECT_TRUE(cl_from_argv.GetArgs().empty());
initial.commit3f4a7322008-07-27 06:49:38 +0900181}
182
gab@chromium.orge399ff42012-10-30 06:31:31 +0900183TEST(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";
193
194 CommandLine cl(CommandLine::NO_PROGRAM);
195 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1));
196 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2));
197 cl.AppendArg(kThirdArgName);
198 cl.AppendArg(kFourthArgName);
199
200#if defined(OS_WIN)
avi@chromium.org0e569b92013-12-26 16:07:56 +0900201 CommandLine::StringType expected_first_arg(
202 base::UTF8ToUTF16(kFirstArgName));
203 CommandLine::StringType expected_second_arg(
204 base::UTF8ToUTF16(kSecondArgName));
205 CommandLine::StringType expected_third_arg(
206 base::UTF8ToUTF16(kThirdArgName));
207 CommandLine::StringType expected_fourth_arg(
208 base::UTF8ToUTF16(kFourthArgName));
gab@chromium.orge399ff42012-10-30 06:31:31 +0900209#elif defined(OS_POSIX)
210 CommandLine::StringType expected_first_arg(kFirstArgName);
211 CommandLine::StringType expected_second_arg(kSecondArgName);
212 CommandLine::StringType expected_third_arg(kThirdArgName);
213 CommandLine::StringType expected_fourth_arg(kFourthArgName);
214#endif
215
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
221
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 EXPECT_EQ(expected_str, cl.GetArgumentsString());
243}
244
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900245// Test methods for appending switches to a command line.
initial.commit3f4a7322008-07-27 06:49:38 +0900246TEST(CommandLineTest, AppendSwitches) {
evan@chromium.orge3fca692009-10-13 11:07:25 +0900247 std::string switch1 = "switch1";
248 std::string switch2 = "switch2";
msw@chromium.org53fbac12011-05-14 10:10:24 +0900249 std::string value2 = "value";
evan@chromium.orge3fca692009-10-13 11:07:25 +0900250 std::string switch3 = "switch3";
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900251 std::string value3 = "a value with spaces";
evan@chromium.orge3fca692009-10-13 11:07:25 +0900252 std::string switch4 = "switch4";
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900253 std::string value4 = "\"a value with quotes\"";
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900254 std::string switch5 = "quotes";
msw@chromium.org53fbac12011-05-14 10:10:24 +0900255 CommandLine::StringType value5 = kTricky;
initial.commit3f4a7322008-07-27 06:49:38 +0900256
evan@chromium.orgc4fee2c2009-10-27 07:39:33 +0900257 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
initial.commit3f4a7322008-07-27 06:49:38 +0900258
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900259 cl.AppendSwitch(switch1);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900260 cl.AppendSwitchASCII(switch2, value2);
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900261 cl.AppendSwitchASCII(switch3, value3);
262 cl.AppendSwitchASCII(switch4, value4);
msw@chromium.org53fbac12011-05-14 10:10:24 +0900263 cl.AppendSwitchNative(switch5, value5);
evan@chromium.org4bbc6132009-01-21 10:00:22 +0900264
initial.commit3f4a7322008-07-27 06:49:38 +0900265 EXPECT_TRUE(cl.HasSwitch(switch1));
266 EXPECT_TRUE(cl.HasSwitch(switch2));
msw@chromium.org53fbac12011-05-14 10:10:24 +0900267 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
initial.commit3f4a7322008-07-27 06:49:38 +0900268 EXPECT_TRUE(cl.HasSwitch(switch3));
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900269 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
estade@chromium.orga048e382008-10-11 06:38:20 +0900270 EXPECT_TRUE(cl.HasSwitch(switch4));
evan@chromium.org25ca7dd2010-07-30 14:59:57 +0900271 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900272 EXPECT_TRUE(cl.HasSwitch(switch5));
msw@chromium.org53fbac12011-05-14 10:10:24 +0900273 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900274
275#if defined(OS_WIN)
msw@chromium.org53fbac12011-05-14 10:10:24 +0900276 EXPECT_EQ(L"Program "
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900277 L"--switch1 "
278 L"--switch2=value "
279 L"--switch3=\"a value with spaces\" "
280 L"--switch4=\"\\\"a value with quotes\\\"\" "
msw@chromium.org53fbac12011-05-14 10:10:24 +0900281 L"--quotes=\"" + kTrickyQuoted + L"\"",
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900282 cl.GetCommandLineString());
evan@chromium.orgcdf80852010-08-11 03:14:19 +0900283#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900284}
tommi@chromium.org877e84e2010-11-15 13:17:52 +0900285
msw@chromium.org53fbac12011-05-14 10:10:24 +0900286TEST(CommandLineTest, AppendSwitchesDashDash) {
287 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"),
288 FILE_PATH_LITERAL("--"),
289 FILE_PATH_LITERAL("--arg1") };
290 CommandLine cl(arraysize(raw_argv), raw_argv);
291
292 cl.AppendSwitch("switch1");
293 cl.AppendSwitchASCII("switch2", "foo");
294
295 cl.AppendArg("--arg2");
296
297 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"),
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900298 cl.GetCommandLineString());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900299 CommandLine::StringVector cl_argv = cl.argv();
300 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]);
301 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]);
302 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]);
303 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]);
304 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]);
305 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]);
306}
307
tommi@chromium.org877e84e2010-11-15 13:17:52 +0900308// Tests that when AppendArguments is called that the program is set correctly
309// on the target CommandLine object and the switches from the source
310// CommandLine are added to the target.
311TEST(CommandLineTest, AppendArguments) {
312 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program")));
313 cl1.AppendSwitch("switch1");
314 cl1.AppendSwitchASCII("switch2", "foo");
315
316 CommandLine cl2(CommandLine::NO_PROGRAM);
317 cl2.AppendArguments(cl1, true);
318 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value());
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900319 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString());
tommi@chromium.org877e84e2010-11-15 13:17:52 +0900320
321 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1")));
322 c1.AppendSwitch("switch1");
323 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2")));
324 c2.AppendSwitch("switch2");
325
326 c1.AppendArguments(c2, true);
327 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value());
328 EXPECT_TRUE(c1.HasSwitch("switch1"));
329 EXPECT_TRUE(c1.HasSwitch("switch2"));
330}
331
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900332#if defined(OS_WIN)
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900333// Make sure that the command line string program paths are quoted as necessary.
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900334// This only makes sense on Windows and the test is basically here to guard
335// against regressions.
336TEST(CommandLineTest, ProgramQuotes) {
msw@chromium.org53fbac12011-05-14 10:10:24 +0900337 // Check that quotes are not added for paths without spaces.
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900338 const FilePath kProgram(L"Program");
msw@chromium.org53fbac12011-05-14 10:10:24 +0900339 CommandLine cl_program(kProgram);
340 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value());
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900341 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900342
343 const FilePath kProgramPath(L"Program Path");
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900344
345 // Check that quotes are not returned from GetProgram().
msw@chromium.org53fbac12011-05-14 10:10:24 +0900346 CommandLine cl_program_path(kProgramPath);
347 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value());
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900348
msw@chromium.org53fbac12011-05-14 10:10:24 +0900349 // Check that quotes are added to command line string paths containing spaces.
msw@chromium.orgc57622c2011-07-20 13:54:52 +0900350 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString());
msw@chromium.org53fbac12011-05-14 10:10:24 +0900351 CommandLine::StringType program_string(cl_program_path.GetProgram().value());
352 EXPECT_EQ('"', cmd_string[0]);
353 EXPECT_EQ(program_string, cmd_string.substr(1, program_string.length()));
354 EXPECT_EQ('"', cmd_string[program_string.length() + 1]);
tommi@chromium.orgd2c09202010-11-30 06:12:22 +0900355}
356#endif
rvargas@google.com58a11842011-07-14 03:03:34 +0900357
358// Calling Init multiple times should not modify the previous CommandLine.
359TEST(CommandLineTest, Init) {
360 CommandLine* initial = CommandLine::ForCurrentProcess();
erikwright@chromium.org506c72b2012-02-28 03:38:12 +0900361 EXPECT_FALSE(CommandLine::Init(0, NULL));
rvargas@google.com58a11842011-07-14 03:03:34 +0900362 CommandLine* current = CommandLine::ForCurrentProcess();
363 EXPECT_EQ(initial, current);
364}