summaryrefslogtreecommitdiff
path: root/absl/strings/str_format_test.cc
diff options
context:
space:
mode:
authorGravatar Phoebe Liang <phoebeliang@google.com>2022-12-07 12:49:08 -0800
committerGravatar Copybara-Service <copybara-worker@google.com>2022-12-07 12:49:54 -0800
commit9bff2a9302a8dbf91712fc215eb2e2cf8ec234e7 (patch)
tree2f34d9f653da6ba62047d525575671349a387271 /absl/strings/str_format_test.cc
parente9787e7d1d46e2b9687b150f18c76d85c2eaee71 (diff)
Fixes issue where AbslStringify() breaks formatting with %d for enums
PiperOrigin-RevId: 493682437 Change-Id: I30f2ac36b998b86c24fe7513cd952b860560a66e
Diffstat (limited to 'absl/strings/str_format_test.cc')
-rw-r--r--absl/strings/str_format_test.cc37
1 files changed, 35 insertions, 2 deletions
diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc
index 36def1e0..5198fb33 100644
--- a/absl/strings/str_format_test.cc
+++ b/absl/strings/str_format_test.cc
@@ -1142,18 +1142,51 @@ TEST_F(FormatExtensionTest, AbslStringifyExampleUsingFormat) {
EXPECT_EQ(absl::StrFormat("a %v z", p), "a (10, 20) z");
}
-enum class EnumWithStringify { Many = 0, Choices = 1 };
+enum class EnumClassWithStringify { Many = 0, Choices = 1 };
+
+template <typename Sink>
+void AbslStringify(Sink& sink, EnumClassWithStringify e) {
+ absl::Format(&sink, "%s",
+ e == EnumClassWithStringify::Many ? "Many" : "Choices");
+}
+
+enum EnumWithStringify { Many, Choices };
template <typename Sink>
void AbslStringify(Sink& sink, EnumWithStringify e) {
absl::Format(&sink, "%s", e == EnumWithStringify::Many ? "Many" : "Choices");
}
-TEST_F(FormatExtensionTest, AbslStringifyWithEnum) {
+TEST_F(FormatExtensionTest, AbslStringifyWithEnumWithV) {
+ const auto e_class = EnumClassWithStringify::Choices;
+ EXPECT_EQ(absl::StrFormat("My choice is %v", e_class),
+ "My choice is Choices");
+
const auto e = EnumWithStringify::Choices;
EXPECT_EQ(absl::StrFormat("My choice is %v", e), "My choice is Choices");
}
+TEST_F(FormatExtensionTest, AbslStringifyEnumWithD) {
+ const auto e_class = EnumClassWithStringify::Many;
+ EXPECT_EQ(absl::StrFormat("My choice is %d", e_class), "My choice is 0");
+
+ const auto e = EnumWithStringify::Choices;
+ EXPECT_EQ(absl::StrFormat("My choice is %d", e), "My choice is 1");
+}
+
+enum class EnumWithLargerValue { x = 32 };
+
+template <typename Sink>
+void AbslStringify(Sink& sink, EnumWithLargerValue e) {
+ absl::Format(&sink, "%s", "Many");
+}
+
+TEST_F(FormatExtensionTest, AbslStringifyEnumOtherSpecifiers) {
+ const auto e = EnumWithLargerValue::x;
+ EXPECT_EQ(absl::StrFormat("My choice is %g", e), "My choice is 32");
+ EXPECT_EQ(absl::StrFormat("My choice is %x", e), "My choice is 20");
+}
+
} // namespace
// Some codegen thunks that we can use to easily dump the generated assembly for