blob: 6544c13a12faea993c6ba61c6438575c4dbf52e1 [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"
[email protected]10e42bf2008-10-15 21:59:0810#include "base/string_util.h"
initial.commitd7cae122008-07-26 21:49:3811#include "testing/gtest/include/gtest/gtest.h"
12
initial.commitd7cae122008-07-26 21:49:3813TEST(CommandLineTest, CommandLineConstructor) {
[email protected]bb975362009-01-21 01:00:2214#if defined(OS_WIN)
[email protected]51343d5a2009-10-26 22:39:3315 CommandLine cl = CommandLine::FromString(
16 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
[email protected]bb975362009-01-21 01:00:2217 L"--other-switches=\"--dog=canine --cat=feline\" "
18 L"-spaetzle=Crepe -=loosevalue flan "
19 L"--input-translation=\"45\"--output-rotation "
20 L"-- -- --not-a-switch "
21 L"\"in the time of submarines...\"");
22 EXPECT_FALSE(cl.command_line_string().empty());
[email protected]4e44b4d2008-08-12 03:12:4123#elif defined(OS_POSIX)
[email protected]bb975362009-01-21 01:00:2224 const char* argv[] = {"program", "--foo=", "-bar",
25 "-spaetzel=pierogi", "-baz", "flim",
[email protected]e63d5982008-08-14 22:09:3926 "--other-switches=--dog=canine --cat=feline",
27 "-spaetzle=Crepe", "-=loosevalue", "flan",
28 "--input-translation=45--output-rotation",
[email protected]02c87962008-10-06 10:25:3529 "--", "--", "--not-a-switch",
[email protected]e63d5982008-08-14 22:09:3930 "in the time of submarines..."};
[email protected]f3adb5c2008-08-07 20:07:3231 CommandLine cl(arraysize(argv), argv);
32#endif
[email protected]b7e0a2a2009-10-13 02:07:2533 EXPECT_FALSE(cl.HasSwitch("cruller"));
34 EXPECT_FALSE(cl.HasSwitch("flim"));
35 EXPECT_FALSE(cl.HasSwitch("program"));
36 EXPECT_FALSE(cl.HasSwitch("dog"));
37 EXPECT_FALSE(cl.HasSwitch("cat"));
38 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
39 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
40 EXPECT_FALSE(cl.HasSwitch("--"));
initial.commitd7cae122008-07-26 21:49:3841
42 EXPECT_EQ(L"program", cl.program());
43
[email protected]b7e0a2a2009-10-13 02:07:2544 EXPECT_TRUE(cl.HasSwitch("foo"));
45 EXPECT_TRUE(cl.HasSwitch("bar"));
46 EXPECT_TRUE(cl.HasSwitch("baz"));
47 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
[email protected]bb975362009-01-21 01:00:2248#if defined(OS_WIN)
[email protected]b7e0a2a2009-10-13 02:07:2549 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
[email protected]bb975362009-01-21 01:00:2250#endif
[email protected]b7e0a2a2009-10-13 02:07:2551 EXPECT_TRUE(cl.HasSwitch("other-switches"));
52 EXPECT_TRUE(cl.HasSwitch("input-translation"));
initial.commitd7cae122008-07-26 21:49:3853
[email protected]c4e52f0d2009-11-06 19:55:1654 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
55 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo"));
56 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
57 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
58 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
59 "other-switches"));
60 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
initial.commitd7cae122008-07-26 21:49:3861
[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)
[email protected]51343d5a2009-10-26 22:39:3389 CommandLine cl = CommandLine::FromString(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) {
[email protected]b7e0a2a2009-10-13 02:07:25101 std::string switch1 = "switch1";
102 std::string switch2 = "switch2";
initial.commitd7cae122008-07-26 21:49:38103 std::wstring value = L"value";
[email protected]b7e0a2a2009-10-13 02:07:25104 std::string switch3 = "switch3";
initial.commitd7cae122008-07-26 21:49:38105 std::wstring value3 = L"a value with spaces";
[email protected]b7e0a2a2009-10-13 02:07:25106 std::string switch4 = "switch4";
initial.commitd7cae122008-07-26 21:49:38107 std::wstring value4 = L"\"a value with quotes\"";
108
[email protected]51343d5a2009-10-26 22:39:33109 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
initial.commitd7cae122008-07-26 21:49:38110
[email protected]bb975362009-01-21 01:00:22111 cl.AppendSwitch(switch1);
112 cl.AppendSwitchWithValue(switch2, value);
113 cl.AppendSwitchWithValue(switch3, value3);
114 cl.AppendSwitchWithValue(switch4, value4);
115
initial.commitd7cae122008-07-26 21:49:38116 EXPECT_TRUE(cl.HasSwitch(switch1));
117 EXPECT_TRUE(cl.HasSwitch(switch2));
118 EXPECT_EQ(value, cl.GetSwitchValue(switch2));
119 EXPECT_TRUE(cl.HasSwitch(switch3));
120 EXPECT_EQ(value3, cl.GetSwitchValue(switch3));
[email protected]8c9510d2008-10-10 21:38:20121 EXPECT_TRUE(cl.HasSwitch(switch4));
[email protected]bb975362009-01-21 01:00:22122 EXPECT_EQ(value4, cl.GetSwitchValue(switch4));
initial.commitd7cae122008-07-26 21:49:38123}