Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: base/command_line_unittest.cc

Issue 1063933002: Take a StringPiece when looking up CommandLine switches. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cmd
Patch Set: Sync and rebase. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/command_line.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/memory/scoped_ptr.h"
11 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace base { 15 namespace base {
15 16
16 // To test Windows quoting behavior, we use a string that has some backslashes 17 // To test Windows quoting behavior, we use a string that has some backslashes
17 // and quotes. 18 // and quotes.
18 // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\" 19 // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\"
19 // Here it is with C-style escapes. 20 // Here it is with C-style escapes.
20 static const CommandLine::StringType kTrickyQuoted = 21 static const CommandLine::StringType kTrickyQuoted =
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 EXPECT_TRUE(cl.HasSwitch("bar")); 65 EXPECT_TRUE(cl.HasSwitch("bar"));
65 #else 66 #else
66 EXPECT_FALSE(cl.HasSwitch("bar")); 67 EXPECT_FALSE(cl.HasSwitch("bar"));
67 #endif 68 #endif
68 EXPECT_TRUE(cl.HasSwitch("baz")); 69 EXPECT_TRUE(cl.HasSwitch("baz"));
69 EXPECT_TRUE(cl.HasSwitch("spaetzle")); 70 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
70 EXPECT_TRUE(cl.HasSwitch("other-switches")); 71 EXPECT_TRUE(cl.HasSwitch("other-switches"));
71 EXPECT_TRUE(cl.HasSwitch("input-translation")); 72 EXPECT_TRUE(cl.HasSwitch("input-translation"));
72 73
73 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); 74 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
74 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); 75 EXPECT_EQ("", cl.GetSwitchValueASCII("foo"));
75 EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); 76 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
76 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); 77 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
77 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( 78 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
78 "other-switches")); 79 "other-switches"));
79 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); 80 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
80 81
81 const CommandLine::StringVector& args = cl.GetArgs(); 82 const CommandLine::StringVector& args = cl.GetArgs();
82 ASSERT_EQ(8U, args.size()); 83 ASSERT_EQ(8U, args.size());
83 84
84 std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); 85 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 128
128 EXPECT_TRUE(cl.HasSwitch("foo")); 129 EXPECT_TRUE(cl.HasSwitch("foo"));
129 EXPECT_TRUE(cl.HasSwitch("bar")); 130 EXPECT_TRUE(cl.HasSwitch("bar"));
130 EXPECT_TRUE(cl.HasSwitch("baz")); 131 EXPECT_TRUE(cl.HasSwitch("baz"));
131 EXPECT_TRUE(cl.HasSwitch("spaetzle")); 132 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
132 EXPECT_TRUE(cl.HasSwitch("other-switches")); 133 EXPECT_TRUE(cl.HasSwitch("other-switches"));
133 EXPECT_TRUE(cl.HasSwitch("input-translation")); 134 EXPECT_TRUE(cl.HasSwitch("input-translation"));
134 EXPECT_TRUE(cl.HasSwitch("quotes")); 135 EXPECT_TRUE(cl.HasSwitch("quotes"));
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 CommandLine::StringVector& args = cl.GetArgs();
146 ASSERT_EQ(5U, args.size()); 147 ASSERT_EQ(5U, args.size());
147 148
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 std::string value4 = "\"a value with quotes\""; 267 std::string value4 = "\"a value with quotes\"";
267 std::string switch5 = "quotes"; 268 std::string switch5 = "quotes";
268 CommandLine::StringType value5 = kTricky; 269 CommandLine::StringType value5 = kTricky;
269 270
270 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); 271 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
271 272
272 cl.AppendSwitch(switch1); 273 cl.AppendSwitch(switch1);
273 cl.AppendSwitchASCII(switch2, value2); 274 cl.AppendSwitchASCII(switch2, value2);
274 cl.AppendSwitchASCII(switch3, value3); 275 cl.AppendSwitchASCII(switch3, value3);
275 cl.AppendSwitchASCII(switch4, value4); 276 cl.AppendSwitchASCII(switch4, value4);
277 cl.AppendSwitchASCII(switch5, value4);
276 cl.AppendSwitchNative(switch5, value5); 278 cl.AppendSwitchNative(switch5, value5);
277 279
278 EXPECT_TRUE(cl.HasSwitch(switch1)); 280 EXPECT_TRUE(cl.HasSwitch(switch1));
279 EXPECT_TRUE(cl.HasSwitch(switch2)); 281 EXPECT_TRUE(cl.HasSwitch(switch2));
280 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2)); 282 EXPECT_EQ(value2, cl.GetSwitchValueASCII(switch2));
281 EXPECT_TRUE(cl.HasSwitch(switch3)); 283 EXPECT_TRUE(cl.HasSwitch(switch3));
282 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); 284 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
283 EXPECT_TRUE(cl.HasSwitch(switch4)); 285 EXPECT_TRUE(cl.HasSwitch(switch4));
284 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); 286 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
285 EXPECT_TRUE(cl.HasSwitch(switch5)); 287 EXPECT_TRUE(cl.HasSwitch(switch5));
286 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5)); 288 EXPECT_EQ(value5, cl.GetSwitchValueNative(switch5));
287 289
288 #if defined(OS_WIN) 290 #if defined(OS_WIN)
289 EXPECT_EQ(L"Program " 291 EXPECT_EQ(L"Program "
290 L"--switch1 " 292 L"--switch1 "
291 L"--switch2=value " 293 L"--switch2=value "
292 L"--switch3=\"a value with spaces\" " 294 L"--switch3=\"a value with spaces\" "
293 L"--switch4=\"\\\"a value with quotes\\\"\" " 295 L"--switch4=\"\\\"a value with quotes\\\"\" "
296 // Even though the switches are unique, appending can add repeat
297 // switches to argv.
298 L"--quotes=\"\\\"a value with quotes\\\"\" "
294 L"--quotes=\"" + kTrickyQuoted + L"\"", 299 L"--quotes=\"" + kTrickyQuoted + L"\"",
295 cl.GetCommandLineString()); 300 cl.GetCommandLineString());
296 #endif 301 #endif
297 } 302 }
298 303
299 TEST(CommandLineTest, AppendSwitchesDashDash) { 304 TEST(CommandLineTest, AppendSwitchesDashDash) {
300 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"), 305 const CommandLine::CharType* raw_argv[] = { FILE_PATH_LITERAL("prog"),
301 FILE_PATH_LITERAL("--"), 306 FILE_PATH_LITERAL("--"),
302 FILE_PATH_LITERAL("--arg1") }; 307 FILE_PATH_LITERAL("--arg1") };
303 CommandLine cl(arraysize(raw_argv), raw_argv); 308 CommandLine cl(arraysize(raw_argv), raw_argv);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 #endif 377 #endif
373 378
374 // Calling Init multiple times should not modify the previous CommandLine. 379 // Calling Init multiple times should not modify the previous CommandLine.
375 TEST(CommandLineTest, Init) { 380 TEST(CommandLineTest, Init) {
376 CommandLine* initial = CommandLine::ForCurrentProcess(); 381 CommandLine* initial = CommandLine::ForCurrentProcess();
377 EXPECT_FALSE(CommandLine::Init(0, NULL)); 382 EXPECT_FALSE(CommandLine::Init(0, NULL));
378 CommandLine* current = CommandLine::ForCurrentProcess(); 383 CommandLine* current = CommandLine::ForCurrentProcess();
379 EXPECT_EQ(initial, current); 384 EXPECT_EQ(initial, current);
380 } 385 }
381 386
387 // Test that copies of CommandLine have a valid StringPiece map.
388 TEST(CommandLineTest, Copy) {
389 scoped_ptr<CommandLine> initial(new CommandLine(CommandLine::NO_PROGRAM));
390 initial->AppendSwitch("a");
391 initial->AppendSwitch("bbbbbbbbbbbbbbb");
392 initial->AppendSwitch("c");
393 CommandLine copy_constructed(*initial);
394 CommandLine assigned = *initial;
395 CommandLine::SwitchMap switch_map = initial->GetSwitches();
396 initial.reset();
397 for (const auto& pair : switch_map)
398 EXPECT_TRUE(copy_constructed.HasSwitch(pair.first));
399 for (const auto& pair : switch_map)
400 EXPECT_TRUE(assigned.HasSwitch(pair.first));
401 }
402
382 } // namespace base 403 } // namespace base
OLDNEW
« no previous file with comments | « base/command_line.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698