blob: ad8aa8f98febcf9f65fc600394b46291ea51a73b [file] [log] [blame]
Chandler Carruth320d9662012-12-04 09:45:34 +00001//===- unittest/Tooling/RefactoringCallbacksTest.cpp ----------------------===//
Daniel Jasper7e222822012-07-16 09:18:17 +00002//
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
Daniel Jasper1975e032012-07-17 08:03:01 +000010#include "clang/Tooling/RefactoringCallbacks.h"
Daniel Jasper1975e032012-07-17 08:03:01 +000011#include "RewriterTestContext.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
Chandler Carruthfa0b3bb2012-12-04 09:53:37 +000013#include "clang/ASTMatchers/ASTMatchers.h"
Douglas Gregor949cc502012-10-23 23:13:50 +000014#include "gtest/gtest.h"
Daniel Jasper7e222822012-07-16 09:18:17 +000015
16namespace clang {
Daniel Jasper6389dd12012-07-17 08:37:03 +000017namespace tooling {
18
19using namespace ast_matchers;
Daniel Jasper7e222822012-07-16 09:18:17 +000020
21template <typename T>
22void expectRewritten(const std::string &Code,
23 const std::string &Expected,
24 const T &AMatcher,
25 RefactoringCallback &Callback) {
26 MatchFinder Finder;
27 Finder.addMatcher(AMatcher, &Callback);
Ahmed Charlesb8984322014-03-07 20:03:18 +000028 std::unique_ptr<tooling::FrontendActionFactory> Factory(
Daniel Jasper7e222822012-07-16 09:18:17 +000029 tooling::newFrontendActionFactory(&Finder));
30 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), Code))
31 << "Parsing error in \"" << Code << "\"";
32 RewriterTestContext Context;
33 FileID ID = Context.createInMemoryFile("input.cc", Code);
34 EXPECT_TRUE(tooling::applyAllReplacements(Callback.getReplacements(),
35 Context.Rewrite));
36 EXPECT_EQ(Expected, Context.getRewrittenText(ID));
37}
38
39TEST(RefactoringCallbacksTest, ReplacesStmtsWithString) {
40 std::string Code = "void f() { int i = 1; }";
41 std::string Expected = "void f() { ; }";
42 ReplaceStmtWithText Callback("id", ";");
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000043 expectRewritten(Code, Expected, id("id", declStmt()), Callback);
Daniel Jasper7e222822012-07-16 09:18:17 +000044}
45
46TEST(RefactoringCallbacksTest, ReplacesStmtsInCalledMacros) {
47 std::string Code = "#define A void f() { int i = 1; }\nA";
48 std::string Expected = "#define A void f() { ; }\nA";
49 ReplaceStmtWithText Callback("id", ";");
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000050 expectRewritten(Code, Expected, id("id", declStmt()), Callback);
Daniel Jasper7e222822012-07-16 09:18:17 +000051}
52
53TEST(RefactoringCallbacksTest, IgnoresStmtsInUncalledMacros) {
54 std::string Code = "#define A void f() { int i = 1; }";
55 std::string Expected = "#define A void f() { int i = 1; }";
56 ReplaceStmtWithText Callback("id", ";");
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000057 expectRewritten(Code, Expected, id("id", declStmt()), Callback);
Daniel Jasper7e222822012-07-16 09:18:17 +000058}
59
60TEST(RefactoringCallbacksTest, ReplacesInteger) {
61 std::string Code = "void f() { int i = 1; }";
62 std::string Expected = "void f() { int i = 2; }";
63 ReplaceStmtWithText Callback("id", "2");
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000064 expectRewritten(Code, Expected, id("id", expr(integerLiteral())),
Daniel Jasper7e222822012-07-16 09:18:17 +000065 Callback);
66}
67
68TEST(RefactoringCallbacksTest, ReplacesStmtWithStmt) {
69 std::string Code = "void f() { int i = false ? 1 : i * 2; }";
70 std::string Expected = "void f() { int i = i * 2; }";
71 ReplaceStmtWithStmt Callback("always-false", "should-be");
72 expectRewritten(Code, Expected,
73 id("always-false", conditionalOperator(
Aaron Ballman512fb642015-09-17 13:30:52 +000074 hasCondition(cxxBoolLiteral(equals(false))),
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000075 hasFalseExpression(id("should-be", expr())))),
Daniel Jasper7e222822012-07-16 09:18:17 +000076 Callback);
77}
78
79TEST(RefactoringCallbacksTest, ReplacesIfStmt) {
80 std::string Code = "bool a; void f() { if (a) f(); else a = true; }";
81 std::string Expected = "bool a; void f() { f(); }";
82 ReplaceIfStmtWithItsBody Callback("id", true);
83 expectRewritten(Code, Expected,
84 id("id", ifStmt(
Daniel Jasperbd3d76d2012-08-24 05:12:34 +000085 hasCondition(implicitCastExpr(hasSourceExpression(
86 declRefExpr(to(varDecl(hasName("a"))))))))),
Daniel Jasper7e222822012-07-16 09:18:17 +000087 Callback);
88}
89
90TEST(RefactoringCallbacksTest, RemovesEntireIfOnEmptyElse) {
91 std::string Code = "void f() { if (false) int i = 0; }";
92 std::string Expected = "void f() { }";
93 ReplaceIfStmtWithItsBody Callback("id", false);
94 expectRewritten(Code, Expected,
Aaron Ballman512fb642015-09-17 13:30:52 +000095 id("id", ifStmt(hasCondition(cxxBoolLiteral(equals(false))))),
Daniel Jasper7e222822012-07-16 09:18:17 +000096 Callback);
97}
98
Alexander Kornienkoab9db512015-06-22 23:07:51 +000099} // end namespace ast_matchers
Daniel Jasper7e222822012-07-16 09:18:17 +0000100} // end namespace clang