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