2012-05-23 01:01:35 +08:00
|
|
|
//===- unittest/Tooling/RewriterTest.cpp ----------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-05-23 01:01:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "RewriterTestContext.h"
|
2015-06-16 18:22:10 +08:00
|
|
|
#include "clang/Tooling/Core/Replacement.h"
|
2012-05-23 01:01:35 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace clang {
|
2015-06-16 18:22:10 +08:00
|
|
|
namespace tooling {
|
|
|
|
namespace {
|
2012-05-23 01:01:35 +08:00
|
|
|
|
|
|
|
TEST(Rewriter, OverwritesChangedFiles) {
|
|
|
|
RewriterTestContext Context;
|
|
|
|
FileID ID = Context.createOnDiskFile("t.cpp", "line1\nline2\nline3\nline4");
|
|
|
|
Context.Rewrite.ReplaceText(Context.getLocation(ID, 2, 1), 5, "replaced");
|
|
|
|
EXPECT_FALSE(Context.Rewrite.overwriteChangedFiles());
|
|
|
|
EXPECT_EQ("line1\nreplaced\nline3\nline4",
|
|
|
|
Context.getFileContentFromDisk("t.cpp"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Rewriter, ContinuesOverwritingFilesOnError) {
|
|
|
|
RewriterTestContext Context;
|
|
|
|
FileID FailingID = Context.createInMemoryFile("invalid/failing.cpp", "test");
|
|
|
|
Context.Rewrite.ReplaceText(Context.getLocation(FailingID, 1, 2), 1, "other");
|
|
|
|
FileID WorkingID = Context.createOnDiskFile(
|
|
|
|
"working.cpp", "line1\nline2\nline3\nline4");
|
|
|
|
Context.Rewrite.ReplaceText(Context.getLocation(WorkingID, 2, 1), 5,
|
|
|
|
"replaced");
|
|
|
|
EXPECT_TRUE(Context.Rewrite.overwriteChangedFiles());
|
|
|
|
EXPECT_EQ("line1\nreplaced\nline3\nline4",
|
|
|
|
Context.getFileContentFromDisk("working.cpp"));
|
|
|
|
}
|
|
|
|
|
2015-06-16 18:22:10 +08:00
|
|
|
TEST(Rewriter, AdjacentInsertAndDelete) {
|
|
|
|
Replacements Replaces;
|
2016-08-01 18:16:37 +08:00
|
|
|
auto Err = Replaces.add(Replacement("<file>", 6, 6, ""));
|
|
|
|
EXPECT_TRUE(!Err);
|
|
|
|
Replaces =
|
|
|
|
Replaces.merge(Replacements(Replacement("<file>", 6, 0, "replaced\n")));
|
|
|
|
|
2016-07-11 21:53:12 +08:00
|
|
|
auto Rewritten = applyAllReplacements("line1\nline2\nline3\nline4", Replaces);
|
|
|
|
EXPECT_TRUE(static_cast<bool>(Rewritten));
|
|
|
|
EXPECT_EQ("line1\nreplaced\nline3\nline4", *Rewritten);
|
2015-06-16 18:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
} // end namespace tooling
|
2012-05-23 01:01:35 +08:00
|
|
|
} // end namespace clang
|