diff options
| author | Eric Liu <ioeric@google.com> | 2016-03-29 16:31:53 +0000 |
|---|---|---|
| committer | Eric Liu <ioeric@google.com> | 2016-03-29 16:31:53 +0000 |
| commit | 6a4597fa13445b50f309fbf1f3ffaa4319a7fbb4 (patch) | |
| tree | 1b31ed104cfe78420c978889bf2b5d33851db8d6 /unittests/Tooling/RefactoringTest.cpp | |
| parent | bdbac4893805e6d92539aef918fff0e18833596e (diff) | |
| download | clang-6a4597fa13445b50f309fbf1f3ffaa4319a7fbb4.tar.gz | |
Added formatAndApplyAllReplacements that works on multiple files in libTooling.
Summary:
formatAndApplyAllReplacements takes a set of Replacements, applies them on a
Rewriter, and reformats the changed code.
Reviewers: klimek, djasper
Subscribers: ioeric, klimek, cfe-commits
Differential Revision: http://reviews.llvm.org/D17852
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@264745 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/Tooling/RefactoringTest.cpp')
| -rw-r--r-- | unittests/Tooling/RefactoringTest.cpp | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/unittests/Tooling/RefactoringTest.cpp b/unittests/Tooling/RefactoringTest.cpp index 73da5a82cb..e89fea5913 100644 --- a/unittests/Tooling/RefactoringTest.cpp +++ b/unittests/Tooling/RefactoringTest.cpp @@ -18,6 +18,7 @@ #include "clang/Basic/FileManager.h" #include "clang/Basic/LangOptions.h" #include "clang/Basic/SourceManager.h" +#include "clang/Format/Format.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/TextDiagnosticPrinter.h" @@ -166,6 +167,39 @@ TEST_F(ReplacementTest, ApplyAllFailsIfOneApplyFails) { EXPECT_EQ("z", Context.getRewrittenText(IDz)); } +TEST_F(ReplacementTest, MultipleFilesReplaceAndFormat) { + // Column limit is 20. + std::string Code1 = "Long *a =\n" + " new Long();\n" + "long x = 1;"; + std::string Expected1 = "auto a = new Long();\n" + "long x =\n" + " 12345678901;"; + std::string Code2 = "int x = 123;\n" + "int y = 0;"; + std::string Expected2 = "int x =\n" + " 1234567890123;\n" + "int y = 10;"; + FileID ID1 = Context.createInMemoryFile("format_1.cpp", Code1); + FileID ID2 = Context.createInMemoryFile("format_2.cpp", Code2); + + tooling::Replacements Replaces; + // Scrambled the order of replacements. + Replaces.insert(tooling::Replacement( + Context.Sources, Context.getLocation(ID2, 1, 12), 0, "4567890123")); + Replaces.insert(tooling::Replacement( + Context.Sources, Context.getLocation(ID1, 1, 1), 6, "auto ")); + Replaces.insert(tooling::Replacement( + Context.Sources, Context.getLocation(ID2, 2, 9), 1, "10")); + Replaces.insert(tooling::Replacement( + Context.Sources, Context.getLocation(ID1, 3, 10), 1, "12345678901")); + + EXPECT_TRUE(formatAndApplyAllReplacements( + Replaces, Context.Rewrite, "{BasedOnStyle: LLVM, ColumnLimit: 20}")); + EXPECT_EQ(Expected1, Context.getRewrittenText(ID1)); + EXPECT_EQ(Expected2, Context.getRewrittenText(ID2)); +} + TEST(ShiftedCodePositionTest, FindsNewCodePosition) { Replacements Replaces; Replaces.insert(Replacement("", 0, 1, "")); @@ -426,7 +460,7 @@ TEST(Range, CalculateRangesOfReplacements) { Replaces.insert(Replacement("foo", 10, 1, "zzzzzz")); Replaces.insert(Replacement("foo", 11, 0, "oooooooooooooooo")); - std::vector<Range> Ranges = calculateChangedRangesInFile(Replaces); + std::vector<Range> Ranges = calculateChangedRanges(Replaces); EXPECT_EQ(3ul, Ranges.size()); EXPECT_TRUE(Ranges[0].getOffset() == 0); |
