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 
17 
18 #include "gtest/gtest.h"
19 #include "absl/flags/flag.h"
21 #include "absl/memory/memory.h"
22 #include "absl/strings/match.h"
23 #include "absl/strings/str_cat.h"
24 
25 ABSL_FLAG(int, int_flag, 201, "int_flag help");
26 ABSL_FLAG(std::string, string_flag, "dflt",
27  absl::StrCat("string_flag", " help"));
28 ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help");
29 
30 namespace {
31 
32 namespace flags = absl::flags_internal;
33 
34 class CommandLineFlagTest : public testing::Test {
35  protected:
36  void SetUp() override { flag_saver_ = absl::make_unique<flags::FlagSaver>(); }
37  void TearDown() override { flag_saver_.reset(); }
38 
39  private:
40  std::unique_ptr<flags::FlagSaver> flag_saver_;
41 };
42 
43 TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) {
44  auto* flag_01 = flags::FindCommandLineFlag("int_flag");
45 
46  ASSERT_TRUE(flag_01);
47  EXPECT_EQ(flag_01->Name(), "int_flag");
48  EXPECT_EQ(flag_01->Help(), "int_flag help");
49  EXPECT_EQ(flag_01->Typename(), "");
50  EXPECT_TRUE(!flag_01->IsRetired());
51  EXPECT_TRUE(flag_01->IsOfType<int>());
52  EXPECT_TRUE(absl::EndsWith(
53  flag_01->Filename(),
54  "absl/flags/internal/commandlineflag_test.cc"));
55 
56  auto* flag_02 = flags::FindCommandLineFlag("string_flag");
57 
58  ASSERT_TRUE(flag_02);
59  EXPECT_EQ(flag_02->Name(), "string_flag");
60  EXPECT_EQ(flag_02->Help(), "string_flag help");
61  EXPECT_EQ(flag_02->Typename(), "");
62  EXPECT_TRUE(!flag_02->IsRetired());
63  EXPECT_TRUE(flag_02->IsOfType<std::string>());
64  EXPECT_TRUE(absl::EndsWith(
65  flag_02->Filename(),
66  "absl/flags/internal/commandlineflag_test.cc"));
67 
68  auto* flag_03 = flags::FindRetiredFlag("bool_retired_flag");
69 
70  ASSERT_TRUE(flag_03);
71  EXPECT_EQ(flag_03->Name(), "bool_retired_flag");
72  EXPECT_EQ(flag_03->Help(), "");
73  EXPECT_EQ(flag_03->Typename(), "");
74  EXPECT_TRUE(flag_03->IsRetired());
75  EXPECT_TRUE(flag_03->IsOfType<bool>());
76  EXPECT_EQ(flag_03->Filename(), "RETIRED");
77 }
78 
79 // --------------------------------------------------------------------
80 
81 TEST_F(CommandLineFlagTest, TestValueAccessMethods) {
82  absl::SetFlag(&FLAGS_int_flag, 301);
83  auto* flag_01 = flags::FindCommandLineFlag("int_flag");
84 
85  ASSERT_TRUE(flag_01);
86  EXPECT_EQ(flag_01->CurrentValue(), "301");
87  EXPECT_EQ(flag_01->DefaultValue(), "201");
88 
89  absl::SetFlag(&FLAGS_string_flag, "new_str_value");
90  auto* flag_02 = flags::FindCommandLineFlag("string_flag");
91 
92  ASSERT_TRUE(flag_02);
93  EXPECT_EQ(flag_02->CurrentValue(), "new_str_value");
94  EXPECT_EQ(flag_02->DefaultValue(), "dflt");
95 }
96 
97 // --------------------------------------------------------------------
98 
99 TEST_F(CommandLineFlagTest, TestSetFromStringCurrentValue) {
100  std::string err;
101 
102  auto* flag_01 = flags::FindCommandLineFlag("int_flag");
103  EXPECT_FALSE(flag_01->on_command_line);
104 
105  EXPECT_TRUE(flag_01->SetFromString("11", flags::SET_FLAGS_VALUE,
107  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
108  EXPECT_FALSE(flag_01->on_command_line);
109 
110  EXPECT_TRUE(flag_01->SetFromString("-123", flags::SET_FLAGS_VALUE,
112  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
113  EXPECT_FALSE(flag_01->on_command_line);
114 
115  EXPECT_TRUE(!flag_01->SetFromString("xyz", flags::SET_FLAGS_VALUE,
117  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
118  EXPECT_EQ(err, "Illegal value 'xyz' specified for flag 'int_flag'");
119  EXPECT_FALSE(flag_01->on_command_line);
120 
121  EXPECT_TRUE(!flag_01->SetFromString("A1", flags::SET_FLAGS_VALUE,
123  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
124  EXPECT_EQ(err, "Illegal value 'A1' specified for flag 'int_flag'");
125  EXPECT_FALSE(flag_01->on_command_line);
126 
127  EXPECT_TRUE(flag_01->SetFromString("0x10", flags::SET_FLAGS_VALUE,
129  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 16);
130  EXPECT_FALSE(flag_01->on_command_line);
131 
132  EXPECT_TRUE(flag_01->SetFromString("011", flags::SET_FLAGS_VALUE,
133  flags::kCommandLine, &err));
134  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
135  EXPECT_TRUE(flag_01->on_command_line);
136 
137  EXPECT_TRUE(!flag_01->SetFromString("", flags::SET_FLAGS_VALUE,
139  EXPECT_EQ(err, "Illegal value '' specified for flag 'int_flag'");
140 
141  auto* flag_02 = flags::FindCommandLineFlag("string_flag");
142  EXPECT_TRUE(flag_02->SetFromString("xyz", flags::SET_FLAGS_VALUE,
144  EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "xyz");
145 
146  EXPECT_TRUE(flag_02->SetFromString("", flags::SET_FLAGS_VALUE,
148  EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "");
149 }
150 
151 // --------------------------------------------------------------------
152 
153 TEST_F(CommandLineFlagTest, TestSetFromStringDefaultValue) {
154  std::string err;
155 
156  auto* flag_01 = flags::FindCommandLineFlag("int_flag");
157 
158  EXPECT_TRUE(flag_01->SetFromString("111", flags::SET_FLAGS_DEFAULT,
160  EXPECT_EQ(flag_01->DefaultValue(), "111");
161 
162  auto* flag_02 = flags::FindCommandLineFlag("string_flag");
163 
164  EXPECT_TRUE(flag_02->SetFromString("abc", flags::SET_FLAGS_DEFAULT,
166  EXPECT_EQ(flag_02->DefaultValue(), "abc");
167 }
168 
169 // --------------------------------------------------------------------
170 
171 TEST_F(CommandLineFlagTest, TestSetFromStringIfDefault) {
172  std::string err;
173 
174  auto* flag_01 = flags::FindCommandLineFlag("int_flag");
175 
176  EXPECT_TRUE(flag_01->SetFromString("22", flags::SET_FLAG_IF_DEFAULT,
178  << err;
179  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
180 
181  EXPECT_TRUE(flag_01->SetFromString("33", flags::SET_FLAG_IF_DEFAULT,
183  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
184  // EXPECT_EQ(err, "ERROR: int_flag is already set to 22");
185 
186  // Reset back to default value
187  EXPECT_TRUE(flag_01->SetFromString("201", flags::SET_FLAGS_VALUE,
189 
190  EXPECT_TRUE(flag_01->SetFromString("33", flags::SET_FLAG_IF_DEFAULT,
192  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 201);
193  // EXPECT_EQ(err, "ERROR: int_flag is already set to 201");
194 }
195 
196 } // namespace
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: str_cat.cc:98
ABSL_FLAG(int, int_flag, 201, "int_flag help")
bool EndsWith(absl::string_view text, absl::string_view suffix)
Definition: match.h:61
TEST_F(GraphCyclesTest, NoCycle)
CommandLineFlag * FindCommandLineFlag(absl::string_view name)
Definition: registry.cc:427
ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help")
T GetFlag(const absl::Flag< T > &flag)
Definition: flag.h:86
void SetFlag(absl::Flag< T > *flag, const T &v)
Definition: flag.h:134
CommandLineFlag * FindRetiredFlag(absl::string_view name)
Definition: registry.cc:442


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:18