18 #include "gtest/gtest.h" 31 std::string TestHelpMsg() {
return "help"; }
33 void* TestMakeDflt() {
36 void TestCallback() {}
39 bool TestConstructionFor() {
41 &absl::flags_internal::FlagMarshallingOps<T>,
43 EXPECT_EQ(f1.Name(),
"f1");
44 EXPECT_EQ(f1.Help(),
"help");
45 EXPECT_EQ(f1.Filename(),
"file");
48 "f2", &TestHelpMsg,
"file", &absl::flags_internal::FlagMarshallingOps<T>,
52 EXPECT_EQ(f2.Name(),
"f2");
53 EXPECT_EQ(f2.Help(),
"help");
54 EXPECT_EQ(f2.Filename(),
"file");
61 UDT(
const UDT&) =
default;
66 TEST(FlagTest, TestConstruction) {
67 TestConstructionFor<bool>();
68 TestConstructionFor<int16_t>();
69 TestConstructionFor<uint16_t>();
70 TestConstructionFor<int32_t>();
71 TestConstructionFor<uint32_t>();
72 TestConstructionFor<int64_t>();
73 TestConstructionFor<uint64_t>();
74 TestConstructionFor<double>();
75 TestConstructionFor<float>();
76 TestConstructionFor<std::string>();
78 TestConstructionFor<UDT>();
99 #if !ABSL_FLAGS_STRIP_NAMES 101 TEST(FlagTest, TestFlagDeclaration) {
103 EXPECT_EQ(FLAGS_test_flag_01.Name(),
"test_flag_01");
104 EXPECT_EQ(FLAGS_test_flag_02.Name(),
"test_flag_02");
105 EXPECT_EQ(FLAGS_test_flag_03.Name(),
"test_flag_03");
106 EXPECT_EQ(FLAGS_test_flag_04.Name(),
"test_flag_04");
107 EXPECT_EQ(FLAGS_test_flag_05.Name(),
"test_flag_05");
108 EXPECT_EQ(FLAGS_test_flag_06.Name(),
"test_flag_06");
109 EXPECT_EQ(FLAGS_test_flag_07.Name(),
"test_flag_07");
110 EXPECT_EQ(FLAGS_test_flag_08.Name(),
"test_flag_08");
111 EXPECT_EQ(FLAGS_test_flag_09.Name(),
"test_flag_09");
112 EXPECT_EQ(FLAGS_test_flag_10.Name(),
"test_flag_10");
113 EXPECT_EQ(FLAGS_test_flag_11.Name(),
"test_flag_11");
115 #endif // !ABSL_FLAGS_STRIP_NAMES 121 ABSL_FLAG(
bool, test_flag_01,
true,
"test flag 01");
122 ABSL_FLAG(
int, test_flag_02, 1234,
"test flag 02");
123 ABSL_FLAG(int16_t, test_flag_03, -34,
"test flag 03");
124 ABSL_FLAG(uint16_t, test_flag_04, 189,
"test flag 04");
125 ABSL_FLAG(int32_t, test_flag_05, 10765,
"test flag 05");
126 ABSL_FLAG(uint32_t, test_flag_06, 40000,
"test flag 06");
127 ABSL_FLAG(int64_t, test_flag_07, -1234567,
"test flag 07");
128 ABSL_FLAG(uint64_t, test_flag_08, 9876543,
"test flag 08");
129 ABSL_FLAG(
double, test_flag_09, -9.876e-50,
"test flag 09");
130 ABSL_FLAG(
float, test_flag_10, 1.234e12f,
"test flag 10");
131 ABSL_FLAG(std::string, test_flag_11,
"",
"test flag 11");
135 #if !ABSL_FLAGS_STRIP_NAMES 136 TEST(FlagTest, TestFlagDefinition) {
139 EXPECT_EQ(FLAGS_test_flag_01.Name(),
"test_flag_01");
140 EXPECT_EQ(FLAGS_test_flag_01.Help(),
"test flag 01");
142 absl::EndsWith(FLAGS_test_flag_01.Filename(), expected_file_name));
144 EXPECT_EQ(FLAGS_test_flag_02.Name(),
"test_flag_02");
145 EXPECT_EQ(FLAGS_test_flag_02.Help(),
"test flag 02");
147 absl::EndsWith(FLAGS_test_flag_02.Filename(), expected_file_name));
149 EXPECT_EQ(FLAGS_test_flag_03.Name(),
"test_flag_03");
150 EXPECT_EQ(FLAGS_test_flag_03.Help(),
"test flag 03");
152 absl::EndsWith(FLAGS_test_flag_03.Filename(), expected_file_name));
154 EXPECT_EQ(FLAGS_test_flag_04.Name(),
"test_flag_04");
155 EXPECT_EQ(FLAGS_test_flag_04.Help(),
"test flag 04");
157 absl::EndsWith(FLAGS_test_flag_04.Filename(), expected_file_name));
159 EXPECT_EQ(FLAGS_test_flag_05.Name(),
"test_flag_05");
160 EXPECT_EQ(FLAGS_test_flag_05.Help(),
"test flag 05");
162 absl::EndsWith(FLAGS_test_flag_05.Filename(), expected_file_name));
164 EXPECT_EQ(FLAGS_test_flag_06.Name(),
"test_flag_06");
165 EXPECT_EQ(FLAGS_test_flag_06.Help(),
"test flag 06");
167 absl::EndsWith(FLAGS_test_flag_06.Filename(), expected_file_name));
169 EXPECT_EQ(FLAGS_test_flag_07.Name(),
"test_flag_07");
170 EXPECT_EQ(FLAGS_test_flag_07.Help(),
"test flag 07");
172 absl::EndsWith(FLAGS_test_flag_07.Filename(), expected_file_name));
174 EXPECT_EQ(FLAGS_test_flag_08.Name(),
"test_flag_08");
175 EXPECT_EQ(FLAGS_test_flag_08.Help(),
"test flag 08");
177 absl::EndsWith(FLAGS_test_flag_08.Filename(), expected_file_name));
179 EXPECT_EQ(FLAGS_test_flag_09.Name(),
"test_flag_09");
180 EXPECT_EQ(FLAGS_test_flag_09.Help(),
"test flag 09");
182 absl::EndsWith(FLAGS_test_flag_09.Filename(), expected_file_name));
184 EXPECT_EQ(FLAGS_test_flag_10.Name(),
"test_flag_10");
185 EXPECT_EQ(FLAGS_test_flag_10.Help(),
"test flag 10");
187 absl::EndsWith(FLAGS_test_flag_10.Filename(), expected_file_name));
189 EXPECT_EQ(FLAGS_test_flag_11.Name(),
"test_flag_11");
190 EXPECT_EQ(FLAGS_test_flag_11.Help(),
"test flag 11");
192 absl::EndsWith(FLAGS_test_flag_11.Filename(), expected_file_name));
194 #endif // !ABSL_FLAGS_STRIP_NAMES 198 TEST(FlagTest, TestDefault) {
207 EXPECT_NEAR(
absl::GetFlag(FLAGS_test_flag_09), -9.876e-50, 1e-55);
208 EXPECT_NEAR(
absl::GetFlag(FLAGS_test_flag_10), 1.234e12f, 1e5f);
214 TEST(FlagTest, TestGetSet) {
240 EXPECT_NEAR(
absl::GetFlag(FLAGS_test_flag_09), 1.00001, 1e-10);
243 EXPECT_NEAR(
absl::GetFlag(FLAGS_test_flag_10), -3.54f, 1e-6f);
251 int GetDflt1() {
return 1; }
255 ABSL_FLAG(
int, test_flag_12, GetDflt1(),
"test flag 12");
261 TEST(FlagTest, TestNonConstexprDefault) {
274 #if !ABSL_FLAGS_STRIP_HELP 275 TEST(FlagTest, TestNonConstexprHelp) {
276 EXPECT_EQ(FLAGS_test_flag_14.Help(),
"test flag 14");
282 int cb_test_value = -1;
287 ABSL_FLAG(
int, test_flag_with_cb, 100,
"").OnUpdate(TestFlagCB);
289 ABSL_FLAG(
int, test_flag_with_lambda_cb, 200,
"").OnUpdate([]() {
290 cb_test_value =
absl::GetFlag(FLAGS_test_flag_with_lambda_cb) +
296 void TestFlagCB() { cb_test_value =
absl::GetFlag(FLAGS_test_flag_with_cb); }
299 TEST(FlagTest, CallbackInvocation) {
301 EXPECT_EQ(
absl::GetFlag(FLAGS_test_flag_with_lambda_cb), 200);
302 EXPECT_EQ(cb_test_value, 300);
305 EXPECT_EQ(cb_test_value, 1);
308 EXPECT_EQ(cb_test_value, 4);
314 CustomUDT() :
a(1),
b(1) {}
315 CustomUDT(
int a_,
int b_) :
a(a_),
b(b_) {}
317 friend bool operator==(
const CustomUDT& f1,
const CustomUDT& f2) {
318 return f1.a == f2.a && f1.b == f2.b;
325 std::vector<absl::string_view> parts =
328 if (parts.size() != 2)
return false;
342 ABSL_FLAG(CustomUDT, test_flag_15, CustomUDT(),
"test flag 15");
346 TEST(FlagTest, TestCustomUDT) {
347 EXPECT_EQ(
absl::GetFlag(FLAGS_test_flag_15), CustomUDT(1, 1));
349 EXPECT_EQ(
absl::GetFlag(FLAGS_test_flag_15), CustomUDT(2, 3));
354 #if 0 // !defined(_WIN32) && GTEST_HAS_DEATH_TEST 356 TEST(Flagtest, TestTypeMismatchValidations) {
360 "Flag 'mistyped_int_flag' is defined as one type and declared " 363 "Flag 'mistyped_int_flag' is defined as one type and declared " 367 "Flag 'mistyped_string_flag' is defined as one type and " 368 "declared as another");
370 absl::SetFlag(&FLAGS_mistyped_string_flag, std::vector<std::string>{}),
371 "Flag 'mistyped_string_flag' is defined as one type and declared as " 381 struct ConversionTestVal {
382 ConversionTestVal() =
default;
383 explicit ConversionTestVal(
int a_in) :
a(a_in) {}
385 enum class ViaImplicitConv { kTen = 10, kEleven };
387 ConversionTestVal(ViaImplicitConv
from) :
a(static_cast<int>(from)) {}
407 ABSL_FLAG(ConversionTestVal, test_flag_16,
408 ConversionTestVal::ViaImplicitConv::kTen,
"test flag 16");
412 TEST(FlagTest, CanSetViaImplicitConversion) {
415 ConversionTestVal::ViaImplicitConv::kEleven);
421 struct NonDfltConstructible {
424 NonDfltConstructible(
int i) :
value(i) {}
428 explicit NonDfltConstructible(
char c) :
value(100 + static_cast<int>(c)) {}
443 ABSL_FLAG(NonDfltConstructible, ndc_flag1, NonDfltConstructible(
'1'),
444 "Flag with non default constructible type");
445 ABSL_FLAG(NonDfltConstructible, ndc_flag2, 0,
446 "Flag with non default constructible type");
450 TEST(FlagTest, TestNonDefaultConstructibleType) {
471 TEST(FlagTest, TestRetiredFlagRegistration) {
472 bool is_bool =
false;
474 EXPECT_TRUE(is_bool);
476 EXPECT_FALSE(is_bool);
478 EXPECT_FALSE(is_bool);
std::string AbslUnparseFlag(const UDT &)
ABSL_FLAG(bool, test_flag_01, true, "test flag 01")
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
strings_internal::Splitter< typename strings_internal::SelectDelimiter< Delimiter >::type, AllowEmpty > StrSplit(strings_internal::ConvertibleToStringView text, Delimiter d)
bool EndsWith(absl::string_view text, absl::string_view suffix)
bool operator==(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view s, int_type *out)
ABSL_DECLARE_FLAG(int64_t, mistyped_int_flag)
bool AbslParseFlag(absl::string_view, UDT *, std::string *)
TEST(Symbolize, Unimplemented)
T GetFlag(const absl::Flag< T > &flag)
void SetFlag(absl::Flag< T > *flag, const T &v)
bool IsRetiredFlag(absl::string_view name, bool *type_is_bool)
ABSL_RETIRED_FLAG(bool, old_bool_flag, true, "old descr")