From e04799fbe3ec281dc21aa1ae1d22e55426144f61 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Tue, 7 May 2019 07:45:41 +0000 Subject: [PATCH] [clangd] Add test that r360116 accidentally fixed a duplicate-edits bug in rename. NFC llvm-svn: 360118 --- clang-tools-extra/clangd/refactor/Rename.cpp | 2 + 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 a260458..98d6fd4 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 4816412..f7148c1 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 -- 2.7.4