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

Side by Side Diff: base/command_line_unittest.cc

Issue 6526040: CommandLine refactoring and cleanup. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Test Created 9 years, 10 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 | Annotate | Revision Log
OLDNEW
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 #define TRICKY_QUOTED L"q\\\"bs1\\bs2\\\\bs3q\\\\\\\""
19 // It should be parsed by Windows as: q"bs1\bs2\\bs3q\" 19 // It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
20 // Here that is with C-style escapes. 20 // Here that is with C-style escapes.
21 #define TRICKY L"q\"bs1\\bs2\\\\bs3q\\\"" 21 #define TRICKY L"q\"bs1\\bs2\\\\bs3q\\\""
22 22
23 TEST(CommandLineTest, CommandLineConstructor) { 23 TEST(CommandLineTest, CommandLineConstructor) {
24 #if defined(OS_WIN) 24 const CommandLine::CharType* argv[] = {
25 CommandLine cl = CommandLine::FromString( 25 FILE_PATH_LITERAL("program"),
26 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " 26 FILE_PATH_LITERAL("--foo="),
27 L"--other-switches=\"--dog=canine --cat=feline\" " 27 FILE_PATH_LITERAL("-bar"),
28 L"-spaetzle=Crepe -=loosevalue flan " 28 FILE_PATH_LITERAL("-spaetzel=pierogi"),
29 L"--input-translation=\"45\"--output-rotation " 29 FILE_PATH_LITERAL("-baz"),
30 L"--quotes=" TRICKY_QUOTED L" " 30 FILE_PATH_LITERAL("flim"),
31 L"-- -- --not-a-switch " 31 FILE_PATH_LITERAL("--other-switches=--dog=canine --cat=feline"),
32 L"\"in the time of submarines...\""); 32 FILE_PATH_LITERAL("-spaetzle=Crepe"),
33 FILE_PATH_LITERAL("-=loosevalue"),
34 FILE_PATH_LITERAL("flan"),
35 FILE_PATH_LITERAL("--input-translation=45--output-rotation"),
36 FILE_PATH_LITERAL("--"),
37 FILE_PATH_LITERAL("--"),
38 FILE_PATH_LITERAL("--not-a-switch"),
39 FILE_PATH_LITERAL("in the time of submarines...")};
40 CommandLine cl(arraysize(argv), argv);
41
33 EXPECT_FALSE(cl.command_line_string().empty()); 42 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")); 43 EXPECT_FALSE(cl.HasSwitch("cruller"));
45 EXPECT_FALSE(cl.HasSwitch("flim")); 44 EXPECT_FALSE(cl.HasSwitch("flim"));
46 EXPECT_FALSE(cl.HasSwitch("program")); 45 EXPECT_FALSE(cl.HasSwitch("program"));
46 EXPECT_FALSE(cl.HasSwitch("dog"));
47 EXPECT_FALSE(cl.HasSwitch("cat"));
48 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
49 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
50 EXPECT_FALSE(cl.HasSwitch("--"));
51
52 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
53 cl.GetProgram().value());
54
55 EXPECT_TRUE(cl.HasSwitch("foo"));
56 EXPECT_TRUE(cl.HasSwitch("bar"));
57 EXPECT_TRUE(cl.HasSwitch("baz"));
58 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
59 EXPECT_TRUE(cl.HasSwitch("other-switches"));
60 EXPECT_TRUE(cl.HasSwitch("input-translation"));
61
62 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
63 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo"));
64 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
65 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
66 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
67 "other-switches"));
68 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
69
70 const std::vector<CommandLine::StringType>& args = cl.args();
71 ASSERT_EQ(5U, args.size());
72
73 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
74 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
75 ++iter;
76 EXPECT_EQ(FILE_PATH_LITERAL("flan"), *iter);
77 ++iter;
78 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
79 ++iter;
80 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
81 ++iter;
82 #if defined(OS_WIN)
83 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter);
84 #elif defined(OS_POSIX)
85 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
86 #endif
87 ++iter;
88 EXPECT_TRUE(iter == args.end());
89
90 //#if defined(OS_POSIX)
91 // // On Windows, arguments are quote-escaped and won't be exactly equivalent.
92 // // On all platforms, sanitized args won't be exactly equivalent.
93 // const std::vector<CommandLine::StringType>& argvec = cl.argv();
94 // for (size_t i = 0; i < argvec.size(); i++)
95 // EXPECT_EQ(0, argvec[i].compare(argv[i]));
96 //#endif
97 }
98
99 TEST(CommandLineTest, CommandLineFromString) {
100 #if defined(OS_WIN)
101 CommandLine cl = CommandLine::FromString(
102 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
103 L"--other-switches=\"--dog=canine --cat=feline\" "
104 L"-spaetzle=Crepe -=loosevalue flan "
105 L"--input-translation=\"45\"--output-rotation "
106 L"--quotes=" TRICKY_QUOTED L" "
107 L"-- -- --not-a-switch "
108 L"\"in the time of submarines...\"");
109
110 EXPECT_FALSE(cl.command_line_string().empty());
111 EXPECT_FALSE(cl.HasSwitch("cruller"));
112 EXPECT_FALSE(cl.HasSwitch("flim"));
113 EXPECT_FALSE(cl.HasSwitch("program"));
47 EXPECT_FALSE(cl.HasSwitch("dog")); 114 EXPECT_FALSE(cl.HasSwitch("dog"));
48 EXPECT_FALSE(cl.HasSwitch("cat")); 115 EXPECT_FALSE(cl.HasSwitch("cat"));
49 EXPECT_FALSE(cl.HasSwitch("output-rotation")); 116 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
50 EXPECT_FALSE(cl.HasSwitch("not-a-switch")); 117 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
51 EXPECT_FALSE(cl.HasSwitch("--")); 118 EXPECT_FALSE(cl.HasSwitch("--"));
52 119
53 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(), 120 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("program")).value(),
54 cl.GetProgram().value()); 121 cl.GetProgram().value());
55 122
56 EXPECT_TRUE(cl.HasSwitch("foo")); 123 EXPECT_TRUE(cl.HasSwitch("foo"));
57 EXPECT_TRUE(cl.HasSwitch("bar")); 124 EXPECT_TRUE(cl.HasSwitch("bar"));
58 EXPECT_TRUE(cl.HasSwitch("baz")); 125 EXPECT_TRUE(cl.HasSwitch("baz"));
59 EXPECT_TRUE(cl.HasSwitch("spaetzle")); 126 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
60 #if defined(OS_WIN)
61 EXPECT_TRUE(cl.HasSwitch("SPAETZLE")); 127 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
62 #endif
63 EXPECT_TRUE(cl.HasSwitch("other-switches")); 128 EXPECT_TRUE(cl.HasSwitch("other-switches"));
64 EXPECT_TRUE(cl.HasSwitch("input-translation")); 129 EXPECT_TRUE(cl.HasSwitch("input-translation"));
65 #if defined(OS_WIN)
66 EXPECT_TRUE(cl.HasSwitch("quotes")); 130 EXPECT_TRUE(cl.HasSwitch("quotes"));
67 #endif
68 131
69 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); 132 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
70 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); 133 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo"));
71 EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); 134 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
72 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); 135 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
73 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( 136 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
74 "other-switches")); 137 "other-switches"));
75 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); 138 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
76 #if defined(OS_WIN)
77 EXPECT_EQ(TRICKY, cl.GetSwitchValueNative("quotes")); 139 EXPECT_EQ(TRICKY, cl.GetSwitchValueNative("quotes"));
78 #endif
79 140
80 const std::vector<CommandLine::StringType>& args = cl.args(); 141 const std::vector<CommandLine::StringType>& args = cl.args();
81 ASSERT_EQ(5U, args.size()); 142 ASSERT_EQ(5U, args.size());
82 143
83 std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); 144 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
84 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); 145 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
85 ++iter; 146 ++iter;
86 EXPECT_EQ(FILE_PATH_LITERAL("flan"), *iter); 147 EXPECT_EQ(FILE_PATH_LITERAL("flan"), *iter);
87 ++iter; 148 ++iter;
88 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); 149 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
89 ++iter; 150 ++iter;
90 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); 151 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
91 ++iter; 152 ++iter;
92 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter); 153 EXPECT_EQ(FILE_PATH_LITERAL("\"in the time of submarines...\""), *iter);
93 ++iter; 154 ++iter;
94 EXPECT_TRUE(iter == args.end()); 155 EXPECT_TRUE(iter == args.end());
95 #if defined(OS_POSIX)
96 const std::vector<std::string>& argvec = cl.argv();
97
98 for (size_t i = 0; i < argvec.size(); i++) {
99 EXPECT_EQ(0, argvec[i].compare(argv[i]));
100 }
101 #endif 156 #endif
102 } 157 }
103 158
104 // Tests behavior with an empty input string. 159 // Tests behavior with an empty input string.
105 TEST(CommandLineTest, EmptyString) { 160 TEST(CommandLineTest, EmptyString) {
106 #if defined(OS_WIN) 161 #if defined(OS_WIN)
107 CommandLine cl = CommandLine::FromString(L""); 162 CommandLine cl_from_string = CommandLine::FromString(L"");
108 EXPECT_TRUE(cl.command_line_string().empty()); 163 EXPECT_TRUE(cl_from_string.command_line_string().empty());
109 EXPECT_TRUE(cl.GetProgram().empty()); 164 EXPECT_TRUE(cl_from_string.GetProgram().empty());
110 #elif defined(OS_POSIX) 165 #endif
111 CommandLine cl(0, NULL); 166 CommandLine cl(0, NULL);
112 EXPECT_TRUE(cl.argv().size() == 0); 167 EXPECT_TRUE(cl.argv().size() == 0);
113 #endif
114 EXPECT_EQ(0U, cl.args().size()); 168 EXPECT_EQ(0U, cl.args().size());
115 } 169 }
116 170
117 // Test methods for appending switches to a command line. 171 // Test methods for appending switches to a command line.
118 TEST(CommandLineTest, AppendSwitches) { 172 TEST(CommandLineTest, AppendSwitches) {
119 std::string switch1 = "switch1"; 173 std::string switch1 = "switch1";
120 std::string switch2 = "switch2"; 174 std::string switch2 = "switch2";
121 std::string value = "value"; 175 std::string value = "value";
122 std::string switch3 = "switch3"; 176 std::string switch3 = "switch3";
123 std::string value3 = "a value with spaces"; 177 std::string value3 = "a value with spaces";
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 EXPECT_EQ(kProgram.value(), cl.GetProgram().value()); 245 EXPECT_EQ(kProgram.value(), cl.GetProgram().value());
192 246
193 // Verify that in the command line string, the program part is always quoted. 247 // Verify that in the command line string, the program part is always quoted.
194 CommandLine::StringType cmd(cl.command_line_string()); 248 CommandLine::StringType cmd(cl.command_line_string());
195 CommandLine::StringType program(cl.GetProgram().value()); 249 CommandLine::StringType program(cl.GetProgram().value());
196 EXPECT_EQ('"', cmd[0]); 250 EXPECT_EQ('"', cmd[0]);
197 EXPECT_EQ(program, cmd.substr(1, program.length())); 251 EXPECT_EQ(program, cmd.substr(1, program.length()));
198 EXPECT_EQ('"', cmd[program.length() + 1]); 252 EXPECT_EQ('"', cmd[program.length() + 1]);
199 } 253 }
200 #endif 254 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698