type_erased_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 <cmath>
19 
20 #include "gtest/gtest.h"
21 #include "absl/flags/flag.h"
22 #include "absl/memory/memory.h"
23 #include "absl/strings/str_cat.h"
24 
25 ABSL_FLAG(int, int_flag, 1, "int_flag help");
26 ABSL_FLAG(std::string, string_flag, "dflt", "string_flag help");
27 ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help");
28 
29 namespace {
30 
31 namespace flags = absl::flags_internal;
32 
33 class TypeErasedTest : public testing::Test {
34  protected:
35  void SetUp() override { flag_saver_ = absl::make_unique<flags::FlagSaver>(); }
36  void TearDown() override { flag_saver_.reset(); }
37 
38  private:
39  std::unique_ptr<flags::FlagSaver> flag_saver_;
40 };
41 
42 // --------------------------------------------------------------------
43 
44 TEST_F(TypeErasedTest, TestGetCommandLineOption) {
45  std::string value;
46  EXPECT_TRUE(flags::GetCommandLineOption("int_flag", &value));
47  EXPECT_EQ(value, "1");
48 
49  EXPECT_TRUE(flags::GetCommandLineOption("string_flag", &value));
50  EXPECT_EQ(value, "dflt");
51 
52  EXPECT_FALSE(flags::GetCommandLineOption("bool_retired_flag", &value));
53 
54  EXPECT_FALSE(flags::GetCommandLineOption("unknown_flag", &value));
55 }
56 
57 // --------------------------------------------------------------------
58 
59 TEST_F(TypeErasedTest, TestSetCommandLineOption) {
60  EXPECT_TRUE(flags::SetCommandLineOption("int_flag", "101"));
61  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
62 
63  EXPECT_TRUE(flags::SetCommandLineOption("string_flag", "asdfgh"));
64  EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
65 
66  EXPECT_FALSE(flags::SetCommandLineOption("bool_retired_flag", "true"));
67 
68  EXPECT_FALSE(flags::SetCommandLineOption("unknown_flag", "true"));
69 }
70 
71 // --------------------------------------------------------------------
72 
73 TEST_F(TypeErasedTest, TestSetCommandLineOptionWithMode_SET_FLAGS_VALUE) {
74  EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "101",
76  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
77 
78  EXPECT_TRUE(flags::SetCommandLineOptionWithMode("string_flag", "asdfgh",
80  EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
81 
82  EXPECT_FALSE(flags::SetCommandLineOptionWithMode("bool_retired_flag", "true",
84 
85  EXPECT_FALSE(flags::SetCommandLineOptionWithMode("unknown_flag", "true",
87 }
88 
89 // --------------------------------------------------------------------
90 
91 TEST_F(TypeErasedTest, TestSetCommandLineOptionWithMode_SET_FLAG_IF_DEFAULT) {
92  EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "101",
94  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
95 
96  // This semantic is broken. We return true instead of false. Value is not
97  // updated.
98  EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "202",
100  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
101 
102  EXPECT_TRUE(flags::SetCommandLineOptionWithMode("string_flag", "asdfgh",
104  EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
105 
106  EXPECT_FALSE(flags::SetCommandLineOptionWithMode("bool_retired_flag", "true",
108 
109  EXPECT_FALSE(flags::SetCommandLineOptionWithMode("unknown_flag", "true",
111 }
112 
113 // --------------------------------------------------------------------
114 
115 TEST_F(TypeErasedTest, TestSetCommandLineOptionWithMode_SET_FLAGS_DEFAULT) {
116  EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "101",
118 
119  EXPECT_TRUE(flags::SetCommandLineOptionWithMode("string_flag", "asdfgh",
121  EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
122 
123  EXPECT_FALSE(flags::SetCommandLineOptionWithMode("bool_retired_flag", "true",
125 
126  EXPECT_FALSE(flags::SetCommandLineOptionWithMode("unknown_flag", "true",
128 
129  // This should be successfull, since flag is still is not set
130  EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "202",
132  EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 202);
133 }
134 
135 // --------------------------------------------------------------------
136 
137 TEST_F(TypeErasedTest, TestIsValidFlagValue) {
138  EXPECT_TRUE(flags::IsValidFlagValue("int_flag", "57"));
139  EXPECT_TRUE(flags::IsValidFlagValue("int_flag", "-101"));
140  EXPECT_FALSE(flags::IsValidFlagValue("int_flag", "1.1"));
141 
142  EXPECT_TRUE(flags::IsValidFlagValue("string_flag", "#%^#%^$%DGHDG$W%adsf"));
143 
144  EXPECT_TRUE(flags::IsValidFlagValue("bool_retired_flag", "true"));
145 }
146 
147 } // namespace
bool GetCommandLineOption(absl::string_view name, std::string *value)
Definition: type_erased.cc:26
bool SetCommandLineOption(absl::string_view name, absl::string_view value)
Definition: type_erased.cc:64
ABSL_FLAG(int, int_flag, 1,"int_flag help")
bool IsValidFlagValue(absl::string_view name, absl::string_view value)
Definition: type_erased.cc:89
bool SetCommandLineOptionWithMode(absl::string_view name, absl::string_view value, FlagSettingMode set_mode)
Definition: type_erased.cc:69
size_t value
TEST_F(GraphCyclesTest, NoCycle)
T GetFlag(const absl::Flag< T > &flag)
Definition: flag.h:86
ABSL_RETIRED_FLAG(bool, bool_retired_flag, false,"bool_retired_flag help")


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