Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 1 | //===- unittest/Tooling/RewriterTest.cpp ----------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "RewriterTestContext.h" |
Daniel Jasper | 0f29127 | 2015-06-16 10:22:10 +0000 | [diff] [blame] | 11 | #include "clang/Tooling/Core/Replacement.h" |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | namespace clang { |
Daniel Jasper | 0f29127 | 2015-06-16 10:22:10 +0000 | [diff] [blame] | 15 | namespace tooling { |
| 16 | namespace { |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 17 | |
| 18 | TEST(Rewriter, OverwritesChangedFiles) { |
| 19 | RewriterTestContext Context; |
| 20 | FileID ID = Context.createOnDiskFile("t.cpp", "line1\nline2\nline3\nline4"); |
| 21 | Context.Rewrite.ReplaceText(Context.getLocation(ID, 2, 1), 5, "replaced"); |
| 22 | EXPECT_FALSE(Context.Rewrite.overwriteChangedFiles()); |
| 23 | EXPECT_EQ("line1\nreplaced\nline3\nline4", |
| 24 | Context.getFileContentFromDisk("t.cpp")); |
| 25 | } |
| 26 | |
| 27 | TEST(Rewriter, ContinuesOverwritingFilesOnError) { |
| 28 | RewriterTestContext Context; |
| 29 | FileID FailingID = Context.createInMemoryFile("invalid/failing.cpp", "test"); |
| 30 | Context.Rewrite.ReplaceText(Context.getLocation(FailingID, 1, 2), 1, "other"); |
| 31 | FileID WorkingID = Context.createOnDiskFile( |
| 32 | "working.cpp", "line1\nline2\nline3\nline4"); |
| 33 | Context.Rewrite.ReplaceText(Context.getLocation(WorkingID, 2, 1), 5, |
| 34 | "replaced"); |
| 35 | EXPECT_TRUE(Context.Rewrite.overwriteChangedFiles()); |
| 36 | EXPECT_EQ("line1\nreplaced\nline3\nline4", |
| 37 | Context.getFileContentFromDisk("working.cpp")); |
| 38 | } |
| 39 | |
Daniel Jasper | 0f29127 | 2015-06-16 10:22:10 +0000 | [diff] [blame] | 40 | TEST(Rewriter, AdjacentInsertAndDelete) { |
| 41 | Replacements Replaces; |
Daniel Jasper | 34688f2 | 2015-06-16 13:15:54 +0000 | [diff] [blame] | 42 | Replaces.insert(Replacement("<file>", 6, 6, "")); |
| 43 | Replaces.insert(Replacement("<file>", 6, 0, "replaced\n")); |
Daniel Jasper | 0f29127 | 2015-06-16 10:22:10 +0000 | [diff] [blame] | 44 | EXPECT_EQ("line1\nreplaced\nline3\nline4", |
| 45 | applyAllReplacements("line1\nline2\nline3\nline4", Replaces)); |
| 46 | } |
| 47 | |
| 48 | } // end namespace |
| 49 | } // end namespace tooling |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 50 | } // end namespace clang |