| 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" |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 TEST(CommandLineTest, GetArgumentsString) { | 183 TEST(CommandLineTest, GetArgumentsString) { |
| 184 static const FilePath::CharType kPath1[] = | 184 static const FilePath::CharType kPath1[] = |
| 185 FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg"); | 185 FILE_PATH_LITERAL("C:\\Some File\\With Spaces.ggg"); |
| 186 static const FilePath::CharType kPath2[] = | 186 static const FilePath::CharType kPath2[] = |
| 187 FILE_PATH_LITERAL("C:\\no\\spaces.ggg"); | 187 FILE_PATH_LITERAL("C:\\no\\spaces.ggg"); |
| 188 | 188 |
| 189 static const char kFirstArgName[] = "first-arg"; | 189 static const char kFirstArgName[] = "first-arg"; |
| 190 static const char kSecondArgName[] = "arg2"; | 190 static const char kSecondArgName[] = "arg2"; |
| 191 static const char kThirdArgName[] = "arg with space"; | 191 static const char kThirdArgName[] = "arg with space"; |
| 192 static const char kFourthArgName[] = "nospace"; | 192 static const char kFourthArgName[] = "nospace"; |
| 193 static const char kFifthArgName[] = "%1"; |
| 193 | 194 |
| 194 CommandLine cl(CommandLine::NO_PROGRAM); | 195 CommandLine cl(CommandLine::NO_PROGRAM); |
| 195 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1)); | 196 cl.AppendSwitchPath(kFirstArgName, FilePath(kPath1)); |
| 196 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2)); | 197 cl.AppendSwitchPath(kSecondArgName, FilePath(kPath2)); |
| 197 cl.AppendArg(kThirdArgName); | 198 cl.AppendArg(kThirdArgName); |
| 198 cl.AppendArg(kFourthArgName); | 199 cl.AppendArg(kFourthArgName); |
| 200 cl.AppendArg(kFifthArgName); |
| 199 | 201 |
| 200 #if defined(OS_WIN) | 202 #if defined(OS_WIN) |
| 201 CommandLine::StringType expected_first_arg( | 203 CommandLine::StringType expected_first_arg( |
| 202 base::UTF8ToUTF16(kFirstArgName)); | 204 base::UTF8ToUTF16(kFirstArgName)); |
| 203 CommandLine::StringType expected_second_arg( | 205 CommandLine::StringType expected_second_arg( |
| 204 base::UTF8ToUTF16(kSecondArgName)); | 206 base::UTF8ToUTF16(kSecondArgName)); |
| 205 CommandLine::StringType expected_third_arg( | 207 CommandLine::StringType expected_third_arg( |
| 206 base::UTF8ToUTF16(kThirdArgName)); | 208 base::UTF8ToUTF16(kThirdArgName)); |
| 207 CommandLine::StringType expected_fourth_arg( | 209 CommandLine::StringType expected_fourth_arg( |
| 208 base::UTF8ToUTF16(kFourthArgName)); | 210 base::UTF8ToUTF16(kFourthArgName)); |
| 211 CommandLine::StringType expected_fifth_arg( |
| 212 base::UTF8ToUTF16(kFifthArgName)); |
| 209 #elif defined(OS_POSIX) | 213 #elif defined(OS_POSIX) |
| 210 CommandLine::StringType expected_first_arg(kFirstArgName); | 214 CommandLine::StringType expected_first_arg(kFirstArgName); |
| 211 CommandLine::StringType expected_second_arg(kSecondArgName); | 215 CommandLine::StringType expected_second_arg(kSecondArgName); |
| 212 CommandLine::StringType expected_third_arg(kThirdArgName); | 216 CommandLine::StringType expected_third_arg(kThirdArgName); |
| 213 CommandLine::StringType expected_fourth_arg(kFourthArgName); | 217 CommandLine::StringType expected_fourth_arg(kFourthArgName); |
| 218 CommandLine::StringType expected_fifth_arg(kFifthArgName); |
| 214 #endif | 219 #endif |
| 215 | 220 |
| 216 #if defined(OS_WIN) | 221 #if defined(OS_WIN) |
| 217 #define QUOTE_ON_WIN FILE_PATH_LITERAL("\"") | 222 #define QUOTE_ON_WIN FILE_PATH_LITERAL("\"") |
| 218 #else | 223 #else |
| 219 #define QUOTE_ON_WIN FILE_PATH_LITERAL("") | 224 #define QUOTE_ON_WIN FILE_PATH_LITERAL("") |
| 220 #endif // OS_WIN | 225 #endif // OS_WIN |
| 221 | 226 |
| 222 CommandLine::StringType expected_str; | 227 CommandLine::StringType expected_str; |
| 223 expected_str.append(FILE_PATH_LITERAL("--")) | 228 expected_str.append(FILE_PATH_LITERAL("--")) |
| 224 .append(expected_first_arg) | 229 .append(expected_first_arg) |
| 225 .append(FILE_PATH_LITERAL("=")) | 230 .append(FILE_PATH_LITERAL("=")) |
| 226 .append(QUOTE_ON_WIN) | 231 .append(QUOTE_ON_WIN) |
| 227 .append(kPath1) | 232 .append(kPath1) |
| 228 .append(QUOTE_ON_WIN) | 233 .append(QUOTE_ON_WIN) |
| 229 .append(FILE_PATH_LITERAL(" ")) | 234 .append(FILE_PATH_LITERAL(" ")) |
| 230 .append(FILE_PATH_LITERAL("--")) | 235 .append(FILE_PATH_LITERAL("--")) |
| 231 .append(expected_second_arg) | 236 .append(expected_second_arg) |
| 232 .append(FILE_PATH_LITERAL("=")) | 237 .append(FILE_PATH_LITERAL("=")) |
| 233 .append(QUOTE_ON_WIN) | 238 .append(QUOTE_ON_WIN) |
| 234 .append(kPath2) | 239 .append(kPath2) |
| 235 .append(QUOTE_ON_WIN) | 240 .append(QUOTE_ON_WIN) |
| 236 .append(FILE_PATH_LITERAL(" ")) | 241 .append(FILE_PATH_LITERAL(" ")) |
| 237 .append(QUOTE_ON_WIN) | 242 .append(QUOTE_ON_WIN) |
| 238 .append(expected_third_arg) | 243 .append(expected_third_arg) |
| 239 .append(QUOTE_ON_WIN) | 244 .append(QUOTE_ON_WIN) |
| 240 .append(FILE_PATH_LITERAL(" ")) | 245 .append(FILE_PATH_LITERAL(" ")) |
| 241 .append(expected_fourth_arg); | 246 .append(expected_fourth_arg) |
| 242 EXPECT_EQ(expected_str, cl.GetArgumentsString()); | 247 .append(FILE_PATH_LITERAL(" ")); |
| 248 |
| 249 CommandLine::StringType expected_str_no_quote_placeholders(expected_str); |
| 250 expected_str_no_quote_placeholders.append(expected_fifth_arg); |
| 251 EXPECT_EQ(expected_str_no_quote_placeholders, cl.GetArgumentsString()); |
| 252 |
| 253 CommandLine::StringType expected_str_quote_placeholders(expected_str); |
| 254 expected_str_quote_placeholders.append(QUOTE_ON_WIN) |
| 255 .append(expected_fifth_arg) |
| 256 .append(QUOTE_ON_WIN); |
| 257 EXPECT_EQ(expected_str_quote_placeholders, cl.GetArgumentsString(true)); |
| 243 } | 258 } |
| 244 | 259 |
| 245 // Test methods for appending switches to a command line. | 260 // Test methods for appending switches to a command line. |
| 246 TEST(CommandLineTest, AppendSwitches) { | 261 TEST(CommandLineTest, AppendSwitches) { |
| 247 std::string switch1 = "switch1"; | 262 std::string switch1 = "switch1"; |
| 248 std::string switch2 = "switch2"; | 263 std::string switch2 = "switch2"; |
| 249 std::string value2 = "value"; | 264 std::string value2 = "value"; |
| 250 std::string switch3 = "switch3"; | 265 std::string switch3 = "switch3"; |
| 251 std::string value3 = "a value with spaces"; | 266 std::string value3 = "a value with spaces"; |
| 252 std::string switch4 = "switch4"; | 267 std::string switch4 = "switch4"; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 | 357 |
| 343 const FilePath kProgramPath(L"Program Path"); | 358 const FilePath kProgramPath(L"Program Path"); |
| 344 | 359 |
| 345 // Check that quotes are not returned from GetProgram(). | 360 // Check that quotes are not returned from GetProgram(). |
| 346 CommandLine cl_program_path(kProgramPath); | 361 CommandLine cl_program_path(kProgramPath); |
| 347 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value()); | 362 EXPECT_EQ(kProgramPath.value(), cl_program_path.GetProgram().value()); |
| 348 | 363 |
| 349 // Check that quotes are added to command line string paths containing spaces. | 364 // Check that quotes are added to command line string paths containing spaces. |
| 350 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString()); | 365 CommandLine::StringType cmd_string(cl_program_path.GetCommandLineString()); |
| 351 EXPECT_EQ(L"\"Program Path\"", cmd_string); | 366 EXPECT_EQ(L"\"Program Path\"", cmd_string); |
| 367 |
| 368 // Check the optional quoting of placeholders in programs. |
| 369 CommandLine cl_quote_placeholder(base::FilePath(L"%1")); |
| 370 EXPECT_EQ(L"%1", cl_quote_placeholder.GetCommandLineString()); |
| 371 EXPECT_EQ(L"\"%1\"", cl_quote_placeholder.GetCommandLineString(true)); |
| 352 } | 372 } |
| 353 #endif | 373 #endif |
| 354 | 374 |
| 355 // Calling Init multiple times should not modify the previous CommandLine. | 375 // Calling Init multiple times should not modify the previous CommandLine. |
| 356 TEST(CommandLineTest, Init) { | 376 TEST(CommandLineTest, Init) { |
| 357 CommandLine* initial = CommandLine::ForCurrentProcess(); | 377 CommandLine* initial = CommandLine::ForCurrentProcess(); |
| 358 EXPECT_FALSE(CommandLine::Init(0, NULL)); | 378 EXPECT_FALSE(CommandLine::Init(0, NULL)); |
| 359 CommandLine* current = CommandLine::ForCurrentProcess(); | 379 CommandLine* current = CommandLine::ForCurrentProcess(); |
| 360 EXPECT_EQ(initial, current); | 380 EXPECT_EQ(initial, current); |
| 361 } | 381 } |
| OLD | NEW |