Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 1 | //===- unittest/Tooling/RewriterTest.cpp ----------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "RewriterTestContext.h" |
Daniel Jasper | 0f29127 | 2015-06-16 10:22:10 +0000 | [diff] [blame] | 10 | #include "clang/Tooling/Core/Replacement.h" |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 11 | #include "gtest/gtest.h" |
| 12 | |
| 13 | namespace clang { |
Daniel Jasper | 0f29127 | 2015-06-16 10:22:10 +0000 | [diff] [blame] | 14 | namespace tooling { |
| 15 | namespace { |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 16 | |
| 17 | TEST(Rewriter, OverwritesChangedFiles) { |
| 18 | RewriterTestContext Context; |
| 19 | FileID ID = Context.createOnDiskFile("t.cpp", "line1\nline2\nline3\nline4"); |
| 20 | Context.Rewrite.ReplaceText(Context.getLocation(ID, 2, 1), 5, "replaced"); |
| 21 | EXPECT_FALSE(Context.Rewrite.overwriteChangedFiles()); |
| 22 | EXPECT_EQ("line1\nreplaced\nline3\nline4", |
| 23 | Context.getFileContentFromDisk("t.cpp")); |
| 24 | } |
| 25 | |
| 26 | TEST(Rewriter, ContinuesOverwritingFilesOnError) { |
| 27 | RewriterTestContext Context; |
| 28 | FileID FailingID = Context.createInMemoryFile("invalid/failing.cpp", "test"); |
| 29 | Context.Rewrite.ReplaceText(Context.getLocation(FailingID, 1, 2), 1, "other"); |
| 30 | FileID WorkingID = Context.createOnDiskFile( |
| 31 | "working.cpp", "line1\nline2\nline3\nline4"); |
| 32 | Context.Rewrite.ReplaceText(Context.getLocation(WorkingID, 2, 1), 5, |
| 33 | "replaced"); |
| 34 | EXPECT_TRUE(Context.Rewrite.overwriteChangedFiles()); |
| 35 | EXPECT_EQ("line1\nreplaced\nline3\nline4", |
| 36 | Context.getFileContentFromDisk("working.cpp")); |
| 37 | } |
| 38 | |
Daniel Jasper | 0f29127 | 2015-06-16 10:22:10 +0000 | [diff] [blame] | 39 | TEST(Rewriter, AdjacentInsertAndDelete) { |
| 40 | Replacements Replaces; |
Eric Liu | 40ef2fb | 2016-08-01 10:16:37 +0000 | [diff] [blame] | 41 | auto Err = Replaces.add(Replacement("<file>", 6, 6, "")); |
| 42 | EXPECT_TRUE(!Err); |
| 43 | Replaces = |
| 44 | Replaces.merge(Replacements(Replacement("<file>", 6, 0, "replaced\n"))); |
| 45 | |
Eric Liu | 4f8d994 | 2016-07-11 13:53:12 +0000 | [diff] [blame] | 46 | auto Rewritten = applyAllReplacements("line1\nline2\nline3\nline4", Replaces); |
| 47 | EXPECT_TRUE(static_cast<bool>(Rewritten)); |
| 48 | EXPECT_EQ("line1\nreplaced\nline3\nline4", *Rewritten); |
Daniel Jasper | 0f29127 | 2015-06-16 10:22:10 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | } // end namespace |
| 52 | } // end namespace tooling |
Manuel Klimek | 78d084d | 2012-05-22 17:01:35 +0000 | [diff] [blame] | 53 | } // end namespace clang |