substitute_test.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/strings/substitute.h"
  15. #include <cstdint>
  16. #include <vector>
  17. #include "gtest/gtest.h"
  18. #include "absl/strings/str_cat.h"
  19. namespace {
  20. TEST(SubstituteTest, Substitute) {
  21. // Basic.
  22. EXPECT_EQ("Hello, world!", absl::Substitute("$0, $1!", "Hello", "world"));
  23. // Non-char* types.
  24. EXPECT_EQ("123 0.2 0.1 foo true false x",
  25. absl::Substitute("$0 $1 $2 $3 $4 $5 $6", 123, 0.2, 0.1f,
  26. std::string("foo"), true, false, 'x'));
  27. // All int types.
  28. EXPECT_EQ(
  29. "-32767 65535 "
  30. "-1234567890 3234567890 "
  31. "-1234567890 3234567890 "
  32. "-1234567890123456789 9234567890123456789",
  33. absl::Substitute(
  34. "$0 $1 $2 $3 $4 $5 $6 $7",
  35. static_cast<short>(-32767), // NOLINT(runtime/int)
  36. static_cast<unsigned short>(65535), // NOLINT(runtime/int)
  37. -1234567890, 3234567890U, -1234567890L, 3234567890UL,
  38. -int64_t{1234567890123456789}, uint64_t{9234567890123456789u}));
  39. // Hex format
  40. EXPECT_EQ("0 1 f ffff0ffff 0123456789abcdef",
  41. absl::Substitute("$0$1$2$3$4 $5", //
  42. absl::Hex(0), absl::Hex(1, absl::kSpacePad2),
  43. absl::Hex(0xf, absl::kSpacePad2),
  44. absl::Hex(int16_t{-1}, absl::kSpacePad5),
  45. absl::Hex(int16_t{-1}, absl::kZeroPad5),
  46. absl::Hex(0x123456789abcdef, absl::kZeroPad16)));
  47. // Dec format
  48. EXPECT_EQ("0 115 -1-0001 81985529216486895",
  49. absl::Substitute("$0$1$2$3$4 $5", //
  50. absl::Dec(0), absl::Dec(1, absl::kSpacePad2),
  51. absl::Dec(0xf, absl::kSpacePad2),
  52. absl::Dec(int16_t{-1}, absl::kSpacePad5),
  53. absl::Dec(int16_t{-1}, absl::kZeroPad5),
  54. absl::Dec(0x123456789abcdef, absl::kZeroPad16)));
  55. // Pointer.
  56. const int* int_p = reinterpret_cast<const int*>(0x12345);
  57. std::string str = absl::Substitute("$0", int_p);
  58. EXPECT_EQ(absl::StrCat("0x", absl::Hex(int_p)), str);
  59. // Volatile Pointer.
  60. // Like C++ streamed I/O, such pointers implicitly become bool
  61. volatile int vol = 237;
  62. volatile int *volatile volptr = &vol;
  63. str = absl::Substitute("$0", volptr);
  64. EXPECT_EQ("true", str);
  65. // null is special. StrCat prints 0x0. Substitute prints NULL.
  66. const uint64_t* null_p = nullptr;
  67. str = absl::Substitute("$0", null_p);
  68. EXPECT_EQ("NULL", str);
  69. // char* is also special.
  70. const char* char_p = "print me";
  71. str = absl::Substitute("$0", char_p);
  72. EXPECT_EQ("print me", str);
  73. char char_buf[16];
  74. strncpy(char_buf, "print me too", sizeof(char_buf));
  75. str = absl::Substitute("$0", char_buf);
  76. EXPECT_EQ("print me too", str);
  77. // null char* is "doubly" special. Represented as the empty string.
  78. char_p = nullptr;
  79. str = absl::Substitute("$0", char_p);
  80. EXPECT_EQ("", str);
  81. // Out-of-order.
  82. EXPECT_EQ("b, a, c, b", absl::Substitute("$1, $0, $2, $1", "a", "b", "c"));
  83. // Literal $
  84. EXPECT_EQ("$", absl::Substitute("$$"));
  85. EXPECT_EQ("$1", absl::Substitute("$$1"));
  86. // Test all overloads.
  87. EXPECT_EQ("a", absl::Substitute("$0", "a"));
  88. EXPECT_EQ("a b", absl::Substitute("$0 $1", "a", "b"));
  89. EXPECT_EQ("a b c", absl::Substitute("$0 $1 $2", "a", "b", "c"));
  90. EXPECT_EQ("a b c d", absl::Substitute("$0 $1 $2 $3", "a", "b", "c", "d"));
  91. EXPECT_EQ("a b c d e",
  92. absl::Substitute("$0 $1 $2 $3 $4", "a", "b", "c", "d", "e"));
  93. EXPECT_EQ("a b c d e f", absl::Substitute("$0 $1 $2 $3 $4 $5", "a", "b", "c",
  94. "d", "e", "f"));
  95. EXPECT_EQ("a b c d e f g", absl::Substitute("$0 $1 $2 $3 $4 $5 $6", "a", "b",
  96. "c", "d", "e", "f", "g"));
  97. EXPECT_EQ("a b c d e f g h",
  98. absl::Substitute("$0 $1 $2 $3 $4 $5 $6 $7", "a", "b", "c", "d", "e",
  99. "f", "g", "h"));
  100. EXPECT_EQ("a b c d e f g h i",
  101. absl::Substitute("$0 $1 $2 $3 $4 $5 $6 $7 $8", "a", "b", "c", "d",
  102. "e", "f", "g", "h", "i"));
  103. EXPECT_EQ("a b c d e f g h i j",
  104. absl::Substitute("$0 $1 $2 $3 $4 $5 $6 $7 $8 $9", "a", "b", "c",
  105. "d", "e", "f", "g", "h", "i", "j"));
  106. EXPECT_EQ("a b c d e f g h i j b0",
  107. absl::Substitute("$0 $1 $2 $3 $4 $5 $6 $7 $8 $9 $10", "a", "b", "c",
  108. "d", "e", "f", "g", "h", "i", "j"));
  109. const char* null_cstring = nullptr;
  110. EXPECT_EQ("Text: ''", absl::Substitute("Text: '$0'", null_cstring));
  111. }
  112. TEST(SubstituteTest, SubstituteAndAppend) {
  113. std::string str = "Hello";
  114. absl::SubstituteAndAppend(&str, ", $0!", "world");
  115. EXPECT_EQ("Hello, world!", str);
  116. // Test all overloads.
  117. str.clear();
  118. absl::SubstituteAndAppend(&str, "$0", "a");
  119. EXPECT_EQ("a", str);
  120. str.clear();
  121. absl::SubstituteAndAppend(&str, "$0 $1", "a", "b");
  122. EXPECT_EQ("a b", str);
  123. str.clear();
  124. absl::SubstituteAndAppend(&str, "$0 $1 $2", "a", "b", "c");
  125. EXPECT_EQ("a b c", str);
  126. str.clear();
  127. absl::SubstituteAndAppend(&str, "$0 $1 $2 $3", "a", "b", "c", "d");
  128. EXPECT_EQ("a b c d", str);
  129. str.clear();
  130. absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4", "a", "b", "c", "d", "e");
  131. EXPECT_EQ("a b c d e", str);
  132. str.clear();
  133. absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5", "a", "b", "c", "d", "e",
  134. "f");
  135. EXPECT_EQ("a b c d e f", str);
  136. str.clear();
  137. absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5 $6", "a", "b", "c", "d",
  138. "e", "f", "g");
  139. EXPECT_EQ("a b c d e f g", str);
  140. str.clear();
  141. absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5 $6 $7", "a", "b", "c", "d",
  142. "e", "f", "g", "h");
  143. EXPECT_EQ("a b c d e f g h", str);
  144. str.clear();
  145. absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5 $6 $7 $8", "a", "b", "c",
  146. "d", "e", "f", "g", "h", "i");
  147. EXPECT_EQ("a b c d e f g h i", str);
  148. str.clear();
  149. absl::SubstituteAndAppend(&str, "$0 $1 $2 $3 $4 $5 $6 $7 $8 $9", "a", "b",
  150. "c", "d", "e", "f", "g", "h", "i", "j");
  151. EXPECT_EQ("a b c d e f g h i j", str);
  152. }
  153. TEST(SubstituteTest, VectorBoolRef) {
  154. std::vector<bool> v = {true, false};
  155. const auto& cv = v;
  156. EXPECT_EQ("true false true false",
  157. absl::Substitute("$0 $1 $2 $3", v[0], v[1], cv[0], cv[1]));
  158. std::string str = "Logic be like: ";
  159. absl::SubstituteAndAppend(&str, "$0 $1 $2 $3", v[0], v[1], cv[0], cv[1]);
  160. EXPECT_EQ("Logic be like: true false true false", str);
  161. }
  162. #ifdef GTEST_HAS_DEATH_TEST
  163. TEST(SubstituteDeathTest, SubstituteDeath) {
  164. EXPECT_DEBUG_DEATH(
  165. static_cast<void>(absl::Substitute(absl::string_view("-$2"), "a", "b")),
  166. "Invalid absl::Substitute\\(\\) format string: asked for \"\\$2\", "
  167. "but only 2 args were given.");
  168. EXPECT_DEBUG_DEATH(
  169. static_cast<void>(absl::Substitute(absl::string_view("-$z-"))),
  170. "Invalid absl::Substitute\\(\\) format string: \"-\\$z-\"");
  171. EXPECT_DEBUG_DEATH(
  172. static_cast<void>(absl::Substitute(absl::string_view("-$"))),
  173. "Invalid absl::Substitute\\(\\) format string: \"-\\$\"");
  174. }
  175. #endif // GTEST_HAS_DEATH_TEST
  176. } // namespace