Home | History | Annotate | Download | only in Tooling
      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"
     11 #include "gtest/gtest.h"
     12 
     13 namespace clang {
     14 
     15 TEST(Rewriter, OverwritesChangedFiles) {
     16   RewriterTestContext Context;
     17   FileID ID = Context.createOnDiskFile("t.cpp", "line1\nline2\nline3\nline4");
     18   Context.Rewrite.ReplaceText(Context.getLocation(ID, 2, 1), 5, "replaced");
     19   EXPECT_FALSE(Context.Rewrite.overwriteChangedFiles());
     20   EXPECT_EQ("line1\nreplaced\nline3\nline4",
     21             Context.getFileContentFromDisk("t.cpp"));
     22 }
     23 
     24 TEST(Rewriter, ContinuesOverwritingFilesOnError) {
     25   RewriterTestContext Context;
     26   FileID FailingID = Context.createInMemoryFile("invalid/failing.cpp", "test");
     27   Context.Rewrite.ReplaceText(Context.getLocation(FailingID, 1, 2), 1, "other");
     28   FileID WorkingID = Context.createOnDiskFile(
     29     "working.cpp", "line1\nline2\nline3\nline4");
     30   Context.Rewrite.ReplaceText(Context.getLocation(WorkingID, 2, 1), 5,
     31                               "replaced");
     32   EXPECT_TRUE(Context.Rewrite.overwriteChangedFiles());
     33   EXPECT_EQ("line1\nreplaced\nline3\nline4",
     34             Context.getFileContentFromDisk("working.cpp"));
     35 }
     36 
     37 } // end namespace clang
     38