blob: bb9f504a0372cccea40c21ea7dd0d5b0cb6f7eb0 [file] [log] [blame]
Daniel Jasperfe7beeb2012-07-16 09:18:17 +00001//===- unittest/ASTMatchers/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
10#include "clang/ASTMatchers/ASTMatchers.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/RefactoringCallbacks.h"
13#include "../Tooling/RewriterTestContext.h"
14#include "gtest/gtest.h"
15
16namespace clang {
17namespace ast_matchers {
18
19template <typename T>
20void expectRewritten(const std::string &Code,
21 const std::string &Expected,
22 const T &AMatcher,
23 RefactoringCallback &Callback) {
24 MatchFinder Finder;
25 Finder.addMatcher(AMatcher, &Callback);
26 OwningPtr<tooling::FrontendActionFactory> Factory(
27 tooling::newFrontendActionFactory(&Finder));
28 ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), Code))
29 << "Parsing error in \"" << Code << "\"";
30 RewriterTestContext Context;
31 FileID ID = Context.createInMemoryFile("input.cc", Code);
32 EXPECT_TRUE(tooling::applyAllReplacements(Callback.getReplacements(),
33 Context.Rewrite));
34 EXPECT_EQ(Expected, Context.getRewrittenText(ID));
35}
36
37TEST(RefactoringCallbacksTest, ReplacesStmtsWithString) {
38 std::string Code = "void f() { int i = 1; }";
39 std::string Expected = "void f() { ; }";
40 ReplaceStmtWithText Callback("id", ";");
41 expectRewritten(Code, Expected, id("id", declarationStatement()), Callback);
42}
43
44TEST(RefactoringCallbacksTest, ReplacesStmtsInCalledMacros) {
45 std::string Code = "#define A void f() { int i = 1; }\nA";
46 std::string Expected = "#define A void f() { ; }\nA";
47 ReplaceStmtWithText Callback("id", ";");
48 expectRewritten(Code, Expected, id("id", declarationStatement()), Callback);
49}
50
51TEST(RefactoringCallbacksTest, IgnoresStmtsInUncalledMacros) {
52 std::string Code = "#define A void f() { int i = 1; }";
53 std::string Expected = "#define A void f() { int i = 1; }";
54 ReplaceStmtWithText Callback("id", ";");
55 expectRewritten(Code, Expected, id("id", declarationStatement()), Callback);
56}
57
58TEST(RefactoringCallbacksTest, ReplacesInteger) {
59 std::string Code = "void f() { int i = 1; }";
60 std::string Expected = "void f() { int i = 2; }";
61 ReplaceStmtWithText Callback("id", "2");
62 expectRewritten(Code, Expected, id("id", expression(integerLiteral())),
63 Callback);
64}
65
66TEST(RefactoringCallbacksTest, ReplacesStmtWithStmt) {
67 std::string Code = "void f() { int i = false ? 1 : i * 2; }";
68 std::string Expected = "void f() { int i = i * 2; }";
69 ReplaceStmtWithStmt Callback("always-false", "should-be");
70 expectRewritten(Code, Expected,
71 id("always-false", conditionalOperator(
72 hasCondition(boolLiteral(equals(false))),
73 hasFalseExpression(id("should-be", expression())))),
74 Callback);
75}
76
77TEST(RefactoringCallbacksTest, ReplacesIfStmt) {
78 std::string Code = "bool a; void f() { if (a) f(); else a = true; }";
79 std::string Expected = "bool a; void f() { f(); }";
80 ReplaceIfStmtWithItsBody Callback("id", true);
81 expectRewritten(Code, Expected,
82 id("id", ifStmt(
83 hasCondition(implicitCast(hasSourceExpression(
84 declarationReference(to(variable(hasName("a"))))))))),
85 Callback);
86}
87
88TEST(RefactoringCallbacksTest, RemovesEntireIfOnEmptyElse) {
89 std::string Code = "void f() { if (false) int i = 0; }";
90 std::string Expected = "void f() { }";
91 ReplaceIfStmtWithItsBody Callback("id", false);
92 expectRewritten(Code, Expected,
93 id("id", ifStmt(hasCondition(boolLiteral(equals(false))))),
94 Callback);
95}
96
97} // end namespace ast_matchers
98} // end namespace clang