1 //===- unittest/Tooling/RewriterTest.cpp ----------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "RewriterTestContext.h"
10 #include "clang/Tooling/Core/Replacement.h"
11 #include "gtest/gtest.h"
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"));
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,
34 EXPECT_TRUE(Context
.Rewrite
.overwriteChangedFiles());
35 EXPECT_EQ("line1\nreplaced\nline3\nline4",
36 Context
.getFileContentFromDisk("working.cpp"));
39 TEST(Rewriter
, AdjacentInsertAndDelete
) {
40 Replacements Replaces
;
41 auto Err
= Replaces
.add(Replacement("<file>", 6, 6, ""));
44 Replaces
.merge(Replacements(Replacement("<file>", 6, 0, "replaced\n")));
46 auto Rewritten
= applyAllReplacements("line1\nline2\nline3\nline4", Replaces
);
47 EXPECT_TRUE(static_cast<bool>(Rewritten
));
48 EXPECT_EQ("line1\nreplaced\nline3\nline4", *Rewritten
);
52 } // end namespace tooling
53 } // end namespace clang