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" |
[email protected] | 10e42bf | 2008-10-15 21:59:08 | [diff] [blame] | 10 | #include "base/string_util.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | TEST(CommandLineTest, CommandLineConstructor) { |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 14 | #if defined(OS_WIN) |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 15 | CommandLine cl = CommandLine::FromString( |
| 16 | L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 17 | L"--other-switches=\"--dog=canine --cat=feline\" " |
| 18 | L"-spaetzle=Crepe -=loosevalue flan " |
| 19 | L"--input-translation=\"45\"--output-rotation " |
| 20 | L"-- -- --not-a-switch " |
| 21 | L"\"in the time of submarines...\""); |
| 22 | EXPECT_FALSE(cl.command_line_string().empty()); |
[email protected] | 4e44b4d | 2008-08-12 03:12:41 | [diff] [blame] | 23 | #elif defined(OS_POSIX) |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 24 | const char* argv[] = {"program", "--foo=", "-bar", |
| 25 | "-spaetzel=pierogi", "-baz", "flim", |
[email protected] | e63d598 | 2008-08-14 22:09:39 | [diff] [blame] | 26 | "--other-switches=--dog=canine --cat=feline", |
| 27 | "-spaetzle=Crepe", "-=loosevalue", "flan", |
| 28 | "--input-translation=45--output-rotation", |
[email protected] | 02c8796 | 2008-10-06 10:25:35 | [diff] [blame] | 29 | "--", "--", "--not-a-switch", |
[email protected] | e63d598 | 2008-08-14 22:09:39 | [diff] [blame] | 30 | "in the time of submarines..."}; |
[email protected] | f3adb5c | 2008-08-07 20:07:32 | [diff] [blame] | 31 | CommandLine cl(arraysize(argv), argv); |
| 32 | #endif |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 33 | EXPECT_FALSE(cl.HasSwitch("cruller")); |
| 34 | EXPECT_FALSE(cl.HasSwitch("flim")); |
| 35 | EXPECT_FALSE(cl.HasSwitch("program")); |
| 36 | EXPECT_FALSE(cl.HasSwitch("dog")); |
| 37 | EXPECT_FALSE(cl.HasSwitch("cat")); |
| 38 | EXPECT_FALSE(cl.HasSwitch("output-rotation")); |
| 39 | EXPECT_FALSE(cl.HasSwitch("not-a-switch")); |
| 40 | EXPECT_FALSE(cl.HasSwitch("--")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 41 | |
| 42 | EXPECT_EQ(L"program", cl.program()); |
| 43 | |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 44 | EXPECT_TRUE(cl.HasSwitch("foo")); |
| 45 | EXPECT_TRUE(cl.HasSwitch("bar")); |
| 46 | EXPECT_TRUE(cl.HasSwitch("baz")); |
| 47 | EXPECT_TRUE(cl.HasSwitch("spaetzle")); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 48 | #if defined(OS_WIN) |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 49 | EXPECT_TRUE(cl.HasSwitch("SPAETZLE")); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 50 | #endif |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 51 | EXPECT_TRUE(cl.HasSwitch("other-switches")); |
| 52 | EXPECT_TRUE(cl.HasSwitch("input-translation")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 53 | |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame^] | 54 | EXPECT_EQ("Crepe", cl.GetSwitchValueASCII("spaetzle")); |
| 55 | EXPECT_EQ("", cl.GetSwitchValueASCII("Foo")); |
| 56 | EXPECT_EQ("", cl.GetSwitchValueASCII("bar")); |
| 57 | EXPECT_EQ("", cl.GetSwitchValueASCII("cruller")); |
| 58 | EXPECT_EQ("--dog=canine --cat=feline", cl.GetSwitchValueASCII( |
| 59 | "other-switches")); |
| 60 | EXPECT_EQ("45--output-rotation", cl.GetSwitchValueASCII("input-translation")); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 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) |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 89 | CommandLine cl = CommandLine::FromString(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) { |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 101 | std::string switch1 = "switch1"; |
| 102 | std::string switch2 = "switch2"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 103 | std::wstring value = L"value"; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 104 | std::string switch3 = "switch3"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 105 | std::wstring value3 = L"a value with spaces"; |
[email protected] | b7e0a2a | 2009-10-13 02:07:25 | [diff] [blame] | 106 | std::string switch4 = "switch4"; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 107 | std::wstring value4 = L"\"a value with quotes\""; |
| 108 | |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 109 | CommandLine cl(FilePath(FILE_PATH_LITERAL("Program"))); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 110 | |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 111 | cl.AppendSwitch(switch1); |
| 112 | cl.AppendSwitchWithValue(switch2, value); |
| 113 | cl.AppendSwitchWithValue(switch3, value3); |
| 114 | cl.AppendSwitchWithValue(switch4, value4); |
| 115 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 116 | EXPECT_TRUE(cl.HasSwitch(switch1)); |
| 117 | EXPECT_TRUE(cl.HasSwitch(switch2)); |
| 118 | EXPECT_EQ(value, cl.GetSwitchValue(switch2)); |
| 119 | EXPECT_TRUE(cl.HasSwitch(switch3)); |
| 120 | EXPECT_EQ(value3, cl.GetSwitchValue(switch3)); |
[email protected] | 8c9510d | 2008-10-10 21:38:20 | [diff] [blame] | 121 | EXPECT_TRUE(cl.HasSwitch(switch4)); |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 122 | EXPECT_EQ(value4, cl.GetSwitchValue(switch4)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 123 | } |