diff options
Diffstat (limited to 'llvm/unittests/Support/CommandLineTest.cpp')
-rw-r--r-- | llvm/unittests/Support/CommandLineTest.cpp | 173 |
1 files changed, 172 insertions, 1 deletions
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp index 4839a31..4bfa8a3 100644 --- a/llvm/unittests/Support/CommandLineTest.cpp +++ b/llvm/unittests/Support/CommandLineTest.cpp @@ -1132,8 +1132,13 @@ TEST(CommandLineTest, GroupingWithValue) { cl::ResetCommandLineParser(); StackOption<bool> OptF("f", cl::Grouping, cl::desc("Some flag")); + StackOption<bool> OptB("b", cl::Grouping, cl::desc("Another flag")); + StackOption<bool> OptD("d", cl::Grouping, cl::ValueDisallowed, + cl::desc("ValueDisallowed option")); StackOption<std::string> OptV("v", cl::Grouping, - cl::desc("Grouping option with a value")); + cl::desc("ValueRequired option")); + StackOption<std::string> OptO("o", cl::Grouping, cl::ValueOptional, + cl::desc("ValueOptional option")); // Should be possible to use an option which requires a value // at the end of a group. @@ -1142,12 +1147,178 @@ TEST(CommandLineTest, GroupingWithValue) { cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls())); EXPECT_TRUE(OptF); EXPECT_STREQ("val1", OptV.c_str()); + OptV.clear(); cl::ResetAllOptionOccurrences(); // Should not crash if it is accidentally used elsewhere in the group. const char *args2[] = {"prog", "-vf", "val2"}; EXPECT_FALSE( cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls())); + OptV.clear(); + cl::ResetAllOptionOccurrences(); + + // Should allow the "opt=value" form at the end of the group + const char *args3[] = {"prog", "-fv=val3"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args3, StringRef(), &llvm::nulls())); + EXPECT_TRUE(OptF); + EXPECT_STREQ("val3", OptV.c_str()); + OptV.clear(); + cl::ResetAllOptionOccurrences(); + + // Should allow assigning a value for a ValueOptional option + // at the end of the group + const char *args4[] = {"prog", "-fo=val4"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args4, StringRef(), &llvm::nulls())); + EXPECT_TRUE(OptF); + EXPECT_STREQ("val4", OptO.c_str()); + OptO.clear(); + cl::ResetAllOptionOccurrences(); + + // Should assign an empty value if a ValueOptional option is used elsewhere + // in the group. + const char *args5[] = {"prog", "-fob"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args5, StringRef(), &llvm::nulls())); + EXPECT_TRUE(OptF); + EXPECT_EQ(1, OptO.getNumOccurrences()); + EXPECT_EQ(1, OptB.getNumOccurrences()); + EXPECT_TRUE(OptO.empty()); + cl::ResetAllOptionOccurrences(); + + // Should not allow an assignment for a ValueDisallowed option. + const char *args6[] = {"prog", "-fd=false"}; + EXPECT_FALSE( + cl::ParseCommandLineOptions(2, args6, StringRef(), &llvm::nulls())); +} + +TEST(CommandLineTest, GroupingAndPrefix) { + cl::ResetCommandLineParser(); + + StackOption<bool> OptF("f", cl::Grouping, cl::desc("Some flag")); + StackOption<bool> OptB("b", cl::Grouping, cl::desc("Another flag")); + StackOption<std::string> OptP("p", cl::Prefix, cl::Grouping, + cl::desc("Prefix and Grouping")); + StackOption<std::string> OptA("a", cl::AlwaysPrefix, cl::Grouping, + cl::desc("AlwaysPrefix and Grouping")); + + // Should be possible to use a cl::Prefix option without grouping. + const char *args1[] = {"prog", "-pval1"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args1, StringRef(), &llvm::nulls())); + EXPECT_STREQ("val1", OptP.c_str()); + OptP.clear(); + cl::ResetAllOptionOccurrences(); + + // Should be possible to pass a value in a separate argument. + const char *args2[] = {"prog", "-p", "val2"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls())); + EXPECT_STREQ("val2", OptP.c_str()); + OptP.clear(); + cl::ResetAllOptionOccurrences(); + + // The "-opt=value" form should work, too. + const char *args3[] = {"prog", "-p=val3"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args3, StringRef(), &llvm::nulls())); + EXPECT_STREQ("val3", OptP.c_str()); + OptP.clear(); + cl::ResetAllOptionOccurrences(); + + // All three previous cases should work the same way if an option with both + // cl::Prefix and cl::Grouping modifiers is used at the end of a group. + const char *args4[] = {"prog", "-fpval4"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args4, StringRef(), &llvm::nulls())); + EXPECT_TRUE(OptF); + EXPECT_STREQ("val4", OptP.c_str()); + OptP.clear(); + cl::ResetAllOptionOccurrences(); + + const char *args5[] = {"prog", "-fp", "val5"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(3, args5, StringRef(), &llvm::nulls())); + EXPECT_TRUE(OptF); + EXPECT_STREQ("val5", OptP.c_str()); + OptP.clear(); + cl::ResetAllOptionOccurrences(); + + const char *args6[] = {"prog", "-fp=val6"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args6, StringRef(), &llvm::nulls())); + EXPECT_TRUE(OptF); + EXPECT_STREQ("val6", OptP.c_str()); + OptP.clear(); + cl::ResetAllOptionOccurrences(); + + // Should assign a value even if the part after a cl::Prefix option is equal + // to the name of another option. + const char *args7[] = {"prog", "-fpb"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args7, StringRef(), &llvm::nulls())); + EXPECT_TRUE(OptF); + EXPECT_STREQ("b", OptP.c_str()); + EXPECT_FALSE(OptB); + OptP.clear(); + cl::ResetAllOptionOccurrences(); + + // Should be possible to use a cl::AlwaysPrefix option without grouping. + const char *args8[] = {"prog", "-aval8"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args8, StringRef(), &llvm::nulls())); + EXPECT_STREQ("val8", OptA.c_str()); + OptA.clear(); + cl::ResetAllOptionOccurrences(); + + // Should not be possible to pass a value in a separate argument. + const char *args9[] = {"prog", "-a", "val9"}; + EXPECT_FALSE( + cl::ParseCommandLineOptions(3, args9, StringRef(), &llvm::nulls())); + cl::ResetAllOptionOccurrences(); + + // With the "-opt=value" form, the "=" symbol should be preserved. + const char *args10[] = {"prog", "-a=val10"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args10, StringRef(), &llvm::nulls())); + EXPECT_STREQ("=val10", OptA.c_str()); + OptA.clear(); + cl::ResetAllOptionOccurrences(); + + // All three previous cases should work the same way if an option with both + // cl::AlwaysPrefix and cl::Grouping modifiers is used at the end of a group. + const char *args11[] = {"prog", "-faval11"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args11, StringRef(), &llvm::nulls())); + EXPECT_TRUE(OptF); + EXPECT_STREQ("val11", OptA.c_str()); + OptA.clear(); + cl::ResetAllOptionOccurrences(); + + const char *args12[] = {"prog", "-fa", "val12"}; + EXPECT_FALSE( + cl::ParseCommandLineOptions(3, args12, StringRef(), &llvm::nulls())); + cl::ResetAllOptionOccurrences(); + + const char *args13[] = {"prog", "-fa=val13"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args13, StringRef(), &llvm::nulls())); + EXPECT_TRUE(OptF); + EXPECT_STREQ("=val13", OptA.c_str()); + OptA.clear(); + cl::ResetAllOptionOccurrences(); + + // Should assign a value even if the part after a cl::AlwaysPrefix option + // is equal to the name of another option. + const char *args14[] = {"prog", "-fab"}; + EXPECT_TRUE( + cl::ParseCommandLineOptions(2, args14, StringRef(), &llvm::nulls())); + EXPECT_TRUE(OptF); + EXPECT_STREQ("b", OptA.c_str()); + EXPECT_FALSE(OptB); + OptA.clear(); + cl::ResetAllOptionOccurrences(); } } // anonymous namespace |