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 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | TEST(CommandLineTest, CommandLineConstructor) { |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 15 | #if defined(OS_WIN) |
| 16 | CommandLine cl(L""); |
| 17 | cl.ParseFromString(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
| 18 | L"--other-switches=\"--dog=canine --cat=feline\" " |
| 19 | L"-spaetzle=Crepe -=loosevalue flan " |
| 20 | L"--input-translation=\"45\"--output-rotation " |
| 21 | L"-- -- --not-a-switch " |
| 22 | L"\"in the time of submarines...\""); |
| 23 | EXPECT_FALSE(cl.command_line_string().empty()); |
[email protected] | 4e44b4d | 2008-08-12 03:12:41 | [diff] [blame] | 24 | #elif defined(OS_POSIX) |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 25 | const char* argv[] = {"program", "--foo=", "-bar", |
| 26 | "-spaetzel=pierogi", "-baz", "flim", |
[email protected] | e63d598 | 2008-08-14 22:09:39 | [diff] [blame] | 27 | "--other-switches=--dog=canine --cat=feline", |
| 28 | "-spaetzle=Crepe", "-=loosevalue", "flan", |
| 29 | "--input-translation=45--output-rotation", |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 30 | "--", "--", "--not-a-switch", |
[email protected] | e63d598 | 2008-08-14 22:09:39 | [diff] [blame] | 31 | "in the time of submarines..."}; |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 32 | CommandLine cl(arraysize(argv), argv); |
| 33 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 34 | EXPECT_FALSE(cl.HasSwitch(L"cruller")); |
| 35 | EXPECT_FALSE(cl.HasSwitch(L"flim")); |
| 36 | EXPECT_FALSE(cl.HasSwitch(L"program")); |
| 37 | EXPECT_FALSE(cl.HasSwitch(L"dog")); |
| 38 | EXPECT_FALSE(cl.HasSwitch(L"cat")); |
| 39 | EXPECT_FALSE(cl.HasSwitch(L"output-rotation")); |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 40 | EXPECT_FALSE(cl.HasSwitch(L"not-a-switch")); |
| 41 | EXPECT_FALSE(cl.HasSwitch(L"--")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 42 | |
| 43 | EXPECT_EQ(L"program", cl.program()); |
| 44 | |
| 45 | EXPECT_TRUE(cl.HasSwitch(L"foo")); |
| 46 | EXPECT_TRUE(cl.HasSwitch(L"bar")); |
| 47 | EXPECT_TRUE(cl.HasSwitch(L"baz")); |
| 48 | EXPECT_TRUE(cl.HasSwitch(L"spaetzle")); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 49 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 50 | EXPECT_TRUE(cl.HasSwitch(L"SPAETZLE")); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 51 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 52 | EXPECT_TRUE(cl.HasSwitch(L"other-switches")); |
| 53 | EXPECT_TRUE(cl.HasSwitch(L"input-translation")); |
| 54 | |
| 55 | EXPECT_EQ(L"Crepe", cl.GetSwitchValue(L"spaetzle")); |
| 56 | EXPECT_EQ(L"", cl.GetSwitchValue(L"Foo")); |
| 57 | EXPECT_EQ(L"", cl.GetSwitchValue(L"bar")); |
| 58 | EXPECT_EQ(L"", cl.GetSwitchValue(L"cruller")); |
| 59 | EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue(L"other-switches")); |
| 60 | EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue(L"input-translation")); |
| 61 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 62 | std::vector<std::wstring> loose_values = cl.GetLooseValues(); |
| 63 | ASSERT_EQ(5U, loose_values.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 64 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 65 | std::vector<std::wstring>::const_iterator iter = loose_values.begin(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 66 | EXPECT_EQ(L"flim", *iter); |
| 67 | ++iter; |
| 68 | EXPECT_EQ(L"flan", *iter); |
| 69 | ++iter; |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 70 | EXPECT_EQ(L"--", *iter); |
| 71 | ++iter; |
| 72 | EXPECT_EQ(L"--not-a-switch", *iter); |
| 73 | ++iter; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 74 | EXPECT_EQ(L"in the time of submarines...", *iter); |
| 75 | ++iter; |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 76 | EXPECT_TRUE(iter == loose_values.end()); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 77 | #if defined(OS_POSIX) |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 78 | const std::vector<std::string>& argvec = cl.argv(); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 79 | |
| 80 | for (size_t i = 0; i < argvec.size(); i++) { |
| 81 | EXPECT_EQ(0, argvec[i].compare(argv[i])); |
| 82 | } |
| 83 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 84 | } |
| 85 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 86 | // Tests behavior with an empty input string. |
| 87 | TEST(CommandLineTest, EmptyString) { |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 88 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 89 | CommandLine cl(L""); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 90 | EXPECT_TRUE(cl.command_line_string().empty()); |
| 91 | EXPECT_TRUE(cl.program().empty()); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 92 | #elif defined(OS_POSIX) |
[email protected] | 4e44b4d | 2008-08-12 03:12:41 | [diff] [blame] | 93 | CommandLine cl(0, NULL); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 94 | EXPECT_TRUE(cl.argv().size() == 0); |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 95 | #endif |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 96 | EXPECT_EQ(0U, cl.GetLooseValues().size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 99 | // Test methods for appending switches to a command line. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 100 | TEST(CommandLineTest, AppendSwitches) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 101 | std::wstring switch1 = L"switch1"; |
| 102 | std::wstring switch2 = L"switch2"; |
| 103 | std::wstring value = L"value"; |
| 104 | std::wstring switch3 = L"switch3"; |
| 105 | std::wstring value3 = L"a value with spaces"; |
| 106 | std::wstring switch4 = L"switch4"; |
| 107 | std::wstring value4 = L"\"a value with quotes\""; |
| 108 | |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 109 | #if defined(OS_WIN) |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 110 | CommandLine cl(L"Program"); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 111 | #elif defined(OS_POSIX) |
| 112 | std::vector<std::string> argv; |
| 113 | argv.push_back(std::string("Program")); |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 114 | CommandLine cl(argv); |
| 115 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 116 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 117 | cl.AppendSwitch(switch1); |
| 118 | cl.AppendSwitchWithValue(switch2, value); |
| 119 | cl.AppendSwitchWithValue(switch3, value3); |
| 120 | cl.AppendSwitchWithValue(switch4, value4); |
| 121 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 122 | EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 123 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
| 124 | EXPECT_EQ(value, cl.GetSwitchValue(switch2)); |
| 125 | EXPECT_TRUE(cl.HasSwitch(switch3)); |
| 126 | EXPECT_EQ(value3, cl.GetSwitchValue(switch3)); |
[email protected] | 8c9510d | 2008-10-10 21:38:20 | [diff] [blame] | 127 | EXPECT_TRUE(cl.HasSwitch(switch4)); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame^] | 128 | EXPECT_EQ(value4, cl.GetSwitchValue(switch4)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 129 | } |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 130 | |