[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 | |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | |
[email protected] | 9d84e1a | 2010-11-15 00:18:30 | [diff] [blame] | 8 | #include "base/basictypes.h" |
[email protected] | e6124ad5 | 2010-11-15 04:17:52 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 10 | #include "base/file_path.h" |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 11 | #include "base/utf_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 14 | // To test Windows quoting behavior, we use a string that has some backslashes |
| 15 | // and quotes. |
| 16 | // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\" |
| 17 | // Here it is with C-style escapes. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 18 | static const CommandLine::StringType kTrickyQuoted = |
| 19 | FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\""); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 20 | // It should be parsed by Windows as: q"bs1\bs2\\bs3q\" |
| 21 | // Here that is with C-style escapes. |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 22 | static const CommandLine::StringType kTricky = |
| 23 | FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\""); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 24 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 25 | TEST(CommandLineTest, CommandLineConstructor) { |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 26 | const CommandLine::CharType* argv[] = { |
| 27 | FILE_PATH_LITERAL("program"), |
| 28 | FILE_PATH_LITERAL("--foo="), |
| 29 | FILE_PATH_LITERAL("-bAr"), |
| 30 | FILE_PATH_LITERAL("-spaetzel=pierogi"), |
| 31 | FILE_PATH_LITERAL("-baz"), |
| 32 | FILE_PATH_LITERAL("flim"), |
| 33 | FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"), |
| 34 | FILE_PATH_LITERAL("-spaetzle=Crepe"), |
| 35 | FILE_PATH_LITERAL("-=loosevalue"), |
[email protected] | 21e342f | 2012-10-19 06:19:59 | [diff] [blame] | 36 | FILE_PATH_LITERAL("-"), |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 37 | FILE_PATH_LITERAL("FLAN"), |
[email protected] | 1fa39f0 | 2011-09-13 15:45:34 | [diff] [blame] | 38 | FILE_PATH_LITERAL("a"), |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 39 | FILE_PATH_LITERAL("--input-translation=45--output-rotation"), |
| 40 | FILE_PATH_LITERAL("--"), |
| 41 | FILE_PATH_LITERAL("--"), |
| 42 | FILE_PATH_LITERAL("--not-a-switch"), |
| 43 | FILE_PATH_LITERAL("\"in the time of submarines...\""), |
| 44 | FILE_PATH_LITERAL("unquoted arg-with-space")}; |
| 45 | CommandLine cl(arraysize(argv), argv); |
| 46 | |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 47 | EXPECT_FALSE(cl.GetCommandLineString().empty()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 48 | EXPECT_FALSE(cl.HasSwitch("cruller")); |
| 49 | EXPECT_FALSE(cl.HasSwitch("flim")); |
| 50 | EXPECT_FALSE(cl.HasSwitch("program")); |
| 51 | EXPECT_FALSE(cl.HasSwitch("dog")); |
| 52 | EXPECT_FALSE(cl.HasSwitch("cat")); |
| 53 | EXPECT_FALSE(cl.HasSwitch("output-rotation")); |
| 54 | EXPECT_FALSE(cl.HasSwitch("not-a-switch")); |
| 55 | EXPECT_FALSE(cl.HasSwitch("--")); |
| 56 | |
| 57 | EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), |
| 58 | cl.GetProgram().value()); |
| 59 | |
| 60 | EXPECT_TRUE(cl.HasSwitch("foo")); |
| 61 | EXPECT_TRUE(cl.HasSwitch("bAr")); |
| 62 | EXPECT_TRUE(cl.HasSwitch("baz")); |
| 63 | EXPECT_TRUE(cl.HasSwitch("spaetzle")); |
| 64 | #if defined(OS_WIN) |
| 65 | EXPECT_TRUE(cl.HasSwitch("SPAETZLE")); |
| 66 | #endif |
| 67 | EXPECT_TRUE(cl.HasSwitch("other-switches")); |
| 68 | EXPECT_TRUE(cl.HasSwitch("input-translation")); |
| 69 | |
| 70 | EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); |
| 71 | EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); |
| 72 | EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); |
| 73 | EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); |
| 74 | EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( |
| 75 | "other-switches")); |
| 76 | EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); |
| 77 | |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 78 | const CommandLine::StringVector& args = cl.GetArgs(); |
[email protected] | 21e342f | 2012-10-19 06:19:59 | [diff] [blame] | 79 | ASSERT_EQ(8U, args.size()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 80 | |
| 81 | std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); |
| 82 | EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); |
| 83 | ++iter; |
[email protected] | 21e342f | 2012-10-19 06:19:59 | [diff] [blame] | 84 | EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter); |
| 85 | ++iter; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 86 | EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter); |
| 87 | ++iter; |
[email protected] | 1fa39f0 | 2011-09-13 15:45:34 | [diff] [blame] | 88 | EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter); |
| 89 | ++iter; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 90 | EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); |
| 91 | ++iter; |
| 92 | EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); |
| 93 | ++iter; |
| 94 | EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter); |
| 95 | ++iter; |
| 96 | EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter); |
| 97 | ++iter; |
| 98 | EXPECT_TRUE(iter == args.end()); |
| 99 | } |
| 100 | |
| 101 | TEST(CommandLineTest, CommandLineFromString) { |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 102 | #if defined(OS_WIN) |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 103 | CommandLine cl = CommandLine::FromString( |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 104 | L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
| 105 | L"--other-switches=\"--dog=canine --cat=feline\" " |
| 106 | L"-spaetzle=Crepe -=loosevalue FLAN " |
| 107 | L"--input-translation=\"45\"--output-rotation " |
| 108 | L"--quotes=" + kTrickyQuoted + L" " |
| 109 | L"-- -- --not-a-switch " |
| 110 | L"\"in the time of submarines...\""); |
| 111 | |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 112 | EXPECT_FALSE(cl.GetCommandLineString().empty()); |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 113 | EXPECT_FALSE(cl.HasSwitch("cruller")); |
| 114 | EXPECT_FALSE(cl.HasSwitch("flim")); |
| 115 | EXPECT_FALSE(cl.HasSwitch("program")); |
| 116 | EXPECT_FALSE(cl.HasSwitch("dog")); |
| 117 | EXPECT_FALSE(cl.HasSwitch("cat")); |
| 118 | EXPECT_FALSE(cl.HasSwitch("output-rotation")); |
| 119 | EXPECT_FALSE(cl.HasSwitch("not-a-switch")); |
| 120 | EXPECT_FALSE(cl.HasSwitch("--")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 121 | |
[email protected] | 78c4c42 | 2010-10-08 00:06:31 | [diff] [blame] | 122 | EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), |
| 123 | cl.GetProgram().value()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 124 | |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 125 | EXPECT_TRUE(cl.HasSwitch("foo")); |
| 126 | EXPECT_TRUE(cl.HasSwitch("bar")); |
| 127 | EXPECT_TRUE(cl.HasSwitch("baz")); |
| 128 | EXPECT_TRUE(cl.HasSwitch("spaetzle")); |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 129 | EXPECT_TRUE(cl.HasSwitch("SPAETZLE")); |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 130 | EXPECT_TRUE(cl.HasSwitch("other-switches")); |
| 131 | EXPECT_TRUE(cl.HasSwitch("input-translation")); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 132 | EXPECT_TRUE(cl.HasSwitch("quotes")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 133 | |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 134 | EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); |
| 135 | EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); |
| 136 | EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); |
| 137 | EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); |
| 138 | EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( |
| 139 | "other-switches")); |
| 140 | EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 141 | EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 142 | |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 143 | const CommandLine::StringVector& args = cl.GetArgs(); |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 144 | ASSERT_EQ(5U, args.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 145 | |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 146 | std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); |
| 147 | EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 148 | ++iter; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 149 | EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 150 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 151 | EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 152 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 153 | EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 154 | ++iter; |
[email protected] | 2e4c50c | 2010-07-21 15:57:23 | [diff] [blame] | 155 | EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *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_TRUE(iter == args.end()); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 158 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 159 | // Check that a generated string produces an equivalent command line. |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 160 | CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString()); |
| 161 | EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString()); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 162 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 163 | } |
| 164 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 165 | // Tests behavior with an empty input string. |
| 166 | TEST(CommandLineTest, EmptyString) { |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 167 | #if defined(OS_WIN) |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 168 | CommandLine cl_from_string = CommandLine::FromString(L""); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 169 | EXPECT_TRUE(cl_from_string.GetCommandLineString().empty()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 170 | EXPECT_TRUE(cl_from_string.GetProgram().empty()); |
| 171 | EXPECT_EQ(1U, cl_from_string.argv().size()); |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 172 | EXPECT_TRUE(cl_from_string.GetArgs().empty()); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 173 | #endif |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 174 | CommandLine cl_from_argv(0, NULL); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 175 | EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 176 | EXPECT_TRUE(cl_from_argv.GetProgram().empty()); |
| 177 | EXPECT_EQ(1U, cl_from_argv.argv().size()); |
[email protected] | 75f1c78 | 2011-07-13 23:41:22 | [diff] [blame] | 178 | EXPECT_TRUE(cl_from_argv.GetArgs().empty()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 181 | // Test methods for appending switches to a command line. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 182 | TEST(CommandLineTest, AppendSwitches) { |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 183 | std::string switch1 = "switch1"; |
| 184 | std::string switch2 = "switch2"; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 185 | std::string value2 = "value"; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 186 | std::string switch3 = "switch3"; |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 187 | std::string value3 = "a value with spaces"; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 188 | std::string switch4 = "switch4"; |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 189 | std::string value4 = "\"a value with quotes\""; |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 190 | std::string switch5 = "quotes"; |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 191 | CommandLine::StringType value5 = kTricky; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 192 | |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 193 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 194 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 195 | cl.AppendSwitch(switch1); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 196 | cl.AppendSwitchASCII(switch2, value2); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 197 | cl.AppendSwitchASCII(switch3, value3); |
| 198 | cl.AppendSwitchASCII(switch4, value4); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 199 | cl.AppendSwitchNative(switch5, value5); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 200 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 201 | EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 202 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 203 | EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 204 | EXPECT_TRUE(cl.HasSwitch(switch3)); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 205 | EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); |
[email protected] | 8c9510d | 2008-10-10 21:38:20 | [diff] [blame] | 206 | EXPECT_TRUE(cl.HasSwitch(switch4)); |
[email protected] | 05076ba2 | 2010-07-30 05:59:57 | [diff] [blame] | 207 | EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 208 | EXPECT_TRUE(cl.HasSwitch(switch5)); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 209 | EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5)); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 210 | |
| 211 | #if defined(OS_WIN) |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 212 | EXPECT_EQ(L"Program " |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 213 | L"--switch1 " |
| 214 | L"--switch2=value " |
| 215 | L"--switch3=\"a value with spaces\" " |
| 216 | L"--switch4=\"\\\"a value with quotes\\\"\" " |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 217 | L"--quotes=\"" + kTrickyQuoted + L"\"", |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 218 | cl.GetCommandLineString()); |
[email protected] | 98a1c268 | 2010-08-10 18:14:19 | [diff] [blame] | 219 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 220 | } |
[email protected] | e6124ad5 | 2010-11-15 04:17:52 | [diff] [blame] | 221 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 222 | TEST(CommandLineTest, AppendSwitchesDashDash) { |
| 223 | const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"), |
| 224 | FILE_PATH_LITERAL("--"), |
| 225 | FILE_PATH_LITERAL("--arg1") }; |
| 226 | CommandLine cl(arraysize(raw_argv), raw_argv); |
| 227 | |
| 228 | cl.AppendSwitch("switch1"); |
| 229 | cl.AppendSwitchASCII("switch2", "foo"); |
| 230 | |
| 231 | cl.AppendArg("--arg2"); |
| 232 | |
| 233 | EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"), |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 234 | cl.GetCommandLineString()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 235 | CommandLine::StringVector cl_argv = cl.argv(); |
| 236 | EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]); |
| 237 | EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]); |
| 238 | EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]); |
| 239 | EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]); |
| 240 | EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]); |
| 241 | EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]); |
| 242 | } |
| 243 | |
[email protected] | e6124ad5 | 2010-11-15 04:17:52 | [diff] [blame] | 244 | // Tests that when AppendArguments is called that the program is set correctly |
| 245 | // on the target CommandLine object and the switches from the source |
| 246 | // CommandLine are added to the target. |
| 247 | TEST(CommandLineTest, AppendArguments) { |
| 248 | CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program"))); |
| 249 | cl1.AppendSwitch("switch1"); |
| 250 | cl1.AppendSwitchASCII("switch2", "foo"); |
| 251 | |
| 252 | CommandLine cl2(CommandLine::NO_PROGRAM); |
| 253 | cl2.AppendArguments(cl1, true); |
| 254 | EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value()); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 255 | EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString()); |
[email protected] | e6124ad5 | 2010-11-15 04:17:52 | [diff] [blame] | 256 | |
| 257 | CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1"))); |
| 258 | c1.AppendSwitch("switch1"); |
| 259 | CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2"))); |
| 260 | c2.AppendSwitch("switch2"); |
| 261 | |
| 262 | c1.AppendArguments(c2, true); |
| 263 | EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value()); |
| 264 | EXPECT_TRUE(c1.HasSwitch("switch1")); |
| 265 | EXPECT_TRUE(c1.HasSwitch("switch2")); |
| 266 | } |
| 267 | |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 268 | #if defined(OS_WIN) |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 269 | // Make sure that the command line string program paths are quoted as necessary. |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 270 | // This only makes sense on Windows and the test is basically here to guard |
| 271 | // against regressions. |
| 272 | TEST(CommandLineTest, ProgramQuotes) { |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 273 | // Check that quotes are not added for paths without spaces. |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 274 | const FilePath kProgram(L"Program"); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 275 | CommandLine cl_program(kProgram); |
| 276 | EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value()); |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 277 | EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 278 | |
| 279 | const FilePath kProgramPath(L"Program Path"); |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 280 | |
| 281 | // Check that quotes are not returned from GetProgram(). |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 282 | CommandLine cl_program_path(kProgramPath); |
| 283 | EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value()); |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 284 | |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 285 | // Check that quotes are added to command line string paths containing spaces. |
[email protected] | 61a4c6f | 2011-07-20 04:54:52 | [diff] [blame] | 286 | CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString()); |
[email protected] | a40ca430 | 2011-05-14 01:10:24 | [diff] [blame] | 287 | CommandLine::StringType program_string(cl_program_path.GetProgram().value()); |
| 288 | EXPECT_EQ('"', cmd_string[0]); |
| 289 | EXPECT_EQ(program_string, cmd_string.substr(1, program_string.length())); |
| 290 | EXPECT_EQ('"', cmd_string[program_string.length() + 1]); |
[email protected] | 450b34ec | 2010-11-29 21:12:22 | [diff] [blame] | 291 | } |
| 292 | #endif |
[email protected] | f96fe2c4 | 2011-07-13 18:03:34 | [diff] [blame] | 293 | |
| 294 | // Calling Init multiple times should not modify the previous CommandLine. |
| 295 | TEST(CommandLineTest, Init) { |
| 296 | CommandLine* initial = CommandLine::ForCurrentProcess(); |
[email protected] | 72e2e242 | 2012-02-27 18:38:12 | [diff] [blame] | 297 | EXPECT_FALSE(CommandLine::Init(0, NULL)); |
[email protected] | f96fe2c4 | 2011-07-13 18:03:34 | [diff] [blame] | 298 | CommandLine* current = CommandLine::ForCurrentProcess(); |
| 299 | EXPECT_EQ(initial, current); |
| 300 | } |