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; |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 42 | auto Err = Replaces.add(Replacement("<file>", 6, 6, "")); |
| 43 | EXPECT_TRUE(!Err); |
| 44 | Replaces = |
| 45 | Replaces.merge(Replacements(Replacement("<file>", 6, 0, "replaced\n"))); |
| 46 | |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 47 | auto Rewritten = applyAllReplacements("line1\nline2\nline3\nline4", Replaces); |
| 48 | EXPECT_TRUE(static_cast<bool>(Rewritten)); |
| 49 | EXPECT_EQ("line1\nreplaced\nline3\nline4", *Rewritten); |
Daniel Jasper | 0f29127 | 2015-06-16 10:22:10 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | } // end namespace |
| 53 | } // end namespace tooling |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 54 | } // end namespace clang |