xref: /llvm-project/mlir/unittests/TableGen/FormatTest.cpp (revision 308571074c13ea2a0758aa085aa02f72150f891e)
1 //===- FormatTest.cpp - TableGen Format Utility Tests ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/TableGen/Format.h"
10 #include "gmock/gmock.h"
11 
12 using mlir::tblgen::FmtContext;
13 using mlir::tblgen::tgfmt;
14 using ::testing::StrEq;
15 
16 TEST(FormatTest, EmptyFmtStr) {
17   FmtContext ctx;
18   std::string result = tgfmt("", &ctx);
19   EXPECT_TRUE(result.empty());
20 }
21 
22 // Allow extra unused positional parameters
23 TEST(FormatTest, EmptyFmtStrExtraParams) {
24   FmtContext ctx;
25   std::string result = tgfmt("", &ctx, "a", "b", "c");
26   EXPECT_TRUE(result.empty());
27 }
28 
29 // Allow unused placeholder substitution in context
30 TEST(FormatTest, EmptyFmtStrPopulatedCtx) {
31   FmtContext ctx;
32   ctx.withBuilder("builder");
33   std::string result = tgfmt("", &ctx);
34   EXPECT_TRUE(result.empty());
35 }
36 
37 TEST(FormatTest, LiteralFmtStr) {
38   FmtContext ctx;
39   std::string result = tgfmt("void foo {}", &ctx);
40   EXPECT_THAT(result, StrEq("void foo {}"));
41 }
42 
43 // Print single dollar literally
44 TEST(FormatTest, AdjacentDollar) {
45   FmtContext ctx;
46   std::string result = tgfmt("$", &ctx);
47   EXPECT_THAT(result, StrEq("$"));
48 }
49 
50 // Print dangling dollar literally
51 TEST(FormatTest, DanglingDollar) {
52   FmtContext ctx;
53   std::string result = tgfmt("foo bar baz$", &ctx);
54   EXPECT_THAT(result, StrEq("foo bar baz$"));
55 }
56 
57 // Allow escape dollars with '$$'
58 TEST(FormatTest, EscapeDollars) {
59   FmtContext ctx;
60   std::string result =
61       tgfmt("$$ $$$$ $$$0 $$$_self", &ctx.withSelf("self"), "-0");
62   EXPECT_THAT(result, StrEq("$ $$ $-0 $self"));
63 }
64 
65 TEST(FormatTest, PositionalFmtStr) {
66   FmtContext ctx;
67   std::string b = "b";
68   int c = 42;
69   char d = 'd';
70   std::string result = tgfmt("$0 $1 $2 $3", &ctx, "a", b, c + 1, d);
71   EXPECT_THAT(result, StrEq("a b 43 d"));
72 }
73 
74 // Output the placeholder if missing substitution
75 TEST(FormatTest, PositionalFmtStrMissingParams) {
76   FmtContext ctx;
77   std::string result = tgfmt("$0 %1 $2", &ctx);
78   EXPECT_THAT(result, StrEq("$0<no-subst-found> %1 $2<no-subst-found>"));
79 }
80 
81 // Allow flexible reference of positional parameters
82 TEST(FormatTest, PositionalFmtStrFlexibleRef) {
83   FmtContext ctx;
84   std::string result = tgfmt("$2 $0 $2", &ctx, "a", "b", "c");
85   EXPECT_THAT(result, StrEq("c a c"));
86 }
87 
88 TEST(FormatTest, PositionalFmtStrNoWhitespace) {
89   FmtContext ctx;
90   std::string result = tgfmt("foo$0bar", &ctx, "-");
91   EXPECT_THAT(result, StrEq("foo-bar"));
92 }
93 
94 TEST(FormatTest, PlaceHolderFmtStrWithSelf) {
95   FmtContext ctx;
96   std::string result = tgfmt("$_self", &ctx.withSelf("sss"));
97   EXPECT_THAT(result, StrEq("sss"));
98 }
99 
100 TEST(FormatTest, PlaceHolderFmtStrWithBuilder) {
101   FmtContext ctx;
102 
103   std::string result = tgfmt("$_builder", &ctx.withBuilder("bbb"));
104   EXPECT_THAT(result, StrEq("bbb"));
105 }
106 
107 TEST(FormatTest, PlaceHolderFmtStrWithOp) {
108   FmtContext ctx;
109   std::string result = tgfmt("$_op", &ctx.withOp("ooo"));
110   EXPECT_THAT(result, StrEq("ooo"));
111 }
112 
113 TEST(FormatTest, PlaceHolderMissingCtx) {
114   std::string result = tgfmt("$_op", nullptr);
115   EXPECT_THAT(result, StrEq("$_op<no-subst-found>"));
116 }
117 
118 TEST(FormatTest, PlaceHolderMissingSubst) {
119   FmtContext ctx;
120   std::string result = tgfmt("$_op", &ctx.withBuilder("builder"));
121   EXPECT_THAT(result, StrEq("$_op<no-subst-found>"));
122 }
123 
124 // Test commonly used delimiters in C++
125 TEST(FormatTest, PlaceHolderFmtStrDelimiter) {
126   FmtContext ctx;
127   ctx.addSubst("m", "");
128   std::string result = tgfmt("$m{$m($m[$m]$m)$m}$m|", &ctx);
129   EXPECT_THAT(result, StrEq("{([])}|"));
130 }
131 
132 // Test allowed characters in placeholder symbol
133 TEST(FormatTest, CustomPlaceHolderFmtStrPlaceHolderChars) {
134   FmtContext ctx;
135   ctx.addSubst("m", "0 ");
136   ctx.addSubst("m1", "1 ");
137   ctx.addSubst("m2C", "2 ");
138   ctx.addSubst("M_3", "3 ");
139   std::string result = tgfmt("$m$m1$m2C$M_3", &ctx);
140   EXPECT_THAT(result, StrEq("0 1 2 3 "));
141 }
142 
143 TEST(FormatTest, CustomPlaceHolderFmtStrUnregisteredPlaceHolders) {
144   FmtContext ctx;
145   std::string result = tgfmt("foo($awesome, $param)", &ctx);
146   EXPECT_THAT(result,
147               StrEq("foo($awesome<no-subst-found>, $param<no-subst-found>)"));
148 }
149 
150 TEST(FormatTest, MixedFmtStr) {
151   FmtContext ctx;
152   ctx.withBuilder("bbb");
153 
154   std::string result = tgfmt("$_builder.build($_self, {$0, $1})",
155                              &ctx.withSelf("sss"), "a", "b");
156   EXPECT_THAT(result, StrEq("bbb.build(sss, {a, b})"));
157 }
158