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/file_path.h" | 10 #include "base/file_path.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 // To test Windows quoting behavior, we use a string that has some backslashes |
| 15 // and quotes. |
| 16 // Consider the command-line argument: q\"bs1\bs2\\bs3q\\\" |
| 17 // Here it is with C-style escapes. |
| 18 #define TRICKY_QUOTED L"q\\\"bs1\\bs2\\\\bs3q\\\\\\\"" |
| 19 // It should be parsed by Windows as: q"bs1\bs2\\bs3q\" |
| 20 // Here that is with C-style escapes. |
| 21 #define TRICKY L"q\"bs1\\bs2\\\\bs3q\\\"" |
| 22 |
14 TEST(CommandLineTest, CommandLineConstructor) { | 23 TEST(CommandLineTest, CommandLineConstructor) { |
15 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
16 CommandLine cl = CommandLine::FromString( | 25 CommandLine cl = CommandLine::FromString( |
17 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " | 26 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
18 L"--other-switches=\"--dog=canine --cat=feline\" " | 27 L"--other-switches=\"--dog=canine --cat=feline\" " |
19 L"-spaetzle=Crepe -=loosevalue flan " | 28 L"-spaetzle=Crepe -=loosevalue flan " |
20 L"--input-translation=\"45\"--output-rotation " | 29 L"--input-translation=\"45\"--output-rotation " |
| 30 L"--quotes=" TRICKY_QUOTED L" " |
21 L"-- -- --not-a-switch " | 31 L"-- -- --not-a-switch " |
22 L"\"in the time of submarines...\""); | 32 L"\"in the time of submarines...\""); |
23 EXPECT_FALSE(cl.command_line_string().empty()); | 33 EXPECT_FALSE(cl.command_line_string().empty()); |
24 #elif defined(OS_POSIX) | 34 #elif defined(OS_POSIX) |
25 const char* argv[] = {"program", "--foo=", "-bar", | 35 const char* argv[] = {"program", "--foo=", "-bar", |
26 "-spaetzel=pierogi", "-baz", "flim", | 36 "-spaetzel=pierogi", "-baz", "flim", |
27 "--other-switches=--dog=canine --cat=feline", | 37 "--other-switches=--dog=canine --cat=feline", |
28 "-spaetzle=Crepe", "-=loosevalue", "flan", | 38 "-spaetzle=Crepe", "-=loosevalue", "flan", |
29 "--input-translation=45--output-rotation", | 39 "--input-translation=45--output-rotation", |
30 "--", "--", "--not-a-switch", | 40 "--", "--", "--not-a-switch", |
(...skipping 13 matching lines...) Expand all Loading... |
44 | 54 |
45 EXPECT_TRUE(cl.HasSwitch("foo")); | 55 EXPECT_TRUE(cl.HasSwitch("foo")); |
46 EXPECT_TRUE(cl.HasSwitch("bar")); | 56 EXPECT_TRUE(cl.HasSwitch("bar")); |
47 EXPECT_TRUE(cl.HasSwitch("baz")); | 57 EXPECT_TRUE(cl.HasSwitch("baz")); |
48 EXPECT_TRUE(cl.HasSwitch("spaetzle")); | 58 EXPECT_TRUE(cl.HasSwitch("spaetzle")); |
49 #if defined(OS_WIN) | 59 #if defined(OS_WIN) |
50 EXPECT_TRUE(cl.HasSwitch("SPAETZLE")); | 60 EXPECT_TRUE(cl.HasSwitch("SPAETZLE")); |
51 #endif | 61 #endif |
52 EXPECT_TRUE(cl.HasSwitch("other-switches")); | 62 EXPECT_TRUE(cl.HasSwitch("other-switches")); |
53 EXPECT_TRUE(cl.HasSwitch("input-translation")); | 63 EXPECT_TRUE(cl.HasSwitch("input-translation")); |
| 64 #if defined(OS_WIN) |
| 65 EXPECT_TRUE(cl.HasSwitch("quotes")); |
| 66 #endif |
54 | 67 |
55 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); | 68 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); |
56 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); | 69 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); |
57 EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); | 70 EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); |
58 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); | 71 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); |
59 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( | 72 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( |
60 "other-switches")); | 73 "other-switches")); |
61 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); | 74 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); |
| 75 #if defined(OS_WIN) |
| 76 EXPECT_EQ(TRICKY, cl.GetSwitchValueNative("quotes")); |
| 77 #endif |
62 | 78 |
63 const std::vector<CommandLine::StringType>& args = cl.args(); | 79 const std::vector<CommandLine::StringType>& args = cl.args(); |
64 ASSERT_EQ(5U, args.size()); | 80 ASSERT_EQ(5U, args.size()); |
65 | 81 |
66 std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); | 82 std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); |
67 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); | 83 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); |
68 ++iter; | 84 ++iter; |
69 EXPECT_EQ(FILE_PATH_LITERAL("flan"), *iter); | 85 EXPECT_EQ(FILE_PATH_LITERAL("flan"), *iter); |
70 ++iter; | 86 ++iter; |
71 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); | 87 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); |
(...skipping 27 matching lines...) Expand all Loading... |
99 | 115 |
100 // Test methods for appending switches to a command line. | 116 // Test methods for appending switches to a command line. |
101 TEST(CommandLineTest, AppendSwitches) { | 117 TEST(CommandLineTest, AppendSwitches) { |
102 std::string switch1 = "switch1"; | 118 std::string switch1 = "switch1"; |
103 std::string switch2 = "switch2"; | 119 std::string switch2 = "switch2"; |
104 std::string value = "value"; | 120 std::string value = "value"; |
105 std::string switch3 = "switch3"; | 121 std::string switch3 = "switch3"; |
106 std::string value3 = "a value with spaces"; | 122 std::string value3 = "a value with spaces"; |
107 std::string switch4 = "switch4"; | 123 std::string switch4 = "switch4"; |
108 std::string value4 = "\"a value with quotes\""; | 124 std::string value4 = "\"a value with quotes\""; |
| 125 std::string switch5 = "quotes"; |
| 126 std::string value5 = WideToASCII(TRICKY); |
109 | 127 |
110 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); | 128 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
111 | 129 |
112 cl.AppendSwitch(switch1); | 130 cl.AppendSwitch(switch1); |
113 cl.AppendSwitchASCII(switch2, value); | 131 cl.AppendSwitchASCII(switch2, value); |
114 cl.AppendSwitchASCII(switch3, value3); | 132 cl.AppendSwitchASCII(switch3, value3); |
115 cl.AppendSwitchASCII(switch4, value4); | 133 cl.AppendSwitchASCII(switch4, value4); |
| 134 cl.AppendSwitchASCII(switch5, value5); |
116 | 135 |
117 EXPECT_TRUE(cl.HasSwitch(switch1)); | 136 EXPECT_TRUE(cl.HasSwitch(switch1)); |
118 EXPECT_TRUE(cl.HasSwitch(switch2)); | 137 EXPECT_TRUE(cl.HasSwitch(switch2)); |
119 EXPECT_EQ(value, cl.GetSwitchValueASCII(switch2)); | 138 EXPECT_EQ(value, cl.GetSwitchValueASCII(switch2)); |
120 EXPECT_TRUE(cl.HasSwitch(switch3)); | 139 EXPECT_TRUE(cl.HasSwitch(switch3)); |
121 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); | 140 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); |
122 EXPECT_TRUE(cl.HasSwitch(switch4)); | 141 EXPECT_TRUE(cl.HasSwitch(switch4)); |
123 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); | 142 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); |
| 143 EXPECT_TRUE(cl.HasSwitch(switch5)); |
| 144 EXPECT_EQ(value5, cl.GetSwitchValueASCII(switch5)); |
| 145 |
| 146 #if defined(OS_WIN) |
| 147 EXPECT_EQ(L"\"Program\" " |
| 148 L"--switch1 " |
| 149 L"--switch2=value " |
| 150 L"--switch3=\"a value with spaces\" " |
| 151 L"--switch4=\"\\\"a value with quotes\\\"\" " |
| 152 L"--quotes=\"" TRICKY_QUOTED L"\"", |
| 153 cl.command_line_string()); |
| 154 #endif |
124 } | 155 } |
OLD | NEW |