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 "clang/Tooling/Core/Replacement.h"
     12 #include "gtest/gtest.h"
     13 
     14 namespace clang {
     15 namespace tooling {
     16 namespace {
     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 
     40 TEST(Rewriter, AdjacentInsertAndDelete) {
     41   Replacements Replaces;
     42   Replaces.insert(Replacement("<file>", 6, 6, ""));
     43   Replaces.insert(Replacement("<file>", 6, 0, "replaced\n"));
     44   EXPECT_EQ("line1\nreplaced\nline3\nline4",
     45             applyAllReplacements("line1\nline2\nline3\nline4", Replaces));
     46 }
     47 
     48 } // end namespace
     49 } // end namespace tooling
     50 } // end namespace clang
     51