usage_config_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"
21 #include "absl/strings/match.h"
22 
23 namespace {
24 
25 class FlagsUsageConfigTest : public testing::Test {
26  protected:
27  void SetUp() override {
28  // Install Default config for the use on this unit test.
29  // Binary may install a custom config before tests are run.
30  absl::FlagsUsageConfig default_config;
31  absl::SetFlagsUsageConfig(default_config);
32  }
33 };
34 
35 namespace flags = absl::flags_internal;
36 
37 bool TstContainsHelpshortFlags(absl::string_view f) {
38  return absl::StartsWith(flags::Basename(f), "progname.");
39 }
40 
41 bool TstContainsHelppackageFlags(absl::string_view f) {
42  return absl::EndsWith(flags::Package(f), "aaa/");
43 }
44 
45 bool TstContainsHelpFlags(absl::string_view f) {
46  return absl::EndsWith(flags::Package(f), "zzz/");
47 }
48 
49 std::string TstVersionString() { return "program 1.0.0"; }
50 
51 std::string TstNormalizeFilename(absl::string_view filename) {
52  return std::string(filename.substr(2));
53 }
54 
55 void TstReportUsageMessage(absl::string_view msg) {}
56 
57 // --------------------------------------------------------------------
58 
59 TEST_F(FlagsUsageConfigTest, TestGetSetFlagsUsageConfig) {
60  EXPECT_TRUE(flags::GetUsageConfig().contains_helpshort_flags);
61  EXPECT_TRUE(flags::GetUsageConfig().contains_help_flags);
62  EXPECT_TRUE(flags::GetUsageConfig().contains_helppackage_flags);
63  EXPECT_TRUE(flags::GetUsageConfig().version_string);
64  EXPECT_TRUE(flags::GetUsageConfig().normalize_filename);
65 
66  absl::FlagsUsageConfig empty_config;
67  empty_config.contains_helpshort_flags = &TstContainsHelpshortFlags;
68  empty_config.contains_help_flags = &TstContainsHelpFlags;
69  empty_config.contains_helppackage_flags = &TstContainsHelppackageFlags;
70  empty_config.version_string = &TstVersionString;
71  empty_config.normalize_filename = &TstNormalizeFilename;
72  absl::SetFlagsUsageConfig(empty_config);
73 
74  EXPECT_TRUE(flags::GetUsageConfig().contains_helpshort_flags);
75  EXPECT_TRUE(flags::GetUsageConfig().contains_help_flags);
76  EXPECT_TRUE(flags::GetUsageConfig().contains_helppackage_flags);
77  EXPECT_TRUE(flags::GetUsageConfig().version_string);
78  EXPECT_TRUE(flags::GetUsageConfig().normalize_filename);
79 }
80 
81 // --------------------------------------------------------------------
82 
83 TEST_F(FlagsUsageConfigTest, TestContainsHelpshortFlags) {
84  flags::SetProgramInvocationName("usage_config_test");
85 
86  auto config = flags::GetUsageConfig();
87  EXPECT_TRUE(config.contains_helpshort_flags("adir/cd/usage_config_test.cc"));
88  EXPECT_TRUE(
89  config.contains_helpshort_flags("aaaa/usage_config_test-main.cc"));
90  EXPECT_TRUE(config.contains_helpshort_flags("abc/usage_config_test_main.cc"));
91  EXPECT_FALSE(config.contains_helpshort_flags("usage_config_main.cc"));
92 
93  absl::FlagsUsageConfig empty_config;
94  empty_config.contains_helpshort_flags = &TstContainsHelpshortFlags;
95  absl::SetFlagsUsageConfig(empty_config);
96 
97  EXPECT_TRUE(
98  flags::GetUsageConfig().contains_helpshort_flags("aaa/progname.cpp"));
99  EXPECT_FALSE(
100  flags::GetUsageConfig().contains_helpshort_flags("aaa/progmane.cpp"));
101 }
102 
103 // --------------------------------------------------------------------
104 
105 TEST_F(FlagsUsageConfigTest, TestContainsHelpFlags) {
106  flags::SetProgramInvocationName("usage_config_test");
107 
108  auto config = flags::GetUsageConfig();
109  EXPECT_TRUE(config.contains_help_flags("zzz/usage_config_test.cc"));
110  EXPECT_TRUE(
111  config.contains_help_flags("bdir/a/zzz/usage_config_test-main.cc"));
112  EXPECT_TRUE(
113  config.contains_help_flags("//aqse/zzz/usage_config_test_main.cc"));
114  EXPECT_FALSE(config.contains_help_flags("zzz/aa/usage_config_main.cc"));
115 
116  absl::FlagsUsageConfig empty_config;
117  empty_config.contains_help_flags = &TstContainsHelpFlags;
118  absl::SetFlagsUsageConfig(empty_config);
119 
120  EXPECT_TRUE(flags::GetUsageConfig().contains_help_flags("zzz/main-body.c"));
121  EXPECT_FALSE(
122  flags::GetUsageConfig().contains_help_flags("zzz/dir/main-body.c"));
123 }
124 
125 // --------------------------------------------------------------------
126 
127 TEST_F(FlagsUsageConfigTest, TestContainsHelppackageFlags) {
128  flags::SetProgramInvocationName("usage_config_test");
129 
130  auto config = flags::GetUsageConfig();
131  EXPECT_TRUE(config.contains_helppackage_flags("aaa/usage_config_test.cc"));
132  EXPECT_TRUE(
133  config.contains_helppackage_flags("bbdir/aaa/usage_config_test-main.cc"));
134  EXPECT_TRUE(config.contains_helppackage_flags(
135  "//aqswde/aaa/usage_config_test_main.cc"));
136  EXPECT_FALSE(config.contains_helppackage_flags("aadir/usage_config_main.cc"));
137 
138  absl::FlagsUsageConfig empty_config;
139  empty_config.contains_helppackage_flags = &TstContainsHelppackageFlags;
140  absl::SetFlagsUsageConfig(empty_config);
141 
142  EXPECT_TRUE(
143  flags::GetUsageConfig().contains_helppackage_flags("aaa/main-body.c"));
144  EXPECT_FALSE(
145  flags::GetUsageConfig().contains_helppackage_flags("aadir/main-body.c"));
146 }
147 
148 // --------------------------------------------------------------------
149 
150 TEST_F(FlagsUsageConfigTest, TestVersionString) {
151  flags::SetProgramInvocationName("usage_config_test");
152 
153 #ifdef NDEBUG
154  std::string expected_output = "usage_config_test\n";
155 #else
156  std::string expected_output =
157  "usage_config_test\nDebug build (NDEBUG not #defined)\n";
158 #endif
159 
160  EXPECT_EQ(flags::GetUsageConfig().version_string(), expected_output);
161 
162  absl::FlagsUsageConfig empty_config;
163  empty_config.version_string = &TstVersionString;
164  absl::SetFlagsUsageConfig(empty_config);
165 
166  EXPECT_EQ(flags::GetUsageConfig().version_string(), "program 1.0.0");
167 }
168 
169 // --------------------------------------------------------------------
170 
171 TEST_F(FlagsUsageConfigTest, TestNormalizeFilename) {
172  // This tests the default implementation.
173  EXPECT_EQ(flags::GetUsageConfig().normalize_filename("a/a.cc"), "a/a.cc");
174  EXPECT_EQ(flags::GetUsageConfig().normalize_filename("/a/a.cc"), "a/a.cc");
175  EXPECT_EQ(flags::GetUsageConfig().normalize_filename("///a/a.cc"), "a/a.cc");
176  EXPECT_EQ(flags::GetUsageConfig().normalize_filename("/"), "");
177 
178  // This tests that the custom implementation is called.
179  absl::FlagsUsageConfig empty_config;
180  empty_config.normalize_filename = &TstNormalizeFilename;
181  absl::SetFlagsUsageConfig(empty_config);
182 
183  EXPECT_EQ(flags::GetUsageConfig().normalize_filename("a/a.cc"), "a.cc");
184  EXPECT_EQ(flags::GetUsageConfig().normalize_filename("aaa/a.cc"), "a/a.cc");
185 
186  // This tests that the default implementation is called.
187  empty_config.normalize_filename = nullptr;
188  absl::SetFlagsUsageConfig(empty_config);
189 
190  EXPECT_EQ(flags::GetUsageConfig().normalize_filename("a/a.cc"), "a/a.cc");
191  EXPECT_EQ(flags::GetUsageConfig().normalize_filename("/a/a.cc"), "a/a.cc");
192  EXPECT_EQ(flags::GetUsageConfig().normalize_filename("///a/a.cc"), "a/a.cc");
193  EXPECT_EQ(flags::GetUsageConfig().normalize_filename("\\a\\a.cc"), "a\\a.cc");
194  EXPECT_EQ(flags::GetUsageConfig().normalize_filename("//"), "");
195  EXPECT_EQ(flags::GetUsageConfig().normalize_filename("\\\\"), "");
196 }
197 
198 } // namespace
string_view substr(size_type pos, size_type n=npos) const
Definition: string_view.h:354
constexpr const char * Basename(const char *fname, int offset)
Definition: raw_logging.h:119
absl::string_view Package(absl::string_view filename)
Definition: path_util.h:49
flags_internal::FlagKindFilter contains_help_flags
Definition: usage_config.h:81
void SetFlagsUsageConfig(FlagsUsageConfig usage_config)
bool EndsWith(absl::string_view text, absl::string_view suffix)
Definition: match.h:61
TEST_F(GraphCyclesTest, NoCycle)
const char * msg
Definition: mutex.cc:254
FlagsUsageConfig GetUsageConfig()
flags_internal::FlagKindFilter contains_helppackage_flags
Definition: usage_config.h:89
bool StartsWith(absl::string_view text, absl::string_view prefix)
Definition: match.h:52
std::function< std::string()> version_string
Definition: usage_config.h:93
void SetProgramInvocationName(absl::string_view prog_name_str)
Definition: program_name.cc:43
flags_internal::FlagKindFilter contains_helpshort_flags
Definition: usage_config.h:73
std::function< std::string(absl::string_view)> normalize_filename
Definition: usage_config.h:104


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