substitute_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/strings/substitute.h"
00016 
00017 #include <cstdint>
00018 #include <vector>
00019 
00020 #include "gtest/gtest.h"
00021 #include "absl/strings/str_cat.h"
00022 
00023 namespace {
00024 
00025 TEST(SubstituteTest, Substitute) {
00026   // Basic.
00027   EXPECT_EQ("Hello, world!", absl::Substitute("$0, $1!", "Hello", "world"));
00028 
00029   // Non-char* types.
00030   EXPECT_EQ("123 0.2 0.1 foo true false x",
00031             absl::Substitute("$0 $1 $2 $3 $4 $5 $6", 123, 0.2, 0.1f,
00032                              std::string("foo"), true, false, 'x'));
00033 
00034   // All int types.
00035   EXPECT_EQ(
00036       "-32767 65535 "
00037       "-1234567890 3234567890 "
00038       "-1234567890 3234567890 "
00039       "-1234567890123456789 9234567890123456789",
00040       absl::Substitute(
00041           "$0 $1 $2 $3 $4 $5 $6 $7",
00042           static_cast<short>(-32767),          // NOLINT(runtime/int)
00043           static_cast<unsigned short>(65535),  // NOLINT(runtime/int)
00044           -1234567890, 3234567890U, -1234567890L, 3234567890UL,
00045           -int64_t{1234567890123456789}, uint64_t{9234567890123456789u}));
00046 
00047   // Hex format
00048   EXPECT_EQ("0 1 f ffff0ffff 0123456789abcdef",
00049             absl::Substitute("$0$1$2$3$4 $5",  //
00050                              absl::Hex(0), absl::Hex(1, absl::kSpacePad2),
00051                              absl::Hex(0xf, absl::kSpacePad2),
00052                              absl::Hex(int16_t{-1}, absl::kSpacePad5),
00053                              absl::Hex(int16_t{-1}, absl::kZeroPad5),
00054                              absl::Hex(0x123456789abcdef, absl::kZeroPad16)));
00055 
00056   // Dec format
00057   EXPECT_EQ("0 115   -1-0001 81985529216486895",
00058             absl::Substitute("$0$1$2$3$4 $5",  //
00059                              absl::Dec(0), absl::Dec(1, absl::kSpacePad2),
00060                              absl::Dec(0xf, absl::kSpacePad2),
00061                              absl::Dec(int16_t{-1}, absl::kSpacePad5),
00062                              absl::Dec(int16_t{-1}, absl::kZeroPad5),
00063                              absl::Dec(0x123456789abcdef, absl::kZeroPad16)));
00064 
00065   // Pointer.
00066   const int* int_p = reinterpret_cast<const int*>(0x12345);
00067   std::string str = absl::Substitute("$0", int_p);
00068   EXPECT_EQ(absl::StrCat("0x", absl::Hex(int_p)), str);
00069 
00070   // Volatile Pointer.
00071   // Like C++ streamed I/O, such pointers implicitly become bool
00072   volatile int vol = 237;
00073   volatile int *volatile volptr = &vol;
00074   str = absl::Substitute("$0", volptr);
00075   EXPECT_EQ("true", str);
00076 
00077   // null is special. StrCat prints 0x0. Substitute prints NULL.
00078   const uint64_t* null_p = nullptr;
00079   str = absl::Substitute("$0", null_p);
00080   EXPECT_EQ("NULL", str);
00081 
00082   // char* is also special.
00083   const char* char_p = "print me";
00084   str = absl::Substitute("$0", char_p);
00085   EXPECT_EQ("print me", str);
00086 
00087   char char_buf[16];
00088   strncpy(char_buf, "print me too", sizeof(char_buf));
00089   str = absl::Substitute("$0", char_buf);
00090   EXPECT_EQ("print me too", str);
00091 
00092   // null char* is "doubly" special. Represented as the empty std::string.
00093   char_p = nullptr;
00094   str = absl::Substitute("$0", char_p);
00095   EXPECT_EQ("", str);
00096 
00097   // Out-of-order.
00098   EXPECT_EQ("b, a, c, b", absl::Substitute("$1, $0, $2, $1", "a", "b", "c"));
00099 
00100   // Literal $
00101   EXPECT_EQ("$", absl::Substitute("$$"));
00102 
00103   EXPECT_EQ("$1", absl::Substitute("$$1"));
00104 
00105   // Test all overloads.
00106   EXPECT_EQ("a", absl::Substitute("$0", "a"));
00107   EXPECT_EQ("a b", absl::Substitute("$0 $1", "a", "b"));
00108   EXPECT_EQ("a b c", absl::Substitute("$0 $1 $2", "a", "b", "c"));
00109   EXPECT_EQ("a b c d", absl::Substitute("$0 $1 $2 $3", "a", "b", "c", "d"));
00110   EXPECT_EQ("a b c d e",
00111             absl::Substitute("$0 $1 $2 $3 $4", "a", "b", "c", "d", "e"));
00112   EXPECT_EQ("a b c d e f", absl::Substitute("$0 $1 $2 $3 $4 $5", "a", "b", "c",
00113                                             "d", "e", "f"));
00114   EXPECT_EQ("a b c d e f g", absl::Substitute("$0 $1 $2 $3 $4 $5 $6", "a", "b",
00115                                               "c", "d", "e", "f", "g"));
00116   EXPECT_EQ("a b c d e f g h",
00117             absl::Substitute("$0 $1 $2 $3 $4 $5 $6 $7", "a", "b", "c", "d", "e",
00118                              "f", "g", "h"));
00119   EXPECT_EQ("a b c d e f g h i",
00120             absl::Substitute("$0 $1 $2 $3 $4 $5 $6 $7 $8", "a", "b", "c", "d",
00121                              "e", "f", "g", "h", "i"));
00122   EXPECT_EQ("a b c d e f g h i j",
00123             absl::Substitute("$0 $1 $2 $3 $4 $5 $6 $7 $8 $9", "a", "b", "c",
00124                              "d", "e", "f", "g", "h", "i", "j"));
00125   EXPECT_EQ("a b c d e f g h i j b0",
00126             absl::Substitute("$0 $1 $2 $3 $4 $5 $6 $7 $8 $9 $10", "a", "b", "c",
00127                              "d", "e", "f", "g", "h", "i", "j"));
00128 
00129   const char* null_cstring = nullptr;
00130   EXPECT_EQ("Text: ''", absl::Substitute("Text: '$0'", null_cstring));
00131 }
00132 
00133 TEST(SubstituteTest, SubstituteAndAppend) {
00134   std::string str = "Hello";
00135   absl::SubstituteAndAppend(&str, ", $0!", "world");
00136   EXPECT_EQ("Hello, world!", str);
00137 
00138   // Test all overloads.
00139   str.clear();
00140   absl::SubstituteAndAppend(&str, "$0", "a");
00141   EXPECT_EQ("a", str);
00142   str.clear();
00143   absl::SubstituteAndAppend(&str, "$0 $1", "a", "b");
00144   EXPECT_EQ("a b", str);
00145   str.clear();
00146   absl::SubstituteAndAppend(&str, "$0 $1 $2", "a", "b", "c");
00147   EXPECT_EQ("a b c", str);
00148   str.clear();
00149   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3", "a", "b", "c", "d");
00150   EXPECT_EQ("a b c d", str);
00151   str.clear();
00152   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4", "a", "b", "c", "d", "e");
00153   EXPECT_EQ("a b c d e", str);
00154   str.clear();
00155   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5", "a", "b", "c", "d", "e",
00156                             "f");
00157   EXPECT_EQ("a b c d e f", str);
00158   str.clear();
00159   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5 $6", "a", "b", "c", "d",
00160                             "e", "f", "g");
00161   EXPECT_EQ("a b c d e f g", str);
00162   str.clear();
00163   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5 $6 $7", "a", "b", "c", "d",
00164                             "e", "f", "g", "h");
00165   EXPECT_EQ("a b c d e f g h", str);
00166   str.clear();
00167   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5 $6 $7 $8", "a", "b", "c",
00168                             "d", "e", "f", "g", "h", "i");
00169   EXPECT_EQ("a b c d e f g h i", str);
00170   str.clear();
00171   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5 $6 $7 $8 $9", "a", "b",
00172                             "c", "d", "e", "f", "g", "h", "i", "j");
00173   EXPECT_EQ("a b c d e f g h i j", str);
00174 }
00175 
00176 TEST(SubstituteTest, VectorBoolRef) {
00177   std::vector<bool> v = {true, false};
00178   const auto& cv = v;
00179   EXPECT_EQ("true false true false",
00180             absl::Substitute("$0 $1 $2 $3", v[0], v[1], cv[0], cv[1]));
00181 
00182   std::string str = "Logic be like: ";
00183   absl::SubstituteAndAppend(&str, "$0 $1 $2 $3", v[0], v[1], cv[0], cv[1]);
00184   EXPECT_EQ("Logic be like: true false true false", str);
00185 }
00186 
00187 #ifdef GTEST_HAS_DEATH_TEST
00188 
00189 TEST(SubstituteDeathTest, SubstituteDeath) {
00190   EXPECT_DEBUG_DEATH(
00191       static_cast<void>(absl::Substitute(absl::string_view("-$2"), "a", "b")),
00192       "Invalid strings::Substitute\\(\\) format std::string: asked for \"\\$2\", "
00193       "but only 2 args were given.");
00194   EXPECT_DEBUG_DEATH(
00195       static_cast<void>(absl::Substitute("-$z-")),
00196       "Invalid strings::Substitute\\(\\) format std::string: \"-\\$z-\"");
00197   EXPECT_DEBUG_DEATH(
00198       static_cast<void>(absl::Substitute("-$")),
00199       "Invalid strings::Substitute\\(\\) format std::string: \"-\\$\"");
00200 }
00201 
00202 #endif  // GTEST_HAS_DEATH_TEST
00203 
00204 }  // namespace


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