license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // 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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | |
| 8 | #include "base/command_line.h" |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 9 | #include "base/basictypes.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 11 | #include "base/string_util.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace { |
| 15 | class CommandLineTest : public testing::Test { |
| 16 | }; |
| 17 | }; |
| 18 | |
| 19 | TEST(CommandLineTest, CommandLineConstructor) { |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 20 | #ifdef OS_WIN |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | 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] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 25 | L"-- -- --not-a-switch " |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 26 | L"\"in the time of submarines...\""); |
[email protected] | 4e44b4d | 2008-08-12 03:12:41 | [diff] [blame] | 27 | #elif defined(OS_POSIX) |
[email protected] | e63d598 | 2008-08-14 22:09:39 | [diff] [blame] | 28 | 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] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 33 | "--", "--", "--not-a-switch", |
[email protected] | e63d598 | 2008-08-14 22:09:39 | [diff] [blame] | 34 | "in the time of submarines..."}; |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 35 | CommandLine cl(arraysize(argv), argv); |
| 36 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 37 | 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] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 44 | EXPECT_FALSE(cl.HasSwitch(L"not-a-switch")); |
| 45 | EXPECT_FALSE(cl.HasSwitch(L"--")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 46 | |
| 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] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 64 | EXPECT_EQ(5U, cl.GetLooseValueCount()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 65 | |
| 66 | CommandLine::LooseValueIterator iter = cl.GetLooseValuesBegin(); |
| 67 | EXPECT_EQ(L"flim", *iter); |
| 68 | ++iter; |
| 69 | EXPECT_EQ(L"flan", *iter); |
| 70 | ++iter; |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 71 | EXPECT_EQ(L"--", *iter); |
| 72 | ++iter; |
| 73 | EXPECT_EQ(L"--not-a-switch", *iter); |
| 74 | ++iter; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 75 | EXPECT_EQ(L"in the time of submarines...", *iter); |
| 76 | ++iter; |
| 77 | EXPECT_TRUE(iter == cl.GetLooseValuesEnd()); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 78 | #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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // These test the command line used to invoke the unit test. |
| 88 | TEST(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. |
| 95 | TEST(CommandLineTest, EmptyString) { |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 96 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 97 | CommandLine cl(L""); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 98 | #elif defined(OS_POSIX) |
[email protected] | 4e44b4d | 2008-08-12 03:12:41 | [diff] [blame] | 99 | CommandLine cl(0, NULL); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 100 | EXPECT_TRUE(cl.argv().size() == 0); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 101 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 102 | EXPECT_TRUE(cl.command_line_string().empty()); |
| 103 | EXPECT_TRUE(cl.program().empty()); |
[email protected] | cb2f363 | 2008-08-14 20:27:29 | [diff] [blame] | 104 | EXPECT_EQ(0U, cl.GetLooseValueCount()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Test static functions for appending switches to a command line. |
| 108 | TEST(CommandLineTest, AppendSwitches) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 109 | 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] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 117 | #if defined(OS_WIN) |
| 118 | std::wstring cl_string = L"Program"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 119 | 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] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 124 | #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.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 136 | |
| 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] | 8c9510d | 2008-10-10 21:38:20 | [diff] [blame] | 142 | EXPECT_TRUE(cl.HasSwitch(switch4)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 143 | EXPECT_EQ(value4.substr(1, value4.length() - 2), cl.GetSwitchValue(switch4)); |
| 144 | } |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 145 | |