aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Tooling/RefactoringTest.cpp
diff options
context:
space:
mode:
authorEric Liu <ioeric@google.com>2016-09-14 13:04:51 +0000
committerEric Liu <ioeric@google.com>2016-09-14 13:04:51 +0000
commitac73ea34a474f578edde75ef4aaa243c8d2847d0 (patch)
tree84a124337368b75694ed672851c8a291690c4608 /clang/unittests/Tooling/RefactoringTest.cpp
parentc2ed91fc4e38364c4c67e2ad5915783e01399476 (diff)
downloadllvm-ac73ea34a474f578edde75ef4aaa243c8d2847d0.zip
llvm-ac73ea34a474f578edde75ef4aaa243c8d2847d0.tar.gz
llvm-ac73ea34a474f578edde75ef4aaa243c8d2847d0.tar.bz2
Supports adding insertion around non-insertion replacements.
Summary: Extend `tooling::Replacements::add()` to support adding order-independent replacements. Two replacements are considered order-independent if one of the following conditions is true: - They do not overlap. (This is already supported.) - One replacement is insertion, and the other is a replacement with length > 0, and the insertion is adjecent to but not contained in the other replacement. In this case, the replacement should always change the original code instead of the inserted text. Reviewers: klimek, djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D24515 llvm-svn: 281457
Diffstat (limited to 'clang/unittests/Tooling/RefactoringTest.cpp')
-rw-r--r--clang/unittests/Tooling/RefactoringTest.cpp77
1 files changed, 72 insertions, 5 deletions
diff --git a/clang/unittests/Tooling/RefactoringTest.cpp b/clang/unittests/Tooling/RefactoringTest.cpp
index d5877ac..acec556 100644
--- a/clang/unittests/Tooling/RefactoringTest.cpp
+++ b/clang/unittests/Tooling/RefactoringTest.cpp
@@ -115,15 +115,16 @@ TEST_F(ReplacementTest, FailAddReplacements) {
llvm::consumeError(std::move(Err));
}
-TEST_F(ReplacementTest, FailAddOverlappingInsertions) {
+TEST_F(ReplacementTest, AddAdjacentInsertionAndReplacement) {
Replacements Replaces;
// Test adding an insertion at the offset of an existing replacement.
auto Err = Replaces.add(Replacement("x.cc", 10, 3, "replace"));
EXPECT_TRUE(!Err);
llvm::consumeError(std::move(Err));
Err = Replaces.add(Replacement("x.cc", 10, 0, "insert"));
- EXPECT_TRUE((bool)Err);
+ EXPECT_TRUE(!Err);
llvm::consumeError(std::move(Err));
+ EXPECT_EQ(Replaces.size(), 2u);
Replaces.clear();
// Test overlap with an existing insertion.
@@ -131,8 +132,9 @@ TEST_F(ReplacementTest, FailAddOverlappingInsertions) {
EXPECT_TRUE(!Err);
llvm::consumeError(std::move(Err));
Err = Replaces.add(Replacement("x.cc", 10, 3, "replace"));
- EXPECT_TRUE((bool)Err);
+ EXPECT_TRUE(!Err);
llvm::consumeError(std::move(Err));
+ EXPECT_EQ(Replaces.size(), 2u);
}
TEST_F(ReplacementTest, FailAddRegression) {
@@ -157,14 +159,24 @@ TEST_F(ReplacementTest, FailAddRegression) {
llvm::consumeError(std::move(Err));
}
-TEST_F(ReplacementTest, FailAddInsertAtOffsetOfReplacement) {
+TEST_F(ReplacementTest, InsertAtOffsetOfReplacement) {
Replacements Replaces;
auto Err = Replaces.add(Replacement("x.cc", 10, 2, ""));
EXPECT_TRUE(!Err);
llvm::consumeError(std::move(Err));
Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
- EXPECT_TRUE((bool)Err);
+ EXPECT_TRUE(!Err);
+ llvm::consumeError(std::move(Err));
+ EXPECT_EQ(Replaces.size(), 2u);
+
+ Replaces.clear();
+ Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
+ EXPECT_TRUE(!Err);
+ llvm::consumeError(std::move(Err));
+ Err = Replaces.add(Replacement("x.cc", 10, 2, ""));
+ EXPECT_TRUE(!Err);
llvm::consumeError(std::move(Err));
+ EXPECT_EQ(Replaces.size(), 2u);
}
TEST_F(ReplacementTest, FailAddInsertAtOtherInsert) {
@@ -175,6 +187,38 @@ TEST_F(ReplacementTest, FailAddInsertAtOtherInsert) {
Err = Replaces.add(Replacement("x.cc", 10, 0, "b"));
EXPECT_TRUE((bool)Err);
llvm::consumeError(std::move(Err));
+
+ Replaces.clear();
+ Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
+ EXPECT_TRUE(!Err);
+ llvm::consumeError(std::move(Err));
+ Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
+ EXPECT_TRUE((bool)Err);
+ llvm::consumeError(std::move(Err));
+
+ Replaces.clear();
+ Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
+ EXPECT_TRUE(!Err);
+ llvm::consumeError(std::move(Err));
+ Err = Replaces.add(Replacement("x.cc", 10, 3, ""));
+ EXPECT_TRUE(!Err);
+ llvm::consumeError(std::move(Err));
+ Err = Replaces.add(Replacement("x.cc", 10, 0, ""));
+ EXPECT_TRUE((bool)Err);
+ llvm::consumeError(std::move(Err));
+}
+
+TEST_F(ReplacementTest, InsertBetweenAdjacentReplacements) {
+ Replacements Replaces;
+ auto Err = Replaces.add(Replacement("x.cc", 10, 5, "a"));
+ EXPECT_TRUE(!Err);
+ llvm::consumeError(std::move(Err));
+ Err = Replaces.add(Replacement("x.cc", 8, 2, "a"));
+ EXPECT_TRUE(!Err);
+ llvm::consumeError(std::move(Err));
+ Err = Replaces.add(Replacement("x.cc", 10, 0, "b"));
+ EXPECT_TRUE(!Err);
+ llvm::consumeError(std::move(Err));
}
TEST_F(ReplacementTest, CanApplyReplacements) {
@@ -189,6 +233,29 @@ TEST_F(ReplacementTest, CanApplyReplacements) {
EXPECT_EQ("line1\nreplaced\nother\nline4", Context.getRewrittenText(ID));
}
+// Verifies that replacement/deletion is applied before insertion at the same
+// offset.
+TEST_F(ReplacementTest, InsertAndDelete) {
+ FileID ID = Context.createInMemoryFile("input.cpp",
+ "line1\nline2\nline3\nline4");
+ Replacements Replaces = toReplacements(
+ {Replacement(Context.Sources, Context.getLocation(ID, 2, 1), 6, ""),
+ Replacement(Context.Sources, Context.getLocation(ID, 2, 1), 0,
+ "other\n")});
+ EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite));
+ EXPECT_EQ("line1\nother\nline3\nline4", Context.getRewrittenText(ID));
+}
+
+TEST_F(ReplacementTest, AdjacentReplacements) {
+ FileID ID = Context.createInMemoryFile("input.cpp",
+ "ab");
+ Replacements Replaces = toReplacements(
+ {Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 1, "x"),
+ Replacement(Context.Sources, Context.getLocation(ID, 1, 2), 1, "y")});
+ EXPECT_TRUE(applyAllReplacements(Replaces, Context.Rewrite));
+ EXPECT_EQ("xy", Context.getRewrittenText(ID));
+}
+
TEST_F(ReplacementTest, SkipsDuplicateReplacements) {
FileID ID = Context.createInMemoryFile("input.cpp",
"line1\nline2\nline3\nline4");