blob: 504c8522b1e01cc0d9e44325bb58ed6afa8c8ecf [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commitd7cae122008-07-26 21:49:384
5#include <string>
6#include <vector>
7
8#include "base/command_line.h"
[email protected]f3adb5c2008-08-07 20:07:329#include "base/basictypes.h"
initial.commitd7cae122008-07-26 21:49:3810#include "base/logging.h"
[email protected]10e42bf2008-10-15 21:59:0811#include "base/string_util.h"
initial.commitd7cae122008-07-26 21:49:3812#include "testing/gtest/include/gtest/gtest.h"
13
14namespace {
15 class CommandLineTest : public testing::Test {
16 };
17};
18
19TEST(CommandLineTest, CommandLineConstructor) {
[email protected]f3adb5c2008-08-07 20:07:3220#ifdef OS_WIN
initial.commitd7cae122008-07-26 21:49:3821 CommandLine cl(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
22 L"--other-switches=\"--dog=canine --cat=feline\" "
23 L"-spaetzle=Crepe -=loosevalue flan "
24 L"--input-translation=\"45\"--output-rotation "
[email protected]02c87962008-10-06 10:25:3525 L"-- -- --not-a-switch "
initial.commitd7cae122008-07-26 21:49:3826 L"\"in the time of submarines...\"");
[email protected]4e44b4d2008-08-12 03:12:4127#elif defined(OS_POSIX)
[email protected]e63d5982008-08-14 22:09:3928 const char* argv[] = {"program", "--foo=", "-bAr",
29 "-Spaetzel=pierogi", "-Baz", "flim",
30 "--other-switches=--dog=canine --cat=feline",
31 "-spaetzle=Crepe", "-=loosevalue", "flan",
32 "--input-translation=45--output-rotation",
[email protected]02c87962008-10-06 10:25:3533 "--", "--", "--not-a-switch",
[email protected]e63d5982008-08-14 22:09:3934 "in the time of submarines..."};
[email protected]f3adb5c2008-08-07 20:07:3235 CommandLine cl(arraysize(argv), argv);
36#endif
initial.commitd7cae122008-07-26 21:49:3837 EXPECT_FALSE(cl.command_line_string().empty());
38 EXPECT_FALSE(cl.HasSwitch(L"cruller"));
39 EXPECT_FALSE(cl.HasSwitch(L"flim"));
40 EXPECT_FALSE(cl.HasSwitch(L"program"));
41 EXPECT_FALSE(cl.HasSwitch(L"dog"));
42 EXPECT_FALSE(cl.HasSwitch(L"cat"));
43 EXPECT_FALSE(cl.HasSwitch(L"output-rotation"));
[email protected]02c87962008-10-06 10:25:3544 EXPECT_FALSE(cl.HasSwitch(L"not-a-switch"));
45 EXPECT_FALSE(cl.HasSwitch(L"--"));
initial.commitd7cae122008-07-26 21:49:3846
47 EXPECT_EQ(L"program", cl.program());
48
49 EXPECT_TRUE(cl.HasSwitch(L"foo"));
50 EXPECT_TRUE(cl.HasSwitch(L"bar"));
51 EXPECT_TRUE(cl.HasSwitch(L"baz"));
52 EXPECT_TRUE(cl.HasSwitch(L"spaetzle"));
53 EXPECT_TRUE(cl.HasSwitch(L"SPAETZLE"));
54 EXPECT_TRUE(cl.HasSwitch(L"other-switches"));
55 EXPECT_TRUE(cl.HasSwitch(L"input-translation"));
56
57 EXPECT_EQ(L"Crepe", cl.GetSwitchValue(L"spaetzle"));
58 EXPECT_EQ(L"", cl.GetSwitchValue(L"Foo"));
59 EXPECT_EQ(L"", cl.GetSwitchValue(L"bar"));
60 EXPECT_EQ(L"", cl.GetSwitchValue(L"cruller"));
61 EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue(L"other-switches"));
62 EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue(L"input-translation"));
63
[email protected]02c87962008-10-06 10:25:3564 EXPECT_EQ(5U, cl.GetLooseValueCount());
initial.commitd7cae122008-07-26 21:49:3865
66 CommandLine::LooseValueIterator iter = cl.GetLooseValuesBegin();
67 EXPECT_EQ(L"flim", *iter);
68 ++iter;
69 EXPECT_EQ(L"flan", *iter);
70 ++iter;
[email protected]02c87962008-10-06 10:25:3571 EXPECT_EQ(L"--", *iter);
72 ++iter;
73 EXPECT_EQ(L"--not-a-switch", *iter);
74 ++iter;
initial.commitd7cae122008-07-26 21:49:3875 EXPECT_EQ(L"in the time of submarines...", *iter);
76 ++iter;
77 EXPECT_TRUE(iter == cl.GetLooseValuesEnd());
[email protected]10e42bf2008-10-15 21:59:0878#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
initial.commitd7cae122008-07-26 21:49:3885}
86
87// These test the command line used to invoke the unit test.
88TEST(CommandLineTest, DefaultConstructor) {
89 CommandLine cl;
90 EXPECT_FALSE(cl.command_line_string().empty());
91 EXPECT_FALSE(cl.program().empty());
92}
93
94// Tests behavior with an empty input string.
95TEST(CommandLineTest, EmptyString) {
[email protected]f3adb5c2008-08-07 20:07:3296#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3897 CommandLine cl(L"");
[email protected]f3adb5c2008-08-07 20:07:3298#elif defined(OS_POSIX)
[email protected]4e44b4d2008-08-12 03:12:4199 CommandLine cl(0, NULL);
[email protected]10e42bf2008-10-15 21:59:08100 EXPECT_TRUE(cl.argv().size() == 0);
[email protected]f3adb5c2008-08-07 20:07:32101#endif
initial.commitd7cae122008-07-26 21:49:38102 EXPECT_TRUE(cl.command_line_string().empty());
103 EXPECT_TRUE(cl.program().empty());
[email protected]cb2f3632008-08-14 20:27:29104 EXPECT_EQ(0U, cl.GetLooseValueCount());
initial.commitd7cae122008-07-26 21:49:38105}
106
107// Test static functions for appending switches to a command line.
108TEST(CommandLineTest, AppendSwitches) {
initial.commitd7cae122008-07-26 21:49:38109 std::wstring switch1 = L"switch1";
110 std::wstring switch2 = L"switch2";
111 std::wstring value = L"value";
112 std::wstring switch3 = L"switch3";
113 std::wstring value3 = L"a value with spaces";
114 std::wstring switch4 = L"switch4";
115 std::wstring value4 = L"\"a value with quotes\"";
116
[email protected]10e42bf2008-10-15 21:59:08117#if defined(OS_WIN)
118 std::wstring cl_string = L"Program";
initial.commitd7cae122008-07-26 21:49:38119 CommandLine::AppendSwitch(&cl_string, switch1);
120 CommandLine::AppendSwitchWithValue(&cl_string, switch2, value);
121 CommandLine::AppendSwitchWithValue(&cl_string, switch3, value3);
122 CommandLine::AppendSwitchWithValue(&cl_string, switch4, value4);
123 CommandLine cl(cl_string);
[email protected]10e42bf2008-10-15 21:59:08124#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
initial.commitd7cae122008-07-26 21:49:38136
137 EXPECT_TRUE(cl.HasSwitch(switch1));
138 EXPECT_TRUE(cl.HasSwitch(switch2));
139 EXPECT_EQ(value, cl.GetSwitchValue(switch2));
140 EXPECT_TRUE(cl.HasSwitch(switch3));
141 EXPECT_EQ(value3, cl.GetSwitchValue(switch3));
[email protected]8c9510d2008-10-10 21:38:20142 EXPECT_TRUE(cl.HasSwitch(switch4));
initial.commitd7cae122008-07-26 21:49:38143 EXPECT_EQ(value4.substr(1, value4.length() - 2), cl.GetSwitchValue(switch4));
144}
[email protected]10e42bf2008-10-15 21:59:08145