diff options
author | Manuel Klimek <klimek@google.com> | 2016-02-29 16:27:41 +0000 |
---|---|---|
committer | Manuel Klimek <klimek@google.com> | 2016-02-29 16:27:41 +0000 |
commit | 4823631af475410cb7c2c47af28ed010bf956d45 (patch) | |
tree | ccd1bb28d24e030a501bca4f08f5a3a8bcd5d7a2 /clang/unittests/Tooling/RefactoringTest.cpp | |
parent | 03a8d2f8ecdbfd531d712e4b0ca0b66c091405db (diff) | |
download | bcm5719-llvm-4823631af475410cb7c2c47af28ed010bf956d45.tar.gz bcm5719-llvm-4823631af475410cb7c2c47af28ed010bf956d45.zip |
Implement new interfaces for code-formatting when applying replacements.
Patch by Eric Liu.
llvm-svn: 262232
Diffstat (limited to 'clang/unittests/Tooling/RefactoringTest.cpp')
-rw-r--r-- | clang/unittests/Tooling/RefactoringTest.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/clang/unittests/Tooling/RefactoringTest.cpp b/clang/unittests/Tooling/RefactoringTest.cpp index ff11aeae117..49a4e1dde78 100644 --- a/clang/unittests/Tooling/RefactoringTest.cpp +++ b/clang/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,35 @@ TEST_F(ReplacementTest, ApplyAllFailsIfOneApplyFails) { EXPECT_EQ("z", Context.getRewrittenText(IDz)); } +TEST_F(ReplacementTest, FormatCodeAfterReplacements) { + // Column limit is 20. + std::string Code = "Type *a =\n" + " new Type();\n" + "g(iiiii, 0, jjjjj,\n" + " 0, kkkkk, 0, mm);\n" + "int bad = format ;"; + std::string Expected = "auto a = new Type();\n" + "g(iiiii, nullptr,\n" + " jjjjj, nullptr,\n" + " kkkkk, nullptr,\n" + " mm);\n" + "int bad = format ;"; + FileID ID = Context.createInMemoryFile("format.cpp", Code); + Replacements Replaces; + Replaces.insert( + Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, "auto ")); + Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 3, 10), + 1, "nullptr")); + Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, + "nullptr")); + Replaces.insert(Replacement(Context.Sources, Context.getLocation(ID, 4, 13), + 1, "nullptr")); + + format::FormatStyle Style = format::getLLVMStyle(); + Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. + EXPECT_EQ(Expected, applyAllReplacementsAndFormat(Code, Replaces, Style)); +} + TEST(ShiftedCodePositionTest, FindsNewCodePosition) { Replacements Replaces; Replaces.insert(Replacement("", 0, 1, "")); @@ -418,6 +448,25 @@ TEST(Range, contains) { EXPECT_FALSE(Range(0, 10).contains(Range(0, 11))); } +TEST(Range, CalculateRangesOfReplacements) { + // Before: aaaabbbbbbz + // After : bbbbbbzzzzzzoooooooooooooooo + Replacements Replaces; + Replaces.insert(Replacement("foo", 0, 4, "")); + Replaces.insert(Replacement("foo", 10, 1, "zzzzzz")); + Replaces.insert(Replacement("foo", 11, 0, "oooooooooooooooo")); + + std::vector<Range> Ranges = calculateChangedRangesInFile(Replaces); + + EXPECT_EQ(3ul, Ranges.size()); + EXPECT_TRUE(Ranges[0].getOffset() == 0); + EXPECT_TRUE(Ranges[0].getLength() == 0); + EXPECT_TRUE(Ranges[1].getOffset() == 6); + EXPECT_TRUE(Ranges[1].getLength() == 6); + EXPECT_TRUE(Ranges[2].getOffset() == 12); + EXPECT_TRUE(Ranges[2].getLength() == 16); +} + TEST(DeduplicateTest, removesDuplicates) { std::vector<Replacement> Input; Input.push_back(Replacement("fileA", 50, 0, " foo ")); |