blob: c477827325512baf59f15da518523894f12292c4 [file] [log] [blame]
initial.commit3f4a7322008-07-27 06:49:38 +09001// 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"
evanm@google.com91cdff82008-08-08 05:07:32 +090034#include "base/basictypes.h"
initial.commit3f4a7322008-07-27 06:49:38 +090035#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) {
evanm@google.com91cdff82008-08-08 05:07:32 +090044#ifdef OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +090045 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...\"");
mmentovai@google.comcd20e5c2008-08-12 12:12:41 +090050#elif defined(OS_POSIX)
tc@google.com1bc8d522008-08-15 07:09:39 +090051 const char* argv[] = {"program", "--foo=", "-bAr",
52 "-Spaetzel=pierogi", "-Baz", "flim",
53 "--other-switches=--dog=canine --cat=feline",
54 "-spaetzle=Crepe", "-=loosevalue", "flan",
55 "--input-translation=45--output-rotation",
56 "in the time of submarines..."};
evanm@google.com91cdff82008-08-08 05:07:32 +090057 CommandLine cl(arraysize(argv), argv);
58#endif
initial.commit3f4a7322008-07-27 06:49:38 +090059 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
darin@google.comf3272802008-08-15 05:27:29 +090084 EXPECT_EQ(3U, cl.GetLooseValueCount());
initial.commit3f4a7322008-07-27 06:49:38 +090085
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) {
evanm@google.com91cdff82008-08-08 05:07:32 +0900105#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900106 CommandLine cl(L"");
evanm@google.com91cdff82008-08-08 05:07:32 +0900107#elif defined(OS_POSIX)
mmentovai@google.comcd20e5c2008-08-12 12:12:41 +0900108 CommandLine cl(0, NULL);
evanm@google.com91cdff82008-08-08 05:07:32 +0900109#endif
initial.commit3f4a7322008-07-27 06:49:38 +0900110 EXPECT_TRUE(cl.command_line_string().empty());
111 EXPECT_TRUE(cl.program().empty());
darin@google.comf3272802008-08-15 05:27:29 +0900112 EXPECT_EQ(0U, cl.GetLooseValueCount());
initial.commit3f4a7322008-07-27 06:49:38 +0900113}
114
115// Test static functions for appending switches to a command line.
evanm@google.com91cdff82008-08-08 05:07:32 +0900116// TODO(pinkerton): non-windows platforms don't have the requisite ctor here, so
117// we need something that tests AppendSwitches in another way (if even desired).
118#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +0900119TEST(CommandLineTest, AppendSwitches) {
120 std::wstring cl_string = L"Program";
121 std::wstring switch1 = L"switch1";
122 std::wstring switch2 = L"switch2";
123 std::wstring value = L"value";
124 std::wstring switch3 = L"switch3";
125 std::wstring value3 = L"a value with spaces";
126 std::wstring switch4 = L"switch4";
127 std::wstring value4 = L"\"a value with quotes\"";
128
129 CommandLine::AppendSwitch(&cl_string, switch1);
130 CommandLine::AppendSwitchWithValue(&cl_string, switch2, value);
131 CommandLine::AppendSwitchWithValue(&cl_string, switch3, value3);
132 CommandLine::AppendSwitchWithValue(&cl_string, switch4, value4);
133 CommandLine cl(cl_string);
134
135 EXPECT_TRUE(cl.HasSwitch(switch1));
136 EXPECT_TRUE(cl.HasSwitch(switch2));
137 EXPECT_EQ(value, cl.GetSwitchValue(switch2));
138 EXPECT_TRUE(cl.HasSwitch(switch3));
139 EXPECT_EQ(value3, cl.GetSwitchValue(switch3));
140 EXPECT_TRUE(cl.HasSwitch(switch2));
141 EXPECT_EQ(value4.substr(1, value4.length() - 2), cl.GetSwitchValue(switch4));
142}
evanm@google.com91cdff82008-08-08 05:07:32 +0900143#endif