initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include <string> |
| 31 | #include <vector> |
| 32 | |
| 33 | #include "base/command_line.h" |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 34 | #include "base/basictypes.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 35 | #include "base/logging.h" |
| 36 | #include "testing/gtest/include/gtest/gtest.h" |
| 37 | |
| 38 | namespace { |
| 39 | class CommandLineTest : public testing::Test { |
| 40 | }; |
| 41 | }; |
| 42 | |
| 43 | TEST(CommandLineTest, CommandLineConstructor) { |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 44 | #ifdef OS_WIN |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 45 | CommandLine cl(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
| 46 | L"--other-switches=\"--dog=canine --cat=feline\" " |
| 47 | L"-spaetzle=Crepe -=loosevalue flan " |
| 48 | L"--input-translation=\"45\"--output-rotation " |
| 49 | L"\"in the time of submarines...\""); |
mmentovai@google.com | cd20e5c | 2008-08-12 12:12:41 +0900 | [diff] [blame] | 50 | #elif defined(OS_POSIX) |
tc@google.com | 1bc8d52 | 2008-08-15 07:09:39 +0900 | [diff] [blame] | 51 | const char* argv[] = {"program", "--foo=", "-bAr", |
| 52 | "-Spaetzel=pierogi", "-Baz", "flim", |
| 53 | "--other-switches=--dog=canine --cat=feline", |
| 54 | "-spaetzle=Crepe", "-=loosevalue", "flan", |
| 55 | "--input-translation=45--output-rotation", |
| 56 | "in the time of submarines..."}; |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 57 | CommandLine cl(arraysize(argv), argv); |
| 58 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 59 | EXPECT_FALSE(cl.command_line_string().empty()); |
| 60 | EXPECT_FALSE(cl.HasSwitch(L"cruller")); |
| 61 | EXPECT_FALSE(cl.HasSwitch(L"flim")); |
| 62 | EXPECT_FALSE(cl.HasSwitch(L"program")); |
| 63 | EXPECT_FALSE(cl.HasSwitch(L"dog")); |
| 64 | EXPECT_FALSE(cl.HasSwitch(L"cat")); |
| 65 | EXPECT_FALSE(cl.HasSwitch(L"output-rotation")); |
| 66 | |
| 67 | EXPECT_EQ(L"program", cl.program()); |
| 68 | |
| 69 | EXPECT_TRUE(cl.HasSwitch(L"foo")); |
| 70 | EXPECT_TRUE(cl.HasSwitch(L"bar")); |
| 71 | EXPECT_TRUE(cl.HasSwitch(L"baz")); |
| 72 | EXPECT_TRUE(cl.HasSwitch(L"spaetzle")); |
| 73 | EXPECT_TRUE(cl.HasSwitch(L"SPAETZLE")); |
| 74 | EXPECT_TRUE(cl.HasSwitch(L"other-switches")); |
| 75 | EXPECT_TRUE(cl.HasSwitch(L"input-translation")); |
| 76 | |
| 77 | EXPECT_EQ(L"Crepe", cl.GetSwitchValue(L"spaetzle")); |
| 78 | EXPECT_EQ(L"", cl.GetSwitchValue(L"Foo")); |
| 79 | EXPECT_EQ(L"", cl.GetSwitchValue(L"bar")); |
| 80 | EXPECT_EQ(L"", cl.GetSwitchValue(L"cruller")); |
| 81 | EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue(L"other-switches")); |
| 82 | EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue(L"input-translation")); |
| 83 | |
darin@google.com | f327280 | 2008-08-15 05:27:29 +0900 | [diff] [blame] | 84 | EXPECT_EQ(3U, cl.GetLooseValueCount()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 85 | |
| 86 | CommandLine::LooseValueIterator iter = cl.GetLooseValuesBegin(); |
| 87 | EXPECT_EQ(L"flim", *iter); |
| 88 | ++iter; |
| 89 | EXPECT_EQ(L"flan", *iter); |
| 90 | ++iter; |
| 91 | EXPECT_EQ(L"in the time of submarines...", *iter); |
| 92 | ++iter; |
| 93 | EXPECT_TRUE(iter == cl.GetLooseValuesEnd()); |
| 94 | } |
| 95 | |
| 96 | // These test the command line used to invoke the unit test. |
| 97 | TEST(CommandLineTest, DefaultConstructor) { |
| 98 | CommandLine cl; |
| 99 | EXPECT_FALSE(cl.command_line_string().empty()); |
| 100 | EXPECT_FALSE(cl.program().empty()); |
| 101 | } |
| 102 | |
| 103 | // Tests behavior with an empty input string. |
| 104 | TEST(CommandLineTest, EmptyString) { |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 105 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 106 | CommandLine cl(L""); |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 107 | #elif defined(OS_POSIX) |
mmentovai@google.com | cd20e5c | 2008-08-12 12:12:41 +0900 | [diff] [blame] | 108 | CommandLine cl(0, NULL); |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 109 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 110 | EXPECT_TRUE(cl.command_line_string().empty()); |
| 111 | EXPECT_TRUE(cl.program().empty()); |
darin@google.com | f327280 | 2008-08-15 05:27:29 +0900 | [diff] [blame] | 112 | EXPECT_EQ(0U, cl.GetLooseValueCount()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Test static functions for appending switches to a command line. |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 116 | // TODO(pinkerton): non-windows platforms don't have the requisite ctor here, so |
| 117 | // we need something that tests AppendSwitches in another way (if even desired). |
| 118 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 119 | TEST(CommandLineTest, AppendSwitches) { |
| 120 | std::wstring cl_string = L"Program"; |
| 121 | std::wstring switch1 = L"switch1"; |
| 122 | std::wstring switch2 = L"switch2"; |
| 123 | std::wstring value = L"value"; |
| 124 | std::wstring switch3 = L"switch3"; |
| 125 | std::wstring value3 = L"a value with spaces"; |
| 126 | std::wstring switch4 = L"switch4"; |
| 127 | std::wstring value4 = L"\"a value with quotes\""; |
| 128 | |
| 129 | CommandLine::AppendSwitch(&cl_string, switch1); |
| 130 | CommandLine::AppendSwitchWithValue(&cl_string, switch2, value); |
| 131 | CommandLine::AppendSwitchWithValue(&cl_string, switch3, value3); |
| 132 | CommandLine::AppendSwitchWithValue(&cl_string, switch4, value4); |
| 133 | CommandLine cl(cl_string); |
| 134 | |
| 135 | EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 136 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
| 137 | EXPECT_EQ(value, cl.GetSwitchValue(switch2)); |
| 138 | EXPECT_TRUE(cl.HasSwitch(switch3)); |
| 139 | EXPECT_EQ(value3, cl.GetSwitchValue(switch3)); |
| 140 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
| 141 | EXPECT_EQ(value4.substr(1, value4.length() - 2), cl.GetSwitchValue(switch4)); |
| 142 | } |
evanm@google.com | 91cdff8 | 2008-08-08 05:07:32 +0900 | [diff] [blame] | 143 | #endif |