1 //===- LineIterator.cpp - Unit tests --------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/LineIterator.h"
10 #include "llvm/Support/MemoryBuffer.h"
11 #include "gtest/gtest.h"
14 using namespace llvm::sys
;
18 TEST(LineIteratorTest
, Basic
) {
19 std::unique_ptr
<MemoryBuffer
> Buffer
= MemoryBuffer::getMemBuffer("line 1\n"
23 line_iterator I
= line_iterator(*Buffer
), E
;
25 EXPECT_FALSE(I
.is_at_eof());
28 EXPECT_EQ("line 1", *I
);
29 EXPECT_EQ(1, I
.line_number());
31 EXPECT_EQ("line 2", *I
);
32 EXPECT_EQ(2, I
.line_number());
34 EXPECT_EQ("line 3", *I
);
35 EXPECT_EQ(3, I
.line_number());
38 EXPECT_TRUE(I
.is_at_eof());
42 TEST(LineIteratorTest
, Ref
) {
43 std::unique_ptr
<MemoryBuffer
> Buffer
= MemoryBuffer::getMemBuffer("line 1\n"
47 line_iterator I
= line_iterator(Buffer
->getMemBufferRef()), E
;
49 EXPECT_FALSE(I
.is_at_eof());
52 EXPECT_EQ("line 1", *I
);
53 EXPECT_EQ(1, I
.line_number());
55 EXPECT_EQ("line 2", *I
);
56 EXPECT_EQ(2, I
.line_number());
58 EXPECT_EQ("line 3", *I
);
59 EXPECT_EQ(3, I
.line_number());
62 EXPECT_TRUE(I
.is_at_eof());
66 TEST(LineIteratorTest
, CommentAndBlankSkipping
) {
67 std::unique_ptr
<MemoryBuffer
> Buffer(
68 MemoryBuffer::getMemBuffer("line 1\n"
76 line_iterator I
= line_iterator(*Buffer
, true, '#'), E
;
78 EXPECT_FALSE(I
.is_at_eof());
81 EXPECT_EQ("line 1", *I
);
82 EXPECT_EQ(1, I
.line_number());
84 EXPECT_EQ("line 2", *I
);
85 EXPECT_EQ(2, I
.line_number());
87 EXPECT_EQ("line 5", *I
);
88 EXPECT_EQ(5, I
.line_number());
91 EXPECT_TRUE(I
.is_at_eof());
95 TEST(LineIteratorTest
, CommentSkippingKeepBlanks
) {
96 std::unique_ptr
<MemoryBuffer
> Buffer(
97 MemoryBuffer::getMemBuffer("line 1\n"
106 line_iterator I
= line_iterator(*Buffer
, false, '#'), E
;
108 EXPECT_FALSE(I
.is_at_eof());
111 EXPECT_EQ("line 1", *I
);
112 EXPECT_EQ(1, I
.line_number());
114 EXPECT_EQ("line 2", *I
);
115 EXPECT_EQ(2, I
.line_number());
118 EXPECT_EQ(5, I
.line_number());
120 EXPECT_EQ("line 6", *I
);
121 EXPECT_EQ(6, I
.line_number());
124 EXPECT_EQ(7, I
.line_number());
127 EXPECT_TRUE(I
.is_at_eof());
132 TEST(LineIteratorTest
, BlankSkipping
) {
133 std::unique_ptr
<MemoryBuffer
> Buffer
= MemoryBuffer::getMemBuffer("\n\n\n"
139 line_iterator I
= line_iterator(*Buffer
), E
;
141 EXPECT_FALSE(I
.is_at_eof());
144 EXPECT_EQ("line 1", *I
);
145 EXPECT_EQ(4, I
.line_number());
147 EXPECT_EQ("line 2", *I
);
148 EXPECT_EQ(8, I
.line_number());
151 EXPECT_TRUE(I
.is_at_eof());
155 TEST(LineIteratorTest
, BlankKeeping
) {
156 std::unique_ptr
<MemoryBuffer
> Buffer
= MemoryBuffer::getMemBuffer("\n\n"
161 line_iterator I
= line_iterator(*Buffer
, false), E
;
163 EXPECT_FALSE(I
.is_at_eof());
167 EXPECT_EQ(1, I
.line_number());
170 EXPECT_EQ(2, I
.line_number());
172 EXPECT_EQ("line 3", *I
);
173 EXPECT_EQ(3, I
.line_number());
176 EXPECT_EQ(4, I
.line_number());
178 EXPECT_EQ("line 5", *I
);
179 EXPECT_EQ(5, I
.line_number());
182 EXPECT_EQ(6, I
.line_number());
185 EXPECT_EQ(7, I
.line_number());
188 EXPECT_TRUE(I
.is_at_eof());
192 TEST(LineIteratorTest
, EmptyBuffers
) {
193 std::unique_ptr
<MemoryBuffer
> Buffer
= MemoryBuffer::getMemBuffer("");
194 EXPECT_TRUE(line_iterator(*Buffer
).is_at_eof());
195 EXPECT_EQ(line_iterator(), line_iterator(*Buffer
));
196 EXPECT_TRUE(line_iterator(*Buffer
, false).is_at_eof());
197 EXPECT_EQ(line_iterator(), line_iterator(*Buffer
, false));
199 Buffer
= MemoryBuffer::getMemBuffer("\n\n\n");
200 EXPECT_TRUE(line_iterator(*Buffer
).is_at_eof());
201 EXPECT_EQ(line_iterator(), line_iterator(*Buffer
));
203 Buffer
= MemoryBuffer::getMemBuffer("# foo\n"
206 EXPECT_TRUE(line_iterator(*Buffer
, true, '#').is_at_eof());
207 EXPECT_EQ(line_iterator(), line_iterator(*Buffer
, true, '#'));
209 Buffer
= MemoryBuffer::getMemBuffer("\n"
212 EXPECT_TRUE(line_iterator(*Buffer
, true, '#').is_at_eof());
213 EXPECT_EQ(line_iterator(), line_iterator(*Buffer
, true, '#'));
216 } // anonymous namespace