aa468ad755
-- 78293ba4eb4981991ae7e6edd25eb9245fcd7515 by Andy Soffer <asoffer@google.com>: internal changes PiperOrigin-RevId: 247073879 -- 7bd97e3aad0c3012b89a39392a6ad3f254e9f3c3 by Derek Mauro <dmauro@google.com>: Release commandline flags PiperOrigin-RevId: 247065920 -- 2a991849fd7b140a43b073076d194b61533fd199 by Tom Manshreck <shreck@google.com>: Add documentation for built-in usage flags PiperOrigin-RevId: 247058863 -- 14157b0de45841706bbd631284191fd94c313db9 by Derek Mauro <dmauro@google.com>: Internal change PiperOrigin-RevId: 247058292 -- ed67e0a80468596e30540b367727a250fa415b68 by Abseil Team <absl-team@google.com>: Internal Change. PiperOrigin-RevId: 246828655 -- e1bc8c2aa3f90b3d56c55c5b7244e718c919265d by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 246822325 -- 907a68f287199f749cb9bdb48571b50bc34731e1 by Eric Fiselier <ericwf@google.com>: Accept vector<bool>::reference in StrCat and friends. Converting vector<bool>::reference to alphanum requires two user defined conversions, which isn't allowed. In order to accept this, we need a special constructor that is only enabled for the bool proxy types. PiperOrigin-RevId: 246626732 -- fe4295fa95cc65dee8c881ba12dd8f516e68c40d by Abseil Team <absl-team@google.com>: Clarify the proper way to define AbslHashValue, and fix confusing indentation. PiperOrigin-RevId: 246406528 -- d7174681a72d4a25c8fd2b4d9f515a0763eff87c by Gennadiy Rozental <rogeeff@google.com>: Internal change PiperOrigin-RevId: 246358214 -- a7fdc19683cf1a5885e9e1af52fcdb2db1eda53b by Abseil Team <absl-team@google.com>: Fix sample template instantiation. PiperOrigin-RevId: 246354617 -- 29a23c6835f872948d09b24d890385bf195bc995 by Abseil Team <absl-team@google.com>: Fix incorrectly copy-pasted static_assert in absl::optional::value_or The static_assert for a move-constructible type previously said the type must be copy-constructible. PiperOrigin-RevId: 246313827 -- 41884d5872d4ea2c67875b00144b8c8a5859a295 by Greg Falcon <gfalcon@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 245418790 -- 7f90a7f94cdd5e21232c749efe952a750b5c43a2 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 245412658 -- 730a329cf047d54b46971fce1781edd857208c2a by Greg Falcon <gfalcon@google.com>: internal change PiperOrigin-RevId: 245293234 -- cf0216be6338200cbb18167d3f3b2e98e372be77 by Greg Falcon <gfalcon@google.com>: Internal change PiperOrigin-RevId: 245288164 GitOrigin-RevId: 78293ba4eb4981991ae7e6edd25eb9245fcd7515 Change-Id: I5ea9a852c36c722bae2d6be65fb7f72473d94ab6
367 lines
12 KiB
C++
367 lines
12 KiB
C++
//
|
|
// Copyright 2019 The Abseil Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include <sstream>
|
|
|
|
#include "gtest/gtest.h"
|
|
#include "absl/flags/flag.h"
|
|
#include "absl/flags/parse.h"
|
|
#include "absl/flags/internal/path_util.h"
|
|
#include "absl/flags/internal/program_name.h"
|
|
#include "absl/flags/internal/usage.h"
|
|
#include "absl/flags/usage_config.h"
|
|
#include "absl/memory/memory.h"
|
|
#include "absl/strings/match.h"
|
|
|
|
ABSL_FLAG(int, usage_reporting_test_flag_01, 101,
|
|
"usage_reporting_test_flag_01 help message");
|
|
ABSL_FLAG(bool, usage_reporting_test_flag_02, false,
|
|
"usage_reporting_test_flag_02 help message");
|
|
ABSL_FLAG(double, usage_reporting_test_flag_03, 1.03,
|
|
"usage_reporting_test_flag_03 help message");
|
|
ABSL_FLAG(int64_t, usage_reporting_test_flag_04, 1000000000000004L,
|
|
"usage_reporting_test_flag_04 help message");
|
|
|
|
struct UDT {
|
|
UDT() = default;
|
|
UDT(const UDT&) = default;
|
|
};
|
|
bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
|
|
std::string AbslUnparseFlag(const UDT&) { return "UDT{}"; }
|
|
|
|
ABSL_FLAG(UDT, usage_reporting_test_flag_05, {},
|
|
"usage_reporting_test_flag_05 help message");
|
|
|
|
namespace {
|
|
|
|
namespace flags = absl::flags_internal;
|
|
|
|
static std::string NormalizeFileName(absl::string_view fname) {
|
|
#ifdef _WIN32
|
|
std::string normalized(fname);
|
|
std::replace(normalized.begin(), normalized.end(), '\\', '/');
|
|
fname = normalized;
|
|
#endif
|
|
|
|
auto absl_pos = fname.find("/absl/");
|
|
if (absl_pos != absl::string_view::npos) {
|
|
fname = fname.substr(absl_pos + 1);
|
|
}
|
|
return std::string(fname);
|
|
}
|
|
|
|
class UsageReportingTest : public testing::Test {
|
|
protected:
|
|
UsageReportingTest() {
|
|
// Install default config for the use on this unit test.
|
|
// Binary may install a custom config before tests are run.
|
|
absl::FlagsUsageConfig default_config;
|
|
default_config.normalize_filename = &NormalizeFileName;
|
|
absl::SetFlagsUsageConfig(default_config);
|
|
}
|
|
|
|
private:
|
|
flags::FlagSaver flag_saver_;
|
|
};
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
using UsageReportingDeathTest = UsageReportingTest;
|
|
|
|
TEST_F(UsageReportingDeathTest, TestSetProgramUsageMessage) {
|
|
EXPECT_EQ(flags::ProgramUsageMessage(), "Custom usage message");
|
|
|
|
#ifndef _WIN32
|
|
// TODO(rogeeff): figure out why this does not work on Windows.
|
|
EXPECT_DEATH(flags::SetProgramUsageMessage("custom usage message"),
|
|
".*SetProgramUsageMessage\\(\\) called twice.*");
|
|
#endif
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_01) {
|
|
const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_01");
|
|
std::stringstream test_buf;
|
|
|
|
flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
|
|
EXPECT_EQ(
|
|
test_buf.str(),
|
|
R"( -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
|
|
default: 101;
|
|
)");
|
|
}
|
|
|
|
TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_02) {
|
|
const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_02");
|
|
std::stringstream test_buf;
|
|
|
|
flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
|
|
EXPECT_EQ(
|
|
test_buf.str(),
|
|
R"( -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
|
|
default: false;
|
|
)");
|
|
}
|
|
|
|
TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_03) {
|
|
const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_03");
|
|
std::stringstream test_buf;
|
|
|
|
flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
|
|
EXPECT_EQ(
|
|
test_buf.str(),
|
|
R"( -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
|
|
default: 1.03;
|
|
)");
|
|
}
|
|
|
|
TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_04) {
|
|
const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_04");
|
|
std::stringstream test_buf;
|
|
|
|
flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
|
|
EXPECT_EQ(
|
|
test_buf.str(),
|
|
R"( -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
|
|
default: 1000000000000004;
|
|
)");
|
|
}
|
|
|
|
TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_05) {
|
|
const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_05");
|
|
std::stringstream test_buf;
|
|
|
|
flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
|
|
EXPECT_EQ(
|
|
test_buf.str(),
|
|
R"( -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
|
|
default: UDT{};
|
|
)");
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
TEST_F(UsageReportingTest, TestFlagsHelpHRF) {
|
|
std::string usage_test_flags_out =
|
|
R"(usage_test: Custom usage message
|
|
|
|
Flags from absl/flags/internal/usage_test.cc:
|
|
-usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
|
|
default: 101;
|
|
-usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
|
|
default: false;
|
|
-usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
|
|
default: 1.03;
|
|
-usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
|
|
default: 1000000000000004;
|
|
-usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
|
|
default: UDT{};
|
|
)";
|
|
|
|
std::stringstream test_buf_01;
|
|
flags::FlagsHelp(test_buf_01, "usage_test.cc",
|
|
flags::HelpFormat::kHumanReadable);
|
|
EXPECT_EQ(test_buf_01.str(), usage_test_flags_out);
|
|
|
|
std::stringstream test_buf_02;
|
|
flags::FlagsHelp(test_buf_02, "flags/internal/usage_test.cc",
|
|
flags::HelpFormat::kHumanReadable);
|
|
EXPECT_EQ(test_buf_02.str(), usage_test_flags_out);
|
|
|
|
std::stringstream test_buf_03;
|
|
flags::FlagsHelp(test_buf_03, "usage_test",
|
|
flags::HelpFormat::kHumanReadable);
|
|
EXPECT_EQ(test_buf_03.str(), usage_test_flags_out);
|
|
|
|
std::stringstream test_buf_04;
|
|
flags::FlagsHelp(test_buf_04, "flags/invalid_file_name.cc",
|
|
flags::HelpFormat::kHumanReadable);
|
|
EXPECT_EQ(test_buf_04.str(),
|
|
R"(usage_test: Custom usage message
|
|
|
|
No modules matched: use -helpfull
|
|
)");
|
|
|
|
std::stringstream test_buf_05;
|
|
flags::FlagsHelp(test_buf_05, "", flags::HelpFormat::kHumanReadable);
|
|
std::string test_out = test_buf_05.str();
|
|
absl::string_view test_out_str(test_out);
|
|
EXPECT_TRUE(
|
|
absl::StartsWith(test_out_str, "usage_test: Custom usage message"));
|
|
EXPECT_TRUE(absl::StrContains(
|
|
test_out_str, "Flags from absl/flags/internal/usage_test.cc:"));
|
|
EXPECT_TRUE(absl::StrContains(test_out_str,
|
|
"Flags from absl/flags/internal/usage.cc:"));
|
|
EXPECT_TRUE(
|
|
absl::StrContains(test_out_str, "-usage_reporting_test_flag_01 "));
|
|
EXPECT_TRUE(absl::StrContains(test_out_str, "-help (show help"))
|
|
<< test_out_str;
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
TEST_F(UsageReportingTest, TestNoUsageFlags) {
|
|
std::stringstream test_buf;
|
|
EXPECT_EQ(flags::HandleUsageFlags(test_buf), -1);
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
TEST_F(UsageReportingTest, TestUsageFlag_helpshort) {
|
|
absl::SetFlag(&FLAGS_helpshort, true);
|
|
|
|
std::stringstream test_buf;
|
|
EXPECT_EQ(flags::HandleUsageFlags(test_buf), 1);
|
|
EXPECT_EQ(test_buf.str(),
|
|
R"(usage_test: Custom usage message
|
|
|
|
Flags from absl/flags/internal/usage_test.cc:
|
|
-usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
|
|
default: 101;
|
|
-usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
|
|
default: false;
|
|
-usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
|
|
default: 1.03;
|
|
-usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
|
|
default: 1000000000000004;
|
|
-usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
|
|
default: UDT{};
|
|
)");
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
TEST_F(UsageReportingTest, TestUsageFlag_help) {
|
|
absl::SetFlag(&FLAGS_help, true);
|
|
|
|
std::stringstream test_buf;
|
|
EXPECT_EQ(flags::HandleUsageFlags(test_buf), 1);
|
|
EXPECT_EQ(test_buf.str(),
|
|
R"(usage_test: Custom usage message
|
|
|
|
Flags from absl/flags/internal/usage_test.cc:
|
|
-usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
|
|
default: 101;
|
|
-usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
|
|
default: false;
|
|
-usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
|
|
default: 1.03;
|
|
-usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
|
|
default: 1000000000000004;
|
|
-usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
|
|
default: UDT{};
|
|
|
|
Try --helpfull to get a list of all flags.
|
|
)");
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
TEST_F(UsageReportingTest, TestUsageFlag_helppackage) {
|
|
absl::SetFlag(&FLAGS_helppackage, true);
|
|
|
|
std::stringstream test_buf;
|
|
EXPECT_EQ(flags::HandleUsageFlags(test_buf), 1);
|
|
EXPECT_EQ(test_buf.str(),
|
|
R"(usage_test: Custom usage message
|
|
|
|
Flags from absl/flags/internal/usage_test.cc:
|
|
-usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
|
|
default: 101;
|
|
-usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
|
|
default: false;
|
|
-usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
|
|
default: 1.03;
|
|
-usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
|
|
default: 1000000000000004;
|
|
-usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
|
|
default: UDT{};
|
|
|
|
Try --helpfull to get a list of all flags.
|
|
)");
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
TEST_F(UsageReportingTest, TestUsageFlag_version) {
|
|
absl::SetFlag(&FLAGS_version, true);
|
|
|
|
std::stringstream test_buf;
|
|
EXPECT_EQ(flags::HandleUsageFlags(test_buf), 0);
|
|
#ifndef NDEBUG
|
|
EXPECT_EQ(test_buf.str(),
|
|
"usage_test\nDebug build (NDEBUG not #defined)\n");
|
|
#else
|
|
EXPECT_EQ(test_buf.str(), "usage_test\n");
|
|
#endif
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
TEST_F(UsageReportingTest, TestUsageFlag_only_check_args) {
|
|
absl::SetFlag(&FLAGS_only_check_args, true);
|
|
|
|
std::stringstream test_buf;
|
|
EXPECT_EQ(flags::HandleUsageFlags(test_buf), 0);
|
|
EXPECT_EQ(test_buf.str(), "");
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
TEST_F(UsageReportingTest, TestUsageFlag_helpon) {
|
|
absl::SetFlag(&FLAGS_helpon, "bla-bla");
|
|
|
|
std::stringstream test_buf_01;
|
|
EXPECT_EQ(flags::HandleUsageFlags(test_buf_01), 1);
|
|
EXPECT_EQ(test_buf_01.str(),
|
|
R"(usage_test: Custom usage message
|
|
|
|
No modules matched: use -helpfull
|
|
)");
|
|
|
|
absl::SetFlag(&FLAGS_helpon, "usage_test");
|
|
|
|
std::stringstream test_buf_02;
|
|
EXPECT_EQ(flags::HandleUsageFlags(test_buf_02), 1);
|
|
EXPECT_EQ(test_buf_02.str(),
|
|
R"(usage_test: Custom usage message
|
|
|
|
Flags from absl/flags/internal/usage_test.cc:
|
|
-usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
|
|
default: 101;
|
|
-usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
|
|
default: false;
|
|
-usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
|
|
default: 1.03;
|
|
-usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
|
|
default: 1000000000000004;
|
|
-usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
|
|
default: UDT{};
|
|
)");
|
|
}
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char* argv[]) {
|
|
absl::GetFlag(FLAGS_undefok); // Force linking of parse.cc
|
|
flags::SetProgramInvocationName("usage_test");
|
|
flags::SetProgramUsageMessage("Custom usage message");
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
}
|