Argyrios Kyrtzidis | 69e6f1f | 2015-03-08 04:00:33 +0000 | [diff] [blame] | 1 | //===- unittests/Rewrite/RewriteBufferTest.cpp - RewriteBuffer tests ------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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-exception |
Argyrios Kyrtzidis | 69e6f1f | 2015-03-08 04:00:33 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/Rewrite/Core/RewriteBuffer.h" |
| 10 | #include "gtest/gtest.h" |
| 11 | |
| 12 | using namespace llvm; |
| 13 | using namespace clang; |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | static void tagRange(unsigned Offset, unsigned Len, StringRef tagName, |
| 18 | RewriteBuffer &Buf) { |
| 19 | std::string BeginTag; |
| 20 | raw_string_ostream(BeginTag) << '<' << tagName << '>'; |
| 21 | std::string EndTag; |
| 22 | raw_string_ostream(EndTag) << "</" << tagName << '>'; |
| 23 | |
| 24 | Buf.InsertTextAfter(Offset, BeginTag); |
| 25 | Buf.InsertTextBefore(Offset+Len, EndTag); |
| 26 | } |
| 27 | |
| 28 | TEST(RewriteBuffer, TagRanges) { |
| 29 | StringRef Input = "hello world"; |
| 30 | const char *Output = "<outer><inner>hello</inner></outer> "; |
| 31 | |
| 32 | RewriteBuffer Buf; |
| 33 | Buf.Initialize(Input); |
| 34 | StringRef RemoveStr = "world"; |
| 35 | size_t Pos = Input.find(RemoveStr); |
| 36 | Buf.RemoveText(Pos, RemoveStr.size()); |
| 37 | |
| 38 | StringRef TagStr = "hello"; |
| 39 | Pos = Input.find(TagStr); |
| 40 | tagRange(Pos, TagStr.size(), "outer", Buf); |
| 41 | tagRange(Pos, TagStr.size(), "inner", Buf); |
| 42 | |
| 43 | std::string Result; |
| 44 | raw_string_ostream OS(Result); |
| 45 | Buf.write(OS); |
| 46 | OS.flush(); |
| 47 | EXPECT_EQ(Output, Result); |
| 48 | } |
| 49 | |
| 50 | } // anonymous namespace |