TEST(Option, OptionParsing) { TestOptTable T; unsigned MAI, MAC; InputArgList AL = T.ParseArgs(Args, MAI, MAC); // Check they all exist. EXPECT_TRUE(AL.hasArg(OPT_A)); EXPECT_TRUE(AL.hasArg(OPT_B)); EXPECT_TRUE(AL.hasArg(OPT_C)); EXPECT_TRUE(AL.hasArg(OPT_D)); EXPECT_TRUE(AL.hasArg(OPT_E)); EXPECT_TRUE(AL.hasArg(OPT_F)); EXPECT_TRUE(AL.hasArg(OPT_G)); // Check the values. EXPECT_EQ("hi", AL.getLastArgValue(OPT_B)); EXPECT_EQ("bye", AL.getLastArgValue(OPT_C)); EXPECT_EQ("adena", AL.getLastArgValue(OPT_D)); std::vector<std::string> Es = AL.getAllArgValues(OPT_E); EXPECT_EQ("apple", Es[0]); EXPECT_EQ("bloom", Es[1]); EXPECT_EQ("42", AL.getLastArgValue(OPT_F)); std::vector<std::string> Gs = AL.getAllArgValues(OPT_G); EXPECT_EQ("chuu", Gs[0]); EXPECT_EQ("2", Gs[1]); // Check the help text. std::string Help; raw_string_ostream RSO(Help); T.PrintHelp(RSO, "test", "title!"); EXPECT_NE(std::string::npos, Help.find("-A")); // Check usage line. T.PrintHelp(RSO, "name [options] file...", "title!"); EXPECT_NE(std::string::npos, Help.find("USAGE: name [options] file...\n")); // Test aliases. auto Cs = AL.filtered(OPT_C); ASSERT_NE(Cs.begin(), Cs.end()); EXPECT_EQ("desu", StringRef((*Cs.begin())->getValue())); ArgStringList ASL; (*Cs.begin())->render(AL, ASL); ASSERT_EQ(2u, ASL.size()); EXPECT_EQ("-C", StringRef(ASL[0])); EXPECT_EQ("desu", StringRef(ASL[1])); }
void TestOptions::printHelp(bool ShowHidden) const { // Based off of swift/lib/Driver/Driver.cpp, at Driver::printHelp //FIXME: should we use IncludedFlagsBitmask and ExcludedFlagsBitmask? // Maybe not for modes such as Interactive, Batch, AutolinkExtract, etc, // as in Driver.cpp. But could be useful for extra info, like HelpHidden. TestOptTable Table; Table.PrintHelp(llvm::outs(), "sourcekitd-test", "SourceKit Testing Tool", ShowHidden); }
TEST(Support, OptionParsing) { TestOptTable T; unsigned MAI, MAC; OwningPtr<InputArgList> AL(T.ParseArgs(Args, Args + (sizeof(Args) / sizeof(Args[0])), MAI, MAC)); // Check they all exist. EXPECT_TRUE(AL->hasArg(OPT_A)); EXPECT_TRUE(AL->hasArg(OPT_B)); EXPECT_TRUE(AL->hasArg(OPT_C)); EXPECT_TRUE(AL->hasArg(OPT_D)); EXPECT_TRUE(AL->hasArg(OPT_E)); EXPECT_TRUE(AL->hasArg(OPT_F)); EXPECT_TRUE(AL->hasArg(OPT_G)); // Check the values. EXPECT_EQ(AL->getLastArgValue(OPT_B), "hi"); EXPECT_EQ(AL->getLastArgValue(OPT_C), "bye"); EXPECT_EQ(AL->getLastArgValue(OPT_D), "adena"); std::vector<std::string> Es = AL->getAllArgValues(OPT_E); EXPECT_EQ(Es[0], "apple"); EXPECT_EQ(Es[1], "bloom"); EXPECT_EQ(AL->getLastArgValue(OPT_F), "42"); std::vector<std::string> Gs = AL->getAllArgValues(OPT_G); EXPECT_EQ(Gs[0], "chuu"); EXPECT_EQ(Gs[1], "2"); // Check the help text. std::string Help; raw_string_ostream RSO(Help); T.PrintHelp(RSO, "test", "title!"); EXPECT_NE(Help.find("-A"), std::string::npos); // Test aliases. arg_iterator Cs = AL->filtered_begin(OPT_C); ASSERT_NE(Cs, AL->filtered_end()); EXPECT_EQ(StringRef((*Cs)->getValue()), "desu"); ArgStringList ASL; (*Cs)->render(*AL, ASL); ASSERT_EQ(ASL.size(), 2u); EXPECT_EQ(StringRef(ASL[0]), "-C"); EXPECT_EQ(StringRef(ASL[1]), "desu"); }