xref: /minix3/external/bsd/llvm/dist/clang/unittests/Tooling/RewriterTest.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc //===- unittest/Tooling/RewriterTest.cpp ----------------------------------===//
2*f4a2713aSLionel Sambuc //
3*f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*f4a2713aSLionel Sambuc //
5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7*f4a2713aSLionel Sambuc //
8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc #include "RewriterTestContext.h"
11*f4a2713aSLionel Sambuc #include "gtest/gtest.h"
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc namespace clang {
14*f4a2713aSLionel Sambuc 
TEST(Rewriter,OverwritesChangedFiles)15*f4a2713aSLionel Sambuc TEST(Rewriter, OverwritesChangedFiles) {
16*f4a2713aSLionel Sambuc   RewriterTestContext Context;
17*f4a2713aSLionel Sambuc   FileID ID = Context.createOnDiskFile("t.cpp", "line1\nline2\nline3\nline4");
18*f4a2713aSLionel Sambuc   Context.Rewrite.ReplaceText(Context.getLocation(ID, 2, 1), 5, "replaced");
19*f4a2713aSLionel Sambuc   EXPECT_FALSE(Context.Rewrite.overwriteChangedFiles());
20*f4a2713aSLionel Sambuc   EXPECT_EQ("line1\nreplaced\nline3\nline4",
21*f4a2713aSLionel Sambuc             Context.getFileContentFromDisk("t.cpp"));
22*f4a2713aSLionel Sambuc }
23*f4a2713aSLionel Sambuc 
TEST(Rewriter,ContinuesOverwritingFilesOnError)24*f4a2713aSLionel Sambuc TEST(Rewriter, ContinuesOverwritingFilesOnError) {
25*f4a2713aSLionel Sambuc   RewriterTestContext Context;
26*f4a2713aSLionel Sambuc   FileID FailingID = Context.createInMemoryFile("invalid/failing.cpp", "test");
27*f4a2713aSLionel Sambuc   Context.Rewrite.ReplaceText(Context.getLocation(FailingID, 1, 2), 1, "other");
28*f4a2713aSLionel Sambuc   FileID WorkingID = Context.createOnDiskFile(
29*f4a2713aSLionel Sambuc     "working.cpp", "line1\nline2\nline3\nline4");
30*f4a2713aSLionel Sambuc   Context.Rewrite.ReplaceText(Context.getLocation(WorkingID, 2, 1), 5,
31*f4a2713aSLionel Sambuc                               "replaced");
32*f4a2713aSLionel Sambuc   EXPECT_TRUE(Context.Rewrite.overwriteChangedFiles());
33*f4a2713aSLionel Sambuc   EXPECT_EQ("line1\nreplaced\nline3\nline4",
34*f4a2713aSLionel Sambuc             Context.getFileContentFromDisk("working.cpp"));
35*f4a2713aSLionel Sambuc }
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc } // end namespace clang
38