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
3 // found in the LICENSE file.
8 #include "base/command_line.h"
9 #include "base/basictypes.h"
10 #include "base/string_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 TEST(CommandLineTest
, CommandLineConstructor
) {
15 CommandLine cl
= CommandLine::FromString(
16 L
"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
17 L
"--other-switches=\"--dog=canine --cat=feline\" "
18 L
"-spaetzle=Crepe -=loosevalue flan "
19 L
"--input-translation=\"45\"--output-rotation "
20 L
"-- -- --not-a-switch "
21 L
"\"in the time of submarines...\"");
22 EXPECT_FALSE(cl
.command_line_string().empty());
23 #elif defined(OS_POSIX)
24 const char* argv
[] = {"program", "--foo=", "-bar",
25 "-spaetzel=pierogi", "-baz", "flim",
26 "--other-switches=--dog=canine --cat=feline",
27 "-spaetzle=Crepe", "-=loosevalue", "flan",
28 "--input-translation=45--output-rotation",
29 "--", "--", "--not-a-switch",
30 "in the time of submarines..."};
31 CommandLine
cl(arraysize(argv
), argv
);
33 EXPECT_FALSE(cl
.HasSwitch("cruller"));
34 EXPECT_FALSE(cl
.HasSwitch("flim"));
35 EXPECT_FALSE(cl
.HasSwitch("program"));
36 EXPECT_FALSE(cl
.HasSwitch("dog"));
37 EXPECT_FALSE(cl
.HasSwitch("cat"));
38 EXPECT_FALSE(cl
.HasSwitch("output-rotation"));
39 EXPECT_FALSE(cl
.HasSwitch("not-a-switch"));
40 EXPECT_FALSE(cl
.HasSwitch("--"));
42 EXPECT_EQ(L
"program", cl
.program());
44 EXPECT_TRUE(cl
.HasSwitch("foo"));
45 EXPECT_TRUE(cl
.HasSwitch("bar"));
46 EXPECT_TRUE(cl
.HasSwitch("baz"));
47 EXPECT_TRUE(cl
.HasSwitch("spaetzle"));
49 EXPECT_TRUE(cl
.HasSwitch("SPAETZLE"));
51 EXPECT_TRUE(cl
.HasSwitch("other-switches"));
52 EXPECT_TRUE(cl
.HasSwitch("input-translation"));
54 EXPECT_EQ("Crepe", cl
.GetSwitchValueASCII("spaetzle"));
55 EXPECT_EQ("", cl
.GetSwitchValueASCII("Foo"));
56 EXPECT_EQ("", cl
.GetSwitchValueASCII("bar"));
57 EXPECT_EQ("", cl
.GetSwitchValueASCII("cruller"));
58 EXPECT_EQ("--dog=canine --cat=feline", cl
.GetSwitchValueASCII(
60 EXPECT_EQ("45--output-rotation", cl
.GetSwitchValueASCII("input-translation"));
62 std::vector
<std::wstring
> loose_values
= cl
.GetLooseValues();
63 ASSERT_EQ(5U, loose_values
.size());
65 std::vector
<std::wstring
>::const_iterator iter
= loose_values
.begin();
66 EXPECT_EQ(L
"flim", *iter
);
68 EXPECT_EQ(L
"flan", *iter
);
70 EXPECT_EQ(L
"--", *iter
);
72 EXPECT_EQ(L
"--not-a-switch", *iter
);
74 EXPECT_EQ(L
"in the time of submarines...", *iter
);
76 EXPECT_TRUE(iter
== loose_values
.end());
78 const std::vector
<std::string
>& argvec
= cl
.argv();
80 for (size_t i
= 0; i
< argvec
.size(); i
++) {
81 EXPECT_EQ(0, argvec
[i
].compare(argv
[i
]));
86 // Tests behavior with an empty input string.
87 TEST(CommandLineTest
, EmptyString
) {
89 CommandLine cl
= CommandLine::FromString(L
"");
90 EXPECT_TRUE(cl
.command_line_string().empty());
91 EXPECT_TRUE(cl
.program().empty());
92 #elif defined(OS_POSIX)
93 CommandLine
cl(0, NULL
);
94 EXPECT_TRUE(cl
.argv().size() == 0);
96 EXPECT_EQ(0U, cl
.GetLooseValues().size());
99 // Test methods for appending switches to a command line.
100 TEST(CommandLineTest
, AppendSwitches
) {
101 std::string switch1
= "switch1";
102 std::string switch2
= "switch2";
103 std::wstring value
= L
"value";
104 std::string switch3
= "switch3";
105 std::wstring value3
= L
"a value with spaces";
106 std::string switch4
= "switch4";
107 std::wstring value4
= L
"\"a value with quotes\"";
109 CommandLine
cl(FilePath(FILE_PATH_LITERAL("Program")));
111 cl
.AppendSwitch(switch1
);
112 cl
.AppendSwitchWithValue(switch2
, value
);
113 cl
.AppendSwitchWithValue(switch3
, value3
);
114 cl
.AppendSwitchWithValue(switch4
, value4
);
116 EXPECT_TRUE(cl
.HasSwitch(switch1
));
117 EXPECT_TRUE(cl
.HasSwitch(switch2
));
118 EXPECT_EQ(value
, cl
.GetSwitchValue(switch2
));
119 EXPECT_TRUE(cl
.HasSwitch(switch3
));
120 EXPECT_EQ(value3
, cl
.GetSwitchValue(switch3
));
121 EXPECT_TRUE(cl
.HasSwitch(switch4
));
122 EXPECT_EQ(value4
, cl
.GetSwitchValue(switch4
));