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
, CommentAndBlankSkipping
) {
43 std::unique_ptr
<MemoryBuffer
> Buffer(
44 MemoryBuffer::getMemBuffer("line 1\n"
52 line_iterator I
= line_iterator(*Buffer
, true, '#'), E
;
54 EXPECT_FALSE(I
.is_at_eof());
57 EXPECT_EQ("line 1", *I
);
58 EXPECT_EQ(1, I
.line_number());
60 EXPECT_EQ("line 2", *I
);
61 EXPECT_EQ(2, I
.line_number());
63 EXPECT_EQ("line 5", *I
);
64 EXPECT_EQ(5, I
.line_number());
67 EXPECT_TRUE(I
.is_at_eof());
71 TEST(LineIteratorTest
, CommentSkippingKeepBlanks
) {
72 std::unique_ptr
<MemoryBuffer
> Buffer(
73 MemoryBuffer::getMemBuffer("line 1\n"
82 line_iterator I
= line_iterator(*Buffer
, false, '#'), E
;
84 EXPECT_FALSE(I
.is_at_eof());
87 EXPECT_EQ("line 1", *I
);
88 EXPECT_EQ(1, I
.line_number());
90 EXPECT_EQ("line 2", *I
);
91 EXPECT_EQ(2, I
.line_number());
94 EXPECT_EQ(5, I
.line_number());
96 EXPECT_EQ("line 6", *I
);
97 EXPECT_EQ(6, I
.line_number());
100 EXPECT_EQ(7, I
.line_number());
103 EXPECT_TRUE(I
.is_at_eof());
108 TEST(LineIteratorTest
, BlankSkipping
) {
109 std::unique_ptr
<MemoryBuffer
> Buffer
= MemoryBuffer::getMemBuffer("\n\n\n"
115 line_iterator I
= line_iterator(*Buffer
), E
;
117 EXPECT_FALSE(I
.is_at_eof());
120 EXPECT_EQ("line 1", *I
);
121 EXPECT_EQ(4, I
.line_number());
123 EXPECT_EQ("line 2", *I
);
124 EXPECT_EQ(8, I
.line_number());
127 EXPECT_TRUE(I
.is_at_eof());
131 TEST(LineIteratorTest
, BlankKeeping
) {
132 std::unique_ptr
<MemoryBuffer
> Buffer
= MemoryBuffer::getMemBuffer("\n\n"
137 line_iterator I
= line_iterator(*Buffer
, false), E
;
139 EXPECT_FALSE(I
.is_at_eof());
143 EXPECT_EQ(1, I
.line_number());
146 EXPECT_EQ(2, I
.line_number());
148 EXPECT_EQ("line 3", *I
);
149 EXPECT_EQ(3, I
.line_number());
152 EXPECT_EQ(4, I
.line_number());
154 EXPECT_EQ("line 5", *I
);
155 EXPECT_EQ(5, I
.line_number());
158 EXPECT_EQ(6, I
.line_number());
161 EXPECT_EQ(7, I
.line_number());
164 EXPECT_TRUE(I
.is_at_eof());
168 TEST(LineIteratorTest
, EmptyBuffers
) {
169 std::unique_ptr
<MemoryBuffer
> Buffer
= MemoryBuffer::getMemBuffer("");
170 EXPECT_TRUE(line_iterator(*Buffer
).is_at_eof());
171 EXPECT_EQ(line_iterator(), line_iterator(*Buffer
));
172 EXPECT_TRUE(line_iterator(*Buffer
, false).is_at_eof());
173 EXPECT_EQ(line_iterator(), line_iterator(*Buffer
, false));
175 Buffer
= MemoryBuffer::getMemBuffer("\n\n\n");
176 EXPECT_TRUE(line_iterator(*Buffer
).is_at_eof());
177 EXPECT_EQ(line_iterator(), line_iterator(*Buffer
));
179 Buffer
= MemoryBuffer::getMemBuffer("# foo\n"
182 EXPECT_TRUE(line_iterator(*Buffer
, true, '#').is_at_eof());
183 EXPECT_EQ(line_iterator(), line_iterator(*Buffer
, true, '#'));
185 Buffer
= MemoryBuffer::getMemBuffer("\n"
188 EXPECT_TRUE(line_iterator(*Buffer
, true, '#').is_at_eof());
189 EXPECT_EQ(line_iterator(), line_iterator(*Buffer
, true, '#'));
192 } // anonymous namespace