RefactoringCallbacksTest.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //===- unittest/Tooling/RefactoringCallbacksTest.cpp ----------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/Tooling/RefactoringCallbacks.h"
  10. #include "RewriterTestContext.h"
  11. #include "clang/ASTMatchers/ASTMatchFinder.h"
  12. #include "clang/ASTMatchers/ASTMatchers.h"
  13. #include "gtest/gtest.h"
  14. namespace clang {
  15. namespace tooling {
  16. using namespace ast_matchers;
  17. template <typename T>
  18. void expectRewritten(const std::string &Code,
  19. const std::string &Expected,
  20. const T &AMatcher,
  21. RefactoringCallback &Callback) {
  22. MatchFinder Finder;
  23. Finder.addMatcher(AMatcher, &Callback);
  24. std::unique_ptr<tooling::FrontendActionFactory> Factory(
  25. tooling::newFrontendActionFactory(&Finder));
  26. ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), Code))
  27. << "Parsing error in \"" << Code << "\"";
  28. RewriterTestContext Context;
  29. FileID ID = Context.createInMemoryFile("input.cc", Code);
  30. EXPECT_TRUE(tooling::applyAllReplacements(Callback.getReplacements(),
  31. Context.Rewrite));
  32. EXPECT_EQ(Expected, Context.getRewrittenText(ID));
  33. }
  34. TEST(RefactoringCallbacksTest, ReplacesStmtsWithString) {
  35. std::string Code = "void f() { int i = 1; }";
  36. std::string Expected = "void f() { ; }";
  37. ReplaceStmtWithText Callback("id", ";");
  38. expectRewritten(Code, Expected, id("id", declStmt()), Callback);
  39. }
  40. TEST(RefactoringCallbacksTest, ReplacesStmtsInCalledMacros) {
  41. std::string Code = "#define A void f() { int i = 1; }\nA";
  42. std::string Expected = "#define A void f() { ; }\nA";
  43. ReplaceStmtWithText Callback("id", ";");
  44. expectRewritten(Code, Expected, id("id", declStmt()), Callback);
  45. }
  46. TEST(RefactoringCallbacksTest, IgnoresStmtsInUncalledMacros) {
  47. std::string Code = "#define A void f() { int i = 1; }";
  48. std::string Expected = "#define A void f() { int i = 1; }";
  49. ReplaceStmtWithText Callback("id", ";");
  50. expectRewritten(Code, Expected, id("id", declStmt()), Callback);
  51. }
  52. TEST(RefactoringCallbacksTest, ReplacesInteger) {
  53. std::string Code = "void f() { int i = 1; }";
  54. std::string Expected = "void f() { int i = 2; }";
  55. ReplaceStmtWithText Callback("id", "2");
  56. expectRewritten(Code, Expected, id("id", expr(integerLiteral())),
  57. Callback);
  58. }
  59. TEST(RefactoringCallbacksTest, ReplacesStmtWithStmt) {
  60. std::string Code = "void f() { int i = false ? 1 : i * 2; }";
  61. std::string Expected = "void f() { int i = i * 2; }";
  62. ReplaceStmtWithStmt Callback("always-false", "should-be");
  63. expectRewritten(Code, Expected,
  64. id("always-false", conditionalOperator(
  65. hasCondition(boolLiteral(equals(false))),
  66. hasFalseExpression(id("should-be", expr())))),
  67. Callback);
  68. }
  69. TEST(RefactoringCallbacksTest, ReplacesIfStmt) {
  70. std::string Code = "bool a; void f() { if (a) f(); else a = true; }";
  71. std::string Expected = "bool a; void f() { f(); }";
  72. ReplaceIfStmtWithItsBody Callback("id", true);
  73. expectRewritten(Code, Expected,
  74. id("id", ifStmt(
  75. hasCondition(implicitCastExpr(hasSourceExpression(
  76. declRefExpr(to(varDecl(hasName("a"))))))))),
  77. Callback);
  78. }
  79. TEST(RefactoringCallbacksTest, RemovesEntireIfOnEmptyElse) {
  80. std::string Code = "void f() { if (false) int i = 0; }";
  81. std::string Expected = "void f() { }";
  82. ReplaceIfStmtWithItsBody Callback("id", false);
  83. expectRewritten(Code, Expected,
  84. id("id", ifStmt(hasCondition(boolLiteral(equals(false))))),
  85. Callback);
  86. }
  87. } // end namespace ast_matchers
  88. } // end namespace clang