#include "clang/Rewrite/Core/RewriteBuffer.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace clang;
namespace {
#define EXPECT_OUTPUT(Buf, Output) EXPECT_EQ(Output, writeOutput(Buf))
static std::string writeOutput(const RewriteBuffer &Buf) {
std::string Result;
raw_string_ostream OS(Result);
Buf.write(OS);
OS.flush();
return Result;
}
static void tagRange(unsigned Offset, unsigned Len, StringRef tagName,
RewriteBuffer &Buf) {
std::string BeginTag;
raw_string_ostream(BeginTag) << '<' << tagName << '>';
std::string EndTag;
raw_string_ostream(EndTag) << "</" << tagName << '>';
Buf.InsertTextAfter(Offset, BeginTag);
Buf.InsertTextBefore(Offset+Len, EndTag);
}
TEST(RewriteBuffer, TagRanges) {
StringRef Input = "hello world";
const char *Output = "<outer><inner>hello</inner></outer> ";
RewriteBuffer Buf;
Buf.Initialize(Input);
StringRef RemoveStr = "world";
size_t Pos = Input.find(RemoveStr);
Buf.RemoveText(Pos, RemoveStr.size());
StringRef TagStr = "hello";
Pos = Input.find(TagStr);
tagRange(Pos, TagStr.size(), "outer", Buf);
tagRange(Pos, TagStr.size(), "inner", Buf);
EXPECT_OUTPUT(Buf, Output);
}
TEST(RewriteBuffer, DISABLED_RemoveLineIfEmpty_XFAIL) {
StringRef Input = "def\n"
"ghi\n"
"jkl\n";
RewriteBuffer Buf;
Buf.Initialize(Input);
Buf.InsertText(0, "abc\n");
EXPECT_OUTPUT(Buf, "abc\n"
"def\n"
"ghi\n"
"jkl\n");
StringRef RemoveStr0 = "def";
Buf.RemoveText(Input.find(RemoveStr0), RemoveStr0.size(),
true);
EXPECT_OUTPUT(Buf, "abc\n"
"ghi\n"
"jkl\n");
StringRef RemoveStr1 = "ghi\n";
Buf.RemoveText(Input.find(RemoveStr1), RemoveStr1.size());
EXPECT_OUTPUT(Buf, "abc\n"
"jkl\n");
}
}