usage_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 <sstream>
17 
18 #include "gtest/gtest.h"
19 #include "absl/flags/flag.h"
20 #include "absl/flags/parse.h"
25 #include "absl/memory/memory.h"
26 #include "absl/strings/match.h"
27 
28 ABSL_FLAG(int, usage_reporting_test_flag_01, 101,
29  "usage_reporting_test_flag_01 help message");
30 ABSL_FLAG(bool, usage_reporting_test_flag_02, false,
31  "usage_reporting_test_flag_02 help message");
32 ABSL_FLAG(double, usage_reporting_test_flag_03, 1.03,
33  "usage_reporting_test_flag_03 help message");
34 ABSL_FLAG(int64_t, usage_reporting_test_flag_04, 1000000000000004L,
35  "usage_reporting_test_flag_04 help message");
36 
37 struct UDT {
38  UDT() = default;
39  UDT(const UDT&) = default;
40 };
41 bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
42 std::string AbslUnparseFlag(const UDT&) { return "UDT{}"; }
43 
44 ABSL_FLAG(UDT, usage_reporting_test_flag_05, {},
45  "usage_reporting_test_flag_05 help message");
46 
47 namespace {
48 
49 namespace flags = absl::flags_internal;
50 
51 static std::string NormalizeFileName(absl::string_view fname) {
52 #ifdef _WIN32
53  std::string normalized(fname);
54  std::replace(normalized.begin(), normalized.end(), '\\', '/');
55  fname = normalized;
56 #endif
57 
58  auto absl_pos = fname.find("/absl/");
59  if (absl_pos != absl::string_view::npos) {
60  fname = fname.substr(absl_pos + 1);
61  }
62  return std::string(fname);
63 }
64 
65 class UsageReportingTest : public testing::Test {
66  protected:
67  UsageReportingTest() {
68  // Install default config for the use on this unit test.
69  // Binary may install a custom config before tests are run.
70  absl::FlagsUsageConfig default_config;
71  default_config.normalize_filename = &NormalizeFileName;
72  absl::SetFlagsUsageConfig(default_config);
73  }
74 
75  private:
76  flags::FlagSaver flag_saver_;
77 };
78 
79 // --------------------------------------------------------------------
80 
81 using UsageReportingDeathTest = UsageReportingTest;
82 
83 TEST_F(UsageReportingDeathTest, TestSetProgramUsageMessage) {
84  EXPECT_EQ(flags::ProgramUsageMessage(), "Custom usage message");
85 
86 #ifndef _WIN32
87  // TODO(rogeeff): figure out why this does not work on Windows.
88  EXPECT_DEATH(flags::SetProgramUsageMessage("custom usage message"),
89  ".*SetProgramUsageMessage\\(\\) called twice.*");
90 #endif
91 }
92 
93 // --------------------------------------------------------------------
94 
95 TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_01) {
96  const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_01");
97  std::stringstream test_buf;
98 
99  flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
100  EXPECT_EQ(
101  test_buf.str(),
102  R"( -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
103  default: 101;
104 )");
105 }
106 
107 TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_02) {
108  const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_02");
109  std::stringstream test_buf;
110 
111  flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
112  EXPECT_EQ(
113  test_buf.str(),
114  R"( -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
115  default: false;
116 )");
117 }
118 
119 TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_03) {
120  const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_03");
121  std::stringstream test_buf;
122 
123  flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
124  EXPECT_EQ(
125  test_buf.str(),
126  R"( -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
127  default: 1.03;
128 )");
129 }
130 
131 TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_04) {
132  const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_04");
133  std::stringstream test_buf;
134 
135  flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
136  EXPECT_EQ(
137  test_buf.str(),
138  R"( -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
139  default: 1000000000000004;
140 )");
141 }
142 
143 TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_05) {
144  const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_05");
145  std::stringstream test_buf;
146 
147  flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
148  EXPECT_EQ(
149  test_buf.str(),
150  R"( -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
151  default: UDT{};
152 )");
153 }
154 
155 // --------------------------------------------------------------------
156 
157 TEST_F(UsageReportingTest, TestFlagsHelpHRF) {
158  std::string usage_test_flags_out =
159  R"(usage_test: Custom usage message
160 
161  Flags from absl/flags/internal/usage_test.cc:
162  -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
163  default: 101;
164  -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
165  default: false;
166  -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
167  default: 1.03;
168  -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
169  default: 1000000000000004;
170  -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
171  default: UDT{};
172 )";
173 
174  std::stringstream test_buf_01;
175  flags::FlagsHelp(test_buf_01, "usage_test.cc",
176  flags::HelpFormat::kHumanReadable);
177  EXPECT_EQ(test_buf_01.str(), usage_test_flags_out);
178 
179  std::stringstream test_buf_02;
180  flags::FlagsHelp(test_buf_02, "flags/internal/usage_test.cc",
181  flags::HelpFormat::kHumanReadable);
182  EXPECT_EQ(test_buf_02.str(), usage_test_flags_out);
183 
184  std::stringstream test_buf_03;
185  flags::FlagsHelp(test_buf_03, "usage_test",
186  flags::HelpFormat::kHumanReadable);
187  EXPECT_EQ(test_buf_03.str(), usage_test_flags_out);
188 
189  std::stringstream test_buf_04;
190  flags::FlagsHelp(test_buf_04, "flags/invalid_file_name.cc",
191  flags::HelpFormat::kHumanReadable);
192  EXPECT_EQ(test_buf_04.str(),
193  R"(usage_test: Custom usage message
194 
195  No modules matched: use -helpfull
196 )");
197 
198  std::stringstream test_buf_05;
199  flags::FlagsHelp(test_buf_05, "", flags::HelpFormat::kHumanReadable);
200  std::string test_out = test_buf_05.str();
201  absl::string_view test_out_str(test_out);
202  EXPECT_TRUE(
203  absl::StartsWith(test_out_str, "usage_test: Custom usage message"));
204  EXPECT_TRUE(absl::StrContains(
205  test_out_str, "Flags from absl/flags/internal/usage_test.cc:"));
206  EXPECT_TRUE(absl::StrContains(test_out_str,
207  "Flags from absl/flags/internal/usage.cc:"));
208  EXPECT_TRUE(
209  absl::StrContains(test_out_str, "-usage_reporting_test_flag_01 "));
210  EXPECT_TRUE(absl::StrContains(test_out_str, "-help (show help"))
211  << test_out_str;
212 }
213 
214 // --------------------------------------------------------------------
215 
216 TEST_F(UsageReportingTest, TestNoUsageFlags) {
217  std::stringstream test_buf;
218  EXPECT_EQ(flags::HandleUsageFlags(test_buf), -1);
219 }
220 
221 // --------------------------------------------------------------------
222 
223 TEST_F(UsageReportingTest, TestUsageFlag_helpshort) {
224  absl::SetFlag(&FLAGS_helpshort, true);
225 
226  std::stringstream test_buf;
227  EXPECT_EQ(flags::HandleUsageFlags(test_buf), 1);
228  EXPECT_EQ(test_buf.str(),
229  R"(usage_test: Custom usage message
230 
231  Flags from absl/flags/internal/usage_test.cc:
232  -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
233  default: 101;
234  -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
235  default: false;
236  -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
237  default: 1.03;
238  -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
239  default: 1000000000000004;
240  -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
241  default: UDT{};
242 )");
243 }
244 
245 // --------------------------------------------------------------------
246 
247 TEST_F(UsageReportingTest, TestUsageFlag_help) {
248  absl::SetFlag(&FLAGS_help, true);
249 
250  std::stringstream test_buf;
251  EXPECT_EQ(flags::HandleUsageFlags(test_buf), 1);
252  EXPECT_EQ(test_buf.str(),
253  R"(usage_test: Custom usage message
254 
255  Flags from absl/flags/internal/usage_test.cc:
256  -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
257  default: 101;
258  -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
259  default: false;
260  -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
261  default: 1.03;
262  -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
263  default: 1000000000000004;
264  -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
265  default: UDT{};
266 
267 Try --helpfull to get a list of all flags.
268 )");
269 }
270 
271 // --------------------------------------------------------------------
272 
273 TEST_F(UsageReportingTest, TestUsageFlag_helppackage) {
274  absl::SetFlag(&FLAGS_helppackage, true);
275 
276  std::stringstream test_buf;
277  EXPECT_EQ(flags::HandleUsageFlags(test_buf), 1);
278  EXPECT_EQ(test_buf.str(),
279  R"(usage_test: Custom usage message
280 
281  Flags from absl/flags/internal/usage_test.cc:
282  -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
283  default: 101;
284  -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
285  default: false;
286  -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
287  default: 1.03;
288  -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
289  default: 1000000000000004;
290  -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
291  default: UDT{};
292 
293 Try --helpfull to get a list of all flags.
294 )");
295 }
296 
297 // --------------------------------------------------------------------
298 
299 TEST_F(UsageReportingTest, TestUsageFlag_version) {
300  absl::SetFlag(&FLAGS_version, true);
301 
302  std::stringstream test_buf;
303  EXPECT_EQ(flags::HandleUsageFlags(test_buf), 0);
304 #ifndef NDEBUG
305  EXPECT_EQ(test_buf.str(),
306  "usage_test\nDebug build (NDEBUG not #defined)\n");
307 #else
308  EXPECT_EQ(test_buf.str(), "usage_test\n");
309 #endif
310 }
311 
312 // --------------------------------------------------------------------
313 
314 TEST_F(UsageReportingTest, TestUsageFlag_only_check_args) {
315  absl::SetFlag(&FLAGS_only_check_args, true);
316 
317  std::stringstream test_buf;
318  EXPECT_EQ(flags::HandleUsageFlags(test_buf), 0);
319  EXPECT_EQ(test_buf.str(), "");
320 }
321 
322 // --------------------------------------------------------------------
323 
324 TEST_F(UsageReportingTest, TestUsageFlag_helpon) {
325  absl::SetFlag(&FLAGS_helpon, "bla-bla");
326 
327  std::stringstream test_buf_01;
328  EXPECT_EQ(flags::HandleUsageFlags(test_buf_01), 1);
329  EXPECT_EQ(test_buf_01.str(),
330  R"(usage_test: Custom usage message
331 
332  No modules matched: use -helpfull
333 )");
334 
335  absl::SetFlag(&FLAGS_helpon, "usage_test");
336 
337  std::stringstream test_buf_02;
338  EXPECT_EQ(flags::HandleUsageFlags(test_buf_02), 1);
339  EXPECT_EQ(test_buf_02.str(),
340  R"(usage_test: Custom usage message
341 
342  Flags from absl/flags/internal/usage_test.cc:
343  -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
344  default: 101;
345  -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
346  default: false;
347  -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
348  default: 1.03;
349  -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
350  default: 1000000000000004;
351  -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
352  default: UDT{};
353 )");
354 }
355 
356 // --------------------------------------------------------------------
357 
358 } // namespace
359 
360 int main(int argc, char* argv[]) {
361  absl::GetFlag(FLAGS_undefok); // Force linking of parse.cc
362  flags::SetProgramInvocationName("usage_test");
363  flags::SetProgramUsageMessage("Custom usage message");
364  ::testing::InitGoogleTest(&argc, argv);
365 
366  return RUN_ALL_TESTS();
367 }
void SetProgramUsageMessage(absl::string_view new_usage_message)
Definition: usage.cc:290
void FlagHelp(std::ostream &out, const flags_internal::CommandLineFlag &flag, HelpFormat format)
Definition: usage.cc:315
std::string AbslUnparseFlag(const UDT &)
Definition: usage_test.cc:42
size_type find(string_view s, size_type pos=0) const noexcept
Definition: string_view.cc:92
static constexpr size_type npos
Definition: string_view.h:159
UDT()=default
void FlagsHelp(std::ostream &out, absl::string_view filter, HelpFormat format)
Definition: usage.cc:324
void SetFlagsUsageConfig(FlagsUsageConfig usage_config)
ABSL_FLAG(int, usage_reporting_test_flag_01, 101,"usage_reporting_test_flag_01 help message")
TEST_F(GraphCyclesTest, NoCycle)
CommandLineFlag * FindCommandLineFlag(absl::string_view name)
Definition: registry.cc:427
int HandleUsageFlags(std::ostream &out)
Definition: usage.cc:334
int main(int argc, char *argv[])
Definition: usage_test.cc:360
absl::string_view ProgramUsageMessage()
Definition: usage.cc:305
bool StartsWith(absl::string_view text, absl::string_view prefix)
Definition: match.h:52
bool AbslParseFlag(absl::string_view, UDT *, std::string *)
Definition: usage_test.cc:41
void SetProgramInvocationName(absl::string_view prog_name_str)
Definition: program_name.cc:43
T GetFlag(const absl::Flag< T > &flag)
Definition: flag.h:86
void SetFlag(absl::Flag< T > *flag, const T &v)
Definition: flag.h:134
bool StrContains(absl::string_view haystack, absl::string_view needle)
Definition: match.h:45
std::function< std::string(absl::string_view)> normalize_filename
Definition: usage_config.h:104


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:37