Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 using base::FilePath; | 14 using base::FilePath; |
|
brettw
2014/10/14 17:46:57
Instead of making the base:: additionss in this fi
| |
| 15 | 15 |
| 16 // To test Windows quoting behavior, we use a string that has some backslashes | 16 // To test Windows quoting behavior, we use a string that has some backslashes |
| 17 // and quotes. | 17 // and quotes. |
| 18 // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\" | 18 // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\" |
| 19 // Here it is with C-style escapes. | 19 // Here it is with C-style escapes. |
| 20 static const CommandLine::StringType kTrickyQuoted = | 20 static const base::CommandLine::StringType kTrickyQuoted = |
| 21 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\""); | 21 FILE_PATH_LITERAL("q\\\"bs1\\bs2\\\\bs3q\\\\\\\""); |
| 22 // It should be parsed by Windows as: q"bs1\bs2\\bs3q\" | 22 // It should be parsed by Windows as: q"bs1\bs2\\bs3q\" |
| 23 // Here that is with C-style escapes. | 23 // Here that is with C-style escapes. |
| 24 static const CommandLine::StringType kTricky = | 24 static const base::CommandLine::StringType kTricky = |
| 25 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\""); | 25 FILE_PATH_LITERAL("q\"bs1\\bs2\\\\bs3q\\\""); |
| 26 | 26 |
| 27 TEST(CommandLineTest, CommandLineConstructor) { | 27 TEST(CommandLineTest, CommandLineConstructor) { |
| 28 const CommandLine::CharType* argv[] = { | 28 const base::CommandLine::CharType* argv[] = { |
| 29 FILE_PATH_LITERAL("program"), | 29 FILE_PATH_LITERAL("program"), |
| 30 FILE_PATH_LITERAL("--foo="), | 30 FILE_PATH_LITERAL("--foo="), |
| 31 FILE_PATH_LITERAL("-bAr"), | 31 FILE_PATH_LITERAL("-bAr"), |
| 32 FILE_PATH_LITERAL("-spaetzel=pierogi"), | 32 FILE_PATH_LITERAL("-spaetzel=pierogi"), |
| 33 FILE_PATH_LITERAL("-baz"), | 33 FILE_PATH_LITERAL("-baz"), |
| 34 FILE_PATH_LITERAL("flim"), | 34 FILE_PATH_LITERAL("flim"), |
| 35 FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"), | 35 FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"), |
| 36 FILE_PATH_LITERAL("-spaetzle=Crepe"), | 36 FILE_PATH_LITERAL("-spaetzle=Crepe"), |
| 37 FILE_PATH_LITERAL("-=loosevalue"), | 37 FILE_PATH_LITERAL("-=loosevalue"), |
| 38 FILE_PATH_LITERAL("-"), | 38 FILE_PATH_LITERAL("-"), |
| 39 FILE_PATH_LITERAL("FLAN"), | 39 FILE_PATH_LITERAL("FLAN"), |
| 40 FILE_PATH_LITERAL("a"), | 40 FILE_PATH_LITERAL("a"), |
| 41 FILE_PATH_LITERAL("--input-translation=45--output-rotation"), | 41 FILE_PATH_LITERAL("--input-translation=45--output-rotation"), |
| 42 FILE_PATH_LITERAL("--"), | 42 FILE_PATH_LITERAL("--"), |
| 43 FILE_PATH_LITERAL("--"), | 43 FILE_PATH_LITERAL("--"), |
| 44 FILE_PATH_LITERAL("--not-a-switch"), | 44 FILE_PATH_LITERAL("--not-a-switch"), |
| 45 FILE_PATH_LITERAL("\"in the time of submarines...\""), | 45 FILE_PATH_LITERAL("\"in the time of submarines...\""), |
| 46 FILE_PATH_LITERAL("unquoted arg-with-space")}; | 46 FILE_PATH_LITERAL("unquoted arg-with-space")}; |
| 47 CommandLine cl(arraysize(argv), argv); | 47 base::CommandLine cl(arraysize(argv), argv); |
| 48 | 48 |
| 49 EXPECT_FALSE(cl.GetCommandLineString().empty()); | 49 EXPECT_FALSE(cl.GetCommandLineString().empty()); |
| 50 EXPECT_FALSE(cl.HasSwitch("cruller")); | 50 EXPECT_FALSE(cl.HasSwitch("cruller")); |
| 51 EXPECT_FALSE(cl.HasSwitch("flim")); | 51 EXPECT_FALSE(cl.HasSwitch("flim")); |
| 52 EXPECT_FALSE(cl.HasSwitch("program")); | 52 EXPECT_FALSE(cl.HasSwitch("program")); |
| 53 EXPECT_FALSE(cl.HasSwitch("dog")); | 53 EXPECT_FALSE(cl.HasSwitch("dog")); |
| 54 EXPECT_FALSE(cl.HasSwitch("cat")); | 54 EXPECT_FALSE(cl.HasSwitch("cat")); |
| 55 EXPECT_FALSE(cl.HasSwitch("output-rotation")); | 55 EXPECT_FALSE(cl.HasSwitch("output-rotation")); |
| 56 EXPECT_FALSE(cl.HasSwitch("not-a-switch")); | 56 EXPECT_FALSE(cl.HasSwitch("not-a-switch")); |
| 57 EXPECT_FALSE(cl.HasSwitch("--")); | 57 EXPECT_FALSE(cl.HasSwitch("--")); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 70 EXPECT_TRUE(cl.HasSwitch("input-translation")); | 70 EXPECT_TRUE(cl.HasSwitch("input-translation")); |
| 71 | 71 |
| 72 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); | 72 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); |
| 73 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); | 73 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); |
| 74 EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); | 74 EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); |
| 75 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); | 75 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); |
| 76 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( | 76 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( |
| 77 "other-switches")); | 77 "other-switches")); |
| 78 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); | 78 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); |
| 79 | 79 |
| 80 const CommandLine::StringVector& args = cl.GetArgs(); | 80 const base::CommandLine::StringVector& args = cl.GetArgs(); |
| 81 ASSERT_EQ(8U, args.size()); | 81 ASSERT_EQ(8U, args.size()); |
| 82 | 82 |
| 83 std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); | 83 std::vector<base::CommandLine::StringType>::const_iterator iter = |
| 84 args.begin(); | |
| 84 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); | 85 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); |
| 85 ++iter; | 86 ++iter; |
| 86 EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter); | 87 EXPECT_EQ(FILE_PATH_LITERAL("-"), *iter); |
| 87 ++iter; | 88 ++iter; |
| 88 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter); | 89 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter); |
| 89 ++iter; | 90 ++iter; |
| 90 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter); | 91 EXPECT_EQ(FILE_PATH_LITERAL("a"), *iter); |
| 91 ++iter; | 92 ++iter; |
| 92 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); | 93 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); |
| 93 ++iter; | 94 ++iter; |
| 94 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); | 95 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); |
| 95 ++iter; | 96 ++iter; |
| 96 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter); | 97 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter); |
| 97 ++iter; | 98 ++iter; |
| 98 EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter); | 99 EXPECT_EQ(FILE_PATH_LITERAL("unquoted arg-with-space"), *iter); |
| 99 ++iter; | 100 ++iter; |
| 100 EXPECT_TRUE(iter == args.end()); | 101 EXPECT_TRUE(iter == args.end()); |
| 101 } | 102 } |
| 102 | 103 |
| 103 TEST(CommandLineTest, CommandLineFromString) { | 104 TEST(CommandLineTest, CommandLineFromString) { |
| 104 #if defined(OS_WIN) | 105 #if defined(OS_WIN) |
| 105 CommandLine cl = CommandLine::FromString( | 106 base::CommandLine cl = base::CommandLine::FromString( |
| 106 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " | 107 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
| 107 L"--other-switches=\"--dog=canine --cat=feline\" " | 108 L"--other-switches=\"--dog=canine --cat=feline\" " |
| 108 L"-spaetzle=Crepe -=loosevalue FLAN " | 109 L"-spaetzle=Crepe -=loosevalue FLAN " |
| 109 L"--input-translation=\"45\"--output-rotation " | 110 L"--input-translation=\"45\"--output-rotation " |
| 110 L"--quotes=" + kTrickyQuoted + L" " | 111 L"--quotes=" + kTrickyQuoted + L" " |
| 111 L"-- -- --not-a-switch " | 112 L"-- -- --not-a-switch " |
| 112 L"\"in the time of submarines...\""); | 113 L"\"in the time of submarines...\""); |
| 113 | 114 |
| 114 EXPECT_FALSE(cl.GetCommandLineString().empty()); | 115 EXPECT_FALSE(cl.GetCommandLineString().empty()); |
| 115 EXPECT_FALSE(cl.HasSwitch("cruller")); | 116 EXPECT_FALSE(cl.HasSwitch("cruller")); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 135 | 136 |
| 136 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); | 137 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); |
| 137 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); | 138 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); |
| 138 EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); | 139 EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); |
| 139 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); | 140 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); |
| 140 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( | 141 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( |
| 141 "other-switches")); | 142 "other-switches")); |
| 142 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); | 143 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); |
| 143 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes")); | 144 EXPECT_EQ(kTricky, cl.GetSwitchValueNative("quotes")); |
| 144 | 145 |
| 145 const CommandLine::StringVector& args = cl.GetArgs(); | 146 const base::CommandLine::StringVector& args = cl.GetArgs(); |
| 146 ASSERT_EQ(5U, args.size()); | 147 ASSERT_EQ(5U, args.size()); |
| 147 | 148 |
| 148 std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); | 149 std::vector<base::CommandLine::StringType>::const_iterator iter = |
| 150 args.begin(); | |
| 149 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); | 151 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); |
| 150 ++iter; | 152 ++iter; |
| 151 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter); | 153 EXPECT_EQ(FILE_PATH_LITERAL("FLAN"), *iter); |
| 152 ++iter; | 154 ++iter; |
| 153 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); | 155 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); |
| 154 ++iter; | 156 ++iter; |
| 155 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); | 157 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); |
| 156 ++iter; | 158 ++iter; |
| 157 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter); | 159 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter); |
| 158 ++iter; | 160 ++iter; |
| 159 EXPECT_TRUE(iter == args.end()); | 161 EXPECT_TRUE(iter == args.end()); |
| 160 | 162 |
| 161 // Check that a generated string produces an equivalent command line. | 163 // Check that a generated string produces an equivalent command line. |
| 162 CommandLine cl_duplicate = CommandLine::FromString(cl.GetCommandLineString()); | 164 base::CommandLine cl_duplicate = |
| 165 base::CommandLine::FromString(cl.GetCommandLineString()); | |
| 163 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString()); | 166 EXPECT_EQ(cl.GetCommandLineString(), cl_duplicate.GetCommandLineString()); |
| 164 #endif | 167 #endif |
| 165 } | 168 } |
| 166 | 169 |
| 167 // Tests behavior with an empty input string. | 170 // Tests behavior with an empty input string. |
| 168 TEST(CommandLineTest, EmptyString) { | 171 TEST(CommandLineTest, EmptyString) { |
| 169 #if defined(OS_WIN) | 172 #if defined(OS_WIN) |
| 170 CommandLine cl_from_string = CommandLine::FromString(L""); | 173 base::CommandLine cl_from_string = base::CommandLine::FromString(L""); |
| 171 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty()); | 174 EXPECT_TRUE(cl_from_string.GetCommandLineString().empty()); |
| 172 EXPECT_TRUE(cl_from_string.GetProgram().empty()); | 175 EXPECT_TRUE(cl_from_string.GetProgram().empty()); |
| 173 EXPECT_EQ(1U, cl_from_string.argv().size()); | 176 EXPECT_EQ(1U, cl_from_string.argv().size()); |
| 174 EXPECT_TRUE(cl_from_string.GetArgs().empty()); | 177 EXPECT_TRUE(cl_from_string.GetArgs().empty()); |
| 175 #endif | 178 #endif |
| 176 CommandLine cl_from_argv(0, NULL); | 179 base::CommandLine cl_from_argv(0, NULL); |
| 177 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty()); | 180 EXPECT_TRUE(cl_from_argv.GetCommandLineString().empty()); |
| 178 EXPECT_TRUE(cl_from_argv.GetProgram().empty()); | 181 EXPECT_TRUE(cl_from_argv.GetProgram().empty()); |
| 179 EXPECT_EQ(1U, cl_from_argv.argv().size()); | 182 EXPECT_EQ(1U, cl_from_argv.argv().size()); |
| 180 EXPECT_TRUE(cl_from_argv.GetArgs().empty()); | 183 EXPECT_TRUE(cl_from_argv.GetArgs().empty()); |
| 181 } | 184 } |
| 182 | 185 |
| 183 TEST(CommandLineTest, GetArgumentsString) { | 186 TEST(CommandLineTest, GetArgumentsString) { |
| 184 static const FilePath::CharType kPath1[] = | 187 static const FilePath::CharType kPath1[] = |
| 185 FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg"); | 188 FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg"); |
| 186 static const FilePath::CharType kPath2[] = | 189 static const FilePath::CharType kPath2[] = |
| 187 FILE_PATH_LITERAL("C:\\no\\spaces.ggg"); | 190 FILE_PATH_LITERAL("C:\\no\\spaces.ggg"); |
| 188 | 191 |
| 189 static const char kFirstArgName[] = "first-arg"; | 192 static const char kFirstArgName[] = "first-arg"; |
| 190 static const char kSecondArgName[] = "arg2"; | 193 static const char kSecondArgName[] = "arg2"; |
| 191 static const char kThirdArgName[] = "arg with space"; | 194 static const char kThirdArgName[] = "arg with space"; |
| 192 static const char kFourthArgName[] = "nospace"; | 195 static const char kFourthArgName[] = "nospace"; |
| 193 static const char kFifthArgName[] = "%1"; | 196 static const char kFifthArgName[] = "%1"; |
| 194 | 197 |
| 195 CommandLine cl(CommandLine::NO_PROGRAM); | 198 base::CommandLine cl(base::CommandLine::NO_PROGRAM); |
| 196 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1)); | 199 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1)); |
| 197 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2)); | 200 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2)); |
| 198 cl.AppendArg(kThirdArgName); | 201 cl.AppendArg(kThirdArgName); |
| 199 cl.AppendArg(kFourthArgName); | 202 cl.AppendArg(kFourthArgName); |
| 200 cl.AppendArg(kFifthArgName); | 203 cl.AppendArg(kFifthArgName); |
| 201 | 204 |
| 202 #if defined(OS_WIN) | 205 #if defined(OS_WIN) |
| 203 CommandLine::StringType expected_first_arg( | 206 base::CommandLine::StringType expected_first_arg( |
| 204 base::UTF8ToUTF16(kFirstArgName)); | 207 base::UTF8ToUTF16(kFirstArgName)); |
| 205 CommandLine::StringType expected_second_arg( | 208 base::CommandLine::StringType expected_second_arg( |
| 206 base::UTF8ToUTF16(kSecondArgName)); | 209 base::UTF8ToUTF16(kSecondArgName)); |
| 207 CommandLine::StringType expected_third_arg( | 210 base::CommandLine::StringType expected_third_arg( |
| 208 base::UTF8ToUTF16(kThirdArgName)); | 211 base::UTF8ToUTF16(kThirdArgName)); |
| 209 CommandLine::StringType expected_fourth_arg( | 212 base::CommandLine::StringType expected_fourth_arg( |
| 210 base::UTF8ToUTF16(kFourthArgName)); | 213 base::UTF8ToUTF16(kFourthArgName)); |
| 211 CommandLine::StringType expected_fifth_arg(base::UTF8ToUTF16(kFifthArgName)); | 214 base::CommandLine::StringType expected_fifth_arg( |
| 215 base::UTF8ToUTF16(kFifthArgName)); | |
| 212 #elif defined(OS_POSIX) | 216 #elif defined(OS_POSIX) |
| 213 CommandLine::StringType expected_first_arg(kFirstArgName); | 217 base::CommandLine::StringType expected_first_arg(kFirstArgName); |
| 214 CommandLine::StringType expected_second_arg(kSecondArgName); | 218 base::CommandLine::StringType expected_second_arg(kSecondArgName); |
| 215 CommandLine::StringType expected_third_arg(kThirdArgName); | 219 base::CommandLine::StringType expected_third_arg(kThirdArgName); |
| 216 CommandLine::StringType expected_fourth_arg(kFourthArgName); | 220 base::CommandLine::StringType expected_fourth_arg(kFourthArgName); |
| 217 CommandLine::StringType expected_fifth_arg(kFifthArgName); | 221 base::CommandLine::StringType expected_fifth_arg(kFifthArgName); |
| 218 #endif | 222 #endif |
| 219 | 223 |
| 220 #if defined(OS_WIN) | 224 #if defined(OS_WIN) |
| 221 #define QUOTE_ON_WIN FILE_PATH_LITERAL("\"") | 225 #define QUOTE_ON_WIN FILE_PATH_LITERAL("\"") |
| 222 #else | 226 #else |
| 223 #define QUOTE_ON_WIN FILE_PATH_LITERAL("") | 227 #define QUOTE_ON_WIN FILE_PATH_LITERAL("") |
| 224 #endif // OS_WIN | 228 #endif // OS_WIN |
| 225 | 229 |
| 226 CommandLine::StringType expected_str; | 230 base::CommandLine::StringType expected_str; |
| 227 expected_str.append(FILE_PATH_LITERAL("--")) | 231 expected_str.append(FILE_PATH_LITERAL("--")) |
| 228 .append(expected_first_arg) | 232 .append(expected_first_arg) |
| 229 .append(FILE_PATH_LITERAL("=")) | 233 .append(FILE_PATH_LITERAL("=")) |
| 230 .append(QUOTE_ON_WIN) | 234 .append(QUOTE_ON_WIN) |
| 231 .append(kPath1) | 235 .append(kPath1) |
| 232 .append(QUOTE_ON_WIN) | 236 .append(QUOTE_ON_WIN) |
| 233 .append(FILE_PATH_LITERAL(" ")) | 237 .append(FILE_PATH_LITERAL(" ")) |
| 234 .append(FILE_PATH_LITERAL("--")) | 238 .append(FILE_PATH_LITERAL("--")) |
| 235 .append(expected_second_arg) | 239 .append(expected_second_arg) |
| 236 .append(FILE_PATH_LITERAL("=")) | 240 .append(FILE_PATH_LITERAL("=")) |
| 237 .append(QUOTE_ON_WIN) | 241 .append(QUOTE_ON_WIN) |
| 238 .append(kPath2) | 242 .append(kPath2) |
| 239 .append(QUOTE_ON_WIN) | 243 .append(QUOTE_ON_WIN) |
| 240 .append(FILE_PATH_LITERAL(" ")) | 244 .append(FILE_PATH_LITERAL(" ")) |
| 241 .append(QUOTE_ON_WIN) | 245 .append(QUOTE_ON_WIN) |
| 242 .append(expected_third_arg) | 246 .append(expected_third_arg) |
| 243 .append(QUOTE_ON_WIN) | 247 .append(QUOTE_ON_WIN) |
| 244 .append(FILE_PATH_LITERAL(" ")) | 248 .append(FILE_PATH_LITERAL(" ")) |
| 245 .append(expected_fourth_arg) | 249 .append(expected_fourth_arg) |
| 246 .append(FILE_PATH_LITERAL(" ")); | 250 .append(FILE_PATH_LITERAL(" ")); |
| 247 | 251 |
| 248 CommandLine::StringType expected_str_no_quote_placeholders(expected_str); | 252 base::CommandLine::StringType expected_str_no_quote_placeholders( |
| 253 expected_str); | |
| 249 expected_str_no_quote_placeholders.append(expected_fifth_arg); | 254 expected_str_no_quote_placeholders.append(expected_fifth_arg); |
| 250 EXPECT_EQ(expected_str_no_quote_placeholders, cl.GetArgumentsString()); | 255 EXPECT_EQ(expected_str_no_quote_placeholders, cl.GetArgumentsString()); |
| 251 | 256 |
| 252 #if defined(OS_WIN) | 257 #if defined(OS_WIN) |
| 253 CommandLine::StringType expected_str_quote_placeholders(expected_str); | 258 base::CommandLine::StringType expected_str_quote_placeholders(expected_str); |
| 254 expected_str_quote_placeholders.append(QUOTE_ON_WIN) | 259 expected_str_quote_placeholders.append(QUOTE_ON_WIN) |
| 255 .append(expected_fifth_arg) | 260 .append(expected_fifth_arg) |
| 256 .append(QUOTE_ON_WIN); | 261 .append(QUOTE_ON_WIN); |
| 257 EXPECT_EQ(expected_str_quote_placeholders, | 262 EXPECT_EQ(expected_str_quote_placeholders, |
| 258 cl.GetArgumentsStringWithPlaceholders()); | 263 cl.GetArgumentsStringWithPlaceholders()); |
| 259 #endif | 264 #endif |
| 260 } | 265 } |
| 261 | 266 |
| 262 // Test methods for appending switches to a command line. | 267 // Test methods for appending switches to a command line. |
| 263 TEST(CommandLineTest, AppendSwitches) { | 268 TEST(CommandLineTest, AppendSwitches) { |
| 264 std::string switch1 = "switch1"; | 269 std::string switch1 = "switch1"; |
| 265 std::string switch2 = "switch2"; | 270 std::string switch2 = "switch2"; |
| 266 std::string value2 = "value"; | 271 std::string value2 = "value"; |
| 267 std::string switch3 = "switch3"; | 272 std::string switch3 = "switch3"; |
| 268 std::string value3 = "a value with spaces"; | 273 std::string value3 = "a value with spaces"; |
| 269 std::string switch4 = "switch4"; | 274 std::string switch4 = "switch4"; |
| 270 std::string value4 = "\"a value with quotes\""; | 275 std::string value4 = "\"a value with quotes\""; |
| 271 std::string switch5 = "quotes"; | 276 std::string switch5 = "quotes"; |
| 272 CommandLine::StringType value5 = kTricky; | 277 base::CommandLine::StringType value5 = kTricky; |
| 273 | 278 |
| 274 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); | 279 base::CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
| 275 | 280 |
| 276 cl.AppendSwitch(switch1); | 281 cl.AppendSwitch(switch1); |
| 277 cl.AppendSwitchASCII(switch2, value2); | 282 cl.AppendSwitchASCII(switch2, value2); |
| 278 cl.AppendSwitchASCII(switch3, value3); | 283 cl.AppendSwitchASCII(switch3, value3); |
| 279 cl.AppendSwitchASCII(switch4, value4); | 284 cl.AppendSwitchASCII(switch4, value4); |
| 280 cl.AppendSwitchNative(switch5, value5); | 285 cl.AppendSwitchNative(switch5, value5); |
| 281 | 286 |
| 282 EXPECT_TRUE(cl.HasSwitch(switch1)); | 287 EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 283 EXPECT_TRUE(cl.HasSwitch(switch2)); | 288 EXPECT_TRUE(cl.HasSwitch(switch2)); |
| 284 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2)); | 289 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2)); |
| 285 EXPECT_TRUE(cl.HasSwitch(switch3)); | 290 EXPECT_TRUE(cl.HasSwitch(switch3)); |
| 286 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); | 291 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); |
| 287 EXPECT_TRUE(cl.HasSwitch(switch4)); | 292 EXPECT_TRUE(cl.HasSwitch(switch4)); |
| 288 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); | 293 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); |
| 289 EXPECT_TRUE(cl.HasSwitch(switch5)); | 294 EXPECT_TRUE(cl.HasSwitch(switch5)); |
| 290 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5)); | 295 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5)); |
| 291 | 296 |
| 292 #if defined(OS_WIN) | 297 #if defined(OS_WIN) |
| 293 EXPECT_EQ(L"Program " | 298 EXPECT_EQ(L"Program " |
| 294 L"--switch1 " | 299 L"--switch1 " |
| 295 L"--switch2=value " | 300 L"--switch2=value " |
| 296 L"--switch3=\"a value with spaces\" " | 301 L"--switch3=\"a value with spaces\" " |
| 297 L"--switch4=\"\\\"a value with quotes\\\"\" " | 302 L"--switch4=\"\\\"a value with quotes\\\"\" " |
| 298 L"--quotes=\"" + kTrickyQuoted + L"\"", | 303 L"--quotes=\"" + kTrickyQuoted + L"\"", |
| 299 cl.GetCommandLineString()); | 304 cl.GetCommandLineString()); |
| 300 #endif | 305 #endif |
| 301 } | 306 } |
| 302 | 307 |
| 303 TEST(CommandLineTest, AppendSwitchesDashDash) { | 308 TEST(CommandLineTest, AppendSwitchesDashDash) { |
| 304 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"), | 309 const base::CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"), |
| 305 FILE_PATH_LITERAL("--"), | 310 FILE_PATH_LITERAL("--"), |
| 306 FILE_PATH_LITERAL("--arg1") }; | 311 FILE_PATH_LITERAL("--arg1") }; |
| 307 CommandLine cl(arraysize(raw_argv), raw_argv); | 312 base::CommandLine cl(arraysize(raw_argv), raw_argv); |
| 308 | 313 |
| 309 cl.AppendSwitch("switch1"); | 314 cl.AppendSwitch("switch1"); |
| 310 cl.AppendSwitchASCII("switch2", "foo"); | 315 cl.AppendSwitchASCII("switch2", "foo"); |
| 311 | 316 |
| 312 cl.AppendArg("--arg2"); | 317 cl.AppendArg("--arg2"); |
| 313 | 318 |
| 314 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"), | 319 EXPECT_EQ(FILE_PATH_LITERAL("prog --switch1 --switch2=foo -- --arg1 --arg2"), |
| 315 cl.GetCommandLineString()); | 320 cl.GetCommandLineString()); |
| 316 CommandLine::StringVector cl_argv = cl.argv(); | 321 base::CommandLine::StringVector cl_argv = cl.argv(); |
| 317 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]); | 322 EXPECT_EQ(FILE_PATH_LITERAL("prog"), cl_argv[0]); |
| 318 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]); | 323 EXPECT_EQ(FILE_PATH_LITERAL("--switch1"), cl_argv[1]); |
| 319 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]); | 324 EXPECT_EQ(FILE_PATH_LITERAL("--switch2=foo"), cl_argv[2]); |
| 320 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]); | 325 EXPECT_EQ(FILE_PATH_LITERAL("--"), cl_argv[3]); |
| 321 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]); | 326 EXPECT_EQ(FILE_PATH_LITERAL("--arg1"), cl_argv[4]); |
| 322 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]); | 327 EXPECT_EQ(FILE_PATH_LITERAL("--arg2"), cl_argv[5]); |
| 323 } | 328 } |
| 324 | 329 |
| 325 // Tests that when AppendArguments is called that the program is set correctly | 330 // Tests that when AppendArguments is called that the program is set correctly |
| 326 // on the target CommandLine object and the switches from the source | 331 // on the target CommandLine object and the switches from the source |
| 327 // CommandLine are added to the target. | 332 // CommandLine are added to the target. |
| 328 TEST(CommandLineTest, AppendArguments) { | 333 TEST(CommandLineTest, AppendArguments) { |
| 329 CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program"))); | 334 base::CommandLine cl1(FilePath(FILE_PATH_LITERAL("Program"))); |
| 330 cl1.AppendSwitch("switch1"); | 335 cl1.AppendSwitch("switch1"); |
| 331 cl1.AppendSwitchASCII("switch2", "foo"); | 336 cl1.AppendSwitchASCII("switch2", "foo"); |
| 332 | 337 |
| 333 CommandLine cl2(CommandLine::NO_PROGRAM); | 338 base::CommandLine cl2(base::CommandLine::NO_PROGRAM); |
| 334 cl2.AppendArguments(cl1, true); | 339 cl2.AppendArguments(cl1, true); |
| 335 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value()); | 340 EXPECT_EQ(cl1.GetProgram().value(), cl2.GetProgram().value()); |
| 336 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString()); | 341 EXPECT_EQ(cl1.GetCommandLineString(), cl2.GetCommandLineString()); |
| 337 | 342 |
| 338 CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1"))); | 343 base::CommandLine c1(FilePath(FILE_PATH_LITERAL("Program1"))); |
| 339 c1.AppendSwitch("switch1"); | 344 c1.AppendSwitch("switch1"); |
| 340 CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2"))); | 345 base::CommandLine c2(FilePath(FILE_PATH_LITERAL("Program2"))); |
| 341 c2.AppendSwitch("switch2"); | 346 c2.AppendSwitch("switch2"); |
| 342 | 347 |
| 343 c1.AppendArguments(c2, true); | 348 c1.AppendArguments(c2, true); |
| 344 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value()); | 349 EXPECT_EQ(c1.GetProgram().value(), c2.GetProgram().value()); |
| 345 EXPECT_TRUE(c1.HasSwitch("switch1")); | 350 EXPECT_TRUE(c1.HasSwitch("switch1")); |
| 346 EXPECT_TRUE(c1.HasSwitch("switch2")); | 351 EXPECT_TRUE(c1.HasSwitch("switch2")); |
| 347 } | 352 } |
| 348 | 353 |
| 349 #if defined(OS_WIN) | 354 #if defined(OS_WIN) |
| 350 // Make sure that the command line string program paths are quoted as necessary. | 355 // Make sure that the command line string program paths are quoted as necessary. |
| 351 // This only makes sense on Windows and the test is basically here to guard | 356 // This only makes sense on Windows and the test is basically here to guard |
| 352 // against regressions. | 357 // against regressions. |
| 353 TEST(CommandLineTest, ProgramQuotes) { | 358 TEST(CommandLineTest, ProgramQuotes) { |
| 354 // Check that quotes are not added for paths without spaces. | 359 // Check that quotes are not added for paths without spaces. |
| 355 const FilePath kProgram(L"Program"); | 360 const FilePath kProgram(L"Program"); |
| 356 CommandLine cl_program(kProgram); | 361 base::CommandLine cl_program(kProgram); |
| 357 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value()); | 362 EXPECT_EQ(kProgram.value(), cl_program.GetProgram().value()); |
| 358 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString()); | 363 EXPECT_EQ(kProgram.value(), cl_program.GetCommandLineString()); |
| 359 | 364 |
| 360 const FilePath kProgramPath(L"Program Path"); | 365 const FilePath kProgramPath(L"Program Path"); |
| 361 | 366 |
| 362 // Check that quotes are not returned from GetProgram(). | 367 // Check that quotes are not returned from GetProgram(). |
| 363 CommandLine cl_program_path(kProgramPath); | 368 base::CommandLine cl_program_path(kProgramPath); |
| 364 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value()); | 369 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value()); |
| 365 | 370 |
| 366 // Check that quotes are added to command line string paths containing spaces. | 371 // Check that quotes are added to command line string paths containing spaces. |
| 367 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString()); | 372 base::CommandLine::StringType cmd_string( |
| 373 cl_program_path.GetCommandLineString()); | |
| 368 EXPECT_EQ(L"\"Program Path\"", cmd_string); | 374 EXPECT_EQ(L"\"Program Path\"", cmd_string); |
| 369 | 375 |
| 370 // Check the optional quoting of placeholders in programs. | 376 // Check the optional quoting of placeholders in programs. |
| 371 CommandLine cl_quote_placeholder(base::FilePath(L"%1")); | 377 base::CommandLine cl_quote_placeholder(base::FilePath(L"%1")); |
| 372 EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString()); | 378 EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString()); |
| 373 EXPECT_EQ(L"\"%1\"", | 379 EXPECT_EQ(L"\"%1\"", |
| 374 cl_quote_placeholder.GetCommandLineStringWithPlaceholders()); | 380 cl_quote_placeholder.GetCommandLineStringWithPlaceholders()); |
| 375 } | 381 } |
| 376 #endif | 382 #endif |
| 377 | 383 |
| 378 // Calling Init multiple times should not modify the previous CommandLine. | 384 // Calling Init multiple times should not modify the previous CommandLine. |
| 379 TEST(CommandLineTest, Init) { | 385 TEST(CommandLineTest, Init) { |
| 380 CommandLine* initial = CommandLine::ForCurrentProcess(); | 386 base::CommandLine* initial = base::CommandLine::ForCurrentProcess(); |
| 381 EXPECT_FALSE(CommandLine::Init(0, NULL)); | 387 EXPECT_FALSE(base::CommandLine::Init(0, NULL)); |
| 382 CommandLine* current = CommandLine::ForCurrentProcess(); | 388 base::CommandLine* current = base::CommandLine::ForCurrentProcess(); |
| 383 EXPECT_EQ(initial, current); | 389 EXPECT_EQ(initial, current); |
| 384 } | 390 } |
| OLD | NEW |