blob: a34cf43da03a7d2e9cf63d6c639368cd28f0ec54 [file] [log] [blame]
[email protected]f1633932010-08-17 23:05:281// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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]5d91c9e2010-07-28 17:25:2810#include "base/file_path.h"
[email protected]f1633932010-08-17 23:05:2811#include "base/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:3812#include "testing/gtest/include/gtest/gtest.h"
13
[email protected]98a1c2682010-08-10 18:14:1914// To test Windows quoting behavior, we use a string that has some backslashes
15// and quotes.
16// Consider the command-line argument: q\"bs1\bs2\\bs3q\\\"
17// Here it is with C-style escapes.
18#define TRICKY_QUOTED L"q\\\"bs1\\bs2\\\\bs3q\\\\\\\""
19// It should be parsed by Windows as: q"bs1\bs2\\bs3q\"
20// Here that is with C-style escapes.
21#define TRICKY L"q\"bs1\\bs2\\\\bs3q\\\""
22
initial.commitd7cae122008-07-26 21:49:3823TEST(CommandLineTest, CommandLineConstructor) {
[email protected]bb975362009-01-21 01:00:2224#if defined(OS_WIN)
[email protected]51343d5a2009-10-26 22:39:3325 CommandLine cl = CommandLine::FromString(
26 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim "
[email protected]bb975362009-01-21 01:00:2227 L"--other-switches=\"--dog=canine --cat=feline\" "
28 L"-spaetzle=Crepe -=loosevalue flan "
29 L"--input-translation=\"45\"--output-rotation "
[email protected]98a1c2682010-08-10 18:14:1930 L"--quotes=" TRICKY_QUOTED L" "
[email protected]bb975362009-01-21 01:00:2231 L"-- -- --not-a-switch "
32 L"\"in the time of submarines...\"");
33 EXPECT_FALSE(cl.command_line_string().empty());
[email protected]4e44b4d2008-08-12 03:12:4134#elif defined(OS_POSIX)
[email protected]bb975362009-01-21 01:00:2235 const char* argv[] = {"program", "--foo=", "-bar",
36 "-spaetzel=pierogi", "-baz", "flim",
[email protected]e63d5982008-08-14 22:09:3937 "--other-switches=--dog=canine --cat=feline",
38 "-spaetzle=Crepe", "-=loosevalue", "flan",
39 "--input-translation=45--output-rotation",
[email protected]02c87962008-10-06 10:25:3540 "--", "--", "--not-a-switch",
[email protected]e63d5982008-08-14 22:09:3941 "in the time of submarines..."};
[email protected]f3adb5c2008-08-07 20:07:3242 CommandLine cl(arraysize(argv), argv);
43#endif
[email protected]b7e0a2a2009-10-13 02:07:2544 EXPECT_FALSE(cl.HasSwitch("cruller"));
45 EXPECT_FALSE(cl.HasSwitch("flim"));
46 EXPECT_FALSE(cl.HasSwitch("program"));
47 EXPECT_FALSE(cl.HasSwitch("dog"));
48 EXPECT_FALSE(cl.HasSwitch("cat"));
49 EXPECT_FALSE(cl.HasSwitch("output-rotation"));
50 EXPECT_FALSE(cl.HasSwitch("not-a-switch"));
51 EXPECT_FALSE(cl.HasSwitch("--"));
initial.commitd7cae122008-07-26 21:49:3852
53 EXPECT_EQ(L"program", cl.program());
54
[email protected]b7e0a2a2009-10-13 02:07:2555 EXPECT_TRUE(cl.HasSwitch("foo"));
56 EXPECT_TRUE(cl.HasSwitch("bar"));
57 EXPECT_TRUE(cl.HasSwitch("baz"));
58 EXPECT_TRUE(cl.HasSwitch("spaetzle"));
[email protected]bb975362009-01-21 01:00:2259#if defined(OS_WIN)
[email protected]b7e0a2a2009-10-13 02:07:2560 EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
[email protected]bb975362009-01-21 01:00:2261#endif
[email protected]b7e0a2a2009-10-13 02:07:2562 EXPECT_TRUE(cl.HasSwitch("other-switches"));
63 EXPECT_TRUE(cl.HasSwitch("input-translation"));
[email protected]98a1c2682010-08-10 18:14:1964#if defined(OS_WIN)
65 EXPECT_TRUE(cl.HasSwitch("quotes"));
66#endif
initial.commitd7cae122008-07-26 21:49:3867
[email protected]c4e52f0d2009-11-06 19:55:1668 EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle"));
69 EXPECT_EQ("", cl.GetSwitchValueASCII("Foo"));
70 EXPECT_EQ("", cl.GetSwitchValueASCII("bar"));
71 EXPECT_EQ("", cl.GetSwitchValueASCII("cruller"));
72 EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII(
73 "other-switches"));
74 EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation"));
[email protected]98a1c2682010-08-10 18:14:1975#if defined(OS_WIN)
76 EXPECT_EQ(TRICKY, cl.GetSwitchValueNative("quotes"));
77#endif
initial.commitd7cae122008-07-26 21:49:3878
[email protected]2e4c50c2010-07-21 15:57:2379 const std::vector<CommandLine::StringType>& args = cl.args();
80 ASSERT_EQ(5U, args.size());
initial.commitd7cae122008-07-26 21:49:3881
[email protected]2e4c50c2010-07-21 15:57:2382 std::vector<CommandLine::StringType>::const_iterator iter = args.begin();
83 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter);
initial.commitd7cae122008-07-26 21:49:3884 ++iter;
[email protected]2e4c50c2010-07-21 15:57:2385 EXPECT_EQ(FILE_PATH_LITERAL("flan"), *iter);
initial.commitd7cae122008-07-26 21:49:3886 ++iter;
[email protected]2e4c50c2010-07-21 15:57:2387 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter);
[email protected]02c87962008-10-06 10:25:3588 ++iter;
[email protected]2e4c50c2010-07-21 15:57:2389 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter);
[email protected]02c87962008-10-06 10:25:3590 ++iter;
[email protected]2e4c50c2010-07-21 15:57:2391 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter);
initial.commitd7cae122008-07-26 21:49:3892 ++iter;
[email protected]2e4c50c2010-07-21 15:57:2393 EXPECT_TRUE(iter == args.end());
[email protected]10e42bf2008-10-15 21:59:0894#if defined(OS_POSIX)
[email protected]bb975362009-01-21 01:00:2295 const std::vector<std::string>& argvec = cl.argv();
[email protected]10e42bf2008-10-15 21:59:0896
97 for (size_t i = 0; i < argvec.size(); i++) {
98 EXPECT_EQ(0, argvec[i].compare(argv[i]));
99 }
100#endif
initial.commitd7cae122008-07-26 21:49:38101}
102
initial.commitd7cae122008-07-26 21:49:38103// Tests behavior with an empty input string.
104TEST(CommandLineTest, EmptyString) {
[email protected]f3adb5c2008-08-07 20:07:32105#if defined(OS_WIN)
[email protected]51343d5a2009-10-26 22:39:33106 CommandLine cl = CommandLine::FromString(L"");
[email protected]bb975362009-01-21 01:00:22107 EXPECT_TRUE(cl.command_line_string().empty());
108 EXPECT_TRUE(cl.program().empty());
[email protected]f3adb5c2008-08-07 20:07:32109#elif defined(OS_POSIX)
[email protected]4e44b4d2008-08-12 03:12:41110 CommandLine cl(0, NULL);
[email protected]10e42bf2008-10-15 21:59:08111 EXPECT_TRUE(cl.argv().size() == 0);
[email protected]f3adb5c2008-08-07 20:07:32112#endif
[email protected]2e4c50c2010-07-21 15:57:23113 EXPECT_EQ(0U, cl.args().size());
initial.commitd7cae122008-07-26 21:49:38114}
115
[email protected]bb975362009-01-21 01:00:22116// Test methods for appending switches to a command line.
initial.commitd7cae122008-07-26 21:49:38117TEST(CommandLineTest, AppendSwitches) {
[email protected]b7e0a2a2009-10-13 02:07:25118 std::string switch1 = "switch1";
119 std::string switch2 = "switch2";
[email protected]05076ba22010-07-30 05:59:57120 std::string value = "value";
[email protected]b7e0a2a2009-10-13 02:07:25121 std::string switch3 = "switch3";
[email protected]05076ba22010-07-30 05:59:57122 std::string value3 = "a value with spaces";
[email protected]b7e0a2a2009-10-13 02:07:25123 std::string switch4 = "switch4";
[email protected]05076ba22010-07-30 05:59:57124 std::string value4 = "\"a value with quotes\"";
[email protected]98a1c2682010-08-10 18:14:19125 std::string switch5 = "quotes";
[email protected]f1633932010-08-17 23:05:28126 std::string value5 = WideToUTF8(TRICKY);
initial.commitd7cae122008-07-26 21:49:38127
[email protected]51343d5a2009-10-26 22:39:33128 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
initial.commitd7cae122008-07-26 21:49:38129
[email protected]bb975362009-01-21 01:00:22130 cl.AppendSwitch(switch1);
[email protected]05076ba22010-07-30 05:59:57131 cl.AppendSwitchASCII(switch2, value);
132 cl.AppendSwitchASCII(switch3, value3);
133 cl.AppendSwitchASCII(switch4, value4);
[email protected]98a1c2682010-08-10 18:14:19134 cl.AppendSwitchASCII(switch5, value5);
[email protected]bb975362009-01-21 01:00:22135
initial.commitd7cae122008-07-26 21:49:38136 EXPECT_TRUE(cl.HasSwitch(switch1));
137 EXPECT_TRUE(cl.HasSwitch(switch2));
[email protected]05076ba22010-07-30 05:59:57138 EXPECT_EQ(value, cl.GetSwitchValueASCII(switch2));
initial.commitd7cae122008-07-26 21:49:38139 EXPECT_TRUE(cl.HasSwitch(switch3));
[email protected]05076ba22010-07-30 05:59:57140 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3));
[email protected]8c9510d2008-10-10 21:38:20141 EXPECT_TRUE(cl.HasSwitch(switch4));
[email protected]05076ba22010-07-30 05:59:57142 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4));
[email protected]98a1c2682010-08-10 18:14:19143 EXPECT_TRUE(cl.HasSwitch(switch5));
144 EXPECT_EQ(value5, cl.GetSwitchValueASCII(switch5));
145
146#if defined(OS_WIN)
147 EXPECT_EQ(L"\"Program\" "
148 L"--switch1 "
149 L"--switch2=value "
150 L"--switch3=\"a value with spaces\" "
151 L"--switch4=\"\\\"a value with quotes\\\"\" "
152 L"--quotes=\"" TRICKY_QUOTED L"\"",
153 cl.command_line_string());
154#endif
initial.commitd7cae122008-07-26 21:49:38155}