blob: ae619d5293213b4a0ece9ec0fc1584cd3fcb6766 [file] [log] [blame]
initial.commitd7cae122008-07-26 21:49:381// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include <string>
31#include <vector>
32
33#include "base/command_line.h"
[email protected]f3adb5c2008-08-07 20:07:3234#include "base/basictypes.h"
initial.commitd7cae122008-07-26 21:49:3835#include "base/logging.h"
36#include "testing/gtest/include/gtest/gtest.h"
37
38namespace {
39 class CommandLineTest : public testing::Test {
40 };
41};
42
43TEST(CommandLineTest, CommandLineConstructor) {
[email protected]f3adb5c2008-08-07 20:07:3244#ifdef OS_WIN
initial.commitd7cae122008-07-26 21:49:3845 CommandLine cl(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
46 L"--other-switches=\"--dog=canine --cat=feline\" "
47 L"-spaetzle=Crepe -=loosevalue flan "
48 L"--input-translation=\"45\"--output-rotation "
49 L"\"in the time of submarines...\"");
[email protected]f3adb5c2008-08-07 20:07:3250#elif OS_POSIX
51 const char* argv[] = {"program", "--foo=", "-bAr",
[email protected]5d426332008-08-08 20:46:2152 "-Spaetzel=pierogi", "-Baz", "flim",
53 "--other-switches=--dog=canine --cat=feline",
[email protected]f3adb5c2008-08-07 20:07:3254 "-spaetzle=Crepe", "-=loosevalue", "flan",
[email protected]5d426332008-08-08 20:46:2155 "--input-translation=45--output-rotation",
56 "in the time of submarines..."};
[email protected]f3adb5c2008-08-07 20:07:3257 CommandLine cl(arraysize(argv), argv);
58#endif
initial.commitd7cae122008-07-26 21:49:3859 EXPECT_FALSE(cl.command_line_string().empty());
60 EXPECT_FALSE(cl.HasSwitch(L"cruller"));
61 EXPECT_FALSE(cl.HasSwitch(L"flim"));
62 EXPECT_FALSE(cl.HasSwitch(L"program"));
63 EXPECT_FALSE(cl.HasSwitch(L"dog"));
64 EXPECT_FALSE(cl.HasSwitch(L"cat"));
65 EXPECT_FALSE(cl.HasSwitch(L"output-rotation"));
66
67 EXPECT_EQ(L"program", cl.program());
68
69 EXPECT_TRUE(cl.HasSwitch(L"foo"));
70 EXPECT_TRUE(cl.HasSwitch(L"bar"));
71 EXPECT_TRUE(cl.HasSwitch(L"baz"));
72 EXPECT_TRUE(cl.HasSwitch(L"spaetzle"));
73 EXPECT_TRUE(cl.HasSwitch(L"SPAETZLE"));
74 EXPECT_TRUE(cl.HasSwitch(L"other-switches"));
75 EXPECT_TRUE(cl.HasSwitch(L"input-translation"));
76
77 EXPECT_EQ(L"Crepe", cl.GetSwitchValue(L"spaetzle"));
78 EXPECT_EQ(L"", cl.GetSwitchValue(L"Foo"));
79 EXPECT_EQ(L"", cl.GetSwitchValue(L"bar"));
80 EXPECT_EQ(L"", cl.GetSwitchValue(L"cruller"));
81 EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue(L"other-switches"));
82 EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue(L"input-translation"));
83
84 EXPECT_EQ(3, cl.GetLooseValueCount());
85
86 CommandLine::LooseValueIterator iter = cl.GetLooseValuesBegin();
87 EXPECT_EQ(L"flim", *iter);
88 ++iter;
89 EXPECT_EQ(L"flan", *iter);
90 ++iter;
91 EXPECT_EQ(L"in the time of submarines...", *iter);
92 ++iter;
93 EXPECT_TRUE(iter == cl.GetLooseValuesEnd());
94}
95
96// These test the command line used to invoke the unit test.
97TEST(CommandLineTest, DefaultConstructor) {
98 CommandLine cl;
99 EXPECT_FALSE(cl.command_line_string().empty());
100 EXPECT_FALSE(cl.program().empty());
101}
102
103// Tests behavior with an empty input string.
104TEST(CommandLineTest, EmptyString) {
[email protected]f3adb5c2008-08-07 20:07:32105#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38106 CommandLine cl(L"");
[email protected]f3adb5c2008-08-07 20:07:32107#elif defined(OS_POSIX)
108 const char* argv[] = {};
109 CommandLine cl(ARRAYSIZE_UNSAFE(argv), argv);
110#endif
initial.commitd7cae122008-07-26 21:49:38111 EXPECT_TRUE(cl.command_line_string().empty());
112 EXPECT_TRUE(cl.program().empty());
113 EXPECT_EQ(0, cl.GetLooseValueCount());
114}
115
116// Test static functions for appending switches to a command line.
[email protected]f3adb5c2008-08-07 20:07:32117// TODO(pinkerton): non-windows platforms don't have the requisite ctor here, so
118// we need something that tests AppendSwitches in another way (if even desired).
119#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:38120TEST(CommandLineTest, AppendSwitches) {
121 std::wstring cl_string = L"Program";
122 std::wstring switch1 = L"switch1";
123 std::wstring switch2 = L"switch2";
124 std::wstring value = L"value";
125 std::wstring switch3 = L"switch3";
126 std::wstring value3 = L"a value with spaces";
127 std::wstring switch4 = L"switch4";
128 std::wstring value4 = L"\"a value with quotes\"";
129
130 CommandLine::AppendSwitch(&cl_string, switch1);
131 CommandLine::AppendSwitchWithValue(&cl_string, switch2, value);
132 CommandLine::AppendSwitchWithValue(&cl_string, switch3, value3);
133 CommandLine::AppendSwitchWithValue(&cl_string, switch4, value4);
134 CommandLine cl(cl_string);
135
136 EXPECT_TRUE(cl.HasSwitch(switch1));
137 EXPECT_TRUE(cl.HasSwitch(switch2));
138 EXPECT_EQ(value, cl.GetSwitchValue(switch2));
139 EXPECT_TRUE(cl.HasSwitch(switch3));
140 EXPECT_EQ(value3, cl.GetSwitchValue(switch3));
141 EXPECT_TRUE(cl.HasSwitch(switch2));
142 EXPECT_EQ(value4.substr(1, value4.length() - 2), cl.GetSwitchValue(switch4));
143}
[email protected]f3adb5c2008-08-07 20:07:32144#endif