diff options
author | Sam McCall <sam.mccall@gmail.com> | 2019-05-07 07:45:41 +0000 |
---|---|---|
committer | Sam McCall <sam.mccall@gmail.com> | 2019-05-07 07:45:41 +0000 |
commit | e04799fbe3ec281dc21aa1ae1d22e55426144f61 (patch) | |
tree | 8c4e27ffd2e1fb422644046d71e8b3f5339ebb4d | |
parent | b30657938ce943a786de8b7dbb829c11003b74f5 (diff) | |
download | bcm5719-llvm-e04799fbe3ec281dc21aa1ae1d22e55426144f61.tar.gz bcm5719-llvm-e04799fbe3ec281dc21aa1ae1d22e55426144f61.zip |
[clangd] Add test that r360116 accidentally fixed a duplicate-edits bug in rename. NFC
llvm-svn: 360118
-rw-r--r-- | clang-tools-extra/clangd/refactor/Rename.cpp | 2 | ||||
-rw-r--r-- | clang-tools-extra/clangd/unittests/RenameTests.cpp | 85 |
2 files changed, 67 insertions, 20 deletions
diff --git a/clang-tools-extra/clangd/refactor/Rename.cpp b/clang-tools-extra/clangd/refactor/Rename.cpp index a2604582192..98d6fd488bb 100644 --- a/clang-tools-extra/clangd/refactor/Rename.cpp +++ b/clang-tools-extra/clangd/refactor/Rename.cpp @@ -66,6 +66,8 @@ renameWithinFile(ParsedAST &AST, llvm::StringRef File, Position Pos, // Right now we only support renaming the main file, so we // drop replacements not for the main file. In the future, we might // also support rename with wider scope. + // Rename sometimes returns duplicate edits (which is a bug). A side-effect of + // adding them to a single Replacements object is these are deduplicated. for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) { for (const auto &Rep : Change.getReplacements()) { if (Rep.getFilePath() == File) diff --git a/clang-tools-extra/clangd/unittests/RenameTests.cpp b/clang-tools-extra/clangd/unittests/RenameTests.cpp index 48164121bc5..f7148c13f4a 100644 --- a/clang-tools-extra/clangd/unittests/RenameTests.cpp +++ b/clang-tools-extra/clangd/unittests/RenameTests.cpp @@ -19,27 +19,72 @@ namespace clangd { namespace { TEST(RenameTest, SingleFile) { - Annotations Code(R"cpp( - void foo() { - fo^o(); - } - )cpp"); - auto TU = TestTU::withCode(Code.code()); - TU.HeaderCode = "void foo();"; // outside main file, will not be touched. + struct Test { + const char* Before; + const char* After; + } Tests[] = { + // Rename function. + { + R"cpp( + void foo() { + fo^o(); + } + )cpp", + R"cpp( + void abcde() { + abcde(); + } + )cpp", + }, + // Rename type. + { + R"cpp( + struct foo{}; + foo test() { + f^oo x; + return x; + } + )cpp", + R"cpp( + struct abcde{}; + abcde test() { + abcde x; + return x; + } + )cpp", + }, + // Rename variable. + { + R"cpp( + void bar() { + if (auto ^foo = 5) { + foo = 3; + } + } + )cpp", + R"cpp( + void bar() { + if (auto abcde = 5) { + abcde = 3; + } + } + )cpp", + }, + }; + for (const Test &T : Tests) { + Annotations Code(T.Before); + auto TU = TestTU::withCode(Code.code()); + TU.HeaderCode = "void foo();"; // outside main file, will not be touched. + auto AST = TU.build(); + auto RenameResult = + renameWithinFile(AST, testPath(TU.Filename), Code.point(), "abcde"); + ASSERT_TRUE(bool(RenameResult)) << RenameResult.takeError(); + auto ApplyResult = + tooling::applyAllReplacements(Code.code(), *RenameResult); + ASSERT_TRUE(bool(ApplyResult)) << ApplyResult.takeError(); - auto AST = TU.build(); - auto RenameResult = - renameWithinFile(AST, testPath(TU.Filename), Code.point(), "abcde"); - ASSERT_TRUE(bool(RenameResult)) << RenameResult.takeError(); - auto ApplyResult = tooling::applyAllReplacements(Code.code(), *RenameResult); - ASSERT_TRUE(bool(ApplyResult)) << ApplyResult.takeError(); - - const char *Want = R"cpp( - void abcde() { - abcde(); - } - )cpp"; - EXPECT_EQ(Want, *ApplyResult); + EXPECT_EQ(T.After, *ApplyResult) << T.Before; + } } } // namespace |