type_erased_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 "absl/flags/internal/type_erased.h"
00017 
00018 #include <cmath>
00019 
00020 #include "gtest/gtest.h"
00021 #include "absl/flags/flag.h"
00022 #include "absl/memory/memory.h"
00023 #include "absl/strings/str_cat.h"
00024 
00025 ABSL_FLAG(int, int_flag, 1, "int_flag help");
00026 ABSL_FLAG(std::string, string_flag, "dflt", "string_flag help");
00027 ABSL_RETIRED_FLAG(bool, bool_retired_flag, false, "bool_retired_flag help");
00028 
00029 namespace {
00030 
00031 namespace flags = absl::flags_internal;
00032 
00033 class TypeErasedTest : public testing::Test {
00034  protected:
00035   void SetUp() override { flag_saver_ = absl::make_unique<flags::FlagSaver>(); }
00036   void TearDown() override { flag_saver_.reset(); }
00037 
00038  private:
00039   std::unique_ptr<flags::FlagSaver> flag_saver_;
00040 };
00041 
00042 // --------------------------------------------------------------------
00043 
00044 TEST_F(TypeErasedTest, TestGetCommandLineOption) {
00045   std::string value;
00046   EXPECT_TRUE(flags::GetCommandLineOption("int_flag", &value));
00047   EXPECT_EQ(value, "1");
00048 
00049   EXPECT_TRUE(flags::GetCommandLineOption("string_flag", &value));
00050   EXPECT_EQ(value, "dflt");
00051 
00052   EXPECT_FALSE(flags::GetCommandLineOption("bool_retired_flag", &value));
00053 
00054   EXPECT_FALSE(flags::GetCommandLineOption("unknown_flag", &value));
00055 }
00056 
00057 // --------------------------------------------------------------------
00058 
00059 TEST_F(TypeErasedTest, TestSetCommandLineOption) {
00060   EXPECT_TRUE(flags::SetCommandLineOption("int_flag", "101"));
00061   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
00062 
00063   EXPECT_TRUE(flags::SetCommandLineOption("string_flag", "asdfgh"));
00064   EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
00065 
00066   EXPECT_FALSE(flags::SetCommandLineOption("bool_retired_flag", "true"));
00067 
00068   EXPECT_FALSE(flags::SetCommandLineOption("unknown_flag", "true"));
00069 }
00070 
00071 // --------------------------------------------------------------------
00072 
00073 TEST_F(TypeErasedTest, TestSetCommandLineOptionWithMode_SET_FLAGS_VALUE) {
00074   EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "101",
00075                                                   flags::SET_FLAGS_VALUE));
00076   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
00077 
00078   EXPECT_TRUE(flags::SetCommandLineOptionWithMode("string_flag", "asdfgh",
00079                                                   flags::SET_FLAGS_VALUE));
00080   EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
00081 
00082   EXPECT_FALSE(flags::SetCommandLineOptionWithMode("bool_retired_flag", "true",
00083                                                    flags::SET_FLAGS_VALUE));
00084 
00085   EXPECT_FALSE(flags::SetCommandLineOptionWithMode("unknown_flag", "true",
00086                                                    flags::SET_FLAGS_VALUE));
00087 }
00088 
00089 // --------------------------------------------------------------------
00090 
00091 TEST_F(TypeErasedTest, TestSetCommandLineOptionWithMode_SET_FLAG_IF_DEFAULT) {
00092   EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "101",
00093                                                   flags::SET_FLAG_IF_DEFAULT));
00094   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
00095 
00096   // This semantic is broken. We return true instead of false. Value is not
00097   // updated.
00098   EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "202",
00099                                                   flags::SET_FLAG_IF_DEFAULT));
00100   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 101);
00101 
00102   EXPECT_TRUE(flags::SetCommandLineOptionWithMode("string_flag", "asdfgh",
00103                                                   flags::SET_FLAG_IF_DEFAULT));
00104   EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
00105 
00106   EXPECT_FALSE(flags::SetCommandLineOptionWithMode("bool_retired_flag", "true",
00107                                                    flags::SET_FLAG_IF_DEFAULT));
00108 
00109   EXPECT_FALSE(flags::SetCommandLineOptionWithMode("unknown_flag", "true",
00110                                                    flags::SET_FLAG_IF_DEFAULT));
00111 }
00112 
00113 // --------------------------------------------------------------------
00114 
00115 TEST_F(TypeErasedTest, TestSetCommandLineOptionWithMode_SET_FLAGS_DEFAULT) {
00116   EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "101",
00117                                                   flags::SET_FLAGS_DEFAULT));
00118 
00119   EXPECT_TRUE(flags::SetCommandLineOptionWithMode("string_flag", "asdfgh",
00120                                                   flags::SET_FLAGS_DEFAULT));
00121   EXPECT_EQ(absl::GetFlag(FLAGS_string_flag), "asdfgh");
00122 
00123   EXPECT_FALSE(flags::SetCommandLineOptionWithMode("bool_retired_flag", "true",
00124                                                    flags::SET_FLAGS_DEFAULT));
00125 
00126   EXPECT_FALSE(flags::SetCommandLineOptionWithMode("unknown_flag", "true",
00127                                                    flags::SET_FLAGS_DEFAULT));
00128 
00129   // This should be successfull, since flag is still is not set
00130   EXPECT_TRUE(flags::SetCommandLineOptionWithMode("int_flag", "202",
00131                                                   flags::SET_FLAG_IF_DEFAULT));
00132   EXPECT_EQ(absl::GetFlag(FLAGS_int_flag), 202);
00133 }
00134 
00135 // --------------------------------------------------------------------
00136 
00137 TEST_F(TypeErasedTest, TestIsValidFlagValue) {
00138   EXPECT_TRUE(flags::IsValidFlagValue("int_flag", "57"));
00139   EXPECT_TRUE(flags::IsValidFlagValue("int_flag", "-101"));
00140   EXPECT_FALSE(flags::IsValidFlagValue("int_flag", "1.1"));
00141 
00142   EXPECT_TRUE(flags::IsValidFlagValue("string_flag", "#%^#%^$%DGHDG$W%adsf"));
00143 
00144   EXPECT_TRUE(flags::IsValidFlagValue("bool_retired_flag", "true"));
00145 }
00146 
00147 }  // namespace


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