00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "absl/flags/internal/commandlineflag.h"
00017
00018 #include "gtest/gtest.h"
00019 #include "absl/flags/flag.h"
00020 #include "absl/flags/internal/registry.h"
00021 #include "absl/memory/memory.h"
00022 #include "absl/strings/match.h"
00023 #include "absl/strings/str_cat.h"
00024
00025 ABSL_FLAG(int, int_flag, 201, "int_flag help");
00026 ABSL_FLAG(std::string, string_flag, "dflt",
00027 absl::StrCat("string_flag", " help"));
00028 ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help");
00029
00030 namespace {
00031
00032 namespace flags = absl::flags_internal;
00033
00034 class CommandLineFlagTest : public testing::Test {
00035 protected:
00036 void SetUp() override { flag_saver_ = absl::make_unique<flags::FlagSaver>(); }
00037 void TearDown() override { flag_saver_.reset(); }
00038
00039 private:
00040 std::unique_ptr<flags::FlagSaver> flag_saver_;
00041 };
00042
00043 TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) {
00044 auto* flag_01 = flags::FindCommandLineFlag("int_flag");
00045
00046 ASSERT_TRUE(flag_01);
00047 EXPECT_EQ(flag_01->Name(), "int_flag");
00048 EXPECT_EQ(flag_01->Help(), "int_flag help");
00049 EXPECT_EQ(flag_01->Typename(), "");
00050 EXPECT_TRUE(!flag_01->IsRetired());
00051 EXPECT_TRUE(flag_01->IsOfType<int>());
00052 EXPECT_TRUE(absl::EndsWith(
00053 flag_01->Filename(),
00054 "absl/flags/internal/commandlineflag_test.cc"));
00055
00056 auto* flag_02 = flags::FindCommandLineFlag("string_flag");
00057
00058 ASSERT_TRUE(flag_02);
00059 EXPECT_EQ(flag_02->Name(), "string_flag");
00060 EXPECT_EQ(flag_02->Help(), "string_flag help");
00061 EXPECT_EQ(flag_02->Typename(), "");
00062 EXPECT_TRUE(!flag_02->IsRetired());
00063 EXPECT_TRUE(flag_02->IsOfType<std::string>());
00064 EXPECT_TRUE(absl::EndsWith(
00065 flag_02->Filename(),
00066 "absl/flags/internal/commandlineflag_test.cc"));
00067
00068 auto* flag_03 = flags::FindRetiredFlag("bool_retired_flag");
00069
00070 ASSERT_TRUE(flag_03);
00071 EXPECT_EQ(flag_03->Name(), "bool_retired_flag");
00072 EXPECT_EQ(flag_03->Help(), "");
00073 EXPECT_EQ(flag_03->Typename(), "");
00074 EXPECT_TRUE(flag_03->IsRetired());
00075 EXPECT_TRUE(flag_03->IsOfType<bool>());
00076 EXPECT_EQ(flag_03->Filename(), "RETIRED");
00077 }
00078
00079
00080
00081 TEST_F(CommandLineFlagTest, TestValueAccessMethods) {
00082 absl::SetFlag(&FLAGS_int_flag, 301);
00083 auto* flag_01 = flags::FindCommandLineFlag("int_flag");
00084
00085 ASSERT_TRUE(flag_01);
00086 EXPECT_EQ(flag_01->CurrentValue(), "301");
00087 EXPECT_EQ(flag_01->DefaultValue(), "201");
00088
00089 absl::SetFlag(&FLAGS_string_flag, "new_str_value");
00090 auto* flag_02 = flags::FindCommandLineFlag("string_flag");
00091
00092 ASSERT_TRUE(flag_02);
00093 EXPECT_EQ(flag_02->CurrentValue(), "new_str_value");
00094 EXPECT_EQ(flag_02->DefaultValue(), "dflt");
00095 }
00096
00097
00098
00099 TEST_F(CommandLineFlagTest, TestSetFromStringCurrentValue) {
00100 std::string err;
00101
00102 auto* flag_01 = flags::FindCommandLineFlag("int_flag");
00103 EXPECT_FALSE(flag_01->on_command_line);
00104
00105 EXPECT_TRUE(flag_01->SetFromString("11", flags::SET_FLAGS_VALUE,
00106 flags::kProgrammaticChange, &err));
00107 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
00108 EXPECT_FALSE(flag_01->on_command_line);
00109
00110 EXPECT_TRUE(flag_01->SetFromString("-123", flags::SET_FLAGS_VALUE,
00111 flags::kProgrammaticChange, &err));
00112 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
00113 EXPECT_FALSE(flag_01->on_command_line);
00114
00115 EXPECT_TRUE(!flag_01->SetFromString("xyz", flags::SET_FLAGS_VALUE,
00116 flags::kProgrammaticChange, &err));
00117 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
00118 EXPECT_EQ(err, "Illegal value 'xyz' specified for flag 'int_flag'");
00119 EXPECT_FALSE(flag_01->on_command_line);
00120
00121 EXPECT_TRUE(!flag_01->SetFromString("A1", flags::SET_FLAGS_VALUE,
00122 flags::kProgrammaticChange, &err));
00123 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), -123);
00124 EXPECT_EQ(err, "Illegal value 'A1' specified for flag 'int_flag'");
00125 EXPECT_FALSE(flag_01->on_command_line);
00126
00127 EXPECT_TRUE(flag_01->SetFromString("0x10", flags::SET_FLAGS_VALUE,
00128 flags::kProgrammaticChange, &err));
00129 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 16);
00130 EXPECT_FALSE(flag_01->on_command_line);
00131
00132 EXPECT_TRUE(flag_01->SetFromString("011", flags::SET_FLAGS_VALUE,
00133 flags::kCommandLine, &err));
00134 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 11);
00135 EXPECT_TRUE(flag_01->on_command_line);
00136
00137 EXPECT_TRUE(!flag_01->SetFromString("", flags::SET_FLAGS_VALUE,
00138 flags::kProgrammaticChange, &err));
00139 EXPECT_EQ(err, "Illegal value '' specified for flag 'int_flag'");
00140
00141 auto* flag_02 = flags::FindCommandLineFlag("string_flag");
00142 EXPECT_TRUE(flag_02->SetFromString("xyz", flags::SET_FLAGS_VALUE,
00143 flags::kProgrammaticChange, &err));
00144 EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "xyz");
00145
00146 EXPECT_TRUE(flag_02->SetFromString("", flags::SET_FLAGS_VALUE,
00147 flags::kProgrammaticChange, &err));
00148 EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "");
00149 }
00150
00151
00152
00153 TEST_F(CommandLineFlagTest, TestSetFromStringDefaultValue) {
00154 std::string err;
00155
00156 auto* flag_01 = flags::FindCommandLineFlag("int_flag");
00157
00158 EXPECT_TRUE(flag_01->SetFromString("111", flags::SET_FLAGS_DEFAULT,
00159 flags::kProgrammaticChange, &err));
00160 EXPECT_EQ(flag_01->DefaultValue(), "111");
00161
00162 auto* flag_02 = flags::FindCommandLineFlag("string_flag");
00163
00164 EXPECT_TRUE(flag_02->SetFromString("abc", flags::SET_FLAGS_DEFAULT,
00165 flags::kProgrammaticChange, &err));
00166 EXPECT_EQ(flag_02->DefaultValue(), "abc");
00167 }
00168
00169
00170
00171 TEST_F(CommandLineFlagTest, TestSetFromStringIfDefault) {
00172 std::string err;
00173
00174 auto* flag_01 = flags::FindCommandLineFlag("int_flag");
00175
00176 EXPECT_TRUE(flag_01->SetFromString("22", flags::SET_FLAG_IF_DEFAULT,
00177 flags::kProgrammaticChange, &err))
00178 << err;
00179 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
00180
00181 EXPECT_TRUE(flag_01->SetFromString("33", flags::SET_FLAG_IF_DEFAULT,
00182 flags::kProgrammaticChange, &err));
00183 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 22);
00184
00185
00186
00187 EXPECT_TRUE(flag_01->SetFromString("201", flags::SET_FLAGS_VALUE,
00188 flags::kProgrammaticChange, &err));
00189
00190 EXPECT_TRUE(flag_01->SetFromString("33", flags::SET_FLAG_IF_DEFAULT,
00191 flags::kProgrammaticChange, &err));
00192 EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 201);
00193
00194 }
00195
00196 }