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:
SetUpTestSuite()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
SetUp()54 void SetUp() override { flag_saver_ = absl::make_unique<absl::FlagSaver>(); }
TearDown()55 void TearDown() override { flag_saver_.reset(); }
56
57 private:
NormalizeFileName(absl::string_view fname)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
TEST_F(CommandLineFlagTest,TestAttributesAccessMethods)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
TEST_F(CommandLineFlagTest,TestValueAccessMethods)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
TEST_F(CommandLineFlagTest,TestParseFromCurrentValue)118 TEST_F(CommandLineFlagTest, TestParseFromCurrentValue) {
119 std::string err;
120
121 auto* flag_01 = absl::FindCommandLineFlag("int_flag");
122 EXPECT_FALSE(
123 flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
124
125 EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
126 *flag_01, "11", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, err));
127 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
128 EXPECT_FALSE(
129 flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
130
131 EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
132 *flag_01, "-123", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
133 err));
134 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
135 EXPECT_FALSE(
136 flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
137
138 EXPECT_TRUE(!flags::PrivateHandleAccessor::ParseFrom(
139 *flag_01, "xyz", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
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(
144 flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
145
146 EXPECT_TRUE(!flags::PrivateHandleAccessor::ParseFrom(
147 *flag_01, "A1", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, err));
148 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
149 EXPECT_EQ(err, "Illegal value 'A1' specified for flag 'int_flag'");
150 EXPECT_FALSE(
151 flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
152
153 EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
154 *flag_01, "0x10", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
155 err));
156 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 16);
157 EXPECT_FALSE(
158 flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
159
160 EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
161 *flag_01, "011", flags::SET_FLAGS_VALUE, flags::kCommandLine, err));
162 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
163 EXPECT_TRUE(flags::PrivateHandleAccessor::IsSpecifiedOnCommandLine(*flag_01));
164
165 EXPECT_TRUE(!flags::PrivateHandleAccessor::ParseFrom(
166 *flag_01, "", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, err));
167 EXPECT_EQ(err, "Illegal value '' specified for flag 'int_flag'");
168
169 auto* flag_02 = absl::FindCommandLineFlag("string_flag");
170 EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
171 *flag_02, "xyz", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
172 err));
173 EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "xyz");
174
175 EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
176 *flag_02, "", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange, err));
177 EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "");
178 }
179
180 // --------------------------------------------------------------------
181
TEST_F(CommandLineFlagTest,TestParseFromDefaultValue)182 TEST_F(CommandLineFlagTest, TestParseFromDefaultValue) {
183 std::string err;
184
185 auto* flag_01 = absl::FindCommandLineFlag("int_flag");
186
187 EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
188 *flag_01, "111", flags::SET_FLAGS_DEFAULT, flags::kProgrammaticChange,
189 err));
190 EXPECT_EQ(flag_01->DefaultValue(), "111");
191
192 auto* flag_02 = absl::FindCommandLineFlag("string_flag");
193
194 EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
195 *flag_02, "abc", flags::SET_FLAGS_DEFAULT, flags::kProgrammaticChange,
196 err));
197 EXPECT_EQ(flag_02->DefaultValue(), "abc");
198 }
199
200 // --------------------------------------------------------------------
201
TEST_F(CommandLineFlagTest,TestParseFromIfDefault)202 TEST_F(CommandLineFlagTest, TestParseFromIfDefault) {
203 std::string err;
204
205 auto* flag_01 = absl::FindCommandLineFlag("int_flag");
206
207 EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
208 *flag_01, "22", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange,
209 err))
210 << err;
211 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
212
213 EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
214 *flag_01, "33", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange,
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
220 EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
221 *flag_01, "201", flags::SET_FLAGS_VALUE, flags::kProgrammaticChange,
222 err));
223
224 EXPECT_TRUE(flags::PrivateHandleAccessor::ParseFrom(
225 *flag_01, "33", flags::SET_FLAG_IF_DEFAULT, flags::kProgrammaticChange,
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
232