OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace { | |
15 class CommandLineTest : public testing::Test { | |
16 }; | |
17 }; | |
18 | |
19 TEST(CommandLineTest, CommandLineConstructor) { | 14 TEST(CommandLineTest, CommandLineConstructor) { |
20 #ifdef OS_WIN | 15 #if defined(OS_WIN) |
21 CommandLine cl(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " | 16 CommandLine cl(L""); |
22 L"--other-switches=\"--dog=canine --cat=feline\" " | 17 cl.ParseFromString(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
23 L"-spaetzle=Crepe -=loosevalue flan " | 18 L"--other-switches=\"--dog=canine --cat=feline\" " |
24 L"--input-translation=\"45\"--output-rotation " | 19 L"-spaetzle=Crepe -=loosevalue flan " |
25 L"-- -- --not-a-switch " | 20 L"--input-translation=\"45\"--output-rotation " |
26 L"\"in the time of submarines...\""); | 21 L"-- -- --not-a-switch " |
| 22 L"\"in the time of submarines...\""); |
| 23 EXPECT_FALSE(cl.command_line_string().empty()); |
27 #elif defined(OS_POSIX) | 24 #elif defined(OS_POSIX) |
28 const char* argv[] = {"program", "--foo=", "-bAr", | 25 const char* argv[] = {"program", "--foo=", "-bar", |
29 "-Spaetzel=pierogi", "-Baz", "flim", | 26 "-spaetzel=pierogi", "-baz", "flim", |
30 "--other-switches=--dog=canine --cat=feline", | 27 "--other-switches=--dog=canine --cat=feline", |
31 "-spaetzle=Crepe", "-=loosevalue", "flan", | 28 "-spaetzle=Crepe", "-=loosevalue", "flan", |
32 "--input-translation=45--output-rotation", | 29 "--input-translation=45--output-rotation", |
33 "--", "--", "--not-a-switch", | 30 "--", "--", "--not-a-switch", |
34 "in the time of submarines..."}; | 31 "in the time of submarines..."}; |
35 CommandLine cl(arraysize(argv), argv); | 32 CommandLine cl(arraysize(argv), argv); |
36 #endif | 33 #endif |
37 EXPECT_FALSE(cl.command_line_string().empty()); | |
38 EXPECT_FALSE(cl.HasSwitch(L"cruller")); | 34 EXPECT_FALSE(cl.HasSwitch(L"cruller")); |
39 EXPECT_FALSE(cl.HasSwitch(L"flim")); | 35 EXPECT_FALSE(cl.HasSwitch(L"flim")); |
40 EXPECT_FALSE(cl.HasSwitch(L"program")); | 36 EXPECT_FALSE(cl.HasSwitch(L"program")); |
41 EXPECT_FALSE(cl.HasSwitch(L"dog")); | 37 EXPECT_FALSE(cl.HasSwitch(L"dog")); |
42 EXPECT_FALSE(cl.HasSwitch(L"cat")); | 38 EXPECT_FALSE(cl.HasSwitch(L"cat")); |
43 EXPECT_FALSE(cl.HasSwitch(L"output-rotation")); | 39 EXPECT_FALSE(cl.HasSwitch(L"output-rotation")); |
44 EXPECT_FALSE(cl.HasSwitch(L"not-a-switch")); | 40 EXPECT_FALSE(cl.HasSwitch(L"not-a-switch")); |
45 EXPECT_FALSE(cl.HasSwitch(L"--")); | 41 EXPECT_FALSE(cl.HasSwitch(L"--")); |
46 | 42 |
47 EXPECT_EQ(L"program", cl.program()); | 43 EXPECT_EQ(L"program", cl.program()); |
48 | 44 |
49 EXPECT_TRUE(cl.HasSwitch(L"foo")); | 45 EXPECT_TRUE(cl.HasSwitch(L"foo")); |
50 EXPECT_TRUE(cl.HasSwitch(L"bar")); | 46 EXPECT_TRUE(cl.HasSwitch(L"bar")); |
51 EXPECT_TRUE(cl.HasSwitch(L"baz")); | 47 EXPECT_TRUE(cl.HasSwitch(L"baz")); |
52 EXPECT_TRUE(cl.HasSwitch(L"spaetzle")); | 48 EXPECT_TRUE(cl.HasSwitch(L"spaetzle")); |
| 49 #if defined(OS_WIN) |
53 EXPECT_TRUE(cl.HasSwitch(L"SPAETZLE")); | 50 EXPECT_TRUE(cl.HasSwitch(L"SPAETZLE")); |
| 51 #endif |
54 EXPECT_TRUE(cl.HasSwitch(L"other-switches")); | 52 EXPECT_TRUE(cl.HasSwitch(L"other-switches")); |
55 EXPECT_TRUE(cl.HasSwitch(L"input-translation")); | 53 EXPECT_TRUE(cl.HasSwitch(L"input-translation")); |
56 | 54 |
57 EXPECT_EQ(L"Crepe", cl.GetSwitchValue(L"spaetzle")); | 55 EXPECT_EQ(L"Crepe", cl.GetSwitchValue(L"spaetzle")); |
58 EXPECT_EQ(L"", cl.GetSwitchValue(L"Foo")); | 56 EXPECT_EQ(L"", cl.GetSwitchValue(L"Foo")); |
59 EXPECT_EQ(L"", cl.GetSwitchValue(L"bar")); | 57 EXPECT_EQ(L"", cl.GetSwitchValue(L"bar")); |
60 EXPECT_EQ(L"", cl.GetSwitchValue(L"cruller")); | 58 EXPECT_EQ(L"", cl.GetSwitchValue(L"cruller")); |
61 EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue(L"other-switches")); | 59 EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue(L"other-switches")); |
62 EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue(L"input-translation")); | 60 EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue(L"input-translation")); |
63 | 61 |
64 EXPECT_EQ(5U, cl.GetLooseValueCount()); | 62 std::vector<std::wstring> loose_values = cl.GetLooseValues(); |
| 63 ASSERT_EQ(5U, loose_values.size()); |
65 | 64 |
66 CommandLine::LooseValueIterator iter = cl.GetLooseValuesBegin(); | 65 std::vector<std::wstring>::const_iterator iter = loose_values.begin(); |
67 EXPECT_EQ(L"flim", *iter); | 66 EXPECT_EQ(L"flim", *iter); |
68 ++iter; | 67 ++iter; |
69 EXPECT_EQ(L"flan", *iter); | 68 EXPECT_EQ(L"flan", *iter); |
70 ++iter; | 69 ++iter; |
71 EXPECT_EQ(L"--", *iter); | 70 EXPECT_EQ(L"--", *iter); |
72 ++iter; | 71 ++iter; |
73 EXPECT_EQ(L"--not-a-switch", *iter); | 72 EXPECT_EQ(L"--not-a-switch", *iter); |
74 ++iter; | 73 ++iter; |
75 EXPECT_EQ(L"in the time of submarines...", *iter); | 74 EXPECT_EQ(L"in the time of submarines...", *iter); |
76 ++iter; | 75 ++iter; |
77 EXPECT_TRUE(iter == cl.GetLooseValuesEnd()); | 76 EXPECT_TRUE(iter == loose_values.end()); |
78 #if defined(OS_POSIX) | 77 #if defined(OS_POSIX) |
79 std::vector<std::string> argvec = cl.argv(); | 78 const std::vector<std::string>& argvec = cl.argv(); |
80 | 79 |
81 for (size_t i = 0; i < argvec.size(); i++) { | 80 for (size_t i = 0; i < argvec.size(); i++) { |
82 EXPECT_EQ(0, argvec[i].compare(argv[i])); | 81 EXPECT_EQ(0, argvec[i].compare(argv[i])); |
83 } | 82 } |
84 #endif | 83 #endif |
85 } | 84 } |
86 | 85 |
87 // These test the command line used to invoke the unit test. | |
88 TEST(CommandLineTest, DefaultConstructor) { | |
89 CommandLine cl; | |
90 EXPECT_FALSE(cl.command_line_string().empty()); | |
91 EXPECT_FALSE(cl.program().empty()); | |
92 } | |
93 | |
94 // Tests behavior with an empty input string. | 86 // Tests behavior with an empty input string. |
95 TEST(CommandLineTest, EmptyString) { | 87 TEST(CommandLineTest, EmptyString) { |
96 #if defined(OS_WIN) | 88 #if defined(OS_WIN) |
97 CommandLine cl(L""); | 89 CommandLine cl(L""); |
| 90 EXPECT_TRUE(cl.command_line_string().empty()); |
| 91 EXPECT_TRUE(cl.program().empty()); |
98 #elif defined(OS_POSIX) | 92 #elif defined(OS_POSIX) |
99 CommandLine cl(0, NULL); | 93 CommandLine cl(0, NULL); |
100 EXPECT_TRUE(cl.argv().size() == 0); | 94 EXPECT_TRUE(cl.argv().size() == 0); |
101 #endif | 95 #endif |
102 EXPECT_TRUE(cl.command_line_string().empty()); | 96 EXPECT_EQ(0U, cl.GetLooseValues().size()); |
103 EXPECT_TRUE(cl.program().empty()); | |
104 EXPECT_EQ(0U, cl.GetLooseValueCount()); | |
105 } | 97 } |
106 | 98 |
107 // Test static functions for appending switches to a command line. | 99 // Test methods for appending switches to a command line. |
108 TEST(CommandLineTest, AppendSwitches) { | 100 TEST(CommandLineTest, AppendSwitches) { |
109 std::wstring switch1 = L"switch1"; | 101 std::wstring switch1 = L"switch1"; |
110 std::wstring switch2 = L"switch2"; | 102 std::wstring switch2 = L"switch2"; |
111 std::wstring value = L"value"; | 103 std::wstring value = L"value"; |
112 std::wstring switch3 = L"switch3"; | 104 std::wstring switch3 = L"switch3"; |
113 std::wstring value3 = L"a value with spaces"; | 105 std::wstring value3 = L"a value with spaces"; |
114 std::wstring switch4 = L"switch4"; | 106 std::wstring switch4 = L"switch4"; |
115 std::wstring value4 = L"\"a value with quotes\""; | 107 std::wstring value4 = L"\"a value with quotes\""; |
116 | 108 |
117 #if defined(OS_WIN) | 109 #if defined(OS_WIN) |
118 std::wstring cl_string = L"Program"; | 110 CommandLine cl(L"Program"); |
119 CommandLine::AppendSwitch(&cl_string, switch1); | |
120 CommandLine::AppendSwitchWithValue(&cl_string, switch2, value); | |
121 CommandLine::AppendSwitchWithValue(&cl_string, switch3, value3); | |
122 CommandLine::AppendSwitchWithValue(&cl_string, switch4, value4); | |
123 CommandLine cl(cl_string); | |
124 #elif defined(OS_POSIX) | 111 #elif defined(OS_POSIX) |
125 std::vector<std::string> argv; | 112 std::vector<std::string> argv; |
126 argv.push_back(std::string("Program")); | 113 argv.push_back(std::string("Program")); |
127 argv.push_back(WideToUTF8(CommandLine::PrefixedSwitchString(switch1))); | |
128 argv.push_back(WideToUTF8(CommandLine::PrefixedSwitchStringWithValue( | |
129 switch2, value))); | |
130 argv.push_back(WideToUTF8(CommandLine::PrefixedSwitchStringWithValue( | |
131 switch3, value3))); | |
132 argv.push_back(WideToUTF8(CommandLine::PrefixedSwitchStringWithValue( | |
133 switch4, value4.substr(1, value4.length() - 2)))); | |
134 CommandLine cl(argv); | 114 CommandLine cl(argv); |
135 #endif | 115 #endif |
136 | 116 |
| 117 cl.AppendSwitch(switch1); |
| 118 cl.AppendSwitchWithValue(switch2, value); |
| 119 cl.AppendSwitchWithValue(switch3, value3); |
| 120 cl.AppendSwitchWithValue(switch4, value4); |
| 121 |
137 EXPECT_TRUE(cl.HasSwitch(switch1)); | 122 EXPECT_TRUE(cl.HasSwitch(switch1)); |
138 EXPECT_TRUE(cl.HasSwitch(switch2)); | 123 EXPECT_TRUE(cl.HasSwitch(switch2)); |
139 EXPECT_EQ(value, cl.GetSwitchValue(switch2)); | 124 EXPECT_EQ(value, cl.GetSwitchValue(switch2)); |
140 EXPECT_TRUE(cl.HasSwitch(switch3)); | 125 EXPECT_TRUE(cl.HasSwitch(switch3)); |
141 EXPECT_EQ(value3, cl.GetSwitchValue(switch3)); | 126 EXPECT_EQ(value3, cl.GetSwitchValue(switch3)); |
142 EXPECT_TRUE(cl.HasSwitch(switch4)); | 127 EXPECT_TRUE(cl.HasSwitch(switch4)); |
143 EXPECT_EQ(value4.substr(1, value4.length() - 2), cl.GetSwitchValue(switch4)); | 128 EXPECT_EQ(value4, cl.GetSwitchValue(switch4)); |
144 } | 129 } |
145 | 130 |
OLD | NEW |