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 "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 class CommandLineTest : public testing::Test { | 15 class CommandLineTest : public testing::Test { |
15 }; | 16 }; |
16 }; | 17 }; |
17 | 18 |
18 TEST(CommandLineTest, CommandLineConstructor) { | 19 TEST(CommandLineTest, CommandLineConstructor) { |
19 #ifdef OS_WIN | 20 #ifdef OS_WIN |
20 CommandLine cl(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " | 21 CommandLine cl(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 ++iter; | 68 ++iter; |
68 EXPECT_EQ(L"flan", *iter); | 69 EXPECT_EQ(L"flan", *iter); |
69 ++iter; | 70 ++iter; |
70 EXPECT_EQ(L"--", *iter); | 71 EXPECT_EQ(L"--", *iter); |
71 ++iter; | 72 ++iter; |
72 EXPECT_EQ(L"--not-a-switch", *iter); | 73 EXPECT_EQ(L"--not-a-switch", *iter); |
73 ++iter; | 74 ++iter; |
74 EXPECT_EQ(L"in the time of submarines...", *iter); | 75 EXPECT_EQ(L"in the time of submarines...", *iter); |
75 ++iter; | 76 ++iter; |
76 EXPECT_TRUE(iter == cl.GetLooseValuesEnd()); | 77 EXPECT_TRUE(iter == cl.GetLooseValuesEnd()); |
| 78 #if defined(OS_POSIX) |
| 79 std::vector<std::string> argvec = cl.argv(); |
| 80 |
| 81 for (size_t i = 0; i < argvec.size(); i++) { |
| 82 EXPECT_EQ(0, argvec[i].compare(argv[i])); |
| 83 } |
| 84 #endif |
77 } | 85 } |
78 | 86 |
79 // These test the command line used to invoke the unit test. | 87 // These test the command line used to invoke the unit test. |
80 TEST(CommandLineTest, DefaultConstructor) { | 88 TEST(CommandLineTest, DefaultConstructor) { |
81 CommandLine cl; | 89 CommandLine cl; |
82 EXPECT_FALSE(cl.command_line_string().empty()); | 90 EXPECT_FALSE(cl.command_line_string().empty()); |
83 EXPECT_FALSE(cl.program().empty()); | 91 EXPECT_FALSE(cl.program().empty()); |
84 } | 92 } |
85 | 93 |
86 // Tests behavior with an empty input string. | 94 // Tests behavior with an empty input string. |
87 TEST(CommandLineTest, EmptyString) { | 95 TEST(CommandLineTest, EmptyString) { |
88 #if defined(OS_WIN) | 96 #if defined(OS_WIN) |
89 CommandLine cl(L""); | 97 CommandLine cl(L""); |
90 #elif defined(OS_POSIX) | 98 #elif defined(OS_POSIX) |
91 CommandLine cl(0, NULL); | 99 CommandLine cl(0, NULL); |
| 100 EXPECT_TRUE(cl.argv().size() == 0); |
92 #endif | 101 #endif |
93 EXPECT_TRUE(cl.command_line_string().empty()); | 102 EXPECT_TRUE(cl.command_line_string().empty()); |
94 EXPECT_TRUE(cl.program().empty()); | 103 EXPECT_TRUE(cl.program().empty()); |
95 EXPECT_EQ(0U, cl.GetLooseValueCount()); | 104 EXPECT_EQ(0U, cl.GetLooseValueCount()); |
96 } | 105 } |
97 | 106 |
98 // Test static functions for appending switches to a command line. | 107 // Test static functions for appending switches to a command line. |
99 // TODO(pinkerton): non-windows platforms don't have the requisite ctor here, so | |
100 // we need something that tests AppendSwitches in another way (if even desired). | |
101 #if defined(OS_WIN) | |
102 TEST(CommandLineTest, AppendSwitches) { | 108 TEST(CommandLineTest, AppendSwitches) { |
103 std::wstring cl_string = L"Program"; | |
104 std::wstring switch1 = L"switch1"; | 109 std::wstring switch1 = L"switch1"; |
105 std::wstring switch2 = L"switch2"; | 110 std::wstring switch2 = L"switch2"; |
106 std::wstring value = L"value"; | 111 std::wstring value = L"value"; |
107 std::wstring switch3 = L"switch3"; | 112 std::wstring switch3 = L"switch3"; |
108 std::wstring value3 = L"a value with spaces"; | 113 std::wstring value3 = L"a value with spaces"; |
109 std::wstring switch4 = L"switch4"; | 114 std::wstring switch4 = L"switch4"; |
110 std::wstring value4 = L"\"a value with quotes\""; | 115 std::wstring value4 = L"\"a value with quotes\""; |
111 | 116 |
| 117 #if defined(OS_WIN) |
| 118 std::wstring cl_string = L"Program"; |
112 CommandLine::AppendSwitch(&cl_string, switch1); | 119 CommandLine::AppendSwitch(&cl_string, switch1); |
113 CommandLine::AppendSwitchWithValue(&cl_string, switch2, value); | 120 CommandLine::AppendSwitchWithValue(&cl_string, switch2, value); |
114 CommandLine::AppendSwitchWithValue(&cl_string, switch3, value3); | 121 CommandLine::AppendSwitchWithValue(&cl_string, switch3, value3); |
115 CommandLine::AppendSwitchWithValue(&cl_string, switch4, value4); | 122 CommandLine::AppendSwitchWithValue(&cl_string, switch4, value4); |
116 CommandLine cl(cl_string); | 123 CommandLine cl(cl_string); |
| 124 #elif defined(OS_POSIX) |
| 125 std::vector<std::string> argv; |
| 126 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); |
| 135 #endif |
117 | 136 |
118 EXPECT_TRUE(cl.HasSwitch(switch1)); | 137 EXPECT_TRUE(cl.HasSwitch(switch1)); |
119 EXPECT_TRUE(cl.HasSwitch(switch2)); | 138 EXPECT_TRUE(cl.HasSwitch(switch2)); |
120 EXPECT_EQ(value, cl.GetSwitchValue(switch2)); | 139 EXPECT_EQ(value, cl.GetSwitchValue(switch2)); |
121 EXPECT_TRUE(cl.HasSwitch(switch3)); | 140 EXPECT_TRUE(cl.HasSwitch(switch3)); |
122 EXPECT_EQ(value3, cl.GetSwitchValue(switch3)); | 141 EXPECT_EQ(value3, cl.GetSwitchValue(switch3)); |
123 EXPECT_TRUE(cl.HasSwitch(switch4)); | 142 EXPECT_TRUE(cl.HasSwitch(switch4)); |
124 EXPECT_EQ(value4.substr(1, value4.length() - 2), cl.GetSwitchValue(switch4)); | 143 EXPECT_EQ(value4.substr(1, value4.length() - 2), cl.GetSwitchValue(switch4)); |
125 } | 144 } |
126 #endif | 145 |
OLD | NEW |