usage_test.cc
Go to the documentation of this file.
00001 //
00002 //  Copyright 2019 The Abseil Authors.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //      https://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 
00016 #include <sstream>
00017 
00018 #include "gtest/gtest.h"
00019 #include "absl/flags/flag.h"
00020 #include "absl/flags/parse.h"
00021 #include "absl/flags/internal/path_util.h"
00022 #include "absl/flags/internal/program_name.h"
00023 #include "absl/flags/internal/usage.h"
00024 #include "absl/flags/usage_config.h"
00025 #include "absl/memory/memory.h"
00026 #include "absl/strings/match.h"
00027 
00028 ABSL_FLAG(int, usage_reporting_test_flag_01, 101,
00029           "usage_reporting_test_flag_01 help message");
00030 ABSL_FLAG(bool, usage_reporting_test_flag_02, false,
00031           "usage_reporting_test_flag_02 help message");
00032 ABSL_FLAG(double, usage_reporting_test_flag_03, 1.03,
00033           "usage_reporting_test_flag_03 help message");
00034 ABSL_FLAG(int64_t, usage_reporting_test_flag_04, 1000000000000004L,
00035           "usage_reporting_test_flag_04 help message");
00036 
00037 struct UDT {
00038   UDT() = default;
00039   UDT(const UDT&) = default;
00040 };
00041 bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
00042 std::string AbslUnparseFlag(const UDT&) { return "UDT{}"; }
00043 
00044 ABSL_FLAG(UDT, usage_reporting_test_flag_05, {},
00045           "usage_reporting_test_flag_05 help message");
00046 
00047 namespace {
00048 
00049 namespace flags = absl::flags_internal;
00050 
00051 static std::string NormalizeFileName(absl::string_view fname) {
00052 #ifdef _WIN32
00053   std::string normalized(fname);
00054   std::replace(normalized.begin(), normalized.end(), '\\', '/');
00055   fname = normalized;
00056 #endif
00057 
00058   auto absl_pos = fname.find("/absl/");
00059   if (absl_pos != absl::string_view::npos) {
00060     fname = fname.substr(absl_pos + 1);
00061   }
00062   return std::string(fname);
00063 }
00064 
00065 class UsageReportingTest : public testing::Test {
00066  protected:
00067   UsageReportingTest() {
00068     // Install default config for the use on this unit test.
00069     // Binary may install a custom config before tests are run.
00070     absl::FlagsUsageConfig default_config;
00071     default_config.normalize_filename = &NormalizeFileName;
00072     absl::SetFlagsUsageConfig(default_config);
00073   }
00074 
00075  private:
00076   flags::FlagSaver flag_saver_;
00077 };
00078 
00079 // --------------------------------------------------------------------
00080 
00081 using UsageReportingDeathTest = UsageReportingTest;
00082 
00083 TEST_F(UsageReportingDeathTest, TestSetProgramUsageMessage) {
00084   EXPECT_EQ(flags::ProgramUsageMessage(), "Custom usage message");
00085 
00086 #ifndef _WIN32
00087   // TODO(rogeeff): figure out why this does not work on Windows.
00088   EXPECT_DEATH(flags::SetProgramUsageMessage("custom usage message"),
00089                ".*SetProgramUsageMessage\\(\\) called twice.*");
00090 #endif
00091 }
00092 
00093 // --------------------------------------------------------------------
00094 
00095 TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_01) {
00096   const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_01");
00097   std::stringstream test_buf;
00098 
00099   flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
00100   EXPECT_EQ(
00101       test_buf.str(),
00102       R"(    -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
00103       default: 101;
00104 )");
00105 }
00106 
00107 TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_02) {
00108   const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_02");
00109   std::stringstream test_buf;
00110 
00111   flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
00112   EXPECT_EQ(
00113       test_buf.str(),
00114       R"(    -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
00115       default: false;
00116 )");
00117 }
00118 
00119 TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_03) {
00120   const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_03");
00121   std::stringstream test_buf;
00122 
00123   flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
00124   EXPECT_EQ(
00125       test_buf.str(),
00126       R"(    -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
00127       default: 1.03;
00128 )");
00129 }
00130 
00131 TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_04) {
00132   const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_04");
00133   std::stringstream test_buf;
00134 
00135   flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
00136   EXPECT_EQ(
00137       test_buf.str(),
00138       R"(    -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
00139       default: 1000000000000004;
00140 )");
00141 }
00142 
00143 TEST_F(UsageReportingTest, TestFlagHelpHRF_on_flag_05) {
00144   const auto* flag = flags::FindCommandLineFlag("usage_reporting_test_flag_05");
00145   std::stringstream test_buf;
00146 
00147   flags::FlagHelp(test_buf, *flag, flags::HelpFormat::kHumanReadable);
00148   EXPECT_EQ(
00149       test_buf.str(),
00150       R"(    -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
00151       default: UDT{};
00152 )");
00153 }
00154 
00155 // --------------------------------------------------------------------
00156 
00157 TEST_F(UsageReportingTest, TestFlagsHelpHRF) {
00158   std::string usage_test_flags_out =
00159       R"(usage_test: Custom usage message
00160 
00161   Flags from absl/flags/internal/usage_test.cc:
00162     -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
00163       default: 101;
00164     -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
00165       default: false;
00166     -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
00167       default: 1.03;
00168     -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
00169       default: 1000000000000004;
00170     -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
00171       default: UDT{};
00172 )";
00173 
00174   std::stringstream test_buf_01;
00175   flags::FlagsHelp(test_buf_01, "usage_test.cc",
00176                    flags::HelpFormat::kHumanReadable);
00177   EXPECT_EQ(test_buf_01.str(), usage_test_flags_out);
00178 
00179   std::stringstream test_buf_02;
00180   flags::FlagsHelp(test_buf_02, "flags/internal/usage_test.cc",
00181                    flags::HelpFormat::kHumanReadable);
00182   EXPECT_EQ(test_buf_02.str(), usage_test_flags_out);
00183 
00184   std::stringstream test_buf_03;
00185   flags::FlagsHelp(test_buf_03, "usage_test",
00186                    flags::HelpFormat::kHumanReadable);
00187   EXPECT_EQ(test_buf_03.str(), usage_test_flags_out);
00188 
00189   std::stringstream test_buf_04;
00190   flags::FlagsHelp(test_buf_04, "flags/invalid_file_name.cc",
00191                    flags::HelpFormat::kHumanReadable);
00192   EXPECT_EQ(test_buf_04.str(),
00193             R"(usage_test: Custom usage message
00194 
00195   No modules matched: use -helpfull
00196 )");
00197 
00198   std::stringstream test_buf_05;
00199   flags::FlagsHelp(test_buf_05, "", flags::HelpFormat::kHumanReadable);
00200   std::string test_out = test_buf_05.str();
00201   absl::string_view test_out_str(test_out);
00202   EXPECT_TRUE(
00203       absl::StartsWith(test_out_str, "usage_test: Custom usage message"));
00204   EXPECT_TRUE(absl::StrContains(
00205       test_out_str, "Flags from absl/flags/internal/usage_test.cc:"));
00206   EXPECT_TRUE(absl::StrContains(test_out_str,
00207                                 "Flags from absl/flags/internal/usage.cc:"));
00208   EXPECT_TRUE(
00209       absl::StrContains(test_out_str, "-usage_reporting_test_flag_01 "));
00210   EXPECT_TRUE(absl::StrContains(test_out_str, "-help (show help"))
00211       << test_out_str;
00212 }
00213 
00214 // --------------------------------------------------------------------
00215 
00216 TEST_F(UsageReportingTest, TestNoUsageFlags) {
00217   std::stringstream test_buf;
00218   EXPECT_EQ(flags::HandleUsageFlags(test_buf), -1);
00219 }
00220 
00221 // --------------------------------------------------------------------
00222 
00223 TEST_F(UsageReportingTest, TestUsageFlag_helpshort) {
00224   absl::SetFlag(&FLAGS_helpshort, true);
00225 
00226   std::stringstream test_buf;
00227   EXPECT_EQ(flags::HandleUsageFlags(test_buf), 1);
00228   EXPECT_EQ(test_buf.str(),
00229             R"(usage_test: Custom usage message
00230 
00231   Flags from absl/flags/internal/usage_test.cc:
00232     -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
00233       default: 101;
00234     -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
00235       default: false;
00236     -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
00237       default: 1.03;
00238     -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
00239       default: 1000000000000004;
00240     -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
00241       default: UDT{};
00242 )");
00243 }
00244 
00245 // --------------------------------------------------------------------
00246 
00247 TEST_F(UsageReportingTest, TestUsageFlag_help) {
00248   absl::SetFlag(&FLAGS_help, true);
00249 
00250   std::stringstream test_buf;
00251   EXPECT_EQ(flags::HandleUsageFlags(test_buf), 1);
00252   EXPECT_EQ(test_buf.str(),
00253             R"(usage_test: Custom usage message
00254 
00255   Flags from absl/flags/internal/usage_test.cc:
00256     -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
00257       default: 101;
00258     -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
00259       default: false;
00260     -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
00261       default: 1.03;
00262     -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
00263       default: 1000000000000004;
00264     -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
00265       default: UDT{};
00266 
00267 Try --helpfull to get a list of all flags.
00268 )");
00269 }
00270 
00271 // --------------------------------------------------------------------
00272 
00273 TEST_F(UsageReportingTest, TestUsageFlag_helppackage) {
00274   absl::SetFlag(&FLAGS_helppackage, true);
00275 
00276   std::stringstream test_buf;
00277   EXPECT_EQ(flags::HandleUsageFlags(test_buf), 1);
00278   EXPECT_EQ(test_buf.str(),
00279             R"(usage_test: Custom usage message
00280 
00281   Flags from absl/flags/internal/usage_test.cc:
00282     -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
00283       default: 101;
00284     -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
00285       default: false;
00286     -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
00287       default: 1.03;
00288     -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
00289       default: 1000000000000004;
00290     -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
00291       default: UDT{};
00292 
00293 Try --helpfull to get a list of all flags.
00294 )");
00295 }
00296 
00297 // --------------------------------------------------------------------
00298 
00299 TEST_F(UsageReportingTest, TestUsageFlag_version) {
00300   absl::SetFlag(&FLAGS_version, true);
00301 
00302   std::stringstream test_buf;
00303   EXPECT_EQ(flags::HandleUsageFlags(test_buf), 0);
00304 #ifndef NDEBUG
00305   EXPECT_EQ(test_buf.str(),
00306             "usage_test\nDebug build (NDEBUG not #defined)\n");
00307 #else
00308   EXPECT_EQ(test_buf.str(), "usage_test\n");
00309 #endif
00310 }
00311 
00312 // --------------------------------------------------------------------
00313 
00314 TEST_F(UsageReportingTest, TestUsageFlag_only_check_args) {
00315   absl::SetFlag(&FLAGS_only_check_args, true);
00316 
00317   std::stringstream test_buf;
00318   EXPECT_EQ(flags::HandleUsageFlags(test_buf), 0);
00319   EXPECT_EQ(test_buf.str(), "");
00320 }
00321 
00322 // --------------------------------------------------------------------
00323 
00324 TEST_F(UsageReportingTest, TestUsageFlag_helpon) {
00325   absl::SetFlag(&FLAGS_helpon, "bla-bla");
00326 
00327   std::stringstream test_buf_01;
00328   EXPECT_EQ(flags::HandleUsageFlags(test_buf_01), 1);
00329   EXPECT_EQ(test_buf_01.str(),
00330             R"(usage_test: Custom usage message
00331 
00332   No modules matched: use -helpfull
00333 )");
00334 
00335   absl::SetFlag(&FLAGS_helpon, "usage_test");
00336 
00337   std::stringstream test_buf_02;
00338   EXPECT_EQ(flags::HandleUsageFlags(test_buf_02), 1);
00339   EXPECT_EQ(test_buf_02.str(),
00340             R"(usage_test: Custom usage message
00341 
00342   Flags from absl/flags/internal/usage_test.cc:
00343     -usage_reporting_test_flag_01 (usage_reporting_test_flag_01 help message);
00344       default: 101;
00345     -usage_reporting_test_flag_02 (usage_reporting_test_flag_02 help message);
00346       default: false;
00347     -usage_reporting_test_flag_03 (usage_reporting_test_flag_03 help message);
00348       default: 1.03;
00349     -usage_reporting_test_flag_04 (usage_reporting_test_flag_04 help message);
00350       default: 1000000000000004;
00351     -usage_reporting_test_flag_05 (usage_reporting_test_flag_05 help message);
00352       default: UDT{};
00353 )");
00354 }
00355 
00356 // --------------------------------------------------------------------
00357 
00358 }  // namespace
00359 
00360 int main(int argc, char* argv[]) {
00361   absl::GetFlag(FLAGS_undefok);  // Force linking of parse.cc
00362   flags::SetProgramInvocationName("usage_test");
00363   flags::SetProgramUsageMessage("Custom usage message");
00364   ::testing::InitGoogleTest(&argc, argv);
00365 
00366   return RUN_ALL_TESTS();
00367 }


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:16