diff options
Diffstat (limited to 'absl/flags/parse_test.cc')
-rw-r--r-- | absl/flags/parse_test.cc | 86 |
1 files changed, 59 insertions, 27 deletions
diff --git a/absl/flags/parse_test.cc b/absl/flags/parse_test.cc index c46bb46e..13606d14 100644 --- a/absl/flags/parse_test.cc +++ b/absl/flags/parse_test.cc @@ -250,19 +250,31 @@ class ParseTest : public testing::Test { // -------------------------------------------------------------------- template <int N> -std::vector<char*> InvokeParse(const char* (&in_argv)[N]) { - return absl::ParseCommandLine(N, const_cast<char**>(in_argv)); +flags::HelpMode InvokeParseAbslOnlyImpl(const char* (&in_argv)[N]) { + std::vector<char*> positional_args; + std::vector<absl::UnrecognizedFlag> unrecognized_flags; + + return flags::ParseAbseilFlagsOnlyImpl(N, const_cast<char**>(in_argv), + positional_args, unrecognized_flags, + flags::UsageFlagsAction::kHandleUsage); } // -------------------------------------------------------------------- template <int N> -bool InvokeParseAbslOnly(const char* (&in_argv)[N]) { +void InvokeParseAbslOnly(const char* (&in_argv)[N]) { std::vector<char*> positional_args; std::vector<absl::UnrecognizedFlag> unrecognized_flags; - return absl::ParseAbseilFlagsOnly(N, const_cast<char**>(in_argv), - positional_args, unrecognized_flags); + absl::ParseAbseilFlagsOnly(2, const_cast<char**>(in_argv), positional_args, + unrecognized_flags); +} + +// -------------------------------------------------------------------- + +template <int N> +std::vector<char*> InvokeParse(const char* (&in_argv)[N]) { + return absl::ParseCommandLine(N, const_cast<char**>(in_argv)); } // -------------------------------------------------------------------- @@ -871,6 +883,7 @@ TEST_F(ParseDeathTest, TestSimpleHelpFlagHandling) { "--help", }; + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args1), flags::HelpMode::kImportant); EXPECT_EXIT(InvokeParse(in_args1), testing::ExitedWithCode(1), ""); const char* in_args2[] = { @@ -879,34 +892,55 @@ TEST_F(ParseDeathTest, TestSimpleHelpFlagHandling) { "--int_flag=3", }; - InvokeParseAbslOnly(in_args2); - - EXPECT_EQ(flags::GetFlagsHelpMode(), flags::HelpMode::kImportant); + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args2), flags::HelpMode::kImportant); EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 3); const char* in_args3[] = {"testbin", "--help", "some_positional_arg"}; - InvokeParseAbslOnly(in_args3); - - EXPECT_EQ(flags::GetFlagsHelpMode(), flags::HelpMode::kImportant); + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args3), flags::HelpMode::kImportant); } // -------------------------------------------------------------------- -TEST_F(ParseDeathTest, TestSubstringHelpFlagHandling) { +TEST_F(ParseTest, TestSubstringHelpFlagHandling) { const char* in_args1[] = { "testbin", "--help=abcd", }; - InvokeParseAbslOnly(in_args1); - - EXPECT_EQ(flags::GetFlagsHelpMode(), flags::HelpMode::kMatch); + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args1), flags::HelpMode::kMatch); EXPECT_EQ(flags::GetFlagsHelpMatchSubstr(), "abcd"); } // -------------------------------------------------------------------- +TEST_F(ParseDeathTest, TestVersionHandling) { + const char* in_args1[] = { + "testbin", + "--version", + }; + + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args1), flags::HelpMode::kVersion); +} + +// -------------------------------------------------------------------- + +TEST_F(ParseTest, TestCheckArgsHandling) { + const char* in_args1[] = {"testbin", "--only_check_args", "--int_flag=211"}; + + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args1), flags::HelpMode::kOnlyCheckArgs); + EXPECT_EXIT(InvokeParseAbslOnly(in_args1), testing::ExitedWithCode(0), ""); + EXPECT_EXIT(InvokeParse(in_args1), testing::ExitedWithCode(0), ""); + + const char* in_args2[] = {"testbin", "--only_check_args", "--unknown_flag=a"}; + + EXPECT_EQ(InvokeParseAbslOnlyImpl(in_args2), flags::HelpMode::kOnlyCheckArgs); + EXPECT_EXIT(InvokeParseAbslOnly(in_args2), testing::ExitedWithCode(0), ""); + EXPECT_EXIT(InvokeParse(in_args2), testing::ExitedWithCode(1), ""); +} + +// -------------------------------------------------------------------- + TEST_F(ParseTest, WasPresentOnCommandLine) { const char* in_args1[] = { "testbin", "arg1", "--bool_flag", @@ -947,8 +981,8 @@ TEST_F(ParseTest, ParseAbseilFlagsOnlySuccess) { std::vector<char*> positional_args; std::vector<absl::UnrecognizedFlag> unrecognized_flags; - EXPECT_TRUE(absl::ParseAbseilFlagsOnly(13, const_cast<char**>(in_args), - positional_args, unrecognized_flags)); + absl::ParseAbseilFlagsOnly(13, const_cast<char**>(in_args), positional_args, + unrecognized_flags); EXPECT_THAT(positional_args, ElementsAreArray( {absl::string_view("testbin"), absl::string_view("arg1"), @@ -964,17 +998,15 @@ TEST_F(ParseTest, ParseAbseilFlagsOnlySuccess) { // -------------------------------------------------------------------- -TEST_F(ParseTest, ParseAbseilFlagsOnlyFailure) { +TEST_F(ParseDeathTest, ParseAbseilFlagsOnlyFailure) { const char* in_args[] = { "testbin", "--int_flag=21.1", }; - std::vector<char*> positional_args; - std::vector<absl::UnrecognizedFlag> unrecognized_flags; - - EXPECT_FALSE(absl::ParseAbseilFlagsOnly(2, const_cast<char**>(in_args), - positional_args, unrecognized_flags)); + EXPECT_DEATH_IF_SUPPORTED( + InvokeParseAbslOnly(in_args), + "Illegal value '21.1' specified for flag 'int_flag'"); } // -------------------------------------------------------------------- @@ -989,8 +1021,8 @@ TEST_F(ParseTest, UndefOkFlagsAreIgnored) { std::vector<char*> positional_args; std::vector<absl::UnrecognizedFlag> unrecognized_flags; - EXPECT_TRUE(absl::ParseAbseilFlagsOnly(6, const_cast<char**>(in_args), - positional_args, unrecognized_flags)); + absl::ParseAbseilFlagsOnly(6, const_cast<char**>(in_args), positional_args, + unrecognized_flags); EXPECT_THAT(positional_args, ElementsAreArray({absl::string_view("testbin"), absl::string_view("value")})); EXPECT_THAT(unrecognized_flags, @@ -1018,8 +1050,8 @@ TEST_F(ParseTest, AllUndefOkFlagsAreIgnored) { std::vector<char*> positional_args; std::vector<absl::UnrecognizedFlag> unrecognized_flags; - EXPECT_TRUE(absl::ParseAbseilFlagsOnly(8, const_cast<char**>(in_args), - positional_args, unrecognized_flags)); + absl::ParseAbseilFlagsOnly(8, const_cast<char**>(in_args), positional_args, + unrecognized_flags); EXPECT_THAT(positional_args, ElementsAreArray({absl::string_view("testbin"), absl::string_view("value"), |