blob: 563b060e2e9f24376f2e361892b1e0c9fdfa737e [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
initial.commitd7cae122008-07-26 21:49:3814TEST(CommandLineTest, CommandLineConstructor) {
[email protected]bb975362009-01-21 01:00:2215#if defined(OS_WIN)
16 CommandLine cl(L"");
17 cl.ParseFromString(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
18 L"--other-switches=\"--dog=canine --cat=feline\" "
19 L"-spaetzle=Crepe -=loosevalue flan "
20 L"--input-translation=\"45\"--output-rotation "
21 L"-- -- --not-a-switch "
22 L"\"in the time of submarines...\"");
23 EXPECT_FALSE(cl.command_line_string().empty());
[email protected]4e44b4d2008-08-12 03:12:4124#elif defined(OS_POSIX)
[email protected]bb975362009-01-21 01:00:2225 const char* argv[] = {"program", "--foo=", "-bar",
26 "-spaetzel=pierogi", "-baz", "flim",
[email protected]e63d5982008-08-14 22:09:3927 "--other-switches=--dog=canine --cat=feline",
28 "-spaetzle=Crepe", "-=loosevalue", "flan",
29 "--input-translation=45--output-rotation",
[email protected]02c87962008-10-06 10:25:3530 "--", "--", "--not-a-switch",
[email protected]e63d5982008-08-14 22:09:3931 "in the time of submarines..."};
[email protected]f3adb5c2008-08-07 20:07:3232 CommandLine cl(arraysize(argv), argv);
33#endif
initial.commitd7cae122008-07-26 21:49:3834 EXPECT_FALSE(cl.HasSwitch(L"cruller"));
35 EXPECT_FALSE(cl.HasSwitch(L"flim"));
36 EXPECT_FALSE(cl.HasSwitch(L"program"));
37 EXPECT_FALSE(cl.HasSwitch(L"dog"));
38 EXPECT_FALSE(cl.HasSwitch(L"cat"));
39 EXPECT_FALSE(cl.HasSwitch(L"output-rotation"));
[email protected]02c87962008-10-06 10:25:3540 EXPECT_FALSE(cl.HasSwitch(L"not-a-switch"));
41 EXPECT_FALSE(cl.HasSwitch(L"--"));
initial.commitd7cae122008-07-26 21:49:3842
43 EXPECT_EQ(L"program", cl.program());
44
45 EXPECT_TRUE(cl.HasSwitch(L"foo"));
46 EXPECT_TRUE(cl.HasSwitch(L"bar"));
47 EXPECT_TRUE(cl.HasSwitch(L"baz"));
48 EXPECT_TRUE(cl.HasSwitch(L"spaetzle"));
[email protected]bb975362009-01-21 01:00:2249#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3850 EXPECT_TRUE(cl.HasSwitch(L"SPAETZLE"));
[email protected]bb975362009-01-21 01:00:2251#endif
initial.commitd7cae122008-07-26 21:49:3852 EXPECT_TRUE(cl.HasSwitch(L"other-switches"));
53 EXPECT_TRUE(cl.HasSwitch(L"input-translation"));
54
55 EXPECT_EQ(L"Crepe", cl.GetSwitchValue(L"spaetzle"));
56 EXPECT_EQ(L"", cl.GetSwitchValue(L"Foo"));
57 EXPECT_EQ(L"", cl.GetSwitchValue(L"bar"));
58 EXPECT_EQ(L"", cl.GetSwitchValue(L"cruller"));
59 EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue(L"other-switches"));
60 EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue(L"input-translation"));
61
[email protected]bb975362009-01-21 01:00:2262 std::vector<std::wstring> loose_values = cl.GetLooseValues();
63 ASSERT_EQ(5U, loose_values.size());
initial.commitd7cae122008-07-26 21:49:3864
[email protected]bb975362009-01-21 01:00:2265 std::vector<std::wstring>::const_iterator iter = loose_values.begin();
initial.commitd7cae122008-07-26 21:49:3866 EXPECT_EQ(L"flim", *iter);
67 ++iter;
68 EXPECT_EQ(L"flan", *iter);
69 ++iter;
[email protected]02c87962008-10-06 10:25:3570 EXPECT_EQ(L"--", *iter);
71 ++iter;
72 EXPECT_EQ(L"--not-a-switch", *iter);
73 ++iter;
initial.commitd7cae122008-07-26 21:49:3874 EXPECT_EQ(L"in the time of submarines...", *iter);
75 ++iter;
[email protected]bb975362009-01-21 01:00:2276 EXPECT_TRUE(iter == loose_values.end());
[email protected]10e42bf2008-10-15 21:59:0877#if defined(OS_POSIX)
[email protected]bb975362009-01-21 01:00:2278 const std::vector<std::string>& argvec = cl.argv();
[email protected]10e42bf2008-10-15 21:59:0879
80 for (size_t i = 0; i < argvec.size(); i++) {
81 EXPECT_EQ(0, argvec[i].compare(argv[i]));
82 }
83#endif
initial.commitd7cae122008-07-26 21:49:3884}
85
initial.commitd7cae122008-07-26 21:49:3886// Tests behavior with an empty input string.
87TEST(CommandLineTest, EmptyString) {
[email protected]f3adb5c2008-08-07 20:07:3288#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3889 CommandLine cl(L"");
[email protected]bb975362009-01-21 01:00:2290 EXPECT_TRUE(cl.command_line_string().empty());
91 EXPECT_TRUE(cl.program().empty());
[email protected]f3adb5c2008-08-07 20:07:3292#elif defined(OS_POSIX)
[email protected]4e44b4d2008-08-12 03:12:4193 CommandLine cl(0, NULL);
[email protected]10e42bf2008-10-15 21:59:0894 EXPECT_TRUE(cl.argv().size() == 0);
[email protected]f3adb5c2008-08-07 20:07:3295#endif
[email protected]bb975362009-01-21 01:00:2296 EXPECT_EQ(0U, cl.GetLooseValues().size());
initial.commitd7cae122008-07-26 21:49:3897}
98
[email protected]bb975362009-01-21 01:00:2299// Test methods for appending switches to a command line.
initial.commitd7cae122008-07-26 21:49:38100TEST(CommandLineTest, AppendSwitches) {
initial.commitd7cae122008-07-26 21:49:38101 std::wstring switch1 = L"switch1";
102 std::wstring switch2 = L"switch2";
103 std::wstring value = L"value";
104 std::wstring switch3 = L"switch3";
105 std::wstring value3 = L"a value with spaces";
106 std::wstring switch4 = L"switch4";
107 std::wstring value4 = L"\"a value with quotes\"";
108
[email protected]10e42bf2008-10-15 21:59:08109#if defined(OS_WIN)
[email protected]bb975362009-01-21 01:00:22110 CommandLine cl(L"Program");
[email protected]10e42bf2008-10-15 21:59:08111#elif defined(OS_POSIX)
112 std::vector<std::string> argv;
113 argv.push_back(std::string("Program"));
[email protected]10e42bf2008-10-15 21:59:08114 CommandLine cl(argv);
115#endif
initial.commitd7cae122008-07-26 21:49:38116
[email protected]bb975362009-01-21 01:00:22117 cl.AppendSwitch(switch1);
118 cl.AppendSwitchWithValue(switch2, value);
119 cl.AppendSwitchWithValue(switch3, value3);
120 cl.AppendSwitchWithValue(switch4, value4);
121
initial.commitd7cae122008-07-26 21:49:38122 EXPECT_TRUE(cl.HasSwitch(switch1));
123 EXPECT_TRUE(cl.HasSwitch(switch2));
124 EXPECT_EQ(value, cl.GetSwitchValue(switch2));
125 EXPECT_TRUE(cl.HasSwitch(switch3));
126 EXPECT_EQ(value3, cl.GetSwitchValue(switch3));
[email protected]8c9510d2008-10-10 21:38:20127 EXPECT_TRUE(cl.HasSwitch(switch4));
[email protected]bb975362009-01-21 01:00:22128 EXPECT_EQ(value4, cl.GetSwitchValue(switch4));
initial.commitd7cae122008-07-26 21:49:38129}
[email protected]10e42bf2008-10-15 21:59:08130