Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 // To test Windows quoting behavior, we use a string that has some backslashes | 14 // To test Windows quoting behavior, we use a string that has some backslashes |
| 15 // and quotes. | 15 // and quotes. |
| 16 // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\" | 16 // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\" |
| 17 // Here it is with C-style escapes. | 17 // Here it is with C-style escapes. |
| 18 #define TRICKY_QUOTED L"q\\\"bs1\\bs2\\\\bs3q\\\\\\\"" | 18 static const CommandLine::StringType kTrickyQuoted = |
| 19 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\""); | |
| 19 // It should be parsed by Windows as: q"bs1\bs2\\bs3q\" | 20 // It should be parsed by Windows as: q"bs1\bs2\\bs3q\" |
| 20 // Here that is with C-style escapes. | 21 // Here that is with C-style escapes. |
| 21 #define TRICKY L"q\"bs1\\bs2\\\\bs3q\\\"" | 22 static const CommandLine::StringType kTricky = |
| 23 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\""); | |
| 22 | 24 |
| 23 TEST(CommandLineTest, CommandLineConstructor) { | 25 TEST(CommandLineTest, CommandLineConstructor) { |
| 24 #if defined(OS_WIN) | 26 const CommandLine::CharType* argv[] = { |
| 25 CommandLine cl = CommandLine::FromString( | 27 FILE_PATH_LITERAL("program"), |
| 26 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " | 28 FILE_PATH_LITERAL("--foo="), |
| 27 L"--other-switches=\"--dog=canine --cat=feline\" " | 29 FILE_PATH_LITERAL("-bAr"), |
| 28 L"-spaetzle=Crepe -=loosevalue flan " | 30 FILE_PATH_LITERAL("-spaetzel=pierogi"), |
| 29 L"--input-translation=\"45\"--output-rotation " | 31 FILE_PATH_LITERAL("-baz"), |
| 30 L"--quotes=" TRICKY_QUOTED L" " | 32 FILE_PATH_LITERAL("flim"), |
| 31 L"-- -- --not-a-switch " | 33 FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"), |
| 32 L"\"in the time of submarines...\""); | 34 FILE_PATH_LITERAL("-spaetzle=Crepe"), |
| 35 FILE_PATH_LITERAL("-=loosevalue"), | |
| 36 FILE_PATH_LITERAL("FLAN"), | |
| 37 FILE_PATH_LITERAL("--input-translation=45--output-rotation"), | |
| 38 FILE_PATH_LITERAL("--"), | |
| 39 FILE_PATH_LITERAL("--"), | |
| 40 FILE_PATH_LITERAL("--not-a-switch"), | |
| 41 FILE_PATH_LITERAL("\"in the time of submarines...\""), | |
| 42 FILE_PATH_LITERAL("unquoted arg-with-space")}; | |
| 43 CommandLine cl(arraysize(argv), argv); | |
| 44 | |
| 33 EXPECT_FALSE(cl.command_line_string().empty()); | 45 EXPECT_FALSE(cl.command_line_string().empty()); |
| 34 #elif defined(OS_POSIX) | |
| 35 const char* argv[] = {"program", "--foo=", "-bar", | |
| 36 "-spaetzel=pierogi", "-baz", "flim", | |
| 37 "--other-switches=--dog=canine --cat=feline", | |
| 38 "-spaetzle=Crepe", "-=loosevalue", "flan", | |
| 39 "--input-translation=45--output-rotation", | |
| 40 "--", "--", "--not-a-switch", | |
| 41 "in the time of submarines..."}; | |
| 42 CommandLine cl(arraysize(argv), argv); | |
| 43 #endif | |
| 44 EXPECT_FALSE(cl.HasSwitch("cruller")); | 46 EXPECT_FALSE(cl.HasSwitch("cruller")); |
| 45 EXPECT_FALSE(cl.HasSwitch("flim")); | 47 EXPECT_FALSE(cl.HasSwitch("flim")); |
| 46 EXPECT_FALSE(cl.HasSwitch("program")); | 48 EXPECT_FALSE(cl.HasSwitch("program")); |
| 49 EXPECT_FALSE(cl.HasSwitch("dog")); | |
| 50 EXPECT_FALSE(cl.HasSwitch("cat")); | |
| 51 EXPECT_FALSE(cl.HasSwitch("output-rotation")); | |
| 52 EXPECT_FALSE(cl.HasSwitch("not-a-switch")); | |
| 53 EXPECT_FALSE(cl.HasSwitch("--")); | |
| 54 | |
| 55 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), | |
| 56 cl.GetProgram().value()); | |
| 57 | |
| 58 EXPECT_TRUE(cl.HasSwitch("foo")); | |
| 59 EXPECT_TRUE(cl.HasSwitch("bAr")); | |
| 60 EXPECT_TRUE(cl.HasSwitch("baz")); | |
| 61 EXPECT_TRUE(cl.HasSwitch("spaetzle")); | |
| 62 EXPECT_TRUE(cl.HasSwitch("other-switches")); | |
| 63 EXPECT_TRUE(cl.HasSwitch("input-translation")); | |
| 64 | |
| 65 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); | |
| 66 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); | |
| 67 EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); | |
| 68 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); | |
| 69 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( | |
| 70 "other-switches")); | |
| 71 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); | |
| 72 | |
| 73 const std::vector<CommandLine::StringType>& args = cl.args(); | |
| 74 ASSERT_EQ(6U, args.size()); | |
| 75 | |
| 76 std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); | |
| 77 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *(iter++)); | |
| 78 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *(iter++)); | |
| 79 EXPECT_EQ(FILE_PATH_LITERAL("--"), *(iter++)); | |
| 80 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *(iter++)); | |
| 81 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *(iter++)); | |
| 82 EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *(iter++)); | |
| 83 EXPECT_TRUE(iter == args.end()); | |
| 84 } | |
| 85 | |
| 86 TEST(CommandLineTest, CommandLineFromString) { | |
| 87 #if defined(OS_WIN) | |
| 88 CommandLine cl = CommandLine::FromString( | |
| 89 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " | |
| 90 L"--other-switches=\"--dog=canine --cat=feline\" " | |
| 91 L"-spaetzle=Crepe -=loosevalue FLAN " | |
| 92 L"--input-translation=\"45\"--output-rotation " | |
| 93 L"--quotes=" + kTrickyQuoted + L" " | |
| 94 L"-- -- --not-a-switch " | |
| 95 L"\"in the time of submarines...\""); | |
| 96 | |
| 97 EXPECT_FALSE(cl.command_line_string().empty()); | |
| 98 EXPECT_FALSE(cl.HasSwitch("cruller")); | |
| 99 EXPECT_FALSE(cl.HasSwitch("flim")); | |
| 100 EXPECT_FALSE(cl.HasSwitch("program")); | |
| 47 EXPECT_FALSE(cl.HasSwitch("dog")); | 101 EXPECT_FALSE(cl.HasSwitch("dog")); |
| 48 EXPECT_FALSE(cl.HasSwitch("cat")); | 102 EXPECT_FALSE(cl.HasSwitch("cat")); |
| 49 EXPECT_FALSE(cl.HasSwitch("output-rotation")); | 103 EXPECT_FALSE(cl.HasSwitch("output-rotation")); |
| 50 EXPECT_FALSE(cl.HasSwitch("not-a-switch")); | 104 EXPECT_FALSE(cl.HasSwitch("not-a-switch")); |
| 51 EXPECT_FALSE(cl.HasSwitch("--")); | 105 EXPECT_FALSE(cl.HasSwitch("--")); |
| 52 | 106 |
| 53 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), | 107 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), |
| 54 cl.GetProgram().value()); | 108 cl.GetProgram().value()); |
| 55 | 109 |
| 56 EXPECT_TRUE(cl.HasSwitch("foo")); | 110 EXPECT_TRUE(cl.HasSwitch("foo")); |
| 57 EXPECT_TRUE(cl.HasSwitch("bar")); | 111 EXPECT_TRUE(cl.HasSwitch("bar")); |
| 58 EXPECT_TRUE(cl.HasSwitch("baz")); | 112 EXPECT_TRUE(cl.HasSwitch("baz")); |
| 59 EXPECT_TRUE(cl.HasSwitch("spaetzle")); | 113 EXPECT_TRUE(cl.HasSwitch("spaetzle")); |
| 60 #if defined(OS_WIN) | |
| 61 EXPECT_TRUE(cl.HasSwitch("SPAETZLE")); | 114 EXPECT_TRUE(cl.HasSwitch("SPAETZLE")); |
| 62 #endif | |
|
Evan Martin
2011/05/10 23:48:32
Isn't this false on non-Windows? It's testing cas
msw
2011/05/11 02:28:12
Sorry for the confusing diff, but this code is in
| |
| 63 EXPECT_TRUE(cl.HasSwitch("other-switches")); | 115 EXPECT_TRUE(cl.HasSwitch("other-switches")); |
| 64 EXPECT_TRUE(cl.HasSwitch("input-translation")); | 116 EXPECT_TRUE(cl.HasSwitch("input-translation")); |
| 65 #if defined(OS_WIN) | |
| 66 EXPECT_TRUE(cl.HasSwitch("quotes")); | 117 EXPECT_TRUE(cl.HasSwitch("quotes")); |
| 67 #endif | |
| 68 | 118 |
| 69 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); | 119 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); |
| 70 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); | 120 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); |
| 71 EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); | 121 EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); |
| 72 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); | 122 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); |
| 73 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( | 123 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( |
| 74 "other-switches")); | 124 "other-switches")); |
| 75 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); | 125 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); |
| 76 #if defined(OS_WIN) | 126 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes")); |
|
Evan Martin
2011/05/10 23:48:32
This shouldn't pass on non-Windows, as there is no
msw
2011/05/11 02:28:12
Same as above (OS_WIN-only test).
| |
| 77 EXPECT_EQ(TRICKY, cl.GetSwitchValueNative("quotes")); | |
| 78 #endif | |
| 79 | 127 |
| 80 const std::vector<CommandLine::StringType>& args = cl.args(); | 128 const std::vector<CommandLine::StringType>& args = cl.args(); |
| 81 ASSERT_EQ(5U, args.size()); | 129 ASSERT_EQ(5U, args.size()); |
| 82 | 130 |
| 83 std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); | 131 std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); |
| 84 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); | 132 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *(iter++)); |
|
Evan Martin
2011/05/10 23:48:32
I'm a little concerned that the EXPECT_EQ macro mi
msw
2011/05/11 02:28:12
Done (reverted here and below). Scott Meyers' "Eff
| |
| 85 ++iter; | 133 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *(iter++)); |
| 86 EXPECT_EQ(FILE_PATH_LITERAL("flan"), *iter); | 134 EXPECT_EQ(FILE_PATH_LITERAL("--"), *(iter++)); |
| 87 ++iter; | 135 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *(iter++)); |
| 88 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); | 136 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *(iter++)); |
| 89 ++iter; | |
| 90 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); | |
| 91 ++iter; | |
| 92 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter); | |
| 93 ++iter; | |
| 94 EXPECT_TRUE(iter == args.end()); | 137 EXPECT_TRUE(iter == args.end()); |
| 95 #if defined(OS_POSIX) | |
| 96 const std::vector<std::string>& argvec = cl.argv(); | |
| 97 | 138 |
| 98 for (size_t i = 0; i < argvec.size(); i++) { | 139 // Check that a generated string produces an equivalent command line. |
| 99 EXPECT_EQ(0, argvec[i].compare(argv[i])); | 140 CommandLine cl_duplicate = CommandLine::FromString(cl.command_line_string()); |
| 100 } | 141 EXPECT_EQ(cl.command_line_string(), cl_duplicate.command_line_string()); |
| 101 #endif | 142 #endif |
| 102 } | 143 } |
| 103 | 144 |
| 104 // Tests behavior with an empty input string. | 145 // Tests behavior with an empty input string. |
| 105 TEST(CommandLineTest, EmptyString) { | 146 TEST(CommandLineTest, EmptyString) { |
| 106 #if defined(OS_WIN) | 147 #if defined(OS_WIN) |
| 107 CommandLine cl = CommandLine::FromString(L""); | 148 CommandLine cl_from_string = CommandLine::FromString(L""); |
| 108 EXPECT_TRUE(cl.command_line_string().empty()); | 149 EXPECT_TRUE(cl_from_string.command_line_string().empty()); |
| 109 EXPECT_TRUE(cl.GetProgram().empty()); | 150 EXPECT_TRUE(cl_from_string.GetProgram().empty()); |
| 110 #elif defined(OS_POSIX) | 151 EXPECT_EQ(1U, cl_from_string.argv().size()); |
| 111 CommandLine cl(0, NULL); | 152 EXPECT_TRUE(cl_from_string.args().empty()); |
| 112 EXPECT_TRUE(cl.argv().empty()); | |
| 113 #endif | 153 #endif |
| 114 EXPECT_TRUE(cl.args().empty()); | 154 CommandLine cl_from_argv(0, NULL); |
| 155 EXPECT_TRUE(cl_from_argv.command_line_string().empty()); | |
| 156 EXPECT_TRUE(cl_from_argv.GetProgram().empty()); | |
| 157 EXPECT_EQ(1U, cl_from_argv.argv().size()); | |
| 158 EXPECT_TRUE(cl_from_argv.args().empty()); | |
| 115 } | 159 } |
| 116 | 160 |
| 117 // Test methods for appending switches to a command line. | 161 // Test methods for appending switches to a command line. |
| 118 TEST(CommandLineTest, AppendSwitches) { | 162 TEST(CommandLineTest, AppendSwitches) { |
| 119 std::string switch1 = "switch1"; | 163 std::string switch1 = "switch1"; |
| 120 std::string switch2 = "switch2"; | 164 std::string switch2 = "switch2"; |
| 121 std::string value = "value"; | 165 std::string value2 = "value"; |
| 122 std::string switch3 = "switch3"; | 166 std::string switch3 = "switch3"; |
| 123 std::string value3 = "a value with spaces"; | 167 std::string value3 = "a value with spaces"; |
| 124 std::string switch4 = "switch4"; | 168 std::string switch4 = "switch4"; |
| 125 std::string value4 = "\"a value with quotes\""; | 169 std::string value4 = "\"a value with quotes\""; |
| 126 std::string switch5 = "quotes"; | 170 std::string switch5 = "quotes"; |
| 127 std::string value5 = WideToUTF8(TRICKY); | 171 CommandLine::StringType value5 = kTricky; |
| 128 | 172 |
| 129 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); | 173 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
| 130 | 174 |
| 131 cl.AppendSwitch(switch1); | 175 cl.AppendSwitch(switch1); |
| 132 cl.AppendSwitchASCII(switch2, value); | 176 cl.AppendSwitchASCII(switch2, value2); |
| 133 cl.AppendSwitchASCII(switch3, value3); | 177 cl.AppendSwitchASCII(switch3, value3); |
| 134 cl.AppendSwitchASCII(switch4, value4); | 178 cl.AppendSwitchASCII(switch4, value4); |
| 135 cl.AppendSwitchASCII(switch5, value5); | 179 cl.AppendSwitchNative(switch5, value5); |
| 136 | 180 |
| 137 EXPECT_TRUE(cl.HasSwitch(switch1)); | 181 EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 138 EXPECT_TRUE(cl.HasSwitch(switch2)); | 182 EXPECT_TRUE(cl.HasSwitch(switch2)); |
| 139 EXPECT_EQ(value, cl.GetSwitchValueASCII(switch2)); | 183 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2)); |
| 140 EXPECT_TRUE(cl.HasSwitch(switch3)); | 184 EXPECT_TRUE(cl.HasSwitch(switch3)); |
| 141 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); | 185 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); |
| 142 EXPECT_TRUE(cl.HasSwitch(switch4)); | 186 EXPECT_TRUE(cl.HasSwitch(switch4)); |
| 143 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); | 187 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); |
| 144 EXPECT_TRUE(cl.HasSwitch(switch5)); | 188 EXPECT_TRUE(cl.HasSwitch(switch5)); |
| 145 EXPECT_EQ(value5, cl.GetSwitchValueASCII(switch5)); | 189 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5)); |
| 146 | 190 |
| 147 #if defined(OS_WIN) | 191 #if defined(OS_WIN) |
| 148 EXPECT_EQ(L"\"Program\" " | 192 EXPECT_EQ(L"Program " |
| 149 L"--switch1 " | 193 L"--switch1 " |
| 150 L"--switch2=value " | 194 L"--switch2=value " |
| 151 L"--switch3=\"a value with spaces\" " | 195 L"--switch3=\"a value with spaces\" " |
| 152 L"--switch4=\"\\\"a value with quotes\\\"\" " | 196 L"--switch4=\"\\\"a value with quotes\\\"\" " |
| 153 L"--quotes=\"" TRICKY_QUOTED L"\"", | 197 L"--quotes=\"" + kTrickyQuoted + L"\"", |
| 154 cl.command_line_string()); | 198 cl.command_line_string()); |
| 155 #endif | 199 #endif |
| 156 } | 200 } |
| 157 | 201 |
| 202 TEST(CommandLineTest, AppendSwitchesDashDash) { | |
| 203 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"), | |
| 204 FILE_PATH_LITERAL("--"), | |
| 205 FILE_PATH_LITERAL("--arg1") }; | |
| 206 CommandLine cl(arraysize(raw_argv), raw_argv); | |
| 207 | |
| 208 cl.AppendSwitch("switch1"); | |
| 209 cl.AppendSwitchASCII("switch2", "foo"); | |
| 210 | |
| 211 cl.AppendArg("--arg2"); | |
| 212 | |
| 213 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"), | |
| 214 cl.command_line_string()); | |
| 215 CommandLine::StringVector cl_argv = cl.argv(); | |
| 216 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]); | |
| 217 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]); | |
| 218 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]); | |
| 219 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]); | |
| 220 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]); | |
| 221 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]); | |
| 222 } | |
| 223 | |
| 158 // Tests that when AppendArguments is called that the program is set correctly | 224 // Tests that when AppendArguments is called that the program is set correctly |
| 159 // on the target CommandLine object and the switches from the source | 225 // on the target CommandLine object and the switches from the source |
| 160 // CommandLine are added to the target. | 226 // CommandLine are added to the target. |
| 161 TEST(CommandLineTest, AppendArguments) { | 227 TEST(CommandLineTest, AppendArguments) { |
| 162 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program"))); | 228 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program"))); |
| 163 cl1.AppendSwitch("switch1"); | 229 cl1.AppendSwitch("switch1"); |
| 164 cl1.AppendSwitchASCII("switch2", "foo"); | 230 cl1.AppendSwitchASCII("switch2", "foo"); |
| 165 | 231 |
| 166 CommandLine cl2(CommandLine::NO_PROGRAM); | 232 CommandLine cl2(CommandLine::NO_PROGRAM); |
| 167 cl2.AppendArguments(cl1, true); | 233 cl2.AppendArguments(cl1, true); |
| 168 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value()); | 234 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value()); |
| 169 EXPECT_EQ(cl1.command_line_string(), cl2.command_line_string()); | 235 EXPECT_EQ(cl1.command_line_string(), cl2.command_line_string()); |
| 170 | 236 |
| 171 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1"))); | 237 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1"))); |
| 172 c1.AppendSwitch("switch1"); | 238 c1.AppendSwitch("switch1"); |
| 173 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2"))); | 239 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2"))); |
| 174 c2.AppendSwitch("switch2"); | 240 c2.AppendSwitch("switch2"); |
| 175 | 241 |
| 176 c1.AppendArguments(c2, true); | 242 c1.AppendArguments(c2, true); |
| 177 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value()); | 243 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value()); |
| 178 EXPECT_TRUE(c1.HasSwitch("switch1")); | 244 EXPECT_TRUE(c1.HasSwitch("switch1")); |
| 179 EXPECT_TRUE(c1.HasSwitch("switch2")); | 245 EXPECT_TRUE(c1.HasSwitch("switch2")); |
| 180 } | 246 } |
| 181 | 247 |
| 182 #if defined(OS_WIN) | 248 #if defined(OS_WIN) |
| 183 // Make sure that the program part of a command line is always quoted. | 249 // Make sure that program paths of command_line_string are quoted as necessary. |
| 184 // This only makes sense on Windows and the test is basically here to guard | 250 // This only makes sense on Windows and the test is basically here to guard |
| 185 // against regressions. | 251 // against regressions. |
| 186 TEST(CommandLineTest, ProgramQuotes) { | 252 TEST(CommandLineTest, ProgramQuotes) { |
| 253 // Check that quotes are not added for paths without spaces. | |
| 187 const FilePath kProgram(L"Program"); | 254 const FilePath kProgram(L"Program"); |
| 255 CommandLine cl_program(kProgram); | |
| 256 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value()); | |
| 257 EXPECT_EQ(kProgram.value(), cl_program.command_line_string()); | |
| 258 | |
| 259 const FilePath kProgramPath(L"Program Path"); | |
| 188 | 260 |
| 189 // Check that quotes are not returned from GetProgram(). | 261 // Check that quotes are not returned from GetProgram(). |
| 190 CommandLine cl(kProgram); | 262 CommandLine cl_program_path(kProgramPath); |
| 191 EXPECT_EQ(kProgram.value(), cl.GetProgram().value()); | 263 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value()); |
| 192 | 264 |
| 193 // Verify that in the command line string, the program part is always quoted. | 265 // Check that quotes are added to command line string paths containing spaces. |
| 194 CommandLine::StringType cmd(cl.command_line_string()); | 266 CommandLine::StringType cmd_string(cl_program_path.command_line_string()); |
| 195 CommandLine::StringType program(cl.GetProgram().value()); | 267 CommandLine::StringType program_string(cl_program_path.GetProgram().value()); |
| 196 EXPECT_EQ('"', cmd[0]); | 268 EXPECT_EQ('"', cmd_string[0]); |
| 197 EXPECT_EQ(program, cmd.substr(1, program.length())); | 269 EXPECT_EQ(program_string, cmd_string.substr(1, program_string.length())); |
| 198 EXPECT_EQ('"', cmd[program.length() + 1]); | 270 EXPECT_EQ('"', cmd_string[program_string.length() + 1]); |
| 199 } | 271 } |
| 200 #endif | 272 #endif |
| OLD | NEW |