• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- unittests/Rewrite/RewriteBufferTest.cpp - RewriteBuffer tests ------===//
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 "clang/Rewrite/Core/RewriteBuffer.h"
11 #include "gtest/gtest.h"
12 
13 using namespace llvm;
14 using namespace clang;
15 
16 namespace {
17 
tagRange(unsigned Offset,unsigned Len,StringRef tagName,RewriteBuffer & Buf)18 static void tagRange(unsigned Offset, unsigned Len, StringRef tagName,
19                      RewriteBuffer &Buf) {
20   std::string BeginTag;
21   raw_string_ostream(BeginTag) << '<' << tagName << '>';
22   std::string EndTag;
23   raw_string_ostream(EndTag) << "</" << tagName << '>';
24 
25   Buf.InsertTextAfter(Offset, BeginTag);
26   Buf.InsertTextBefore(Offset+Len, EndTag);
27 }
28 
TEST(RewriteBuffer,TagRanges)29 TEST(RewriteBuffer, TagRanges) {
30   StringRef Input = "hello world";
31   const char *Output = "<outer><inner>hello</inner></outer> ";
32 
33   RewriteBuffer Buf;
34   Buf.Initialize(Input);
35   StringRef RemoveStr = "world";
36   size_t Pos = Input.find(RemoveStr);
37   Buf.RemoveText(Pos, RemoveStr.size());
38 
39   StringRef TagStr = "hello";
40   Pos = Input.find(TagStr);
41   tagRange(Pos, TagStr.size(), "outer", Buf);
42   tagRange(Pos, TagStr.size(), "inner", Buf);
43 
44   std::string Result;
45   raw_string_ostream OS(Result);
46   Buf.write(OS);
47   OS.flush();
48   EXPECT_EQ(Output, Result);
49 }
50 
51 } // anonymous namespace
52