Home | History | Annotate | Download | only in base
      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.
      4 
      5 #include <string>
      6 #include <vector>
      7 
      8 #include "base/command_line.h"
      9 #include "base/basictypes.h"
     10 #include "base/string_util.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 TEST(CommandLineTest, CommandLineConstructor) {
     14 #if defined(OS_WIN)
     15   CommandLine cl = CommandLine::FromString(
     16                      L"program --foo= -bAr  /Spaetzel=pierogi /Baz flim "
     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());
     23 #elif defined(OS_POSIX)
     24   const char* argv[] = {"program", "--foo=", "-bar",
     25                         "-spaetzel=pierogi", "-baz", "flim",
     26                         "--other-switches=--dog=canine --cat=feline",
     27                         "-spaetzle=Crepe", "-=loosevalue", "flan",
     28                         "--input-translation=45--output-rotation",
     29                         "--", "--", "--not-a-switch",
     30                         "in the time of submarines..."};
     31   CommandLine cl(arraysize(argv), argv);
     32 #endif
     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("--"));
     41 
     42   EXPECT_EQ(L"program", cl.program());
     43 
     44   EXPECT_TRUE(cl.HasSwitch("foo"));
     45   EXPECT_TRUE(cl.HasSwitch("bar"));
     46   EXPECT_TRUE(cl.HasSwitch("baz"));
     47   EXPECT_TRUE(cl.HasSwitch("spaetzle"));
     48 #if defined(OS_WIN)
     49   EXPECT_TRUE(cl.HasSwitch("SPAETZLE"));
     50 #endif
     51   EXPECT_TRUE(cl.HasSwitch("other-switches"));
     52   EXPECT_TRUE(cl.HasSwitch("input-translation"));
     53 
     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"));
     61 
     62   std::vector<std::wstring> loose_values = cl.GetLooseValues();
     63   ASSERT_EQ(5U, loose_values.size());
     64 
     65   std::vector<std::wstring>::const_iterator iter = loose_values.begin();
     66   EXPECT_EQ(L"flim", *iter);
     67   ++iter;
     68   EXPECT_EQ(L"flan", *iter);
     69   ++iter;
     70   EXPECT_EQ(L"--", *iter);
     71   ++iter;
     72   EXPECT_EQ(L"--not-a-switch", *iter);
     73   ++iter;
     74   EXPECT_EQ(L"in the time of submarines...", *iter);
     75   ++iter;
     76   EXPECT_TRUE(iter == loose_values.end());
     77 #if defined(OS_POSIX)
     78   const std::vector<std::string>& argvec = cl.argv();
     79 
     80   for (size_t i = 0; i < argvec.size(); i++) {
     81     EXPECT_EQ(0, argvec[i].compare(argv[i]));
     82   }
     83 #endif
     84 }
     85 
     86 // Tests behavior with an empty input string.
     87 TEST(CommandLineTest, EmptyString) {
     88 #if defined(OS_WIN)
     89   CommandLine cl = CommandLine::FromString(L"");
     90   EXPECT_TRUE(cl.command_line_string().empty());
     91   EXPECT_TRUE(cl.program().empty());
     92 #elif defined(OS_POSIX)
     93   CommandLine cl(0, NULL);
     94   EXPECT_TRUE(cl.argv().size() == 0);
     95 #endif
     96   EXPECT_EQ(0U, cl.GetLooseValues().size());
     97 }
     98 
     99 // Test methods for appending switches to a command line.
    100 TEST(CommandLineTest, AppendSwitches) {
    101   std::string switch1 = "switch1";
    102   std::string switch2 = "switch2";
    103   std::wstring value = L"value";
    104   std::string switch3 = "switch3";
    105   std::wstring value3 = L"a value with spaces";
    106   std::string switch4 = "switch4";
    107   std::wstring value4 = L"\"a value with quotes\"";
    108 
    109   CommandLine cl(FilePath(FILE_PATH_LITERAL("Program")));
    110 
    111   cl.AppendSwitch(switch1);
    112   cl.AppendSwitchWithValue(switch2, value);
    113   cl.AppendSwitchWithValue(switch3, value3);
    114   cl.AppendSwitchWithValue(switch4, value4);
    115 
    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));
    121   EXPECT_TRUE(cl.HasSwitch(switch4));
    122   EXPECT_EQ(value4, cl.GetSwitchValue(switch4));
    123 }
    124