abseil-cpp/absl/flags/commandlineflag_test.cc
Go to the documentation of this file.
1 //
2 // Copyright 2019 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "absl/flags/commandlineflag.h"
17 
18 #include <memory>
19 #include <string>
20 
21 #include "gtest/gtest.h"
22 #include "absl/flags/flag.h"
23 #include "absl/flags/internal/commandlineflag.h"
24 #include "absl/flags/internal/private_handle_accessor.h"
25 #include "absl/flags/reflection.h"
26 #include "absl/flags/usage_config.h"
27 #include "absl/memory/memory.h"
28 #include "absl/strings/match.h"
29 #include "absl/strings/str_cat.h"
30 #include "absl/strings/string_view.h"
31 
32 ABSL_FLAG(int, int_flag, 201, "int_flag help");
33 ABSL_FLAG(std::string, string_flag, "dflt",
34  absl::StrCat("string_flag", " help"));
35 ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help");
36 
37 // These are only used to test default values.
38 ABSL_FLAG(int, int_flag2, 201, "");
39 ABSL_FLAG(std::string, string_flag2, "dflt", "");
40 
41 namespace {
42 
43 namespace flags = absl::flags_internal;
44 
45 class CommandLineFlagTest : public testing::Test {
46  protected:
47  static void SetUpTestSuite() {
48  // Install a function to normalize filenames before this test is run.
49  absl::FlagsUsageConfig default_config;
50  default_config.normalize_filename = &CommandLineFlagTest::NormalizeFileName;
51  absl::SetFlagsUsageConfig(default_config);
52  }
53 
54  void SetUp() override { flag_saver_ = absl::make_unique<absl::FlagSaver>(); }
55  void TearDown() override { flag_saver_.reset(); }
56 
57  private:
58  static std::string NormalizeFileName(absl::string_view fname) {
59 #ifdef _WIN32
60  std::string normalized(fname);
61  std::replace(normalized.begin(), normalized.end(), '\\', '/');
62  fname = normalized;
63 #endif
64  return std::string(fname);
65  }
66 
67  std::unique_ptr<absl::FlagSaver> flag_saver_;
68 };
69 
70 TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) {
71  auto* flag_01 = absl::FindCommandLineFlag("int_flag");
72 
73  ASSERT_TRUE(flag_01);
74  EXPECT_EQ(flag_01->Name(), "int_flag");
75  EXPECT_EQ(flag_01->Help(), "int_flag help");
76  EXPECT_TRUE(!flag_01->IsRetired());
77  EXPECT_TRUE(flag_01->IsOfType<int>());
78  EXPECT_TRUE(!flag_01->IsOfType<bool>());
79  EXPECT_TRUE(!flag_01->IsOfType<std::string>());
80  EXPECT_TRUE(absl::EndsWith(flag_01->Filename(),
81  "absl/flags/commandlineflag_test.cc"))
82  << flag_01->Filename();
83 
84  auto* flag_02 = absl::FindCommandLineFlag("string_flag");
85 
86  ASSERT_TRUE(flag_02);
87  EXPECT_EQ(flag_02->Name(), "string_flag");
88  EXPECT_EQ(flag_02->Help(), "string_flag help");
89  EXPECT_TRUE(!flag_02->IsRetired());
90  EXPECT_TRUE(flag_02->IsOfType<std::string>());
91  EXPECT_TRUE(!flag_02->IsOfType<bool>());
92  EXPECT_TRUE(!flag_02->IsOfType<int>());
93  EXPECT_TRUE(absl::EndsWith(flag_02->Filename(),
94  "absl/flags/commandlineflag_test.cc"))
95  << flag_02->Filename();
96 }
97 
98 // --------------------------------------------------------------------
99 
100 TEST_F(CommandLineFlagTest, TestValueAccessMethods) {
101  absl::SetFlag(&FLAGS_int_flag2, 301);
102  auto* flag_01 = absl::FindCommandLineFlag("int_flag2");
103 
104  ASSERT_TRUE(flag_01);
105  EXPECT_EQ(flag_01->CurrentValue(), "301");
106  EXPECT_EQ(flag_01->DefaultValue(), "201");
107 
108  absl::SetFlag(&FLAGS_string_flag2, "new_str_value");
109  auto* flag_02 = absl::FindCommandLineFlag("string_flag2");
110 
111  ASSERT_TRUE(flag_02);
112  EXPECT_EQ(flag_02->CurrentValue(), "new_str_value");
113  EXPECT_EQ(flag_02->DefaultValue(), "dflt");
114 }
115 
116 // --------------------------------------------------------------------
117 
118 TEST_F(CommandLineFlagTest, TestParseFromCurrentValue) {
120 
121  auto* flag_01 = absl::FindCommandLineFlag("int_flag");
122  EXPECT_FALSE(
124 
127  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
128  EXPECT_FALSE(
130 
133  err));
134  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
135  EXPECT_FALSE(
137 
140  err));
141  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
142  EXPECT_EQ(err, "Illegal value 'xyz' specified for flag 'int_flag'");
143  EXPECT_FALSE(
145 
148  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
149  EXPECT_EQ(err, "Illegal value 'A1' specified for flag 'int_flag'");
150  EXPECT_FALSE(
152 
155  err));
156  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 16);
157  EXPECT_FALSE(
159 
161  *flag_01, "011", flags::SET_FLAGS_VALUE, flags::kCommandLine, err));
162  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
164 
167  EXPECT_EQ(err, "Illegal value '' specified for flag 'int_flag'");
168 
169  auto* flag_02 = absl::FindCommandLineFlag("string_flag");
172  err));
173  EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "xyz");
174 
177  EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "");
178 }
179 
180 // --------------------------------------------------------------------
181 
182 TEST_F(CommandLineFlagTest, TestParseFromDefaultValue) {
184 
185  auto* flag_01 = absl::FindCommandLineFlag("int_flag");
186 
189  err));
190  EXPECT_EQ(flag_01->DefaultValue(), "111");
191 
192  auto* flag_02 = absl::FindCommandLineFlag("string_flag");
193 
196  err));
197  EXPECT_EQ(flag_02->DefaultValue(), "abc");
198 }
199 
200 // --------------------------------------------------------------------
201 
202 TEST_F(CommandLineFlagTest, TestParseFromIfDefault) {
204 
205  auto* flag_01 = absl::FindCommandLineFlag("int_flag");
206 
209  err))
210  << err;
211  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
212 
215  err));
216  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
217  // EXPECT_EQ(err, "ERROR: int_flag is already set to 22");
218 
219  // Reset back to default value
222  err));
223 
226  err));
227  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 201);
228  // EXPECT_EQ(err, "ERROR: int_flag is already set to 201");
229 }
230 
231 } // namespace
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
absl::SetFlag
void SetFlag(absl::Flag< T > *flag, const T &v)
Definition: abseil-cpp/absl/flags/flag.h:110
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
absl::flags_internal::kProgrammaticChange
@ kProgrammaticChange
Definition: abseil-cpp/absl/flags/internal/commandlineflag.h:51
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::Test::SetUpTestSuite
static void SetUpTestSuite()
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:417
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error_ref_leak.err
err
Definition: error_ref_leak.py:35
absl::FindCommandLineFlag
CommandLineFlag * FindCommandLineFlag(absl::string_view name)
Definition: abseil-cpp/absl/flags/reflection.cc:336
absl::flags_internal::SET_FLAG_IF_DEFAULT
@ SET_FLAG_IF_DEFAULT
Definition: abseil-cpp/absl/flags/internal/commandlineflag.h:39
absl::FlagsUsageConfig::normalize_filename
std::function< std::string(absl::string_view)> normalize_filename
Definition: abseil-cpp/absl/flags/usage_config.h:106
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
testing::Test::TearDown
virtual void TearDown()
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:2270
absl::SetFlagsUsageConfig
void SetFlagsUsageConfig(FlagsUsageConfig usage_config)
Definition: abseil-cpp/absl/flags/usage_config.cc:138
absl::flags_internal::SET_FLAGS_VALUE
@ SET_FLAGS_VALUE
Definition: abseil-cpp/absl/flags/internal/commandlineflag.h:36
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
ABSL_FLAG
ABSL_FLAG(int, int_flag, 201, "int_flag help")
absl::flags_internal::PrivateHandleAccessor::ParseFrom
static bool ParseFrom(CommandLineFlag &flag, absl::string_view value, flags_internal::FlagSettingMode set_mode, flags_internal::ValueSource source, std::string &error)
Definition: abseil-cpp/absl/flags/internal/private_handle_accessor.cc:54
absl::FlagsUsageConfig
Definition: abseil-cpp/absl/flags/usage_config.h:68
testing::Test::SetUp
virtual void SetUp()
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:2264
ABSL_RETIRED_FLAG
ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help")
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
absl::flags_internal::SET_FLAGS_DEFAULT
@ SET_FLAGS_DEFAULT
Definition: abseil-cpp/absl/flags/internal/commandlineflag.h:43
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
absl::flags_internal::PrivateHandleAccessor::IsSpecifiedOnCommandLine
static bool IsSpecifiedOnCommandLine(const CommandLineFlag &flag)
Definition: abseil-cpp/absl/flags/internal/private_handle_accessor.cc:39
absl::flags_internal::kCommandLine
@ kCommandLine
Definition: abseil-cpp/absl/flags/internal/commandlineflag.h:49
absl::EndsWith
bool EndsWith(absl::string_view text, absl::string_view suffix) noexcept
Definition: third_party/abseil-cpp/absl/strings/match.h:68
TEST_F
#define TEST_F(test_fixture, test_name)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2367


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:59