summaryrefslogtreecommitdiff
path: root/absl/flags/flag_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'absl/flags/flag_test.cc')
-rw-r--r--absl/flags/flag_test.cc130
1 files changed, 83 insertions, 47 deletions
diff --git a/absl/flags/flag_test.cc b/absl/flags/flag_test.cc
index 1bcd7e96..4984d284 100644
--- a/absl/flags/flag_test.cc
+++ b/absl/flags/flag_test.cc
@@ -15,11 +15,25 @@
#include "absl/flags/flag.h"
+#include <stdint.h>
+
+#include <cmath>
+#include <string>
+#include <vector>
+
#include "gtest/gtest.h"
+#include "absl/base/attributes.h"
+#include "absl/flags/config.h"
+#include "absl/flags/declare.h"
+#include "absl/flags/internal/commandlineflag.h"
+#include "absl/flags/internal/flag.h"
+#include "absl/flags/internal/registry.h"
+#include "absl/flags/usage_config.h"
#include "absl/strings/match.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
+#include "absl/strings/string_view.h"
ABSL_DECLARE_FLAG(int64_t, mistyped_int_flag);
ABSL_DECLARE_FLAG(std::vector<std::string>, mistyped_string_flag);
@@ -28,7 +42,7 @@ namespace {
namespace flags = absl::flags_internal;
-std::string TestHelpMsg() { return "help"; }
+std::string TestHelpMsg() { return "dynamic help"; }
template <typename T>
void* TestMakeDflt() {
return new T{};
@@ -37,20 +51,21 @@ void TestCallback() {}
template <typename T>
bool TestConstructionFor() {
- constexpr flags::Flag<T> f1("f1", &TestHelpMsg, "file",
- &absl::flags_internal::FlagMarshallingOps<T>,
- &TestMakeDflt<T>);
+ constexpr flags::FlagHelpArg help_arg{flags::FlagHelpMsg("literal help"),
+ flags::FlagHelpKind::kLiteral};
+ constexpr flags::Flag<T> f1("f1", "file", help_arg, &TestMakeDflt<T>);
EXPECT_EQ(f1.Name(), "f1");
- EXPECT_EQ(f1.Help(), "help");
+ EXPECT_EQ(f1.Help(), "literal help");
EXPECT_EQ(f1.Filename(), "file");
ABSL_CONST_INIT static flags::Flag<T> f2(
- "f2", &TestHelpMsg, "file", &absl::flags_internal::FlagMarshallingOps<T>,
+ "f2", "file",
+ {flags::FlagHelpMsg(&TestHelpMsg), flags::FlagHelpKind::kGenFunc},
&TestMakeDflt<T>);
flags::FlagRegistrar<T, false>(&f2).OnUpdate(TestCallback);
EXPECT_EQ(f2.Name(), "f2");
- EXPECT_EQ(f2.Help(), "help");
+ EXPECT_EQ(f2.Help(), "dynamic help");
EXPECT_EQ(f2.Filename(), "file");
return true;
@@ -63,7 +78,27 @@ struct UDT {
bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
std::string AbslUnparseFlag(const UDT&) { return ""; }
-TEST(FlagTest, TestConstruction) {
+class FlagTest : public testing::Test {
+ protected:
+ static void SetUpTestSuite() {
+ // Install a function to normalize filenames before this test is run.
+ absl::FlagsUsageConfig default_config;
+ default_config.normalize_filename = &FlagTest::NormalizeFileName;
+ absl::SetFlagsUsageConfig(default_config);
+ }
+
+ private:
+ static std::string NormalizeFileName(absl::string_view fname) {
+#ifdef _WIN32
+ std::string normalized(fname);
+ std::replace(normalized.begin(), normalized.end(), '\\', '/');
+ fname = normalized;
+#endif
+ return std::string(fname);
+ }
+};
+
+TEST_F(FlagTest, TestConstruction) {
TestConstructionFor<bool>();
TestConstructionFor<int16_t>();
TestConstructionFor<uint16_t>();
@@ -98,7 +133,7 @@ namespace {
#if !ABSL_FLAGS_STRIP_NAMES
-TEST(FlagTest, TestFlagDeclaration) {
+TEST_F(FlagTest, TestFlagDeclaration) {
// test that we can access flag objects.
EXPECT_EQ(FLAGS_test_flag_01.Name(), "test_flag_01");
EXPECT_EQ(FLAGS_test_flag_02.Name(), "test_flag_02");
@@ -133,69 +168,69 @@ ABSL_FLAG(std::string, test_flag_11, "", "test flag 11");
namespace {
#if !ABSL_FLAGS_STRIP_NAMES
-TEST(FlagTest, TestFlagDefinition) {
+TEST_F(FlagTest, TestFlagDefinition) {
absl::string_view expected_file_name = "absl/flags/flag_test.cc";
EXPECT_EQ(FLAGS_test_flag_01.Name(), "test_flag_01");
EXPECT_EQ(FLAGS_test_flag_01.Help(), "test flag 01");
- EXPECT_TRUE(
- absl::EndsWith(FLAGS_test_flag_01.Filename(), expected_file_name));
+ EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_01.Filename(), expected_file_name))
+ << FLAGS_test_flag_01.Filename();
EXPECT_EQ(FLAGS_test_flag_02.Name(), "test_flag_02");
EXPECT_EQ(FLAGS_test_flag_02.Help(), "test flag 02");
- EXPECT_TRUE(
- absl::EndsWith(FLAGS_test_flag_02.Filename(), expected_file_name));
+ EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_02.Filename(), expected_file_name))
+ << FLAGS_test_flag_02.Filename();
EXPECT_EQ(FLAGS_test_flag_03.Name(), "test_flag_03");
EXPECT_EQ(FLAGS_test_flag_03.Help(), "test flag 03");
- EXPECT_TRUE(
- absl::EndsWith(FLAGS_test_flag_03.Filename(), expected_file_name));
+ EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_03.Filename(), expected_file_name))
+ << FLAGS_test_flag_03.Filename();
EXPECT_EQ(FLAGS_test_flag_04.Name(), "test_flag_04");
EXPECT_EQ(FLAGS_test_flag_04.Help(), "test flag 04");
- EXPECT_TRUE(
- absl::EndsWith(FLAGS_test_flag_04.Filename(), expected_file_name));
+ EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_04.Filename(), expected_file_name))
+ << FLAGS_test_flag_04.Filename();
EXPECT_EQ(FLAGS_test_flag_05.Name(), "test_flag_05");
EXPECT_EQ(FLAGS_test_flag_05.Help(), "test flag 05");
- EXPECT_TRUE(
- absl::EndsWith(FLAGS_test_flag_05.Filename(), expected_file_name));
+ EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_05.Filename(), expected_file_name))
+ << FLAGS_test_flag_05.Filename();
EXPECT_EQ(FLAGS_test_flag_06.Name(), "test_flag_06");
EXPECT_EQ(FLAGS_test_flag_06.Help(), "test flag 06");
- EXPECT_TRUE(
- absl::EndsWith(FLAGS_test_flag_06.Filename(), expected_file_name));
+ EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_06.Filename(), expected_file_name))
+ << FLAGS_test_flag_06.Filename();
EXPECT_EQ(FLAGS_test_flag_07.Name(), "test_flag_07");
EXPECT_EQ(FLAGS_test_flag_07.Help(), "test flag 07");
- EXPECT_TRUE(
- absl::EndsWith(FLAGS_test_flag_07.Filename(), expected_file_name));
+ EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_07.Filename(), expected_file_name))
+ << FLAGS_test_flag_07.Filename();
EXPECT_EQ(FLAGS_test_flag_08.Name(), "test_flag_08");
EXPECT_EQ(FLAGS_test_flag_08.Help(), "test flag 08");
- EXPECT_TRUE(
- absl::EndsWith(FLAGS_test_flag_08.Filename(), expected_file_name));
+ EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_08.Filename(), expected_file_name))
+ << FLAGS_test_flag_08.Filename();
EXPECT_EQ(FLAGS_test_flag_09.Name(), "test_flag_09");
EXPECT_EQ(FLAGS_test_flag_09.Help(), "test flag 09");
- EXPECT_TRUE(
- absl::EndsWith(FLAGS_test_flag_09.Filename(), expected_file_name));
+ EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_09.Filename(), expected_file_name))
+ << FLAGS_test_flag_09.Filename();
EXPECT_EQ(FLAGS_test_flag_10.Name(), "test_flag_10");
EXPECT_EQ(FLAGS_test_flag_10.Help(), "test flag 10");
- EXPECT_TRUE(
- absl::EndsWith(FLAGS_test_flag_10.Filename(), expected_file_name));
+ EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_10.Filename(), expected_file_name))
+ << FLAGS_test_flag_10.Filename();
EXPECT_EQ(FLAGS_test_flag_11.Name(), "test_flag_11");
EXPECT_EQ(FLAGS_test_flag_11.Help(), "test flag 11");
- EXPECT_TRUE(
- absl::EndsWith(FLAGS_test_flag_11.Filename(), expected_file_name));
+ EXPECT_TRUE(absl::EndsWith(FLAGS_test_flag_11.Filename(), expected_file_name))
+ << FLAGS_test_flag_11.Filename();
}
#endif // !ABSL_FLAGS_STRIP_NAMES
// --------------------------------------------------------------------
-TEST(FlagTest, TestDefault) {
+TEST_F(FlagTest, TestDefault) {
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_01), true);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_02), 1234);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_03), -34);
@@ -211,7 +246,7 @@ TEST(FlagTest, TestDefault) {
// --------------------------------------------------------------------
-TEST(FlagTest, TestGetSet) {
+TEST_F(FlagTest, TestGetSet) {
absl::SetFlag(&FLAGS_test_flag_01, false);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_01), false);
@@ -258,7 +293,7 @@ ABSL_FLAG(std::string, test_flag_13, absl::StrCat("AAA", "BBB"),
namespace {
-TEST(FlagTest, TestNonConstexprDefault) {
+TEST_F(FlagTest, TestNonConstexprDefault) {
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_12), 1);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_13), "AAABBB");
}
@@ -272,7 +307,7 @@ ABSL_FLAG(bool, test_flag_14, true, absl::StrCat("test ", "flag ", "14"));
namespace {
#if !ABSL_FLAGS_STRIP_HELP
-TEST(FlagTest, TestNonConstexprHelp) {
+TEST_F(FlagTest, TestNonConstexprHelp) {
EXPECT_EQ(FLAGS_test_flag_14.Help(), "test flag 14");
}
#endif //! ABSL_FLAGS_STRIP_HELP
@@ -296,7 +331,7 @@ namespace {
void TestFlagCB() { cb_test_value = absl::GetFlag(FLAGS_test_flag_with_cb); }
// Tests side-effects of callback invocation.
-TEST(FlagTest, CallbackInvocation) {
+TEST_F(FlagTest, CallbackInvocation) {
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_with_cb), 100);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_with_lambda_cb), 200);
EXPECT_EQ(cb_test_value, 300);
@@ -343,7 +378,7 @@ ABSL_FLAG(CustomUDT, test_flag_15, CustomUDT(), "test flag 15");
namespace {
-TEST(FlagTest, TestCustomUDT) {
+TEST_F(FlagTest, TestCustomUDT) {
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_15), CustomUDT(1, 1));
absl::SetFlag(&FLAGS_test_flag_15, CustomUDT(2, 3));
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_15), CustomUDT(2, 3));
@@ -351,19 +386,20 @@ TEST(FlagTest, TestCustomUDT) {
// MSVC produces link error on the type mismatch.
// Linux does not have build errors and validations work as expected.
-#if 0 // !defined(_WIN32) && GTEST_HAS_DEATH_TEST
+#if !defined(_WIN32) && GTEST_HAS_DEATH_TEST
+
+using FlagDeathTest = FlagTest;
-TEST(Flagtest, TestTypeMismatchValidations) {
- // For builtin types, GetFlag() only does validation in debug mode.
+TEST_F(FlagDeathTest, TestTypeMismatchValidations) {
EXPECT_DEBUG_DEATH(
- absl::GetFlag(FLAGS_mistyped_int_flag),
+ static_cast<void>(absl::GetFlag(FLAGS_mistyped_int_flag)),
"Flag 'mistyped_int_flag' is defined as one type and declared "
"as another");
- EXPECT_DEATH(absl::SetFlag(&FLAGS_mistyped_int_flag, 0),
+ EXPECT_DEATH(absl::SetFlag(&FLAGS_mistyped_int_flag, 1),
"Flag 'mistyped_int_flag' is defined as one type and declared "
"as another");
- EXPECT_DEATH(absl::GetFlag(FLAGS_mistyped_string_flag),
+ EXPECT_DEATH(static_cast<void>(absl::GetFlag(FLAGS_mistyped_string_flag)),
"Flag 'mistyped_string_flag' is defined as one type and "
"declared as another");
EXPECT_DEATH(
@@ -409,7 +445,7 @@ ABSL_FLAG(ConversionTestVal, test_flag_16,
namespace {
-TEST(FlagTest, CanSetViaImplicitConversion) {
+TEST_F(FlagTest, CanSetViaImplicitConversion) {
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_16).a, 10);
absl::SetFlag(&FLAGS_test_flag_16,
ConversionTestVal::ViaImplicitConv::kEleven);
@@ -447,7 +483,7 @@ ABSL_FLAG(NonDfltConstructible, ndc_flag2, 0,
namespace {
-TEST(FlagTest, TestNonDefaultConstructibleType) {
+TEST_F(FlagTest, TestNonDefaultConstructibleType) {
EXPECT_EQ(absl::GetFlag(FLAGS_ndc_flag1).value, '1' + 100);
EXPECT_EQ(absl::GetFlag(FLAGS_ndc_flag2).value, 0);
@@ -468,7 +504,7 @@ ABSL_RETIRED_FLAG(std::string, old_str_flag, "", absl::StrCat("old ", "descr"));
namespace {
-TEST(FlagTest, TestRetiredFlagRegistration) {
+TEST_F(FlagTest, TestRetiredFlagRegistration) {
bool is_bool = false;
EXPECT_TRUE(flags::IsRetiredFlag("old_bool_flag", &is_bool));
EXPECT_TRUE(is_bool);