[email protected] | 72e2e242 | 2012-02-27 18:38:12 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 5 | #include "base/command_line.h" |
| 6 | |
| 7 | #include <memory> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 12 | #include "base/strings/strcat.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 14 | #include "build/build_config.h" |
Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 15 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
pgal.u-szeged | 421dddb | 2014-11-25 12:55:02 | [diff] [blame] | 18 | namespace base { |
[email protected] | 023ad6ab | 2013-02-17 05:07:23 | [diff] [blame] | 19 | |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 20 | // To test Windows quoting behavior, we use a string that has some backslashes |
| 21 | // and quotes. |
| 22 | // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\" |
| 23 | // Here it is with C-style escapes. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 24 | static const CommandLine::StringType kTrickyQuoted = |
| 25 | FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\""); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 26 | // It should be parsed by Windows as: q"bs1\bs2\\bs3q\" |
| 27 | // Here that is with C-style escapes. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 28 | static const CommandLine::StringType kTricky = |
| 29 | FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\""); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 30 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 31 | TEST(CommandLineTest, CommandLineConstructor) { |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 32 | const CommandLine::CharType* argv[] = { |
| 33 | FILE_PATH_LITERAL("program"), |
| 34 | FILE_PATH_LITERAL("--foo="), |
| 35 | FILE_PATH_LITERAL("-bAr"), |
| 36 | FILE_PATH_LITERAL("-spaetzel=pierogi"), |
| 37 | FILE_PATH_LITERAL("-baz"), |
| 38 | FILE_PATH_LITERAL("flim"), |
| 39 | FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"), |
| 40 | FILE_PATH_LITERAL("-spaetzle=Crepe"), |
| 41 | FILE_PATH_LITERAL("-=loosevalue"), |
[email protected] | 21e342f | 2012-10-19 06:19:59 | [diff] [blame] | 42 | FILE_PATH_LITERAL("-"), |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 43 | FILE_PATH_LITERAL("FLAN"), |
[email protected] | 1fa39f0 | 2011-09-13 15:45:34 | [diff] [blame] | 44 | FILE_PATH_LITERAL("a"), |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 45 | FILE_PATH_LITERAL("--input-translation=45--output-rotation"), |
| 46 | FILE_PATH_LITERAL("--"), |
| 47 | FILE_PATH_LITERAL("--"), |
| 48 | FILE_PATH_LITERAL("--not-a-switch"), |
| 49 | FILE_PATH_LITERAL("\"in the time of submarines...\""), |
| 50 | FILE_PATH_LITERAL("unquoted arg-with-space")}; |
jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 51 | CommandLine cl(size(argv), argv); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 52 | |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 53 | EXPECT_FALSE(cl.GetCommandLineString().empty()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 54 | EXPECT_FALSE(cl.HasSwitch("cruller")); |
| 55 | EXPECT_FALSE(cl.HasSwitch("flim")); |
| 56 | EXPECT_FALSE(cl.HasSwitch("program")); |
| 57 | EXPECT_FALSE(cl.HasSwitch("dog")); |
| 58 | EXPECT_FALSE(cl.HasSwitch("cat")); |
| 59 | EXPECT_FALSE(cl.HasSwitch("output-rotation")); |
| 60 | EXPECT_FALSE(cl.HasSwitch("not-a-switch")); |
| 61 | EXPECT_FALSE(cl.HasSwitch("--")); |
| 62 | |
| 63 | EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), |
| 64 | cl.GetProgram().value()); |
| 65 | |
| 66 | EXPECT_TRUE(cl.HasSwitch("foo")); |
jackhou | b20cbb4 | 2015-04-22 02:21:44 | [diff] [blame] | 67 | #if defined(OS_WIN) |
| 68 | EXPECT_TRUE(cl.HasSwitch("bar")); |
| 69 | #else |
| 70 | EXPECT_FALSE(cl.HasSwitch("bar")); |
| 71 | #endif |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 72 | EXPECT_TRUE(cl.HasSwitch("baz")); |
| 73 | EXPECT_TRUE(cl.HasSwitch("spaetzle")); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 74 | EXPECT_TRUE(cl.HasSwitch("other-switches")); |
| 75 | EXPECT_TRUE(cl.HasSwitch("input-translation")); |
| 76 | |
| 77 | EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 78 | EXPECT_EQ("", cl.GetSwitchValueASCII("foo")); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 79 | EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); |
| 80 | EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); |
| 81 | EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( |
| 82 | "other-switches")); |
| 83 | EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); |
| 84 | |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 85 | const CommandLine::StringVector& args = cl.GetArgs(); |
[email protected] | 21e342f | 2012-10-19 06:19:59 | [diff] [blame] | 86 | ASSERT_EQ(8U, args.size()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 87 | |
jdoerrie | 1c4b8ff | 2018-10-03 00:10:57 | [diff] [blame] | 88 | auto iter = args.begin(); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 89 | EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); |
| 90 | ++iter; |
[email protected] | 21e342f | 2012-10-19 06:19:59 | [diff] [blame] | 91 | EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter); |
| 92 | ++iter; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 93 | EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter); |
| 94 | ++iter; |
[email protected] | 1fa39f0 | 2011-09-13 15:45:34 | [diff] [blame] | 95 | EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter); |
| 96 | ++iter; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 97 | EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); |
| 98 | ++iter; |
| 99 | EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); |
| 100 | ++iter; |
| 101 | EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter); |
| 102 | ++iter; |
| 103 | EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter); |
| 104 | ++iter; |
| 105 | EXPECT_TRUE(iter == args.end()); |
| 106 | } |
| 107 | |
| 108 | TEST(CommandLineTest, CommandLineFromString) { |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 109 | #if defined(OS_WIN) |
Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 110 | CommandLine cl = CommandLine::FromString( |
| 111 | L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
| 112 | L"--other-switches=\"--dog=canine --cat=feline\" " |
| 113 | L"-spaetzle=Crepe -=loosevalue FLAN " |
| 114 | L"--input-translation=\"45\"--output-rotation " |
| 115 | L"--quotes=" + |
| 116 | kTrickyQuoted + |
| 117 | L" -- -- --not-a-switch \"in the time of submarines...\""); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 118 | |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 119 | EXPECT_FALSE(cl.GetCommandLineString().empty()); |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 120 | EXPECT_FALSE(cl.HasSwitch("cruller")); |
| 121 | EXPECT_FALSE(cl.HasSwitch("flim")); |
| 122 | EXPECT_FALSE(cl.HasSwitch("program")); |
| 123 | EXPECT_FALSE(cl.HasSwitch("dog")); |
| 124 | EXPECT_FALSE(cl.HasSwitch("cat")); |
| 125 | EXPECT_FALSE(cl.HasSwitch("output-rotation")); |
| 126 | EXPECT_FALSE(cl.HasSwitch("not-a-switch")); |
| 127 | EXPECT_FALSE(cl.HasSwitch("--")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 128 | |
[email protected] | 78c4c42 | 2010-10-08 00:06:31 | [diff] [blame] | 129 | EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), |
| 130 | cl.GetProgram().value()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 131 | |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 132 | EXPECT_TRUE(cl.HasSwitch("foo")); |
| 133 | EXPECT_TRUE(cl.HasSwitch("bar")); |
| 134 | EXPECT_TRUE(cl.HasSwitch("baz")); |
| 135 | EXPECT_TRUE(cl.HasSwitch("spaetzle")); |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 136 | EXPECT_TRUE(cl.HasSwitch("other-switches")); |
| 137 | EXPECT_TRUE(cl.HasSwitch("input-translation")); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 138 | EXPECT_TRUE(cl.HasSwitch("quotes")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 139 | |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 140 | EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 141 | EXPECT_EQ("", cl.GetSwitchValueASCII("foo")); |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 142 | EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); |
| 143 | EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); |
| 144 | EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( |
| 145 | "other-switches")); |
| 146 | EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 147 | EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 148 | |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 149 | const CommandLine::StringVector& args = cl.GetArgs(); |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 150 | ASSERT_EQ(5U, args.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 151 | |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 152 | std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); |
| 153 | EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 154 | ++iter; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 155 | EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 156 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 157 | EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 158 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 159 | EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 160 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 161 | EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 162 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 163 | EXPECT_TRUE(iter == args.end()); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 164 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 165 | // Check that a generated string produces an equivalent command line. |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 166 | CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString()); |
| 167 | EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString()); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 168 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 169 | } |
| 170 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 171 | // Tests behavior with an empty input string. |
| 172 | TEST(CommandLineTest, EmptyString) { |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 173 | #if defined(OS_WIN) |
Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 174 | CommandLine cl_from_string = CommandLine::FromString(std::wstring()); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 175 | EXPECT_TRUE(cl_from_string.GetCommandLineString().empty()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 176 | EXPECT_TRUE(cl_from_string.GetProgram().empty()); |
| 177 | EXPECT_EQ(1U, cl_from_string.argv().size()); |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 178 | EXPECT_TRUE(cl_from_string.GetArgs().empty()); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 179 | #endif |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 180 | CommandLine cl_from_argv(0, nullptr); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 181 | EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 182 | EXPECT_TRUE(cl_from_argv.GetProgram().empty()); |
| 183 | EXPECT_EQ(1U, cl_from_argv.argv().size()); |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 184 | EXPECT_TRUE(cl_from_argv.GetArgs().empty()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 185 | } |
| 186 | |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 187 | TEST(CommandLineTest, GetArgumentsString) { |
| 188 | static const FilePath::CharType kPath1[] = |
| 189 | FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg"); |
| 190 | static const FilePath::CharType kPath2[] = |
| 191 | FILE_PATH_LITERAL("C:\\no\\spaces.ggg"); |
| 192 | |
| 193 | static const char kFirstArgName[] = "first-arg"; |
| 194 | static const char kSecondArgName[] = "arg2"; |
| 195 | static const char kThirdArgName[] = "arg with space"; |
| 196 | static const char kFourthArgName[] = "nospace"; |
| 197 | |
| 198 | CommandLine cl(CommandLine::NO_PROGRAM); |
| 199 | cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1)); |
| 200 | cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2)); |
| 201 | cl.AppendArg(kThirdArgName); |
| 202 | cl.AppendArg(kFourthArgName); |
| 203 | |
| 204 | #if defined(OS_WIN) |
Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 205 | CommandLine::StringType expected_first_arg(UTF8ToWide(kFirstArgName)); |
| 206 | CommandLine::StringType expected_second_arg(UTF8ToWide(kSecondArgName)); |
| 207 | CommandLine::StringType expected_third_arg(UTF8ToWide(kThirdArgName)); |
| 208 | CommandLine::StringType expected_fourth_arg(UTF8ToWide(kFourthArgName)); |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 209 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 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("--")) |
Jesse McKenna | 452f831 | 2020-05-29 23:06:56 | [diff] [blame] | 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(" ")) |
Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 241 | .append(expected_fourth_arg); |
| 242 | EXPECT_EQ(expected_str, cl.GetArgumentsString()); |
[email protected] | 45f982e | 2012-10-29 21:31:31 | [diff] [blame] | 243 | } |
| 244 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 245 | // Test methods for appending switches to a command line. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 246 | TEST(CommandLineTest, AppendSwitches) { |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 247 | std::string switch1 = "switch1"; |
| 248 | std::string switch2 = "switch2"; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 249 | std::string value2 = "value"; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 250 | std::string switch3 = "switch3"; |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 251 | std::string value3 = "a value with spaces"; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 252 | std::string switch4 = "switch4"; |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 253 | std::string value4 = "\"a value with quotes\""; |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 254 | std::string switch5 = "quotes"; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 255 | CommandLine::StringType value5 = kTricky; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 256 | |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 257 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 258 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 259 | cl.AppendSwitch(switch1); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 260 | cl.AppendSwitchASCII(switch2, value2); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 261 | cl.AppendSwitchASCII(switch3, value3); |
| 262 | cl.AppendSwitchASCII(switch4, value4); |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 263 | cl.AppendSwitchASCII(switch5, value4); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 264 | cl.AppendSwitchNative(switch5, value5); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 265 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 266 | EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 267 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 268 | EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 269 | EXPECT_TRUE(cl.HasSwitch(switch3)); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 270 | EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); |
[email protected] | 8c9510d | 2008-10-10 21:38:20 | [diff] [blame] | 271 | EXPECT_TRUE(cl.HasSwitch(switch4)); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 272 | EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 273 | EXPECT_TRUE(cl.HasSwitch(switch5)); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 274 | EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5)); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 275 | |
| 276 | #if defined(OS_WIN) |
jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 277 | EXPECT_EQ( |
Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 278 | L"Program " |
| 279 | L"--switch1 " |
| 280 | L"--switch2=value " |
| 281 | L"--switch3=\"a value with spaces\" " |
| 282 | L"--switch4=\"\\\"a value with quotes\\\"\" " |
| 283 | // Even though the switches are unique, appending can add repeat |
| 284 | // switches to argv. |
| 285 | L"--quotes=\"\\\"a value with quotes\\\"\" " |
| 286 | L"--quotes=\"" + |
| 287 | kTrickyQuoted + L"\"", |
jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 288 | cl.GetCommandLineString()); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 289 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 290 | } |
[email protected] | e6124ad5 | 2010-11-15 04:17:52 | [diff] [blame] | 291 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 292 | TEST(CommandLineTest, AppendSwitchesDashDash) { |
| 293 | const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"), |
| 294 | FILE_PATH_LITERAL("--"), |
| 295 | FILE_PATH_LITERAL("--arg1") }; |
jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 296 | CommandLine cl(size(raw_argv), raw_argv); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 297 | |
Avi Drissman | e3b70bf | 2019-01-04 19:50:22 | [diff] [blame] | 298 | cl.AppendSwitch("switch1"); |
| 299 | cl.AppendSwitchASCII("switch2", "foo"); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 300 | |
Avi Drissman | e3b70bf | 2019-01-04 19:50:22 | [diff] [blame] | 301 | cl.AppendArg("--arg2"); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 302 | |
Avi Drissman | e3b70bf | 2019-01-04 19:50:22 | [diff] [blame] | 303 | EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"), |
| 304 | cl.GetCommandLineString()); |
| 305 | CommandLine::StringVector cl_argv = cl.argv(); |
| 306 | EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]); |
| 307 | EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]); |
| 308 | EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]); |
| 309 | EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]); |
| 310 | EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]); |
| 311 | EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 312 | } |
| 313 | |
Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 314 | #if defined(OS_WIN) |
| 315 | TEST(CommandLineTest, GetCommandLineStringForShell) { |
| 316 | CommandLine cl = CommandLine::FromString( |
| 317 | FILE_PATH_LITERAL("program --switch /switch2 --")); |
| 318 | EXPECT_EQ( |
| 319 | cl.GetCommandLineStringForShell(), |
| 320 | FILE_PATH_LITERAL("program --switch /switch2 -- --single-argument %1")); |
| 321 | } |
Jesse McKenna | 185ceda2 | 2021-03-10 06:47:55 | [diff] [blame] | 322 | |
| 323 | TEST(CommandLineTest, GetCommandLineStringWithUnsafeInsertSequences) { |
| 324 | CommandLine cl(FilePath(FILE_PATH_LITERAL("program"))); |
| 325 | cl.AppendSwitchASCII("switch", "%1"); |
| 326 | cl.AppendSwitch("%2"); |
| 327 | cl.AppendArg("%3"); |
| 328 | EXPECT_EQ(FILE_PATH_LITERAL("program --switch=%1 --%2 %3"), |
| 329 | cl.GetCommandLineStringWithUnsafeInsertSequences()); |
| 330 | } |
Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 331 | #endif // defined(OS_WIN) |
| 332 | |
[email protected] | e6124ad5 | 2010-11-15 04:17:52 | [diff] [blame] | 333 | // Tests that when AppendArguments is called that the program is set correctly |
| 334 | // on the target CommandLine object and the switches from the source |
| 335 | // CommandLine are added to the target. |
| 336 | TEST(CommandLineTest, AppendArguments) { |
| 337 | CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program"))); |
| 338 | cl1.AppendSwitch("switch1"); |
| 339 | cl1.AppendSwitchASCII("switch2", "foo"); |
| 340 | |
| 341 | CommandLine cl2(CommandLine::NO_PROGRAM); |
| 342 | cl2.AppendArguments(cl1, true); |
| 343 | EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value()); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 344 | EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString()); |
[email protected] | e6124ad5 | 2010-11-15 04:17:52 | [diff] [blame] | 345 | |
| 346 | CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1"))); |
| 347 | c1.AppendSwitch("switch1"); |
| 348 | CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2"))); |
| 349 | c2.AppendSwitch("switch2"); |
| 350 | |
| 351 | c1.AppendArguments(c2, true); |
| 352 | EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value()); |
| 353 | EXPECT_TRUE(c1.HasSwitch("switch1")); |
| 354 | EXPECT_TRUE(c1.HasSwitch("switch2")); |
| 355 | } |
| 356 | |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 357 | #if defined(OS_WIN) |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 358 | // Make sure that the command line string program paths are quoted as necessary. |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 359 | // This only makes sense on Windows and the test is basically here to guard |
| 360 | // against regressions. |
| 361 | TEST(CommandLineTest, ProgramQuotes) { |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 362 | // Check that quotes are not added for paths without spaces. |
Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 363 | const FilePath kProgram(L"Program"); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 364 | CommandLine cl_program(kProgram); |
| 365 | EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value()); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 366 | EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 367 | |
Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 368 | const FilePath kProgramPath(L"Program Path"); |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 369 | |
| 370 | // Check that quotes are not returned from GetProgram(). |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 371 | CommandLine cl_program_path(kProgramPath); |
| 372 | EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value()); |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 373 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 374 | // Check that quotes are added to command line string paths containing spaces. |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 375 | CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString()); |
Jan Wilken Dörrie | da77fd43 | 2019-10-24 21:40:34 | [diff] [blame] | 376 | EXPECT_EQ(L"\"Program Path\"", cmd_string); |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 377 | } |
| 378 | #endif |
[email protected] | f96fe2c4 | 2011-07-13 18:03:34 | [diff] [blame] | 379 | |
| 380 | // Calling Init multiple times should not modify the previous CommandLine. |
| 381 | TEST(CommandLineTest, Init) { |
arihc | e89963ac | 2015-08-12 23:45:48 | [diff] [blame] | 382 | // Call Init without checking output once so we know it's been called |
| 383 | // whether or not the test runner does so. |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 384 | CommandLine::Init(0, nullptr); |
[email protected] | f96fe2c4 | 2011-07-13 18:03:34 | [diff] [blame] | 385 | CommandLine* initial = CommandLine::ForCurrentProcess(); |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 386 | EXPECT_FALSE(CommandLine::Init(0, nullptr)); |
[email protected] | f96fe2c4 | 2011-07-13 18:03:34 | [diff] [blame] | 387 | CommandLine* current = CommandLine::ForCurrentProcess(); |
| 388 | EXPECT_EQ(initial, current); |
| 389 | } |
pgal.u-szeged | 421dddb | 2014-11-25 12:55:02 | [diff] [blame] | 390 | |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 391 | // Test that copies of CommandLine have a valid StringPiece map. |
| 392 | TEST(CommandLineTest, Copy) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 393 | std::unique_ptr<CommandLine> initial( |
| 394 | new CommandLine(CommandLine::NO_PROGRAM)); |
jackhou | 1bd9da9 | 2015-05-21 04:48:00 | [diff] [blame] | 395 | initial->AppendSwitch("a"); |
| 396 | initial->AppendSwitch("bbbbbbbbbbbbbbb"); |
| 397 | initial->AppendSwitch("c"); |
| 398 | CommandLine copy_constructed(*initial); |
| 399 | CommandLine assigned = *initial; |
| 400 | CommandLine::SwitchMap switch_map = initial->GetSwitches(); |
| 401 | initial.reset(); |
| 402 | for (const auto& pair : switch_map) |
| 403 | EXPECT_TRUE(copy_constructed.HasSwitch(pair.first)); |
| 404 | for (const auto& pair : switch_map) |
| 405 | EXPECT_TRUE(assigned.HasSwitch(pair.first)); |
| 406 | } |
| 407 | |
skyostil | d851aa1 | 2017-03-29 17:38:35 | [diff] [blame] | 408 | TEST(CommandLineTest, PrependSimpleWrapper) { |
| 409 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
| 410 | cl.AppendSwitch("a"); |
| 411 | cl.AppendSwitch("b"); |
| 412 | cl.PrependWrapper(FILE_PATH_LITERAL("wrapper --foo --bar")); |
| 413 | |
| 414 | EXPECT_EQ(6u, cl.argv().size()); |
| 415 | EXPECT_EQ(FILE_PATH_LITERAL("wrapper"), cl.argv()[0]); |
| 416 | EXPECT_EQ(FILE_PATH_LITERAL("--foo"), cl.argv()[1]); |
| 417 | EXPECT_EQ(FILE_PATH_LITERAL("--bar"), cl.argv()[2]); |
| 418 | EXPECT_EQ(FILE_PATH_LITERAL("Program"), cl.argv()[3]); |
| 419 | EXPECT_EQ(FILE_PATH_LITERAL("--a"), cl.argv()[4]); |
| 420 | EXPECT_EQ(FILE_PATH_LITERAL("--b"), cl.argv()[5]); |
| 421 | } |
| 422 | |
| 423 | TEST(CommandLineTest, PrependComplexWrapper) { |
| 424 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
| 425 | cl.AppendSwitch("a"); |
| 426 | cl.AppendSwitch("b"); |
| 427 | cl.PrependWrapper( |
| 428 | FILE_PATH_LITERAL("wrapper --foo='hello world' --bar=\"let's go\"")); |
| 429 | |
| 430 | EXPECT_EQ(6u, cl.argv().size()); |
| 431 | EXPECT_EQ(FILE_PATH_LITERAL("wrapper"), cl.argv()[0]); |
| 432 | EXPECT_EQ(FILE_PATH_LITERAL("--foo='hello world'"), cl.argv()[1]); |
| 433 | EXPECT_EQ(FILE_PATH_LITERAL("--bar=\"let's go\""), cl.argv()[2]); |
| 434 | EXPECT_EQ(FILE_PATH_LITERAL("Program"), cl.argv()[3]); |
| 435 | EXPECT_EQ(FILE_PATH_LITERAL("--a"), cl.argv()[4]); |
| 436 | EXPECT_EQ(FILE_PATH_LITERAL("--b"), cl.argv()[5]); |
| 437 | } |
| 438 | |
Avi Drissman | 1aa6cb9 | 2019-01-23 15:58:38 | [diff] [blame] | 439 | TEST(CommandLineTest, RemoveSwitch) { |
Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 440 | const std::string switch1 = "switch1"; |
| 441 | const std::string switch2 = "switch2"; |
| 442 | const std::string value2 = "value"; |
Avi Drissman | 1aa6cb9 | 2019-01-23 15:58:38 | [diff] [blame] | 443 | |
| 444 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
| 445 | |
| 446 | cl.AppendSwitch(switch1); |
| 447 | cl.AppendSwitchASCII(switch2, value2); |
| 448 | |
| 449 | EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 450 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
| 451 | EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2)); |
Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 452 | EXPECT_THAT(cl.argv(), |
| 453 | testing::ElementsAre(FILE_PATH_LITERAL("Program"), |
| 454 | FILE_PATH_LITERAL("--switch1"), |
| 455 | FILE_PATH_LITERAL("--switch2=value"))); |
Avi Drissman | 1aa6cb9 | 2019-01-23 15:58:38 | [diff] [blame] | 456 | |
| 457 | cl.RemoveSwitch(switch1); |
| 458 | |
| 459 | EXPECT_FALSE(cl.HasSwitch(switch1)); |
| 460 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
| 461 | EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2)); |
Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 462 | EXPECT_THAT(cl.argv(), |
| 463 | testing::ElementsAre(FILE_PATH_LITERAL("Program"), |
| 464 | FILE_PATH_LITERAL("--switch2=value"))); |
| 465 | } |
| 466 | |
| 467 | TEST(CommandLineTest, RemoveSwitchWithValue) { |
| 468 | const std::string switch1 = "switch1"; |
| 469 | const std::string switch2 = "switch2"; |
| 470 | const std::string value2 = "value"; |
| 471 | |
| 472 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
| 473 | |
| 474 | cl.AppendSwitch(switch1); |
| 475 | cl.AppendSwitchASCII(switch2, value2); |
| 476 | |
| 477 | EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 478 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
| 479 | EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2)); |
| 480 | EXPECT_THAT(cl.argv(), |
| 481 | testing::ElementsAre(FILE_PATH_LITERAL("Program"), |
| 482 | FILE_PATH_LITERAL("--switch1"), |
| 483 | FILE_PATH_LITERAL("--switch2=value"))); |
| 484 | |
| 485 | cl.RemoveSwitch(switch2); |
| 486 | |
| 487 | EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 488 | EXPECT_FALSE(cl.HasSwitch(switch2)); |
| 489 | EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"), |
| 490 | FILE_PATH_LITERAL("--switch1"))); |
| 491 | } |
| 492 | |
Andrei Polushin | 2ec89bc | 2019-07-30 20:47:17 | [diff] [blame] | 493 | TEST(CommandLineTest, RemoveSwitchDropsMultipleSameSwitches) { |
| 494 | const std::string switch1 = "switch1"; |
| 495 | const std::string value2 = "value2"; |
| 496 | |
| 497 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
| 498 | |
| 499 | cl.AppendSwitch(switch1); |
| 500 | cl.AppendSwitchASCII(switch1, value2); |
| 501 | |
| 502 | EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 503 | EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch1)); |
| 504 | EXPECT_THAT(cl.argv(), |
| 505 | testing::ElementsAre(FILE_PATH_LITERAL("Program"), |
| 506 | FILE_PATH_LITERAL("--switch1"), |
| 507 | FILE_PATH_LITERAL("--switch1=value2"))); |
| 508 | |
| 509 | cl.RemoveSwitch(switch1); |
| 510 | |
| 511 | EXPECT_FALSE(cl.HasSwitch(switch1)); |
| 512 | EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"))); |
| 513 | } |
| 514 | |
Pavol Marko | bf16b81 | 2019-06-14 00:53:12 | [diff] [blame] | 515 | TEST(CommandLineTest, AppendAndRemoveSwitchWithDefaultPrefix) { |
| 516 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
| 517 | |
| 518 | cl.AppendSwitch("foo"); |
| 519 | EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"), |
| 520 | FILE_PATH_LITERAL("--foo"))); |
| 521 | EXPECT_EQ(0u, cl.GetArgs().size()); |
| 522 | |
| 523 | cl.RemoveSwitch("foo"); |
| 524 | EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"))); |
| 525 | EXPECT_EQ(0u, cl.GetArgs().size()); |
| 526 | } |
| 527 | |
| 528 | TEST(CommandLineTest, AppendAndRemoveSwitchWithAlternativePrefix) { |
| 529 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
| 530 | |
| 531 | cl.AppendSwitch("-foo"); |
| 532 | EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"), |
| 533 | FILE_PATH_LITERAL("-foo"))); |
| 534 | EXPECT_EQ(0u, cl.GetArgs().size()); |
| 535 | |
| 536 | cl.RemoveSwitch("foo"); |
| 537 | EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"))); |
| 538 | EXPECT_EQ(0u, cl.GetArgs().size()); |
| 539 | } |
| 540 | |
| 541 | TEST(CommandLineTest, AppendAndRemoveSwitchPreservesOtherSwitchesAndArgs) { |
| 542 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
| 543 | |
| 544 | cl.AppendSwitch("foo"); |
| 545 | cl.AppendSwitch("bar"); |
| 546 | cl.AppendArg("arg"); |
| 547 | EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"), |
| 548 | FILE_PATH_LITERAL("--foo"), |
| 549 | FILE_PATH_LITERAL("--bar"), |
| 550 | FILE_PATH_LITERAL("arg"))); |
| 551 | EXPECT_THAT(cl.GetArgs(), testing::ElementsAre(FILE_PATH_LITERAL("arg"))); |
| 552 | |
| 553 | cl.RemoveSwitch("foo"); |
| 554 | EXPECT_THAT(cl.argv(), testing::ElementsAre(FILE_PATH_LITERAL("Program"), |
| 555 | FILE_PATH_LITERAL("--bar"), |
| 556 | FILE_PATH_LITERAL("arg"))); |
| 557 | EXPECT_THAT(cl.GetArgs(), testing::ElementsAre(FILE_PATH_LITERAL("arg"))); |
Avi Drissman | 1aa6cb9 | 2019-01-23 15:58:38 | [diff] [blame] | 558 | } |
| 559 | |
John Rummell | b1d5fcb | 2019-04-27 01:13:33 | [diff] [blame] | 560 | TEST(CommandLineTest, MultipleSameSwitch) { |
| 561 | const CommandLine::CharType* argv[] = { |
| 562 | FILE_PATH_LITERAL("program"), |
| 563 | FILE_PATH_LITERAL("--foo=one"), // --foo first time |
| 564 | FILE_PATH_LITERAL("-baz"), |
| 565 | FILE_PATH_LITERAL("--foo=two") // --foo second time |
| 566 | }; |
| 567 | CommandLine cl(size(argv), argv); |
| 568 | |
| 569 | EXPECT_TRUE(cl.HasSwitch("foo")); |
| 570 | EXPECT_TRUE(cl.HasSwitch("baz")); |
| 571 | |
| 572 | EXPECT_EQ("two", cl.GetSwitchValueASCII("foo")); |
| 573 | } |
| 574 | |
Jesse McKenna | 036150c | 2020-07-17 21:11:17 | [diff] [blame] | 575 | #if defined(OS_WIN) |
| 576 | TEST(CommandLineTest, ParseAsSingleArgument) { |
| 577 | CommandLine cl = CommandLine::FromString( |
| 578 | FILE_PATH_LITERAL("program --switch_before arg_before " |
| 579 | "--single-argument arg with spaces \"and quotes\" \"")); |
| 580 | |
| 581 | EXPECT_FALSE(cl.GetCommandLineString().empty()); |
| 582 | EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")), cl.GetProgram()); |
| 583 | EXPECT_TRUE(cl.HasSwitch("switch_before")); |
| 584 | EXPECT_EQ(cl.GetArgs(), CommandLine::StringVector({FILE_PATH_LITERAL( |
| 585 | "arg with spaces \"and quotes\" \"")})); |
| 586 | |
| 587 | CommandLine cl_without_arg = |
| 588 | CommandLine::FromString(FILE_PATH_LITERAL("program --single-argument ")); |
| 589 | |
| 590 | EXPECT_FALSE(cl_without_arg.GetCommandLineString().empty()); |
| 591 | EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")), |
| 592 | cl_without_arg.GetProgram()); |
| 593 | EXPECT_TRUE(cl_without_arg.GetArgs().empty()); |
| 594 | } |
| 595 | #endif // defined(OS_WIN) |
| 596 | |
pgal.u-szeged | 421dddb | 2014-11-25 12:55:02 | [diff] [blame] | 597 | } // namespace base |