initial.commit | d7cae12 | 2008-07-26 21:49:38 | [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" |
| 34 | #include "base/logging.h" |
| 35 | #include "testing/gtest/include/gtest/gtest.h" |
| 36 | |
| 37 | namespace { |
| 38 | class CommandLineTest : public testing::Test { |
| 39 | }; |
| 40 | }; |
| 41 | |
| 42 | TEST(CommandLineTest, CommandLineConstructor) { |
| 43 | CommandLine cl(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
| 44 | L"--other-switches=\"--dog=canine --cat=feline\" " |
| 45 | L"-spaetzle=Crepe -=loosevalue flan " |
| 46 | L"--input-translation=\"45\"--output-rotation " |
| 47 | L"\"in the time of submarines...\""); |
| 48 | |
| 49 | EXPECT_FALSE(cl.command_line_string().empty()); |
| 50 | EXPECT_FALSE(cl.HasSwitch(L"cruller")); |
| 51 | EXPECT_FALSE(cl.HasSwitch(L"flim")); |
| 52 | EXPECT_FALSE(cl.HasSwitch(L"program")); |
| 53 | EXPECT_FALSE(cl.HasSwitch(L"dog")); |
| 54 | EXPECT_FALSE(cl.HasSwitch(L"cat")); |
| 55 | EXPECT_FALSE(cl.HasSwitch(L"output-rotation")); |
| 56 | |
| 57 | EXPECT_EQ(L"program", cl.program()); |
| 58 | |
| 59 | EXPECT_TRUE(cl.HasSwitch(L"foo")); |
| 60 | EXPECT_TRUE(cl.HasSwitch(L"bar")); |
| 61 | EXPECT_TRUE(cl.HasSwitch(L"baz")); |
| 62 | EXPECT_TRUE(cl.HasSwitch(L"spaetzle")); |
| 63 | EXPECT_TRUE(cl.HasSwitch(L"SPAETZLE")); |
| 64 | EXPECT_TRUE(cl.HasSwitch(L"other-switches")); |
| 65 | EXPECT_TRUE(cl.HasSwitch(L"input-translation")); |
| 66 | |
| 67 | EXPECT_EQ(L"Crepe", cl.GetSwitchValue(L"spaetzle")); |
| 68 | EXPECT_EQ(L"", cl.GetSwitchValue(L"Foo")); |
| 69 | EXPECT_EQ(L"", cl.GetSwitchValue(L"bar")); |
| 70 | EXPECT_EQ(L"", cl.GetSwitchValue(L"cruller")); |
| 71 | EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue(L"other-switches")); |
| 72 | EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue(L"input-translation")); |
| 73 | |
| 74 | EXPECT_EQ(3, cl.GetLooseValueCount()); |
| 75 | |
| 76 | CommandLine::LooseValueIterator iter = cl.GetLooseValuesBegin(); |
| 77 | EXPECT_EQ(L"flim", *iter); |
| 78 | ++iter; |
| 79 | EXPECT_EQ(L"flan", *iter); |
| 80 | ++iter; |
| 81 | EXPECT_EQ(L"in the time of submarines...", *iter); |
| 82 | ++iter; |
| 83 | EXPECT_TRUE(iter == cl.GetLooseValuesEnd()); |
| 84 | } |
| 85 | |
| 86 | // These test the command line used to invoke the unit test. |
| 87 | TEST(CommandLineTest, DefaultConstructor) { |
| 88 | CommandLine cl; |
| 89 | EXPECT_FALSE(cl.command_line_string().empty()); |
| 90 | EXPECT_FALSE(cl.program().empty()); |
| 91 | } |
| 92 | |
| 93 | // Tests behavior with an empty input string. |
| 94 | TEST(CommandLineTest, EmptyString) { |
| 95 | CommandLine cl(L""); |
| 96 | EXPECT_TRUE(cl.command_line_string().empty()); |
| 97 | EXPECT_TRUE(cl.program().empty()); |
| 98 | EXPECT_EQ(0, cl.GetLooseValueCount()); |
| 99 | } |
| 100 | |
| 101 | // Test static functions for appending switches to a command line. |
| 102 | TEST(CommandLineTest, AppendSwitches) { |
| 103 | std::wstring cl_string = L"Program"; |
| 104 | std::wstring switch1 = L"switch1"; |
| 105 | std::wstring switch2 = L"switch2"; |
| 106 | std::wstring value = L"value"; |
| 107 | std::wstring switch3 = L"switch3"; |
| 108 | std::wstring value3 = L"a value with spaces"; |
| 109 | std::wstring switch4 = L"switch4"; |
| 110 | std::wstring value4 = L"\"a value with quotes\""; |
| 111 | |
| 112 | CommandLine::AppendSwitch(&cl_string, switch1); |
| 113 | CommandLine::AppendSwitchWithValue(&cl_string, switch2, value); |
| 114 | CommandLine::AppendSwitchWithValue(&cl_string, switch3, value3); |
| 115 | CommandLine::AppendSwitchWithValue(&cl_string, switch4, value4); |
| 116 | CommandLine cl(cl_string); |
| 117 | |
| 118 | EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 119 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
| 120 | EXPECT_EQ(value, cl.GetSwitchValue(switch2)); |
| 121 | EXPECT_TRUE(cl.HasSwitch(switch3)); |
| 122 | EXPECT_EQ(value3, cl.GetSwitchValue(switch3)); |
| 123 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
| 124 | EXPECT_EQ(value4.substr(1, value4.length() - 2), cl.GetSwitchValue(switch4)); |
| 125 | } |