brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.0 KiB · e744940 Raw
54 lines · cpp
1//===- unittest/Tooling/RewriterTest.cpp ----------------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "RewriterTestContext.h"10#include "clang/Tooling/Core/Replacement.h"11#include "gtest/gtest.h"12 13namespace clang {14namespace tooling {15namespace {16 17TEST(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 26TEST(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 39TEST(Rewriter, AdjacentInsertAndDelete) {40  Replacements Replaces;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 46  auto Rewritten = applyAllReplacements("line1\nline2\nline3\nline4", Replaces);47  EXPECT_TRUE(static_cast<bool>(Rewritten));48  EXPECT_EQ("line1\nreplaced\nline3\nline4", *Rewritten);49}50 51} // end namespace52} // end namespace tooling53} // end namespace clang54