blob: f3d63cb29117a97604f1ad252768e9c87fa36344 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#include <string>
6#include <vector>
7
8#include "base/command_line.h"
evanm@google.com91cdff82008-08-08 05:07:32 +09009#include "base/basictypes.h"
initial.commit3f4a7322008-07-27 06:49:38 +090010#include "base/logging.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace {
14 class CommandLineTest : public testing::Test {
15 };
16};
17
18TEST(CommandLineTest, CommandLineConstructor) {
evanm@google.com91cdff82008-08-08 05:07:32 +090019#ifdef OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +090020 CommandLine cl(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
21 L"--other-switches=\"--dog=canine --cat=feline\" "
22 L"-spaetzle=Crepe -=loosevalue flan "
23 L"--input-translation=\"45\"--output-rotation "
24 L"\"in the time of submarines...\"");
mmentovai@google.comcd20e5c2008-08-12 12:12:41 +090025#elif defined(OS_POSIX)
tc@google.com1bc8d522008-08-15 07:09:39 +090026 const char* argv[] = {"program", "--foo=", "-bAr",
27 "-Spaetzel=pierogi", "-Baz", "flim",
28 "--other-switches=--dog=canine --cat=feline",
29 "-spaetzle=Crepe", "-=loosevalue", "flan",
30 "--input-translation=45--output-rotation",
31 "in the time of submarines..."};
evanm@google.com91cdff82008-08-08 05:07:32 +090032 CommandLine cl(arraysize(argv), argv);
33#endif
initial.commit3f4a7322008-07-27 06:49:38 +090034 EXPECT_FALSE(cl.command_line_string().empty());
35 EXPECT_FALSE(cl.HasSwitch(L"cruller"));
36 EXPECT_FALSE(cl.HasSwitch(L"flim"));
37 EXPECT_FALSE(cl.HasSwitch(L"program"));
38 EXPECT_FALSE(cl.HasSwitch(L"dog"));
39 EXPECT_FALSE(cl.HasSwitch(L"cat"));
40 EXPECT_FALSE(cl.HasSwitch(L"output-rotation"));
41
42 EXPECT_EQ(L"program", cl.program());
43
44 EXPECT_TRUE(cl.HasSwitch(L"foo"));
45 EXPECT_TRUE(cl.HasSwitch(L"bar"));
46 EXPECT_TRUE(cl.HasSwitch(L"baz"));
47 EXPECT_TRUE(cl.HasSwitch(L"spaetzle"));
48 EXPECT_TRUE(cl.HasSwitch(L"SPAETZLE"));
49 EXPECT_TRUE(cl.HasSwitch(L"other-switches"));
50 EXPECT_TRUE(cl.HasSwitch(L"input-translation"));
51
52 EXPECT_EQ(L"Crepe", cl.GetSwitchValue(L"spaetzle"));
53 EXPECT_EQ(L"", cl.GetSwitchValue(L"Foo"));
54 EXPECT_EQ(L"", cl.GetSwitchValue(L"bar"));
55 EXPECT_EQ(L"", cl.GetSwitchValue(L"cruller"));
56 EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue(L"other-switches"));
57 EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue(L"input-translation"));
58
darin@google.comf3272802008-08-15 05:27:29 +090059 EXPECT_EQ(3U, cl.GetLooseValueCount());
initial.commit3f4a7322008-07-27 06:49:38 +090060
61 CommandLine::LooseValueIterator iter = cl.GetLooseValuesBegin();
62 EXPECT_EQ(L"flim", *iter);
63 ++iter;
64 EXPECT_EQ(L"flan", *iter);
65 ++iter;
66 EXPECT_EQ(L"in the time of submarines...", *iter);
67 ++iter;
68 EXPECT_TRUE(iter == cl.GetLooseValuesEnd());
69}
70
71// These test the command line used to invoke the unit test.
72TEST(CommandLineTest, DefaultConstructor) {
73 CommandLine cl;
74 EXPECT_FALSE(cl.command_line_string().empty());
75 EXPECT_FALSE(cl.program().empty());
76}
77
78// Tests behavior with an empty input string.
79TEST(CommandLineTest, EmptyString) {
evanm@google.com91cdff82008-08-08 05:07:32 +090080#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090081 CommandLine cl(L"");
evanm@google.com91cdff82008-08-08 05:07:32 +090082#elif defined(OS_POSIX)
mmentovai@google.comcd20e5c2008-08-12 12:12:41 +090083 CommandLine cl(0, NULL);
evanm@google.com91cdff82008-08-08 05:07:32 +090084#endif
initial.commit3f4a7322008-07-27 06:49:38 +090085 EXPECT_TRUE(cl.command_line_string().empty());
86 EXPECT_TRUE(cl.program().empty());
darin@google.comf3272802008-08-15 05:27:29 +090087 EXPECT_EQ(0U, cl.GetLooseValueCount());
initial.commit3f4a7322008-07-27 06:49:38 +090088}
89
90// Test static functions for appending switches to a command line.
evanm@google.com91cdff82008-08-08 05:07:32 +090091// TODO(pinkerton): non-windows platforms don't have the requisite ctor here, so
92// we need something that tests AppendSwitches in another way (if even desired).
93#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090094TEST(CommandLineTest, AppendSwitches) {
95 std::wstring cl_string = L"Program";
96 std::wstring switch1 = L"switch1";
97 std::wstring switch2 = L"switch2";
98 std::wstring value = L"value";
99 std::wstring switch3 = L"switch3";
100 std::wstring value3 = L"a value with spaces";
101 std::wstring switch4 = L"switch4";
102 std::wstring value4 = L"\"a value with quotes\"";
103
104 CommandLine::AppendSwitch(&cl_string, switch1);
105 CommandLine::AppendSwitchWithValue(&cl_string, switch2, value);
106 CommandLine::AppendSwitchWithValue(&cl_string, switch3, value3);
107 CommandLine::AppendSwitchWithValue(&cl_string, switch4, value4);
108 CommandLine cl(cl_string);
109
110 EXPECT_TRUE(cl.HasSwitch(switch1));
111 EXPECT_TRUE(cl.HasSwitch(switch2));
112 EXPECT_EQ(value, cl.GetSwitchValue(switch2));
113 EXPECT_TRUE(cl.HasSwitch(switch3));
114 EXPECT_EQ(value3, cl.GetSwitchValue(switch3));
115 EXPECT_TRUE(cl.HasSwitch(switch2));
116 EXPECT_EQ(value4.substr(1, value4.length() - 2), cl.GetSwitchValue(switch4));
117}
evanm@google.com91cdff82008-08-08 05:07:32 +0900118#endif
license.botf003cfe2008-08-24 09:55:55 +0900119