diff options
| author | Eric Liu <ioeric@google.com> | 2016-09-14 13:04:51 +0000 |
|---|---|---|
| committer | Eric Liu <ioeric@google.com> | 2016-09-14 13:04:51 +0000 |
| commit | 8933d16ef8df7e334b51a66c908cf1681fbe9005 (patch) | |
| tree | fe5b4e7a092db2b595da7d49327ac4b1f7311c4f /unittests/Tooling/RefactoringTest.cpp | |
| parent | b19f8fa7328ea6963987cc52ecf46cde361cb181 (diff) | |
| download | clang-8933d16ef8df7e334b51a66c908cf1681fbe9005.tar.gz | |
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
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@281457 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/Tooling/RefactoringTest.cpp')
| -rw-r--r-- | unittests/Tooling/RefactoringTest.cpp | 77 |
1 files changed, 72 insertions, 5 deletions
diff --git a/unittests/Tooling/RefactoringTest.cpp b/unittests/Tooling/RefactoringTest.cpp index d5877ac2f9..acec55642c 100644 --- a/unittests/Tooling/RefactoringTest.cpp +++ b/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"); |
