[clang][extract-api] Emit "navigator" property of "name" in SymbolGraph
[llvm-project.git] / mlir / unittests / TableGen / FormatTest.cpp
blob0cae408bc3fb81dc6dd4ee4a4aa04e46e967a091
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 //===----------------------------------------------------------------------===//
9 #include "mlir/TableGen/Format.h"
10 #include "gmock/gmock.h"
12 using mlir::tblgen::FmtContext;
13 using mlir::tblgen::tgfmt;
14 using ::testing::StrEq;
16 TEST(FormatTest, EmptyFmtStr) {
17 FmtContext ctx;
18 std::string result = std::string(tgfmt("", &ctx));
19 EXPECT_TRUE(result.empty());
22 /// Allow extra unused positional parameters.
23 TEST(FormatTest, EmptyFmtStrExtraParams) {
24 FmtContext ctx;
25 std::string result = std::string(tgfmt("", &ctx, "a", "b", "c"));
26 EXPECT_TRUE(result.empty());
29 /// Allow unused placeholder substitution in context.
30 TEST(FormatTest, EmptyFmtStrPopulatedCtx) {
31 FmtContext ctx;
32 ctx.withBuilder("builder");
33 std::string result = std::string(tgfmt("", &ctx));
34 EXPECT_TRUE(result.empty());
37 TEST(FormatTest, LiteralFmtStr) {
38 FmtContext ctx;
39 std::string result = std::string(tgfmt("void foo {}", &ctx));
40 EXPECT_THAT(result, StrEq("void foo {}"));
43 /// Print single dollar literally.
44 TEST(FormatTest, AdjacentDollar) {
45 FmtContext ctx;
46 std::string result = std::string(tgfmt("$", &ctx));
47 EXPECT_THAT(result, StrEq("$"));
50 /// Print dangling dollar literally.
51 TEST(FormatTest, DanglingDollar) {
52 FmtContext ctx;
53 std::string result = std::string(tgfmt("foo bar baz$", &ctx));
54 EXPECT_THAT(result, StrEq("foo bar baz$"));
57 /// Allow escape dollars with '$$'.
58 TEST(FormatTest, EscapeDollars) {
59 FmtContext ctx;
60 std::string result =
61 std::string(tgfmt("$$ $$$$ $$$0 $$$_self", &ctx.withSelf("self"), "-0"));
62 EXPECT_THAT(result, StrEq("$ $$ $-0 $self"));
65 TEST(FormatTest, PositionalFmtStr) {
66 FmtContext ctx;
67 std::string b = "b";
68 int c = 42;
69 char d = 'd';
70 std::string result =
71 std::string(tgfmt("$0 $1 $2 $3", &ctx, "a", b, c + 1, d));
72 EXPECT_THAT(result, StrEq("a b 43 d"));
75 /// Output the placeholder if missing substitution.
76 TEST(FormatTest, PositionalFmtStrMissingParams) {
77 FmtContext ctx;
78 std::string result = std::string(tgfmt("$0 %1 $2", &ctx));
79 EXPECT_THAT(result, StrEq("$0<no-subst-found> %1 $2<no-subst-found>"));
82 /// Allow flexible reference of positional parameters.
83 TEST(FormatTest, PositionalFmtStrFlexibleRef) {
84 FmtContext ctx;
85 std::string result = std::string(tgfmt("$2 $0 $2", &ctx, "a", "b", "c"));
86 EXPECT_THAT(result, StrEq("c a c"));
89 TEST(FormatTest, PositionalFmtStrNoWhitespace) {
90 FmtContext ctx;
91 std::string result = std::string(tgfmt("foo$0bar", &ctx, "-"));
92 EXPECT_THAT(result, StrEq("foo-bar"));
95 TEST(FormatTest, PlaceHolderFmtStrWithSelf) {
96 FmtContext ctx;
97 std::string result = std::string(tgfmt("$_self", &ctx.withSelf("sss")));
98 EXPECT_THAT(result, StrEq("sss"));
101 TEST(FormatTest, PlaceHolderFmtStrWithBuilder) {
102 FmtContext ctx;
104 std::string result = std::string(tgfmt("$_builder", &ctx.withBuilder("bbb")));
105 EXPECT_THAT(result, StrEq("bbb"));
108 TEST(FormatTest, PlaceHolderFmtStrWithOp) {
109 FmtContext ctx;
110 std::string result = std::string(tgfmt("$_op", &ctx.withOp("ooo")));
111 EXPECT_THAT(result, StrEq("ooo"));
114 TEST(FormatTest, PlaceHolderMissingCtx) {
115 std::string result = std::string(tgfmt("$_op", nullptr));
116 EXPECT_THAT(result, StrEq("$_op<no-subst-found>"));
119 TEST(FormatTest, PlaceHolderMissingSubst) {
120 FmtContext ctx;
121 std::string result = std::string(tgfmt("$_op", &ctx.withBuilder("builder")));
122 EXPECT_THAT(result, StrEq("$_op<no-subst-found>"));
125 /// Test commonly used delimiters in C++.
126 TEST(FormatTest, PlaceHolderFmtStrDelimiter) {
127 FmtContext ctx;
128 ctx.addSubst("m", "");
129 std::string result = std::string(tgfmt("$m{$m($m[$m]$m)$m}$m|", &ctx));
130 EXPECT_THAT(result, StrEq("{([])}|"));
133 /// Test allowed characters in placeholder symbol.
134 TEST(FormatTest, CustomPlaceHolderFmtStrPlaceHolderChars) {
135 FmtContext ctx;
136 ctx.addSubst("m", "0 ");
137 ctx.addSubst("m1", "1 ");
138 ctx.addSubst("m2C", "2 ");
139 ctx.addSubst("M_3", "3 ");
140 std::string result = std::string(tgfmt("$m$m1$m2C$M_3", &ctx));
141 EXPECT_THAT(result, StrEq("0 1 2 3 "));
144 TEST(FormatTest, CustomPlaceHolderFmtStrUnregisteredPlaceHolders) {
145 FmtContext ctx;
146 std::string result = std::string(tgfmt("foo($awesome, $param)", &ctx));
147 EXPECT_THAT(result,
148 StrEq("foo($awesome<no-subst-found>, $param<no-subst-found>)"));
151 TEST(FormatTest, MixedFmtStr) {
152 FmtContext ctx;
153 ctx.withBuilder("bbb");
155 std::string result = std::string(tgfmt("$_builder.build($_self, {$0, $1})",
156 &ctx.withSelf("sss"), "a", "b"));
157 EXPECT_THAT(result, StrEq("bbb.build(sss, {a, b})"));