summaryrefslogtreecommitdiff
path: root/absl/flags
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2020-08-11 10:36:30 -0700
committerGravatar Gennadiy Rozental <rogeeff@google.com>2020-08-12 02:02:29 -0400
commit1b7e751e58ce7d40ac7d1ffd944a8e6928994dfa (patch)
tree3a9a34e5a000310bf149bd4151221111196f17c9 /absl/flags
parentce4bc927755fdf0ed03d679d9c7fa041175bb3cb (diff)
Export of internal Abseil changes
-- feb01287934e6824d3a7be58a835e1d0254a353e by Derek Mauro <dmauro@google.com>: Add a general question template to our issue templates PiperOrigin-RevId: 326051081 -- cf3c0ab96f179e5396574b0a1dd655d6f48b031c by Derek Mauro <dmauro@google.com>: Add a bug report issue template PiperOrigin-RevId: 326026210 -- e67506083474736fee2a98c585383481b6bf6f2f by Xiaoyi Zhang <zhangxy@google.com>: Internal change PiperOrigin-RevId: 325885389 -- 59c6ad5f6004e8c62ceb28611cbc88cd52b65a16 by Gennadiy Rozental <rogeeff@google.com>: Internal change PiperOrigin-RevId: 325836079 GitOrigin-RevId: feb01287934e6824d3a7be58a835e1d0254a353e Change-Id: I303617dfb5f05aea0bec089c84e2b486f12882b2
Diffstat (limited to 'absl/flags')
-rw-r--r--absl/flags/BUILD.bazel1
-rw-r--r--absl/flags/reflection_test.cc181
2 files changed, 182 insertions, 0 deletions
diff --git a/absl/flags/BUILD.bazel b/absl/flags/BUILD.bazel
index 524e7027..92bd4f15 100644
--- a/absl/flags/BUILD.bazel
+++ b/absl/flags/BUILD.bazel
@@ -465,6 +465,7 @@ cc_test(
":marshalling",
":reflection",
"//absl/memory",
+ "//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
diff --git a/absl/flags/reflection_test.cc b/absl/flags/reflection_test.cc
index 9781e597..2da0a0ec 100644
--- a/absl/flags/reflection_test.cc
+++ b/absl/flags/reflection_test.cc
@@ -23,6 +23,8 @@
#include "absl/flags/internal/commandlineflag.h"
#include "absl/flags/marshalling.h"
#include "absl/memory/memory.h"
+#include "absl/strings/str_cat.h"
+#include "absl/strings/str_split.h"
ABSL_FLAG(int, int_flag, 1, "int_flag help");
ABSL_FLAG(std::string, string_flag, "dflt", "string_flag help");
@@ -57,4 +59,183 @@ TEST_F(ReflectionTest, TestFindCommandLineFlag) {
EXPECT_NE(handle, nullptr);
}
+// --------------------------------------------------------------------
+
+struct CustomUDT {
+ CustomUDT() : a(1), b(1) {}
+ CustomUDT(int a_, int b_) : a(a_), b(b_) {}
+
+ friend bool operator==(const CustomUDT& f1, const CustomUDT& f2) {
+ return f1.a == f2.a && f1.b == f2.b;
+ }
+
+ int a;
+ int b;
+};
+bool AbslParseFlag(absl::string_view in, CustomUDT* f, std::string*) {
+ std::vector<absl::string_view> parts =
+ absl::StrSplit(in, ':', absl::SkipWhitespace());
+
+ if (parts.size() != 2) return false;
+
+ if (!absl::SimpleAtoi(parts[0], &f->a)) return false;
+
+ if (!absl::SimpleAtoi(parts[1], &f->b)) return false;
+
+ return true;
+}
+std::string AbslUnparseFlag(const CustomUDT& f) {
+ return absl::StrCat(f.a, ":", f.b);
+}
+
+} // namespace
+
+// --------------------------------------------------------------------
+
+ABSL_FLAG(bool, test_flag_01, true, "");
+ABSL_FLAG(int, test_flag_02, 1234, "");
+ABSL_FLAG(int16_t, test_flag_03, -34, "");
+ABSL_FLAG(uint16_t, test_flag_04, 189, "");
+ABSL_FLAG(int32_t, test_flag_05, 10765, "");
+ABSL_FLAG(uint32_t, test_flag_06, 40000, "");
+ABSL_FLAG(int64_t, test_flag_07, -1234567, "");
+ABSL_FLAG(uint64_t, test_flag_08, 9876543, "");
+ABSL_FLAG(double, test_flag_09, -9.876e-50, "");
+ABSL_FLAG(float, test_flag_10, 1.234e12f, "");
+ABSL_FLAG(std::string, test_flag_11, "", "");
+ABSL_FLAG(absl::Duration, test_flag_12, absl::Minutes(10), "");
+static int counter = 0;
+ABSL_FLAG(int, test_flag_13, 200, "").OnUpdate([]() { counter++; });
+ABSL_FLAG(CustomUDT, test_flag_14, {}, "");
+
+namespace {
+
+TEST_F(ReflectionTest, TestFlagSaverInScope) {
+ {
+ absl::FlagSaver s;
+ counter = 0;
+ absl::SetFlag(&FLAGS_test_flag_01, false);
+ absl::SetFlag(&FLAGS_test_flag_02, -1021);
+ absl::SetFlag(&FLAGS_test_flag_03, 6009);
+ absl::SetFlag(&FLAGS_test_flag_04, 44);
+ absl::SetFlag(&FLAGS_test_flag_05, +800);
+ absl::SetFlag(&FLAGS_test_flag_06, -40978756);
+ absl::SetFlag(&FLAGS_test_flag_07, 23405);
+ absl::SetFlag(&FLAGS_test_flag_08, 975310);
+ absl::SetFlag(&FLAGS_test_flag_09, 1.00001);
+ absl::SetFlag(&FLAGS_test_flag_10, -3.54f);
+ absl::SetFlag(&FLAGS_test_flag_11, "asdf");
+ absl::SetFlag(&FLAGS_test_flag_12, absl::Hours(20));
+ absl::SetFlag(&FLAGS_test_flag_13, 4);
+ absl::SetFlag(&FLAGS_test_flag_14, CustomUDT{-1, -2});
+ }
+
+ 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);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_04), 189);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_05), 10765);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_06), 40000);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_07), -1234567);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 9876543);
+ EXPECT_NEAR(absl::GetFlag(FLAGS_test_flag_09), -9.876e-50, 1e-55);
+ EXPECT_NEAR(absl::GetFlag(FLAGS_test_flag_10), 1.234e12f, 1e5f);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_11), "");
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_12), absl::Minutes(10));
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_13), 200);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_14), CustomUDT{});
+ EXPECT_EQ(counter, 2);
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(ReflectionTest, TestFlagSaverVsUpdateViaReflection) {
+ {
+ absl::FlagSaver s;
+ counter = 0;
+ std::string error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_01")->ParseFrom("false", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_02")->ParseFrom("-4536", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_03")->ParseFrom("111", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_04")->ParseFrom("909", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_05")->ParseFrom("-2004", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_06")->ParseFrom("1000023", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_07")->ParseFrom("69305", &error))
+ << error;
+ EXPECT_TRUE(absl::FindCommandLineFlag("test_flag_08")
+ ->ParseFrom("1000000001", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_09")->ParseFrom("2.09021", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_10")->ParseFrom("-33.1", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_11")->ParseFrom("ADD_FOO", &error))
+ << error;
+ EXPECT_TRUE(absl::FindCommandLineFlag("test_flag_12")
+ ->ParseFrom("3h11m16s", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_13")->ParseFrom("0", &error))
+ << error;
+ EXPECT_TRUE(
+ absl::FindCommandLineFlag("test_flag_14")->ParseFrom("10:1", &error))
+ << error;
+ }
+
+ 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);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_04), 189);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_05), 10765);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_06), 40000);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_07), -1234567);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 9876543);
+ EXPECT_NEAR(absl::GetFlag(FLAGS_test_flag_09), -9.876e-50, 1e-55);
+ EXPECT_NEAR(absl::GetFlag(FLAGS_test_flag_10), 1.234e12f, 1e5f);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_11), "");
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_12), absl::Minutes(10));
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_13), 200);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_14), CustomUDT{});
+ EXPECT_EQ(counter, 2);
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(ReflectionTest, TestMultipleFlagSaversInEnclosedScopes) {
+ {
+ absl::FlagSaver s;
+ absl::SetFlag(&FLAGS_test_flag_08, 10);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 10);
+ {
+ absl::FlagSaver s;
+ absl::SetFlag(&FLAGS_test_flag_08, 20);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 20);
+ {
+ absl::FlagSaver s;
+ absl::SetFlag(&FLAGS_test_flag_08, -200);
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), -200);
+ }
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 20);
+ }
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 10);
+ }
+ EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_08), 9876543);
+}
+
} // namespace