diff options
author | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-21 01:00:22 +0000 |
---|---|---|
committer | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-21 01:00:22 +0000 |
commit | bb97536b768ac68fcbc4605c35461a798ef6e5ff (patch) | |
tree | 479bde96cd05a9e1f9d2746dd82313e2eb715e8e /base/command_line_unittest.cc | |
parent | 8731a63268015f4e5d684833c11a1b44bd9ae468 (diff) | |
download | chromium_src-bb97536b768ac68fcbc4605c35461a798ef6e5ff.zip chromium_src-bb97536b768ac68fcbc4605c35461a798ef6e5ff.tar.gz chromium_src-bb97536b768ac68fcbc4605c35461a798ef6e5ff.tar.bz2 |
Make CommandLine into a normal object, with some statics for getting at the current process's command line.
One explicit goal of this change is to *not* deal with the string/wstring issues at the API on POSIX; the functions are the same as before, which means they remain as broken as before. (I did try to fix the internals, though, so migrating the callers is now possible by adding platform-appropriate hooks.)
Review URL: http://codereview.chromium.org/18248
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8347 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/command_line_unittest.cc')
-rw-r--r-- | base/command_line_unittest.cc | 73 |
1 files changed, 29 insertions, 44 deletions
diff --git a/base/command_line_unittest.cc b/base/command_line_unittest.cc index 504c852..563b060 100644 --- a/base/command_line_unittest.cc +++ b/base/command_line_unittest.cc @@ -11,22 +11,19 @@ #include "base/string_util.h" #include "testing/gtest/include/gtest/gtest.h" -namespace { - class CommandLineTest : public testing::Test { - }; -}; - TEST(CommandLineTest, CommandLineConstructor) { -#ifdef OS_WIN - CommandLine cl(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " - L"--other-switches=\"--dog=canine --cat=feline\" " - L"-spaetzle=Crepe -=loosevalue flan " - L"--input-translation=\"45\"--output-rotation " - L"-- -- --not-a-switch " - L"\"in the time of submarines...\""); +#if defined(OS_WIN) + CommandLine cl(L""); + cl.ParseFromString(L"program --foo= -bAr /Spaetzel=pierogi /Baz flim " + L"--other-switches=\"--dog=canine --cat=feline\" " + L"-spaetzle=Crepe -=loosevalue flan " + L"--input-translation=\"45\"--output-rotation " + L"-- -- --not-a-switch " + L"\"in the time of submarines...\""); + EXPECT_FALSE(cl.command_line_string().empty()); #elif defined(OS_POSIX) - const char* argv[] = {"program", "--foo=", "-bAr", - "-Spaetzel=pierogi", "-Baz", "flim", + const char* argv[] = {"program", "--foo=", "-bar", + "-spaetzel=pierogi", "-baz", "flim", "--other-switches=--dog=canine --cat=feline", "-spaetzle=Crepe", "-=loosevalue", "flan", "--input-translation=45--output-rotation", @@ -34,7 +31,6 @@ TEST(CommandLineTest, CommandLineConstructor) { "in the time of submarines..."}; CommandLine cl(arraysize(argv), argv); #endif - EXPECT_FALSE(cl.command_line_string().empty()); EXPECT_FALSE(cl.HasSwitch(L"cruller")); EXPECT_FALSE(cl.HasSwitch(L"flim")); EXPECT_FALSE(cl.HasSwitch(L"program")); @@ -50,7 +46,9 @@ TEST(CommandLineTest, CommandLineConstructor) { EXPECT_TRUE(cl.HasSwitch(L"bar")); EXPECT_TRUE(cl.HasSwitch(L"baz")); EXPECT_TRUE(cl.HasSwitch(L"spaetzle")); +#if defined(OS_WIN) EXPECT_TRUE(cl.HasSwitch(L"SPAETZLE")); +#endif EXPECT_TRUE(cl.HasSwitch(L"other-switches")); EXPECT_TRUE(cl.HasSwitch(L"input-translation")); @@ -61,9 +59,10 @@ TEST(CommandLineTest, CommandLineConstructor) { EXPECT_EQ(L"--dog=canine --cat=feline", cl.GetSwitchValue(L"other-switches")); EXPECT_EQ(L"45--output-rotation", cl.GetSwitchValue(L"input-translation")); - EXPECT_EQ(5U, cl.GetLooseValueCount()); + std::vector<std::wstring> loose_values = cl.GetLooseValues(); + ASSERT_EQ(5U, loose_values.size()); - CommandLine::LooseValueIterator iter = cl.GetLooseValuesBegin(); + std::vector<std::wstring>::const_iterator iter = loose_values.begin(); EXPECT_EQ(L"flim", *iter); ++iter; EXPECT_EQ(L"flan", *iter); @@ -74,9 +73,9 @@ TEST(CommandLineTest, CommandLineConstructor) { ++iter; EXPECT_EQ(L"in the time of submarines...", *iter); ++iter; - EXPECT_TRUE(iter == cl.GetLooseValuesEnd()); + EXPECT_TRUE(iter == loose_values.end()); #if defined(OS_POSIX) - std::vector<std::string> argvec = cl.argv(); + const std::vector<std::string>& argvec = cl.argv(); for (size_t i = 0; i < argvec.size(); i++) { EXPECT_EQ(0, argvec[i].compare(argv[i])); @@ -84,27 +83,20 @@ TEST(CommandLineTest, CommandLineConstructor) { #endif } -// These test the command line used to invoke the unit test. -TEST(CommandLineTest, DefaultConstructor) { - CommandLine cl; - EXPECT_FALSE(cl.command_line_string().empty()); - EXPECT_FALSE(cl.program().empty()); -} - // Tests behavior with an empty input string. TEST(CommandLineTest, EmptyString) { #if defined(OS_WIN) CommandLine cl(L""); + EXPECT_TRUE(cl.command_line_string().empty()); + EXPECT_TRUE(cl.program().empty()); #elif defined(OS_POSIX) CommandLine cl(0, NULL); EXPECT_TRUE(cl.argv().size() == 0); #endif - EXPECT_TRUE(cl.command_line_string().empty()); - EXPECT_TRUE(cl.program().empty()); - EXPECT_EQ(0U, cl.GetLooseValueCount()); + EXPECT_EQ(0U, cl.GetLooseValues().size()); } -// Test static functions for appending switches to a command line. +// Test methods for appending switches to a command line. TEST(CommandLineTest, AppendSwitches) { std::wstring switch1 = L"switch1"; std::wstring switch2 = L"switch2"; @@ -115,31 +107,24 @@ TEST(CommandLineTest, AppendSwitches) { std::wstring value4 = L"\"a value with quotes\""; #if defined(OS_WIN) - std::wstring cl_string = L"Program"; - CommandLine::AppendSwitch(&cl_string, switch1); - CommandLine::AppendSwitchWithValue(&cl_string, switch2, value); - CommandLine::AppendSwitchWithValue(&cl_string, switch3, value3); - CommandLine::AppendSwitchWithValue(&cl_string, switch4, value4); - CommandLine cl(cl_string); + CommandLine cl(L"Program"); #elif defined(OS_POSIX) std::vector<std::string> argv; argv.push_back(std::string("Program")); - argv.push_back(WideToUTF8(CommandLine::PrefixedSwitchString(switch1))); - argv.push_back(WideToUTF8(CommandLine::PrefixedSwitchStringWithValue( - switch2, value))); - argv.push_back(WideToUTF8(CommandLine::PrefixedSwitchStringWithValue( - switch3, value3))); - argv.push_back(WideToUTF8(CommandLine::PrefixedSwitchStringWithValue( - switch4, value4.substr(1, value4.length() - 2)))); CommandLine cl(argv); #endif + cl.AppendSwitch(switch1); + cl.AppendSwitchWithValue(switch2, value); + cl.AppendSwitchWithValue(switch3, value3); + cl.AppendSwitchWithValue(switch4, value4); + EXPECT_TRUE(cl.HasSwitch(switch1)); EXPECT_TRUE(cl.HasSwitch(switch2)); EXPECT_EQ(value, cl.GetSwitchValue(switch2)); EXPECT_TRUE(cl.HasSwitch(switch3)); EXPECT_EQ(value3, cl.GetSwitchValue(switch3)); EXPECT_TRUE(cl.HasSwitch(switch4)); - EXPECT_EQ(value4.substr(1, value4.length() - 2), cl.GetSwitchValue(switch4)); + EXPECT_EQ(value4, cl.GetSwitchValue(switch4)); } |