Home | History | Annotate | Download | only in Option
      1 //===- unittest/Support/OptionParsingTest.cpp - OptTable tests ------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "llvm/ADT/OwningPtr.h"
     11 #include "llvm/Option/Arg.h"
     12 #include "llvm/Option/ArgList.h"
     13 #include "llvm/Option/Option.h"
     14 #include "gtest/gtest.h"
     15 
     16 using namespace llvm;
     17 using namespace llvm::opt;
     18 
     19 enum ID {
     20   OPT_INVALID = 0, // This is not an option ID.
     21 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, FLAGS, PARAM, \
     22               HELPTEXT, METAVAR) OPT_##ID,
     23 #include "Opts.inc"
     24   LastOption
     25 #undef OPTION
     26 };
     27 
     28 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
     29 #include "Opts.inc"
     30 #undef PREFIX
     31 
     32 static const OptTable::Info InfoTable[] = {
     33 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, FLAGS, PARAM, \
     34                HELPTEXT, METAVAR)   \
     35   { PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, PARAM, \
     36     FLAGS, OPT_##GROUP, OPT_##ALIAS },
     37 #include "Opts.inc"
     38 #undef OPTION
     39 };
     40 
     41 namespace {
     42 class TestOptTable : public OptTable {
     43 public:
     44   TestOptTable()
     45     : OptTable(InfoTable, sizeof(InfoTable) / sizeof(InfoTable[0])) {}
     46 };
     47 }
     48 
     49 const char *Args[] = {
     50   "-A",
     51   "-Bhi",
     52   "--C=desu",
     53   "-C", "bye",
     54   "-D,adena",
     55   "-E", "apple", "bloom",
     56   "-Fblarg",
     57   "-F", "42",
     58   "-Gchuu", "2"
     59   };
     60 
     61 TEST(Support, OptionParsing) {
     62   TestOptTable T;
     63   unsigned MAI, MAC;
     64   OwningPtr<InputArgList>
     65     AL(T.ParseArgs(Args,
     66                    Args + (sizeof(Args) / sizeof(Args[0])),
     67                    MAI,
     68                    MAC));
     69 
     70   // Check they all exist.
     71   EXPECT_TRUE(AL->hasArg(OPT_A));
     72   EXPECT_TRUE(AL->hasArg(OPT_B));
     73   EXPECT_TRUE(AL->hasArg(OPT_C));
     74   EXPECT_TRUE(AL->hasArg(OPT_D));
     75   EXPECT_TRUE(AL->hasArg(OPT_E));
     76   EXPECT_TRUE(AL->hasArg(OPT_F));
     77   EXPECT_TRUE(AL->hasArg(OPT_G));
     78 
     79   // Check the values.
     80   EXPECT_EQ(AL->getLastArgValue(OPT_B), "hi");
     81   EXPECT_EQ(AL->getLastArgValue(OPT_C), "bye");
     82   EXPECT_EQ(AL->getLastArgValue(OPT_D), "adena");
     83   std::vector<std::string> Es = AL->getAllArgValues(OPT_E);
     84   EXPECT_EQ(Es[0], "apple");
     85   EXPECT_EQ(Es[1], "bloom");
     86   EXPECT_EQ(AL->getLastArgValue(OPT_F), "42");
     87   std::vector<std::string> Gs = AL->getAllArgValues(OPT_G);
     88   EXPECT_EQ(Gs[0], "chuu");
     89   EXPECT_EQ(Gs[1], "2");
     90 
     91   // Check the help text.
     92   std::string Help;
     93   raw_string_ostream RSO(Help);
     94   T.PrintHelp(RSO, "test", "title!");
     95   EXPECT_NE(Help.find("-A"), std::string::npos);
     96 
     97   // Test aliases.
     98   arg_iterator Cs = AL->filtered_begin(OPT_C);
     99   ASSERT_NE(Cs, AL->filtered_end());
    100   EXPECT_EQ(StringRef((*Cs)->getValue()), "desu");
    101   ArgStringList ASL;
    102   (*Cs)->render(*AL, ASL);
    103   ASSERT_EQ(ASL.size(), 2u);
    104   EXPECT_EQ(StringRef(ASL[0]), "-C");
    105   EXPECT_EQ(StringRef(ASL[1]), "desu");
    106 }
    107