1 #include <string>
2
3 #include "gmock/gmock.h"
4 #include "gtest/gtest.h"
5 #include "absl/strings/str_format.h"
6
7 namespace absl {
8 ABSL_NAMESPACE_BEGIN
9 namespace str_format_internal {
10 namespace {
11
ConvToString(FormatConversionCharSet conv)12 std::string ConvToString(FormatConversionCharSet conv) {
13 std::string out;
14 #define CONV_SET_CASE(c) \
15 if (Contains(conv, FormatConversionCharSetInternal::c)) { \
16 out += #c; \
17 }
18 ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(CONV_SET_CASE, )
19 #undef CONV_SET_CASE
20 if (Contains(conv, FormatConversionCharSetInternal::kStar)) {
21 out += "*";
22 }
23 return out;
24 }
25
TEST(StrFormatChecker,ArgumentToConv)26 TEST(StrFormatChecker, ArgumentToConv) {
27 FormatConversionCharSet conv = ArgumentToConv<std::string>();
28 EXPECT_EQ(ConvToString(conv), "s");
29
30 conv = ArgumentToConv<const char*>();
31 EXPECT_EQ(ConvToString(conv), "sp");
32
33 conv = ArgumentToConv<double>();
34 EXPECT_EQ(ConvToString(conv), "fFeEgGaA");
35
36 conv = ArgumentToConv<int>();
37 EXPECT_EQ(ConvToString(conv), "cdiouxXfFeEgGaA*");
38
39 conv = ArgumentToConv<std::string*>();
40 EXPECT_EQ(ConvToString(conv), "p");
41 }
42
43 #ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
44
45 struct Case {
46 bool result;
47 const char* format;
48 };
49
50 template <typename... Args>
ValidFormat(const char * format)51 constexpr Case ValidFormat(const char* format) {
52 return {ValidFormatImpl<ArgumentToConv<Args>()...>(format), format};
53 }
54
TEST(StrFormatChecker,ValidFormat)55 TEST(StrFormatChecker, ValidFormat) {
56 // We want to make sure these expressions are constexpr and they have the
57 // expected value.
58 // If they are not constexpr the attribute will just ignore them and not give
59 // a compile time error.
60 enum e {};
61 enum class e2 {};
62 constexpr Case trues[] = {
63 ValidFormat<>("abc"), //
64
65 ValidFormat<e>("%d"), //
66 ValidFormat<e2>("%d"), //
67 ValidFormat<int>("%% %d"), //
68 ValidFormat<int>("%ld"), //
69 ValidFormat<int>("%lld"), //
70 ValidFormat<std::string>("%s"), //
71 ValidFormat<std::string>("%10s"), //
72 ValidFormat<int>("%.10x"), //
73 ValidFormat<int, int>("%*.3x"), //
74 ValidFormat<int>("%1.d"), //
75 ValidFormat<int>("%.d"), //
76 ValidFormat<int, double>("%d %g"), //
77 ValidFormat<int, std::string>("%*s"), //
78 ValidFormat<int, double>("%.*f"), //
79 ValidFormat<void (*)(), volatile int*>("%p %p"), //
80 ValidFormat<string_view, const char*, double, void*>(
81 "string_view=%s const char*=%s double=%f void*=%p)"),
82
83 ValidFormat<int>("%% %1$d"), //
84 ValidFormat<int>("%1$ld"), //
85 ValidFormat<int>("%1$lld"), //
86 ValidFormat<std::string>("%1$s"), //
87 ValidFormat<std::string>("%1$10s"), //
88 ValidFormat<int>("%1$.10x"), //
89 ValidFormat<int>("%1$*1$.*1$d"), //
90 ValidFormat<int, int>("%1$*2$.3x"), //
91 ValidFormat<int>("%1$1.d"), //
92 ValidFormat<int>("%1$.d"), //
93 ValidFormat<double, int>("%2$d %1$g"), //
94 ValidFormat<int, std::string>("%2$*1$s"), //
95 ValidFormat<int, double>("%2$.*1$f"), //
96 ValidFormat<void*, string_view, const char*, double>(
97 "string_view=%2$s const char*=%3$s double=%4$f void*=%1$p "
98 "repeat=%3$s)")};
99
100 for (Case c : trues) {
101 EXPECT_TRUE(c.result) << c.format;
102 }
103
104 constexpr Case falses[] = {
105 ValidFormat<int>(""), //
106
107 ValidFormat<e>("%s"), //
108 ValidFormat<e2>("%s"), //
109 ValidFormat<>("%s"), //
110 ValidFormat<>("%r"), //
111 ValidFormat<int>("%s"), //
112 ValidFormat<int>("%.1.d"), //
113 ValidFormat<int>("%*1d"), //
114 ValidFormat<int>("%1-d"), //
115 ValidFormat<std::string, int>("%*s"), //
116 ValidFormat<int>("%*d"), //
117 ValidFormat<std::string>("%p"), //
118 ValidFormat<int (*)(int)>("%d"), //
119
120 ValidFormat<>("%3$d"), //
121 ValidFormat<>("%1$r"), //
122 ValidFormat<int>("%1$s"), //
123 ValidFormat<int>("%1$.1.d"), //
124 ValidFormat<int>("%1$*2$1d"), //
125 ValidFormat<int>("%1$1-d"), //
126 ValidFormat<std::string, int>("%2$*1$s"), //
127 ValidFormat<std::string>("%1$p"),
128
129 ValidFormat<int, int>("%d %2$d"), //
130 };
131
132 for (Case c : falses) {
133 EXPECT_FALSE(c.result) << c.format;
134 }
135 }
136
TEST(StrFormatChecker,LongFormat)137 TEST(StrFormatChecker, LongFormat) {
138 #define CHARS_X_40 "1234567890123456789012345678901234567890"
139 #define CHARS_X_400 \
140 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 \
141 CHARS_X_40 CHARS_X_40 CHARS_X_40
142 #define CHARS_X_4000 \
143 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400 \
144 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400
145 constexpr char long_format[] =
146 CHARS_X_4000 "%d" CHARS_X_4000 "%s" CHARS_X_4000;
147 constexpr bool is_valid = ValidFormat<int, std::string>(long_format).result;
148 EXPECT_TRUE(is_valid);
149 }
150
151 #endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
152
153 } // namespace
154 } // namespace str_format_internal
155 ABSL_NAMESPACE_END
156 } // namespace absl
157