1 //===- unittest/Tooling/StencilTest.cpp -----------------------------------===// 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 "clang/Tooling/Transformer/Stencil.h" 10 #include "clang/AST/ASTTypeTraits.h" 11 #include "clang/AST/Expr.h" 12 #include "clang/ASTMatchers/ASTMatchers.h" 13 #include "clang/Tooling/FixIt.h" 14 #include "clang/Tooling/Tooling.h" 15 #include "llvm/Support/Error.h" 16 #include "llvm/Testing/Support/Error.h" 17 #include "gmock/gmock.h" 18 #include "gtest/gtest.h" 19 20 using namespace clang; 21 using namespace transformer; 22 using namespace ast_matchers; 23 24 namespace { 25 using ::llvm::Failed; 26 using ::llvm::HasValue; 27 using ::llvm::StringError; 28 using ::testing::AllOf; 29 using ::testing::HasSubstr; 30 using MatchResult = MatchFinder::MatchResult; 31 32 // Create a valid translation-unit from a statement. 33 static std::string wrapSnippet(StringRef ExtraPreface, 34 StringRef StatementCode) { 35 constexpr char Preface[] = R"cc( 36 namespace N { class C {}; } 37 namespace { class AnonC {}; } 38 struct S { int Field; }; 39 struct Smart { 40 S* operator->() const; 41 S& operator*() const; 42 }; 43 )cc"; 44 return (Preface + ExtraPreface + "auto stencil_test_snippet = []{" + 45 StatementCode + "};") 46 .str(); 47 } 48 49 static DeclarationMatcher wrapMatcher(const StatementMatcher &Matcher) { 50 return varDecl(hasName("stencil_test_snippet"), 51 hasDescendant(compoundStmt(hasAnySubstatement(Matcher)))); 52 } 53 54 struct TestMatch { 55 // The AST unit from which `result` is built. We bundle it because it backs 56 // the result. Users are not expected to access it. 57 std::unique_ptr<ASTUnit> AstUnit; 58 // The result to use in the test. References `ast_unit`. 59 MatchResult Result; 60 }; 61 62 // Matches `Matcher` against the statement `StatementCode` and returns the 63 // result. Handles putting the statement inside a function and modifying the 64 // matcher correspondingly. `Matcher` should match one of the statements in 65 // `StatementCode` exactly -- that is, produce exactly one match. However, 66 // `StatementCode` may contain other statements not described by `Matcher`. 67 // `ExtraPreface` (optionally) adds extra decls to the TU, before the code. 68 static llvm::Optional<TestMatch> matchStmt(StringRef StatementCode, 69 StatementMatcher Matcher, 70 StringRef ExtraPreface = "") { 71 auto AstUnit = tooling::buildASTFromCodeWithArgs( 72 wrapSnippet(ExtraPreface, StatementCode), {"-Wno-unused-value"}); 73 if (AstUnit == nullptr) { 74 ADD_FAILURE() << "AST construction failed"; 75 return llvm::None; 76 } 77 ASTContext &Context = AstUnit->getASTContext(); 78 auto Matches = ast_matchers::match(wrapMatcher(Matcher), Context); 79 // We expect a single, exact match for the statement. 80 if (Matches.size() != 1) { 81 ADD_FAILURE() << "Wrong number of matches: " << Matches.size(); 82 return llvm::None; 83 } 84 return TestMatch{std::move(AstUnit), MatchResult(Matches[0], &Context)}; 85 } 86 87 class StencilTest : public ::testing::Test { 88 protected: 89 // Verifies that the given stencil fails when evaluated on a valid match 90 // result. Binds a statement to "stmt", a (non-member) ctor-initializer to 91 // "init", an expression to "expr" and a (nameless) declaration to "decl". 92 void testError(const Stencil &Stencil, 93 ::testing::Matcher<std::string> Matcher) { 94 const std::string Snippet = R"cc( 95 struct A {}; 96 class F : public A { 97 public: 98 F(int) {} 99 }; 100 F(1); 101 )cc"; 102 auto StmtMatch = matchStmt( 103 Snippet, 104 stmt(hasDescendant( 105 cxxConstructExpr( 106 hasDeclaration(decl(hasDescendant(cxxCtorInitializer( 107 isBaseInitializer()) 108 .bind("init"))) 109 .bind("decl"))) 110 .bind("expr"))) 111 .bind("stmt")); 112 ASSERT_TRUE(StmtMatch); 113 if (auto ResultOrErr = Stencil->eval(StmtMatch->Result)) { 114 ADD_FAILURE() << "Expected failure but succeeded: " << *ResultOrErr; 115 } else { 116 auto Err = llvm::handleErrors(ResultOrErr.takeError(), 117 [&Matcher](const StringError &Err) { 118 EXPECT_THAT(Err.getMessage(), Matcher); 119 }); 120 if (Err) { 121 ADD_FAILURE() << "Unhandled error: " << llvm::toString(std::move(Err)); 122 } 123 } 124 } 125 126 // Tests failures caused by references to unbound nodes. `unbound_id` is the 127 // id that will cause the failure. 128 void testUnboundNodeError(const Stencil &Stencil, StringRef UnboundId) { 129 testError(Stencil, 130 AllOf(HasSubstr(std::string(UnboundId)), HasSubstr("not bound"))); 131 } 132 }; 133 134 TEST_F(StencilTest, SingleStatement) { 135 StringRef Condition("C"), Then("T"), Else("E"); 136 const std::string Snippet = R"cc( 137 if (true) 138 return 1; 139 else 140 return 0; 141 )cc"; 142 auto StmtMatch = matchStmt( 143 Snippet, ifStmt(hasCondition(expr().bind(Condition)), 144 hasThen(stmt().bind(Then)), hasElse(stmt().bind(Else)))); 145 ASSERT_TRUE(StmtMatch); 146 // Invert the if-then-else. 147 auto Stencil = 148 cat("if (!", node(std::string(Condition)), ") ", 149 statement(std::string(Else)), " else ", statement(std::string(Then))); 150 EXPECT_THAT_EXPECTED(Stencil->eval(StmtMatch->Result), 151 HasValue("if (!true) return 0; else return 1;")); 152 } 153 154 TEST_F(StencilTest, UnboundNode) { 155 const std::string Snippet = R"cc( 156 if (true) 157 return 1; 158 else 159 return 0; 160 )cc"; 161 auto StmtMatch = matchStmt(Snippet, ifStmt(hasCondition(stmt().bind("a1")), 162 hasThen(stmt().bind("a2")))); 163 ASSERT_TRUE(StmtMatch); 164 auto Stencil = cat("if(!", node("a1"), ") ", node("UNBOUND"), ";"); 165 auto ResultOrErr = Stencil->eval(StmtMatch->Result); 166 EXPECT_TRUE(llvm::errorToBool(ResultOrErr.takeError())) 167 << "Expected unbound node, got " << *ResultOrErr; 168 } 169 170 // Tests that a stencil with a single parameter (`Id`) evaluates to the expected 171 // string, when `Id` is bound to the expression-statement in `Snippet`. 172 void testExpr(StringRef Id, StringRef Snippet, const Stencil &Stencil, 173 StringRef Expected) { 174 auto StmtMatch = matchStmt(Snippet, expr().bind(Id)); 175 ASSERT_TRUE(StmtMatch); 176 EXPECT_THAT_EXPECTED(Stencil->eval(StmtMatch->Result), 177 HasValue(std::string(Expected))); 178 } 179 180 void testFailure(StringRef Id, StringRef Snippet, const Stencil &Stencil, 181 testing::Matcher<std::string> MessageMatcher) { 182 auto StmtMatch = matchStmt(Snippet, expr().bind(Id)); 183 ASSERT_TRUE(StmtMatch); 184 EXPECT_THAT_EXPECTED(Stencil->eval(StmtMatch->Result), 185 Failed<StringError>(testing::Property( 186 &StringError::getMessage, MessageMatcher))); 187 } 188 189 TEST_F(StencilTest, SelectionOp) { 190 StringRef Id = "id"; 191 testExpr(Id, "3;", cat(node(std::string(Id))), "3"); 192 } 193 194 TEST_F(StencilTest, IfBoundOpBound) { 195 StringRef Id = "id"; 196 testExpr(Id, "3;", ifBound(Id, cat("5"), cat("7")), "5"); 197 } 198 199 TEST_F(StencilTest, IfBoundOpUnbound) { 200 StringRef Id = "id"; 201 testExpr(Id, "3;", ifBound("other", cat("5"), cat("7")), "7"); 202 } 203 204 TEST_F(StencilTest, ExpressionOpNoParens) { 205 StringRef Id = "id"; 206 testExpr(Id, "3;", expression(Id), "3"); 207 } 208 209 // Don't parenthesize a parens expression. 210 TEST_F(StencilTest, ExpressionOpNoParensParens) { 211 StringRef Id = "id"; 212 testExpr(Id, "(3);", expression(Id), "(3)"); 213 } 214 215 TEST_F(StencilTest, ExpressionOpBinaryOpParens) { 216 StringRef Id = "id"; 217 testExpr(Id, "3+4;", expression(Id), "(3+4)"); 218 } 219 220 // `expression` shares code with other ops, so we get sufficient coverage of the 221 // error handling code with this test. If that changes in the future, more error 222 // tests should be added. 223 TEST_F(StencilTest, ExpressionOpUnbound) { 224 StringRef Id = "id"; 225 testFailure(Id, "3;", expression("ACACA"), 226 AllOf(HasSubstr("ACACA"), HasSubstr("not bound"))); 227 } 228 229 TEST_F(StencilTest, DerefPointer) { 230 StringRef Id = "id"; 231 testExpr(Id, "int *x; x;", deref(Id), "*x"); 232 } 233 234 TEST_F(StencilTest, DerefBinOp) { 235 StringRef Id = "id"; 236 testExpr(Id, "int *x; x + 1;", deref(Id), "*(x + 1)"); 237 } 238 239 TEST_F(StencilTest, DerefAddressExpr) { 240 StringRef Id = "id"; 241 testExpr(Id, "int x; &x;", deref(Id), "x"); 242 } 243 244 TEST_F(StencilTest, AddressOfValue) { 245 StringRef Id = "id"; 246 testExpr(Id, "int x; x;", addressOf(Id), "&x"); 247 } 248 249 TEST_F(StencilTest, AddressOfDerefExpr) { 250 StringRef Id = "id"; 251 testExpr(Id, "int *x; *x;", addressOf(Id), "x"); 252 } 253 254 TEST_F(StencilTest, MaybeDerefValue) { 255 StringRef Id = "id"; 256 testExpr(Id, "int x; x;", maybeDeref(Id), "x"); 257 } 258 259 TEST_F(StencilTest, MaybeDerefPointer) { 260 StringRef Id = "id"; 261 testExpr(Id, "int *x; x;", maybeDeref(Id), "*x"); 262 } 263 264 TEST_F(StencilTest, MaybeDerefBinOp) { 265 StringRef Id = "id"; 266 testExpr(Id, "int *x; x + 1;", maybeDeref(Id), "*(x + 1)"); 267 } 268 269 TEST_F(StencilTest, MaybeDerefAddressExpr) { 270 StringRef Id = "id"; 271 testExpr(Id, "int x; &x;", maybeDeref(Id), "x"); 272 } 273 274 TEST_F(StencilTest, MaybeDerefSmartPointer) { 275 StringRef Id = "id"; 276 std::string Snippet = R"cc( 277 Smart x; 278 x; 279 )cc"; 280 testExpr(Id, Snippet, maybeDeref(Id), "*x"); 281 } 282 283 // Tests that unique_ptr specifically is handled. 284 TEST_F(StencilTest, MaybeDerefSmartPointerUniquePtr) { 285 StringRef Id = "id"; 286 // We deliberately specify `unique_ptr` as empty to verify that it matches 287 // because of its name, rather than its contents. 288 StringRef ExtraPreface = 289 "namespace std { template <typename T> class unique_ptr {}; }\n"; 290 StringRef Snippet = R"cc( 291 std::unique_ptr<int> x; 292 x; 293 )cc"; 294 auto StmtMatch = matchStmt(Snippet, expr().bind(Id), ExtraPreface); 295 ASSERT_TRUE(StmtMatch); 296 EXPECT_THAT_EXPECTED(maybeDeref(Id)->eval(StmtMatch->Result), 297 HasValue(std::string("*x"))); 298 } 299 300 TEST_F(StencilTest, MaybeDerefSmartPointerFromMemberExpr) { 301 StringRef Id = "id"; 302 std::string Snippet = "Smart x; x->Field;"; 303 auto StmtMatch = 304 matchStmt(Snippet, memberExpr(hasObjectExpression(expr().bind(Id)))); 305 ASSERT_TRUE(StmtMatch); 306 const Stencil Stencil = maybeDeref(Id); 307 EXPECT_THAT_EXPECTED(Stencil->eval(StmtMatch->Result), HasValue("*x")); 308 } 309 310 TEST_F(StencilTest, MaybeAddressOfPointer) { 311 StringRef Id = "id"; 312 testExpr(Id, "int *x; x;", maybeAddressOf(Id), "x"); 313 } 314 315 TEST_F(StencilTest, MaybeAddressOfValue) { 316 StringRef Id = "id"; 317 testExpr(Id, "int x; x;", addressOf(Id), "&x"); 318 } 319 320 TEST_F(StencilTest, MaybeAddressOfBinOp) { 321 StringRef Id = "id"; 322 testExpr(Id, "int x; x + 1;", maybeAddressOf(Id), "&(x + 1)"); 323 } 324 325 TEST_F(StencilTest, MaybeAddressOfDerefExpr) { 326 StringRef Id = "id"; 327 testExpr(Id, "int *x; *x;", addressOf(Id), "x"); 328 } 329 330 TEST_F(StencilTest, MaybeAddressOfSmartPointer) { 331 StringRef Id = "id"; 332 testExpr(Id, "Smart x; x;", maybeAddressOf(Id), "x"); 333 } 334 335 TEST_F(StencilTest, MaybeAddressOfSmartPointerFromMemberCall) { 336 StringRef Id = "id"; 337 std::string Snippet = "Smart x; x->Field;"; 338 auto StmtMatch = 339 matchStmt(Snippet, memberExpr(hasObjectExpression(expr().bind(Id)))); 340 ASSERT_TRUE(StmtMatch); 341 const Stencil Stencil = maybeAddressOf(Id); 342 EXPECT_THAT_EXPECTED(Stencil->eval(StmtMatch->Result), HasValue("x")); 343 } 344 345 TEST_F(StencilTest, MaybeAddressOfSmartPointerDerefNoCancel) { 346 StringRef Id = "id"; 347 testExpr(Id, "Smart x; *x;", maybeAddressOf(Id), "&*x"); 348 } 349 350 TEST_F(StencilTest, AccessOpValue) { 351 StringRef Snippet = R"cc( 352 S x; 353 x; 354 )cc"; 355 StringRef Id = "id"; 356 testExpr(Id, Snippet, access(Id, "field"), "x.field"); 357 } 358 359 TEST_F(StencilTest, AccessOpValueExplicitText) { 360 StringRef Snippet = R"cc( 361 S x; 362 x; 363 )cc"; 364 StringRef Id = "id"; 365 testExpr(Id, Snippet, access(Id, cat("field")), "x.field"); 366 } 367 368 TEST_F(StencilTest, AccessOpValueAddress) { 369 StringRef Snippet = R"cc( 370 S x; 371 &x; 372 )cc"; 373 StringRef Id = "id"; 374 testExpr(Id, Snippet, access(Id, "field"), "x.field"); 375 } 376 377 TEST_F(StencilTest, AccessOpPointer) { 378 StringRef Snippet = R"cc( 379 S *x; 380 x; 381 )cc"; 382 StringRef Id = "id"; 383 testExpr(Id, Snippet, access(Id, "field"), "x->field"); 384 } 385 386 TEST_F(StencilTest, AccessOpPointerDereference) { 387 StringRef Snippet = R"cc( 388 S *x; 389 *x; 390 )cc"; 391 StringRef Id = "id"; 392 testExpr(Id, Snippet, access(Id, "field"), "x->field"); 393 } 394 395 TEST_F(StencilTest, AccessOpExplicitThis) { 396 using clang::ast_matchers::hasObjectExpression; 397 using clang::ast_matchers::memberExpr; 398 399 // Set up the code so we can bind to a use of this. 400 StringRef Snippet = R"cc( 401 class C { 402 public: 403 int x; 404 int foo() { return this->x; } 405 }; 406 )cc"; 407 auto StmtMatch = matchStmt( 408 Snippet, 409 traverse(TK_AsIs, returnStmt(hasReturnValue(ignoringImplicit(memberExpr( 410 hasObjectExpression(expr().bind("obj")))))))); 411 ASSERT_TRUE(StmtMatch); 412 const Stencil Stencil = access("obj", "field"); 413 EXPECT_THAT_EXPECTED(Stencil->eval(StmtMatch->Result), 414 HasValue("this->field")); 415 } 416 417 TEST_F(StencilTest, AccessOpImplicitThis) { 418 using clang::ast_matchers::hasObjectExpression; 419 using clang::ast_matchers::memberExpr; 420 421 // Set up the code so we can bind to a use of (implicit) this. 422 StringRef Snippet = R"cc( 423 class C { 424 public: 425 int x; 426 int foo() { return x; } 427 }; 428 )cc"; 429 auto StmtMatch = 430 matchStmt(Snippet, returnStmt(hasReturnValue(ignoringImplicit(memberExpr( 431 hasObjectExpression(expr().bind("obj"))))))); 432 ASSERT_TRUE(StmtMatch); 433 const Stencil Stencil = access("obj", "field"); 434 EXPECT_THAT_EXPECTED(Stencil->eval(StmtMatch->Result), HasValue("field")); 435 } 436 437 TEST_F(StencilTest, DescribeType) { 438 std::string Snippet = "int *x; x;"; 439 std::string Expected = "int *"; 440 auto StmtMatch = 441 matchStmt(Snippet, declRefExpr(hasType(qualType().bind("type")))); 442 ASSERT_TRUE(StmtMatch); 443 EXPECT_THAT_EXPECTED(describe("type")->eval(StmtMatch->Result), 444 HasValue(std::string(Expected))); 445 } 446 447 TEST_F(StencilTest, DescribeSugaredType) { 448 std::string Snippet = "using Ty = int; Ty *x; x;"; 449 std::string Expected = "Ty *"; 450 auto StmtMatch = 451 matchStmt(Snippet, declRefExpr(hasType(qualType().bind("type")))); 452 ASSERT_TRUE(StmtMatch); 453 EXPECT_THAT_EXPECTED(describe("type")->eval(StmtMatch->Result), 454 HasValue(std::string(Expected))); 455 } 456 457 TEST_F(StencilTest, DescribeDeclType) { 458 std::string Snippet = "S s; s;"; 459 std::string Expected = "S"; 460 auto StmtMatch = 461 matchStmt(Snippet, declRefExpr(hasType(qualType().bind("type")))); 462 ASSERT_TRUE(StmtMatch); 463 EXPECT_THAT_EXPECTED(describe("type")->eval(StmtMatch->Result), 464 HasValue(std::string(Expected))); 465 } 466 467 TEST_F(StencilTest, DescribeQualifiedType) { 468 std::string Snippet = "N::C c; c;"; 469 std::string Expected = "N::C"; 470 auto StmtMatch = 471 matchStmt(Snippet, declRefExpr(hasType(qualType().bind("type")))); 472 ASSERT_TRUE(StmtMatch); 473 EXPECT_THAT_EXPECTED(describe("type")->eval(StmtMatch->Result), 474 HasValue(std::string(Expected))); 475 } 476 477 TEST_F(StencilTest, DescribeUnqualifiedType) { 478 std::string Snippet = "using N::C; C c; c;"; 479 std::string Expected = "N::C"; 480 auto StmtMatch = 481 matchStmt(Snippet, declRefExpr(hasType(qualType().bind("type")))); 482 ASSERT_TRUE(StmtMatch); 483 EXPECT_THAT_EXPECTED(describe("type")->eval(StmtMatch->Result), 484 HasValue(std::string(Expected))); 485 } 486 487 TEST_F(StencilTest, DescribeAnonNamespaceType) { 488 std::string Snippet = "AnonC c; c;"; 489 std::string Expected = "(anonymous namespace)::AnonC"; 490 auto StmtMatch = 491 matchStmt(Snippet, declRefExpr(hasType(qualType().bind("type")))); 492 ASSERT_TRUE(StmtMatch); 493 EXPECT_THAT_EXPECTED(describe("type")->eval(StmtMatch->Result), 494 HasValue(std::string(Expected))); 495 } 496 497 TEST_F(StencilTest, RunOp) { 498 StringRef Id = "id"; 499 auto SimpleFn = [Id](const MatchResult &R) { 500 return std::string(R.Nodes.getNodeAs<Stmt>(Id) != nullptr ? "Bound" 501 : "Unbound"); 502 }; 503 testExpr(Id, "3;", run(SimpleFn), "Bound"); 504 } 505 506 TEST_F(StencilTest, CatOfMacroRangeSucceeds) { 507 StringRef Snippet = R"cpp( 508 #define MACRO 3.77 509 double foo(double d); 510 foo(MACRO);)cpp"; 511 512 auto StmtMatch = 513 matchStmt(Snippet, callExpr(callee(functionDecl(hasName("foo"))), 514 argumentCountIs(1), 515 hasArgument(0, expr().bind("arg")))); 516 ASSERT_TRUE(StmtMatch); 517 Stencil S = cat(node("arg")); 518 EXPECT_THAT_EXPECTED(S->eval(StmtMatch->Result), HasValue("MACRO")); 519 } 520 521 TEST_F(StencilTest, CatOfMacroArgRangeSucceeds) { 522 StringRef Snippet = R"cpp( 523 #define MACRO(a, b) a + b 524 MACRO(2, 3);)cpp"; 525 526 auto StmtMatch = 527 matchStmt(Snippet, binaryOperator(hasRHS(expr().bind("rhs")))); 528 ASSERT_TRUE(StmtMatch); 529 Stencil S = cat(node("rhs")); 530 EXPECT_THAT_EXPECTED(S->eval(StmtMatch->Result), HasValue("3")); 531 } 532 533 TEST_F(StencilTest, CatOfMacroArgSubRangeSucceeds) { 534 StringRef Snippet = R"cpp( 535 #define MACRO(a, b) a + b 536 int foo(int); 537 MACRO(2, foo(3));)cpp"; 538 539 auto StmtMatch = matchStmt( 540 Snippet, binaryOperator(hasRHS(callExpr( 541 callee(functionDecl(hasName("foo"))), argumentCountIs(1), 542 hasArgument(0, expr().bind("arg")))))); 543 ASSERT_TRUE(StmtMatch); 544 Stencil S = cat(node("arg")); 545 EXPECT_THAT_EXPECTED(S->eval(StmtMatch->Result), HasValue("3")); 546 } 547 548 TEST_F(StencilTest, CatOfInvalidRangeFails) { 549 StringRef Snippet = R"cpp( 550 #define MACRO (3.77) 551 double foo(double d); 552 foo(MACRO);)cpp"; 553 554 auto StmtMatch = 555 matchStmt(Snippet, callExpr(callee(functionDecl(hasName("foo"))), 556 argumentCountIs(1), 557 hasArgument(0, expr().bind("arg")))); 558 ASSERT_TRUE(StmtMatch); 559 Stencil S = cat(node("arg")); 560 Expected<std::string> Result = S->eval(StmtMatch->Result); 561 ASSERT_THAT_EXPECTED(Result, Failed<StringError>()); 562 llvm::handleAllErrors(Result.takeError(), [](const llvm::StringError &E) { 563 EXPECT_THAT(E.getMessage(), AllOf(HasSubstr("selected range"), 564 HasSubstr("macro expansion"))); 565 }); 566 } 567 568 // The `StencilToStringTest` tests verify that the string representation of the 569 // stencil combinator matches (as best possible) the spelling of the 570 // combinator's construction. Exceptions include those combinators that have no 571 // explicit spelling (like raw text) and those supporting non-printable 572 // arguments (like `run`, `selection`). 573 574 TEST(StencilToStringTest, RawTextOp) { 575 auto S = cat("foo bar baz"); 576 StringRef Expected = R"("foo bar baz")"; 577 EXPECT_EQ(S->toString(), Expected); 578 } 579 580 TEST(StencilToStringTest, RawTextOpEscaping) { 581 auto S = cat("foo \"bar\" baz\\n"); 582 StringRef Expected = R"("foo \"bar\" baz\\n")"; 583 EXPECT_EQ(S->toString(), Expected); 584 } 585 586 TEST(StencilToStringTest, DescribeOp) { 587 auto S = describe("Id"); 588 StringRef Expected = R"repr(describe("Id"))repr"; 589 EXPECT_EQ(S->toString(), Expected); 590 } 591 592 TEST(StencilToStringTest, DebugPrintNodeOp) { 593 auto S = dPrint("Id"); 594 StringRef Expected = R"repr(dPrint("Id"))repr"; 595 EXPECT_EQ(S->toString(), Expected); 596 } 597 598 TEST(StencilToStringTest, ExpressionOp) { 599 auto S = expression("Id"); 600 StringRef Expected = R"repr(expression("Id"))repr"; 601 EXPECT_EQ(S->toString(), Expected); 602 } 603 604 TEST(StencilToStringTest, DerefOp) { 605 auto S = deref("Id"); 606 StringRef Expected = R"repr(deref("Id"))repr"; 607 EXPECT_EQ(S->toString(), Expected); 608 } 609 610 TEST(StencilToStringTest, AddressOfOp) { 611 auto S = addressOf("Id"); 612 StringRef Expected = R"repr(addressOf("Id"))repr"; 613 EXPECT_EQ(S->toString(), Expected); 614 } 615 616 TEST(StencilToStringTest, SelectionOp) { 617 auto S1 = cat(node("node1")); 618 EXPECT_EQ(S1->toString(), "selection(...)"); 619 } 620 621 TEST(StencilToStringTest, AccessOpText) { 622 auto S = access("Id", "memberData"); 623 StringRef Expected = R"repr(access("Id", "memberData"))repr"; 624 EXPECT_EQ(S->toString(), Expected); 625 } 626 627 TEST(StencilToStringTest, AccessOpSelector) { 628 auto S = access("Id", cat(name("otherId"))); 629 StringRef Expected = R"repr(access("Id", selection(...)))repr"; 630 EXPECT_EQ(S->toString(), Expected); 631 } 632 633 TEST(StencilToStringTest, AccessOpStencil) { 634 auto S = access("Id", cat("foo_", "bar")); 635 StringRef Expected = R"repr(access("Id", seq("foo_", "bar")))repr"; 636 EXPECT_EQ(S->toString(), Expected); 637 } 638 639 TEST(StencilToStringTest, IfBoundOp) { 640 auto S = ifBound("Id", cat("trueText"), access("exprId", "memberData")); 641 StringRef Expected = 642 R"repr(ifBound("Id", "trueText", access("exprId", "memberData")))repr"; 643 EXPECT_EQ(S->toString(), Expected); 644 } 645 646 TEST(StencilToStringTest, RunOp) { 647 auto F1 = [](const MatchResult &R) { return "foo"; }; 648 auto S1 = run(F1); 649 EXPECT_EQ(S1->toString(), "run(...)"); 650 } 651 652 TEST(StencilToStringTest, Sequence) { 653 auto S = cat("foo", access("x", "m()"), "bar", 654 ifBound("x", cat("t"), access("e", "f"))); 655 StringRef Expected = R"repr(seq("foo", access("x", "m()"), "bar", )repr" 656 R"repr(ifBound("x", "t", access("e", "f"))))repr"; 657 EXPECT_EQ(S->toString(), Expected); 658 } 659 660 TEST(StencilToStringTest, SequenceEmpty) { 661 auto S = cat(); 662 StringRef Expected = "seq()"; 663 EXPECT_EQ(S->toString(), Expected); 664 } 665 666 TEST(StencilToStringTest, SequenceSingle) { 667 auto S = cat("foo"); 668 StringRef Expected = "\"foo\""; 669 EXPECT_EQ(S->toString(), Expected); 670 } 671 672 TEST(StencilToStringTest, SequenceFromVector) { 673 auto S = catVector({cat("foo"), access("x", "m()"), cat("bar"), 674 ifBound("x", cat("t"), access("e", "f"))}); 675 StringRef Expected = R"repr(seq("foo", access("x", "m()"), "bar", )repr" 676 R"repr(ifBound("x", "t", access("e", "f"))))repr"; 677 EXPECT_EQ(S->toString(), Expected); 678 } 679 } // namespace 680