Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "absl/flags/usage_config.h"
00017
00018 #include "gtest/gtest.h"
00019 #include "absl/flags/internal/path_util.h"
00020 #include "absl/flags/internal/program_name.h"
00021 #include "absl/strings/match.h"
00022
00023 namespace {
00024
00025 class FlagsUsageConfigTest : public testing::Test {
00026 protected:
00027 void SetUp() override {
00028
00029
00030 absl::FlagsUsageConfig default_config;
00031 absl::SetFlagsUsageConfig(default_config);
00032 }
00033 };
00034
00035 namespace flags = absl::flags_internal;
00036
00037 bool TstContainsHelpshortFlags(absl::string_view f) {
00038 return absl::StartsWith(flags::Basename(f), "progname.");
00039 }
00040
00041 bool TstContainsHelppackageFlags(absl::string_view f) {
00042 return absl::EndsWith(flags::Package(f), "aaa/");
00043 }
00044
00045 bool TstContainsHelpFlags(absl::string_view f) {
00046 return absl::EndsWith(flags::Package(f), "zzz/");
00047 }
00048
00049 std::string TstVersionString() { return "program 1.0.0"; }
00050
00051 std::string TstNormalizeFilename(absl::string_view filename) {
00052 return std::string(filename.substr(2));
00053 }
00054
00055 void TstReportUsageMessage(absl::string_view msg) {}
00056
00057
00058
00059 TEST_F(FlagsUsageConfigTest, TestGetSetFlagsUsageConfig) {
00060 EXPECT_TRUE(flags::GetUsageConfig().contains_helpshort_flags);
00061 EXPECT_TRUE(flags::GetUsageConfig().contains_help_flags);
00062 EXPECT_TRUE(flags::GetUsageConfig().contains_helppackage_flags);
00063 EXPECT_TRUE(flags::GetUsageConfig().version_string);
00064 EXPECT_TRUE(flags::GetUsageConfig().normalize_filename);
00065
00066 absl::FlagsUsageConfig empty_config;
00067 empty_config.contains_helpshort_flags = &TstContainsHelpshortFlags;
00068 empty_config.contains_help_flags = &TstContainsHelpFlags;
00069 empty_config.contains_helppackage_flags = &TstContainsHelppackageFlags;
00070 empty_config.version_string = &TstVersionString;
00071 empty_config.normalize_filename = &TstNormalizeFilename;
00072 absl::SetFlagsUsageConfig(empty_config);
00073
00074 EXPECT_TRUE(flags::GetUsageConfig().contains_helpshort_flags);
00075 EXPECT_TRUE(flags::GetUsageConfig().contains_help_flags);
00076 EXPECT_TRUE(flags::GetUsageConfig().contains_helppackage_flags);
00077 EXPECT_TRUE(flags::GetUsageConfig().version_string);
00078 EXPECT_TRUE(flags::GetUsageConfig().normalize_filename);
00079 }
00080
00081
00082
00083 TEST_F(FlagsUsageConfigTest, TestContainsHelpshortFlags) {
00084 flags::SetProgramInvocationName("usage_config_test");
00085
00086 auto config = flags::GetUsageConfig();
00087 EXPECT_TRUE(config.contains_helpshort_flags("adir/cd/usage_config_test.cc"));
00088 EXPECT_TRUE(
00089 config.contains_helpshort_flags("aaaa/usage_config_test-main.cc"));
00090 EXPECT_TRUE(config.contains_helpshort_flags("abc/usage_config_test_main.cc"));
00091 EXPECT_FALSE(config.contains_helpshort_flags("usage_config_main.cc"));
00092
00093 absl::FlagsUsageConfig empty_config;
00094 empty_config.contains_helpshort_flags = &TstContainsHelpshortFlags;
00095 absl::SetFlagsUsageConfig(empty_config);
00096
00097 EXPECT_TRUE(
00098 flags::GetUsageConfig().contains_helpshort_flags("aaa/progname.cpp"));
00099 EXPECT_FALSE(
00100 flags::GetUsageConfig().contains_helpshort_flags("aaa/progmane.cpp"));
00101 }
00102
00103
00104
00105 TEST_F(FlagsUsageConfigTest, TestContainsHelpFlags) {
00106 flags::SetProgramInvocationName("usage_config_test");
00107
00108 auto config = flags::GetUsageConfig();
00109 EXPECT_TRUE(config.contains_help_flags("zzz/usage_config_test.cc"));
00110 EXPECT_TRUE(
00111 config.contains_help_flags("bdir/a/zzz/usage_config_test-main.cc"));
00112 EXPECT_TRUE(
00113 config.contains_help_flags("//aqse/zzz/usage_config_test_main.cc"));
00114 EXPECT_FALSE(config.contains_help_flags("zzz/aa/usage_config_main.cc"));
00115
00116 absl::FlagsUsageConfig empty_config;
00117 empty_config.contains_help_flags = &TstContainsHelpFlags;
00118 absl::SetFlagsUsageConfig(empty_config);
00119
00120 EXPECT_TRUE(flags::GetUsageConfig().contains_help_flags("zzz/main-body.c"));
00121 EXPECT_FALSE(
00122 flags::GetUsageConfig().contains_help_flags("zzz/dir/main-body.c"));
00123 }
00124
00125
00126
00127 TEST_F(FlagsUsageConfigTest, TestContainsHelppackageFlags) {
00128 flags::SetProgramInvocationName("usage_config_test");
00129
00130 auto config = flags::GetUsageConfig();
00131 EXPECT_TRUE(config.contains_helppackage_flags("aaa/usage_config_test.cc"));
00132 EXPECT_TRUE(
00133 config.contains_helppackage_flags("bbdir/aaa/usage_config_test-main.cc"));
00134 EXPECT_TRUE(config.contains_helppackage_flags(
00135 "//aqswde/aaa/usage_config_test_main.cc"));
00136 EXPECT_FALSE(config.contains_helppackage_flags("aadir/usage_config_main.cc"));
00137
00138 absl::FlagsUsageConfig empty_config;
00139 empty_config.contains_helppackage_flags = &TstContainsHelppackageFlags;
00140 absl::SetFlagsUsageConfig(empty_config);
00141
00142 EXPECT_TRUE(
00143 flags::GetUsageConfig().contains_helppackage_flags("aaa/main-body.c"));
00144 EXPECT_FALSE(
00145 flags::GetUsageConfig().contains_helppackage_flags("aadir/main-body.c"));
00146 }
00147
00148
00149
00150 TEST_F(FlagsUsageConfigTest, TestVersionString) {
00151 flags::SetProgramInvocationName("usage_config_test");
00152
00153 #ifdef NDEBUG
00154 std::string expected_output = "usage_config_test\n";
00155 #else
00156 std::string expected_output =
00157 "usage_config_test\nDebug build (NDEBUG not #defined)\n";
00158 #endif
00159
00160 EXPECT_EQ(flags::GetUsageConfig().version_string(), expected_output);
00161
00162 absl::FlagsUsageConfig empty_config;
00163 empty_config.version_string = &TstVersionString;
00164 absl::SetFlagsUsageConfig(empty_config);
00165
00166 EXPECT_EQ(flags::GetUsageConfig().version_string(), "program 1.0.0");
00167 }
00168
00169
00170
00171 TEST_F(FlagsUsageConfigTest, TestNormalizeFilename) {
00172
00173 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("a/a.cc"), "a/a.cc");
00174 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("/a/a.cc"), "a/a.cc");
00175 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("///a/a.cc"), "a/a.cc");
00176 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("/"), "");
00177
00178
00179 absl::FlagsUsageConfig empty_config;
00180 empty_config.normalize_filename = &TstNormalizeFilename;
00181 absl::SetFlagsUsageConfig(empty_config);
00182
00183 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("a/a.cc"), "a.cc");
00184 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("aaa/a.cc"), "a/a.cc");
00185
00186
00187 empty_config.normalize_filename = nullptr;
00188 absl::SetFlagsUsageConfig(empty_config);
00189
00190 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("a/a.cc"), "a/a.cc");
00191 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("/a/a.cc"), "a/a.cc");
00192 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("///a/a.cc"), "a/a.cc");
00193 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("\\a\\a.cc"), "a\\a.cc");
00194 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("//"), "");
00195 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("\\\\"), "");
00196 }
00197
00198 }