148a6aa6cSLei Zhang //===- FormatTest.cpp - TableGen Format Utility Tests ---------------------===//
248a6aa6cSLei Zhang //
330857107SMehdi Amini // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
456222a06SMehdi Amini // See https://llvm.org/LICENSE.txt for license information.
556222a06SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
648a6aa6cSLei Zhang //
756222a06SMehdi Amini //===----------------------------------------------------------------------===//
848a6aa6cSLei Zhang
948a6aa6cSLei Zhang #include "mlir/TableGen/Format.h"
1048a6aa6cSLei Zhang #include "gmock/gmock.h"
1148a6aa6cSLei Zhang
1248a6aa6cSLei Zhang using mlir::tblgen::FmtContext;
1348a6aa6cSLei Zhang using mlir::tblgen::tgfmt;
1448a6aa6cSLei Zhang using ::testing::StrEq;
1548a6aa6cSLei Zhang
TEST(FormatTest,EmptyFmtStr)1648a6aa6cSLei Zhang TEST(FormatTest, EmptyFmtStr) {
1748a6aa6cSLei Zhang FmtContext ctx;
18adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("", &ctx));
1948a6aa6cSLei Zhang EXPECT_TRUE(result.empty());
2048a6aa6cSLei Zhang }
2148a6aa6cSLei Zhang
22*032810f5SRahul Joshi /// Allow extra unused positional parameters.
TEST(FormatTest,EmptyFmtStrExtraParams)2348a6aa6cSLei Zhang TEST(FormatTest, EmptyFmtStrExtraParams) {
2448a6aa6cSLei Zhang FmtContext ctx;
25adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("", &ctx, "a", "b", "c"));
2648a6aa6cSLei Zhang EXPECT_TRUE(result.empty());
2748a6aa6cSLei Zhang }
2848a6aa6cSLei Zhang
29*032810f5SRahul Joshi /// Allow unused placeholder substitution in context.
TEST(FormatTest,EmptyFmtStrPopulatedCtx)3048a6aa6cSLei Zhang TEST(FormatTest, EmptyFmtStrPopulatedCtx) {
3148a6aa6cSLei Zhang FmtContext ctx;
3248a6aa6cSLei Zhang ctx.withBuilder("builder");
33adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("", &ctx));
3448a6aa6cSLei Zhang EXPECT_TRUE(result.empty());
3548a6aa6cSLei Zhang }
3648a6aa6cSLei Zhang
TEST(FormatTest,LiteralFmtStr)3748a6aa6cSLei Zhang TEST(FormatTest, LiteralFmtStr) {
3848a6aa6cSLei Zhang FmtContext ctx;
39adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("void foo {}", &ctx));
4048a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("void foo {}"));
4148a6aa6cSLei Zhang }
4248a6aa6cSLei Zhang
43*032810f5SRahul Joshi /// Print single dollar literally.
TEST(FormatTest,AdjacentDollar)4448a6aa6cSLei Zhang TEST(FormatTest, AdjacentDollar) {
4548a6aa6cSLei Zhang FmtContext ctx;
46adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("$", &ctx));
4748a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("$"));
4848a6aa6cSLei Zhang }
4948a6aa6cSLei Zhang
50*032810f5SRahul Joshi /// Print dangling dollar literally.
TEST(FormatTest,DanglingDollar)5148a6aa6cSLei Zhang TEST(FormatTest, DanglingDollar) {
5248a6aa6cSLei Zhang FmtContext ctx;
53adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("foo bar baz$", &ctx));
5448a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("foo bar baz$"));
5548a6aa6cSLei Zhang }
5648a6aa6cSLei Zhang
57*032810f5SRahul Joshi /// Allow escape dollars with '$$'.
TEST(FormatTest,EscapeDollars)5848a6aa6cSLei Zhang TEST(FormatTest, EscapeDollars) {
5948a6aa6cSLei Zhang FmtContext ctx;
6048a6aa6cSLei Zhang std::string result =
61adcd0268SBenjamin Kramer std::string(tgfmt("$$ $$$$ $$$0 $$$_self", &ctx.withSelf("self"), "-0"));
6248a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("$ $$ $-0 $self"));
6348a6aa6cSLei Zhang }
6448a6aa6cSLei Zhang
TEST(FormatTest,PositionalFmtStr)6548a6aa6cSLei Zhang TEST(FormatTest, PositionalFmtStr) {
6648a6aa6cSLei Zhang FmtContext ctx;
6748a6aa6cSLei Zhang std::string b = "b";
6848a6aa6cSLei Zhang int c = 42;
6948a6aa6cSLei Zhang char d = 'd';
70adcd0268SBenjamin Kramer std::string result =
71adcd0268SBenjamin Kramer std::string(tgfmt("$0 $1 $2 $3", &ctx, "a", b, c + 1, d));
7248a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("a b 43 d"));
7348a6aa6cSLei Zhang }
7448a6aa6cSLei Zhang
75*032810f5SRahul Joshi /// Output the placeholder if missing substitution.
TEST(FormatTest,PositionalFmtStrMissingParams)7648a6aa6cSLei Zhang TEST(FormatTest, PositionalFmtStrMissingParams) {
7748a6aa6cSLei Zhang FmtContext ctx;
78adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("$0 %1 $2", &ctx));
7948a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("$0<no-subst-found> %1 $2<no-subst-found>"));
8048a6aa6cSLei Zhang }
8148a6aa6cSLei Zhang
82*032810f5SRahul Joshi /// Allow flexible reference of positional parameters.
TEST(FormatTest,PositionalFmtStrFlexibleRef)8348a6aa6cSLei Zhang TEST(FormatTest, PositionalFmtStrFlexibleRef) {
8448a6aa6cSLei Zhang FmtContext ctx;
85adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("$2 $0 $2", &ctx, "a", "b", "c"));
8648a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("c a c"));
8748a6aa6cSLei Zhang }
8848a6aa6cSLei Zhang
TEST(FormatTest,PositionalFmtStrNoWhitespace)8948a6aa6cSLei Zhang TEST(FormatTest, PositionalFmtStrNoWhitespace) {
9048a6aa6cSLei Zhang FmtContext ctx;
91adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("foo$0bar", &ctx, "-"));
9248a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("foo-bar"));
9348a6aa6cSLei Zhang }
9448a6aa6cSLei Zhang
TEST(FormatTest,PlaceHolderFmtStrWithSelf)9548a6aa6cSLei Zhang TEST(FormatTest, PlaceHolderFmtStrWithSelf) {
9648a6aa6cSLei Zhang FmtContext ctx;
97adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("$_self", &ctx.withSelf("sss")));
9848a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("sss"));
9948a6aa6cSLei Zhang }
10048a6aa6cSLei Zhang
TEST(FormatTest,PlaceHolderFmtStrWithBuilder)10148a6aa6cSLei Zhang TEST(FormatTest, PlaceHolderFmtStrWithBuilder) {
10248a6aa6cSLei Zhang FmtContext ctx;
10348a6aa6cSLei Zhang
104adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("$_builder", &ctx.withBuilder("bbb")));
10548a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("bbb"));
10648a6aa6cSLei Zhang }
10748a6aa6cSLei Zhang
TEST(FormatTest,PlaceHolderMissingCtx)10848a6aa6cSLei Zhang TEST(FormatTest, PlaceHolderMissingCtx) {
109adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("$_op", nullptr));
11048a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("$_op<no-subst-found>"));
11148a6aa6cSLei Zhang }
11248a6aa6cSLei Zhang
TEST(FormatTest,PlaceHolderMissingSubst)11348a6aa6cSLei Zhang TEST(FormatTest, PlaceHolderMissingSubst) {
11448a6aa6cSLei Zhang FmtContext ctx;
115adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("$_op", &ctx.withBuilder("builder")));
11648a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("$_op<no-subst-found>"));
11748a6aa6cSLei Zhang }
11848a6aa6cSLei Zhang
119*032810f5SRahul Joshi /// Test commonly used delimiters in C++.
TEST(FormatTest,PlaceHolderFmtStrDelimiter)12048a6aa6cSLei Zhang TEST(FormatTest, PlaceHolderFmtStrDelimiter) {
12148a6aa6cSLei Zhang FmtContext ctx;
12248a6aa6cSLei Zhang ctx.addSubst("m", "");
123adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("$m{$m($m[$m]$m)$m}$m|", &ctx));
12448a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("{([])}|"));
12548a6aa6cSLei Zhang }
12648a6aa6cSLei Zhang
127*032810f5SRahul Joshi /// Test allowed characters in placeholder symbol.
TEST(FormatTest,CustomPlaceHolderFmtStrPlaceHolderChars)12848a6aa6cSLei Zhang TEST(FormatTest, CustomPlaceHolderFmtStrPlaceHolderChars) {
12948a6aa6cSLei Zhang FmtContext ctx;
13048a6aa6cSLei Zhang ctx.addSubst("m", "0 ");
13148a6aa6cSLei Zhang ctx.addSubst("m1", "1 ");
13248a6aa6cSLei Zhang ctx.addSubst("m2C", "2 ");
13348a6aa6cSLei Zhang ctx.addSubst("M_3", "3 ");
134adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("$m$m1$m2C$M_3", &ctx));
13548a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("0 1 2 3 "));
13648a6aa6cSLei Zhang }
13748a6aa6cSLei Zhang
TEST(FormatTest,CustomPlaceHolderFmtStrUnregisteredPlaceHolders)13848a6aa6cSLei Zhang TEST(FormatTest, CustomPlaceHolderFmtStrUnregisteredPlaceHolders) {
13948a6aa6cSLei Zhang FmtContext ctx;
140adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("foo($awesome, $param)", &ctx));
14148a6aa6cSLei Zhang EXPECT_THAT(result,
14248a6aa6cSLei Zhang StrEq("foo($awesome<no-subst-found>, $param<no-subst-found>)"));
14348a6aa6cSLei Zhang }
14448a6aa6cSLei Zhang
TEST(FormatTest,MixedFmtStr)14548a6aa6cSLei Zhang TEST(FormatTest, MixedFmtStr) {
14648a6aa6cSLei Zhang FmtContext ctx;
14748a6aa6cSLei Zhang ctx.withBuilder("bbb");
14848a6aa6cSLei Zhang
149adcd0268SBenjamin Kramer std::string result = std::string(tgfmt("$_builder.build($_self, {$0, $1})",
150adcd0268SBenjamin Kramer &ctx.withSelf("sss"), "a", "b"));
15148a6aa6cSLei Zhang EXPECT_THAT(result, StrEq("bbb.build(sss, {a, b})"));
15248a6aa6cSLei Zhang }
153