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