command_line_unittest.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 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. 4 5#include <string> 6#include <vector> 7 8#include "base/command_line.h" 9#include "base/basictypes.h" 10#include "base/file_path.h" 11#include "base/utf_string_conversions.h" 12#include "testing/gtest/include/gtest/gtest.h" 13 14// 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 23TEST(CommandLineTest, CommandLineConstructor) { 24#if defined(OS_WIN) 25 CommandLine cl = CommandLine::FromString( 26 L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " 27 L"--other-switches=\"--dog=canine --cat=feline\" " 28 L"-spaetzle=Crepe -=loosevalue flan " 29 L"--input-translation=\"45\"--output-rotation " 30 L"--quotes=" TRICKY_QUOTED L" " 31 L"-- -- --not-a-switch " 32 L"\"in the time of submarines...\""); 33 EXPECT_FALSE(cl.command_line_string().empty()); 34#elif defined(OS_POSIX) 35 const char* argv[] = {"program", "--foo=", "-bar", 36 "-spaetzel=pierogi", "-baz", "flim", 37 "--other-switches=--dog=canine --cat=feline", 38 "-spaetzle=Crepe", "-=loosevalue", "flan", 39 "--input-translation=45--output-rotation", 40 "--", "--", "--not-a-switch", 41 "in the time of submarines..."}; 42 CommandLine cl(arraysize(argv), argv); 43#endif 44 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("--")); 52 53 EXPECT_EQ(L"program", cl.program()); 54 55 EXPECT_TRUE(cl.HasSwitch("foo")); 56 EXPECT_TRUE(cl.HasSwitch("bar")); 57 EXPECT_TRUE(cl.HasSwitch("baz")); 58 EXPECT_TRUE(cl.HasSwitch("spaetzle")); 59#if defined(OS_WIN) 60 EXPECT_TRUE(cl.HasSwitch("SPAETZLE")); 61#endif 62 EXPECT_TRUE(cl.HasSwitch("other-switches")); 63 EXPECT_TRUE(cl.HasSwitch("input-translation")); 64#if defined(OS_WIN) 65 EXPECT_TRUE(cl.HasSwitch("quotes")); 66#endif 67 68 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")); 75#if defined(OS_WIN) 76 EXPECT_EQ(TRICKY, cl.GetSwitchValueNative("quotes")); 77#endif 78 79 const std::vector<CommandLine::StringType>& args = cl.args(); 80 ASSERT_EQ(5U, args.size()); 81 82 std::vector<CommandLine::StringType>::const_iterator iter = args.begin(); 83 EXPECT_EQ(FILE_PATH_LITERAL("flim"), *iter); 84 ++iter; 85 EXPECT_EQ(FILE_PATH_LITERAL("flan"), *iter); 86 ++iter; 87 EXPECT_EQ(FILE_PATH_LITERAL("--"), *iter); 88 ++iter; 89 EXPECT_EQ(FILE_PATH_LITERAL("--not-a-switch"), *iter); 90 ++iter; 91 EXPECT_EQ(FILE_PATH_LITERAL("in the time of submarines..."), *iter); 92 ++iter; 93 EXPECT_TRUE(iter == args.end()); 94#if defined(OS_POSIX) 95 const std::vector<std::string>& argvec = cl.argv(); 96 97 for (size_t i = 0; i < argvec.size(); i++) { 98 EXPECT_EQ(0, argvec[i].compare(argv[i])); 99 } 100#endif 101} 102 103// Tests behavior with an empty input string. 104TEST(CommandLineTest, EmptyString) { 105#if defined(OS_WIN) 106 CommandLine cl = CommandLine::FromString(L""); 107 EXPECT_TRUE(cl.command_line_string().empty()); 108 EXPECT_TRUE(cl.program().empty()); 109#elif defined(OS_POSIX) 110 CommandLine cl(0, NULL); 111 EXPECT_TRUE(cl.argv().size() == 0); 112#endif 113 EXPECT_EQ(0U, cl.args().size()); 114} 115 116// Test methods for appending switches to a command line. 117TEST(CommandLineTest, AppendSwitches) { 118 std::string switch1 = "switch1"; 119 std::string switch2 = "switch2"; 120 std::string value = "value"; 121 std::string switch3 = "switch3"; 122 std::string value3 = "a value with spaces"; 123 std::string switch4 = "switch4"; 124 std::string value4 = "\"a value with quotes\""; 125 std::string switch5 = "quotes"; 126 std::string value5 = WideToUTF8(TRICKY); 127 128 CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); 129 130 cl.AppendSwitch(switch1); 131 cl.AppendSwitchASCII(switch2, value); 132 cl.AppendSwitchASCII(switch3, value3); 133 cl.AppendSwitchASCII(switch4, value4); 134 cl.AppendSwitchASCII(switch5, value5); 135 136 EXPECT_TRUE(cl.HasSwitch(switch1)); 137 EXPECT_TRUE(cl.HasSwitch(switch2)); 138 EXPECT_EQ(value, cl.GetSwitchValueASCII(switch2)); 139 EXPECT_TRUE(cl.HasSwitch(switch3)); 140 EXPECT_EQ(value3, cl.GetSwitchValueASCII(switch3)); 141 EXPECT_TRUE(cl.HasSwitch(switch4)); 142 EXPECT_EQ(value4, cl.GetSwitchValueASCII(switch4)); 143 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 155} 156